-
Notifications
You must be signed in to change notification settings - Fork 5
Slim down dependencies #534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f5ee757
Replace ROBOT catalog mapper dependency
balhoff 8d4e881
Replace owl-diff markdown rendering dependency
balhoff 9c7a70e
Update ELK and expand query ranges directly
balhoff 322fca1
Replace Jena convenience dependency
balhoff 41e147d
Update Circe dependency hygiene
balhoff 377abba
Replace better-files with java.nio.file
balhoff e97c5d2
Replace commons-codec SHA-1 usage
balhoff 3cfcd24
Query anonymous ranges without ontology mutation
balhoff 6adf556
Handle namespaced catalog entries
balhoff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
src/main/scala/org/monarchinitiative/dosdp/CatalogXmlIRIMapper.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package org.monarchinitiative.dosdp | ||
|
|
||
| import org.semanticweb.owlapi.model.{IRI, OWLOntologyIRIMapper} | ||
| import org.xml.sax.Attributes | ||
| import org.xml.sax.helpers.DefaultHandler | ||
|
|
||
| import java.io.{File, FileInputStream, InputStream} | ||
| import java.net.URL | ||
| import java.util.HashMap | ||
| import javax.xml.parsers.SAXParserFactory | ||
| import scala.util.control.NonFatal | ||
|
|
||
| /** | ||
| * OWL ontology IRI mapper backed by OASIS-style catalog XML `uri` entries. | ||
| * | ||
| * Adapted from ROBOT's `org.obolibrary.robot.CatalogXmlIRIMapper` and | ||
| * `CatalogElementHandler` implementations (BSD-3-Clause). Keeping this local | ||
| * avoids depending on all of `robot-core` for one small mapper. | ||
| */ | ||
| final class CatalogXmlIRIMapper private (mappings: java.util.Map[IRI, IRI]) extends OWLOntologyIRIMapper { | ||
|
|
||
| def this(catalogFile: String) = | ||
| this(CatalogXmlIRIMapper.parseCatalogFile(new File(catalogFile).getAbsoluteFile)) | ||
|
|
||
| def this(catalogFile: File) = | ||
| this(CatalogXmlIRIMapper.parseCatalogFile(catalogFile.getAbsoluteFile)) | ||
|
|
||
| def this(catalogFile: File, parentFolder: File) = | ||
| this(CatalogXmlIRIMapper.parseCatalogXml(new FileInputStream(catalogFile), Option(parentFolder))) | ||
|
|
||
| def this(catalogIRI: IRI) = | ||
| this(CatalogXmlIRIMapper.parseCatalogXml(catalogIRI.toURI.toURL)) | ||
|
|
||
| def this(catalogURL: URL) = | ||
| this(CatalogXmlIRIMapper.parseCatalogXml(catalogURL)) | ||
|
|
||
| def this(catalogURL: URL, parentFolder: File) = | ||
| this(CatalogXmlIRIMapper.parseCatalogXml(catalogURL.openStream(), Option(parentFolder))) | ||
|
|
||
| override def getDocumentIRI(ontologyIRI: IRI): IRI = | ||
| mappings.get(ontologyIRI) | ||
|
|
||
| } | ||
|
|
||
| private object CatalogXmlIRIMapper { | ||
|
|
||
| private def parseCatalogFile(catalogFile: File): java.util.Map[IRI, IRI] = | ||
| parseCatalogXml(new FileInputStream(catalogFile), Option(catalogFile.getParentFile)) | ||
|
|
||
| private def parseCatalogXml(catalogURL: URL): java.util.Map[IRI, IRI] = | ||
| if (catalogURL.getProtocol == "file") { | ||
| val catalogFile = new File(catalogURL.toURI) | ||
| parseCatalogXml(new FileInputStream(catalogFile), Option(catalogFile.getParentFile)) | ||
| } else parseCatalogXml(catalogURL.openStream(), None) | ||
|
|
||
| private def parseCatalogXml(inputStream: InputStream, parentFolder: Option[File]): java.util.Map[IRI, IRI] = { | ||
| val mappings = new HashMap[IRI, IRI]() | ||
| try { | ||
| val factory = SAXParserFactory.newInstance() | ||
| factory.setValidating(false) | ||
| factory.setNamespaceAware(true) | ||
| disableExternalXml(factory) | ||
| val saxParser = factory.newSAXParser() | ||
| saxParser.parse(inputStream, new CatalogElementHandler(parentFolder, mappings)) | ||
| mappings | ||
| } finally inputStream.close() | ||
| } | ||
|
|
||
| private def disableExternalXml(factory: SAXParserFactory): Unit = { | ||
| setFeatureIfSupported(factory, "http://xml.org/sax/features/external-general-entities", false) | ||
| setFeatureIfSupported(factory, "http://xml.org/sax/features/external-parameter-entities", false) | ||
| setFeatureIfSupported(factory, "http://apache.org/xml/features/nonvalidating/load-external-dtd", false) | ||
| try factory.setXIncludeAware(false) | ||
| catch { | ||
| case NonFatal(_) => () | ||
| } | ||
| } | ||
|
|
||
| private def setFeatureIfSupported(factory: SAXParserFactory, feature: String, value: Boolean): Unit = | ||
| try factory.setFeature(feature, value) | ||
| catch { | ||
| case NonFatal(_) => () | ||
| } | ||
|
|
||
| private final class CatalogElementHandler(parentFolder: Option[File], mappings: java.util.Map[IRI, IRI]) extends DefaultHandler { | ||
|
|
||
| override def startElement(uri: String, localName: String, qName: String, attributes: Attributes): Unit = { | ||
| if (elementName(localName, qName) != "uri") return | ||
|
|
||
| val fromString = attributeValue(attributes, "name") | ||
| val toString = attributeValue(attributes, "uri") | ||
| if ((fromString == null) || (toString == null)) return | ||
|
|
||
| mappedIRI(toString).foreach { toIRI => | ||
| mappings.put(IRI.create(fromString), toIRI) | ||
| } | ||
| } | ||
|
|
||
| private def elementName(localName: String, qName: String): String = | ||
| if (localName != null && localName.nonEmpty) localName | ||
| else Option(qName).fold("")(_.split(':').last) | ||
|
|
||
| private def attributeValue(attributes: Attributes, name: String): String = | ||
| Option(attributes.getValue("", name)).orElse(Option(attributes.getValue(name))).orNull | ||
|
|
||
| private def mappedIRI(value: String): Option[IRI] = | ||
| parentFolder.filter(_ => !value.contains(":")) match { | ||
| case Some(parent) => | ||
| try { | ||
| val file = new File(value) | ||
| val resolved = if (file.isAbsolute) file else new File(parent, value) | ||
| Some(IRI.create(resolved.getCanonicalFile)) | ||
| } catch { | ||
| case NonFatal(_) => None | ||
| } | ||
| case None => Some(IRI.create(value)) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/scala/org/monarchinitiative/dosdp/ManchesterSyntaxOWLObjectRenderer.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package org.monarchinitiative.dosdp | ||
|
|
||
| import org.semanticweb.owlapi.apibinding.OWLManager | ||
| import org.semanticweb.owlapi.io.OWLObjectRenderer | ||
| import org.semanticweb.owlapi.manchestersyntax.renderer.ManchesterOWLSyntaxObjectRenderer | ||
| import org.semanticweb.owlapi.model.{IRI, OWLDataVisitor, OWLLiteral, OWLObject} | ||
| import org.semanticweb.owlapi.util.{ShortFormProvider, SimpleShortFormProvider} | ||
| import org.semanticweb.owlapi.vocab.XSDVocabulary | ||
|
|
||
| import java.io.{StringWriter, Writer} | ||
|
|
||
| /** | ||
| * Manchester syntax renderer that applies short forms to bare IRIs. | ||
| * | ||
| * Adapted from owl-diff's `ManchesterSyntaxOWLObjectRenderer`. The | ||
| * literal escaping is implemented locally so this | ||
| * project does not need owl-diff's transitive `commons-text` dependency. | ||
| */ | ||
| class ManchesterSyntaxOWLObjectRenderer extends OWLObjectRenderer { | ||
|
|
||
| import ManchesterSyntaxOWLObjectRenderer._ | ||
|
|
||
| private object WriterDelegate extends Writer { | ||
|
|
||
| private var delegate = new StringWriter() | ||
|
|
||
| def reset(): Unit = delegate = new StringWriter() | ||
|
|
||
| override def toString: String = delegate.getBuffer.toString | ||
|
|
||
| override def close(): Unit = delegate.close() | ||
|
|
||
| override def flush(): Unit = delegate.flush() | ||
|
|
||
| override def write(cbuf: Array[Char], off: Int, len: Int): Unit = delegate.write(cbuf, off, len) | ||
|
|
||
| } | ||
|
|
||
| private var renderer: ManchesterOWLSyntaxObjectRenderer = new BetterIRIRenderer(WriterDelegate, new SimpleShortFormProvider()) | ||
|
|
||
| override def render(obj: OWLObject): String = synchronized { | ||
| WriterDelegate.reset() | ||
| obj.accept(renderer) | ||
| WriterDelegate.toString | ||
| } | ||
|
|
||
| override def setShortFormProvider(shortFormProvider: ShortFormProvider): Unit = synchronized { | ||
| renderer = new BetterIRIRenderer(WriterDelegate, shortFormProvider) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| object ManchesterSyntaxOWLObjectRenderer { | ||
|
|
||
| private val factory = OWLManager.getOWLDataFactory | ||
|
|
||
| private def escapeHtmlText(text: String): String = | ||
| text.flatMap { | ||
| case '&' => "&" | ||
| case '<' => "<" | ||
| case '>' => ">" | ||
| case '"' => """ | ||
| case char => char.toString | ||
| } | ||
|
|
||
| class BetterIRIRenderer(writer: Writer, entityShortFormProvider: ShortFormProvider) extends ManchesterOWLSyntaxObjectRenderer(writer, entityShortFormProvider) { | ||
|
|
||
| override def visit(iri: IRI): Unit = visit(factory.getOWLClass(iri)) | ||
|
|
||
| override def visit(node: OWLLiteral): Unit = { | ||
|
|
||
| // xsd:decimal is the default datatype for literal forms like "33.3" | ||
| // with no specified datatype | ||
| if (XSDVocabulary.DECIMAL.getIRI.equals(node.getDatatype.getIRI)) { | ||
| write(node.getLiteral) | ||
| } else if (node.getDatatype.isFloat) { | ||
| write(node.getLiteral) | ||
| write("f") | ||
| } else if (node.getDatatype.isInteger) { | ||
| write(node.getLiteral) | ||
| } else if (node.getDatatype.isBoolean) { | ||
| write(node.getLiteral) | ||
| } else { | ||
| pushTab(getIndent) | ||
| write("\"") | ||
| write(escapeHtmlText(node.getLiteral)) | ||
| write("\"") | ||
| if (node.hasLang()) { | ||
| write("@") | ||
| write(node.getLang) | ||
| } else if (!node.isRDFPlainLiteral) { | ||
| write("^^") | ||
| node.getDatatype.accept(this: OWLDataVisitor) | ||
| } | ||
| popTab() | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/scala/org/monarchinitiative/dosdp/MarkdownLinkShortFormProvider.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package org.monarchinitiative.dosdp | ||
|
|
||
| import org.semanticweb.owlapi.model.OWLEntity | ||
| import org.semanticweb.owlapi.util.ShortFormProvider | ||
|
|
||
| /** | ||
| * Short-form provider that renders OWL entities as Markdown links. | ||
| * | ||
| * Adapted from owl-diff's `MarkdownLinkShortFormProvider`. Keeping | ||
| * this local avoids depending on all of `owl-diff` for | ||
| * one small docs-rendering helper. | ||
| */ | ||
| class MarkdownLinkShortFormProvider(labelProvider: ShortFormProvider) extends ShortFormProvider { | ||
|
|
||
| override def getShortForm(entity: OWLEntity): String = { | ||
| val label = labelProvider.getShortForm(entity) | ||
| val iri = entity.getIRI.toString | ||
| s"[$label]($iri)" | ||
| } | ||
|
|
||
| override def dispose(): Unit = () | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.