All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.partiql.lang.partiqlisl.ResourceAuthority.kt Maven / Gradle / Ivy

There is a newer version: 1.0.0-perf.1
Show newest version
package org.partiql.lang.partiqlisl

import com.amazon.ion.IonSystem
import com.amazon.ion.IonValue
import com.amazon.ionschema.Authority
import com.amazon.ionschema.IonSchemaSystem
import com.amazon.ionschema.util.CloseableIterator
import java.io.InputStream

class ResourceAuthority(
    val rootPackage: String,
    val classLoader: ClassLoader,
    val ion: IonSystem
) : Authority {
    override fun iteratorFor(iss: IonSchemaSystem, id: String): CloseableIterator {
        val resourceName = "$rootPackage/$id"
        var str: InputStream? = classLoader.getResourceAsStream(resourceName)
            ?: error("Failed to load schema with resource name '$resourceName'")

        return object : CloseableIterator {

            private var stream = str
            private var reader = ion.newReader(stream).also { it.next() }
            private var iter = ion.iterate(reader)

            override fun hasNext() = iter.hasNext()
            override fun next() = iter.next()
            override fun close() {
                try {
                    reader?.close()
                    stream?.close()
                } finally {
                    reader = null
                    stream = null
                }
            }
        }
    }
}

fun getResourceAuthority(ion: IonSystem) = ResourceAuthority("org/partiql/schemas", ResourceAuthority::class.java.getClassLoader(), ion)

fun loadPartiqlIsl(iss: IonSchemaSystem) = iss.loadSchema("partiql.isl")




© 2015 - 2024 Weber Informatics LLC | Privacy Policy