Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 0 additions & 49 deletions core/src/main/java/org/opencds/cqf/cql/ls/core/ContentService.java

This file was deleted.

39 changes: 39 additions & 0 deletions core/src/main/java/org/opencds/cqf/cql/ls/core/ContentService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.opencds.cqf.cql.ls.core

import org.apache.commons.lang3.NotImplementedException
import org.hl7.elm.r1.VersionedIdentifier
import org.slf4j.LoggerFactory
import java.io.InputStream
import java.net.URI

interface ContentService {
fun locate(root: URI, identifier: VersionedIdentifier): Set<URI> {
throw NotImplementedException()
}

fun read(root: URI, identifier: VersionedIdentifier): InputStream? {
val locations = locate(root, identifier)
if (locations.isEmpty()) return null

if (locations.size > 1) {
val allLocations = locations.joinToString("%n") { it.toString() }
throw IllegalStateException(
"more than one location was found for library: ${identifier.id} version: ${identifier.version} in the current workspace:%n$allLocations"
)
}
return read(locations.first())
}

fun read(uri: URI): InputStream? {
return try {
uri.toURL().openStream()
} catch (e: Exception) {
log.warn("error opening stream for: $uri", e)
null
}
}

companion object {
private val log = LoggerFactory.getLogger(ContentService::class.java)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.opencds.cqf.cql.ls.core.utility

import kotlinx.io.Buffer
import kotlinx.io.Source
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets

object Converters {
@JvmStatic
@Throws(IOException::class)
fun inputStreamToString(inputStream: InputStream): String {
val sb = StringBuilder()
BufferedReader(InputStreamReader(inputStream, StandardCharsets.UTF_8)).use { reader ->
var line: String?
while (reader.readLine().also { line = it } != null) {
if (sb.isNotEmpty()) sb.append("\n")
sb.append(line)
}
}
return sb.toString()
}

@JvmStatic
fun stringToSource(text: String): Source {
val buffer = Buffer()
val bytes = text.toByteArray()
buffer.write(bytes, 0, bytes.size)
return buffer
}

@JvmStatic
@Throws(IOException::class)
fun inputStreamToSource(inputStream: InputStream): Source {
return stringToSource(inputStreamToString(inputStream))
}
}
104 changes: 0 additions & 104 deletions core/src/main/java/org/opencds/cqf/cql/ls/core/utility/Uris.java

This file was deleted.

76 changes: 76 additions & 0 deletions core/src/main/java/org/opencds/cqf/cql/ls/core/utility/Uris.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.opencds.cqf.cql.ls.core.utility

import org.apache.commons.lang3.SystemUtils
import java.io.File
import java.net.URI

object Uris {
private const val FILE_UNC_PREFIX = "file:////"
private const val FILE_SCHEME = "file"

@JvmStatic
fun getHead(uri: URI): URI {
val path = uri.rawPath ?: return uri
val index = path.lastIndexOf("/")
return if (index > -1) withPath(uri, path.substring(0, index)) ?: uri else uri
}

@JvmStatic
fun withPath(uri: URI, path: String): URI? {
return try {
URI.create(
(if (uri.scheme != null) "${uri.scheme}:" else "") +
"//" + createAuthority(uri.rawAuthority) + createPath(path) +
createQuery(uri.rawQuery) + createFragment(uri.rawFragment)
)
} catch (e: Exception) {
null
}
}

@JvmStatic
fun addPath(uri: URI, path: String): URI? {
return withPath(uri, stripTrailingSlash(uri.rawPath) + createPath(path))
}

@JvmStatic
fun parseOrNull(uriString: String): URI? {
return try {
var uri = URI(uriString)
if (SystemUtils.IS_OS_WINDOWS && FILE_SCHEME == uri.scheme) {
uri = File(uri.schemeSpecificPart).toURI()
}
uri
} catch (e: Exception) {
null
}
}

@JvmStatic
fun toClientUri(uri: URI?): String? {
if (uri == null) return null
var uriString = uri.toString()
if (SystemUtils.IS_OS_WINDOWS && uriString.startsWith(FILE_UNC_PREFIX)) {
uriString = uriString.replace(FILE_UNC_PREFIX, "file://")
}
return uriString
}

private fun createAuthority(rawAuthority: String?) = rawAuthority ?: ""

private fun stripTrailingSlash(path: String?): String {
if (path.isNullOrEmpty()) return ""
return if (path.endsWith("/")) path.dropLast(1) else path
}

private fun createPath(pathValue: String?) = ensurePrefix("/", pathValue)

private fun createQuery(queryValue: String?) = ensurePrefix("?", queryValue)

private fun createFragment(fragmentValue: String?) = ensurePrefix("#", fragmentValue)

private fun ensurePrefix(prefix: String, value: String?): String {
if (value.isNullOrEmpty()) return ""
return if (value.startsWith(prefix)) value else prefix + value
}
}
Loading
Loading