-
Notifications
You must be signed in to change notification settings - Fork 2
Implement the base version of the Rdf ToJelly command #42
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 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a72d32
Add tests
Karolina-Bogacka 0346d3b
Fix tests by fixing CleanUpAfterTest
Karolina-Bogacka c60e4fe
Store current code state
Karolina-Bogacka 16daac9
Fix problems with parallel execution of test suites
Karolina-Bogacka 86e25aa
Add further fixes
Karolina-Bogacka c072015
Fix IO problems in tests
Karolina-Bogacka d22d347
Delete unnecessary mechanisms; Mock stdin through JellyCommand
Karolina-Bogacka 62220ac
Final comment cleanup etc
Karolina-Bogacka 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
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
5 changes: 4 additions & 1 deletion
5
src/main/scala/eu/neverblink/jelly/cli/command/rdf/RdfCommandPrintUtil.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
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
84 changes: 84 additions & 0 deletions
84
src/main/scala/eu/neverblink/jelly/cli/command/rdf/RdfToJelly.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,84 @@ | ||
| package eu.neverblink.jelly.cli.command.rdf | ||
| import caseapp.* | ||
| import com.google.protobuf.InvalidProtocolBufferException | ||
| import eu.neverblink.jelly.cli.* | ||
| import eu.neverblink.jelly.cli.command.rdf.RdfFormatOption.* | ||
| import eu.ostrzyciel.jelly.convert.jena.riot.JellyLanguage | ||
| import eu.ostrzyciel.jelly.core.RdfProtoSerializationError | ||
| import org.apache.jena.riot.system.StreamRDFWriter | ||
| import org.apache.jena.riot.{RDFLanguages, RDFParser, RiotException} | ||
|
|
||
| import java.io.{InputStream, OutputStream} | ||
|
|
||
| object RdfToJellyPrint extends RdfCommandPrintUtil: | ||
| override val defaultFormat: RdfFormatOption = JellyBinary | ||
|
|
||
| case class RdfToJellyOptions( | ||
| @Recurse | ||
| common: JellyOptions = JellyOptions(), | ||
| @ExtraName("to") outputFile: Option[String] = None, | ||
| @ValueDescription("Input format.") | ||
| @HelpMessage( | ||
| RdfToJellyPrint.helpMsg, | ||
| ) | ||
| @ExtraName("in-format") inputFormat: Option[String] = None, | ||
| ) extends HasJellyOptions | ||
|
|
||
| object RdfToJelly extends JellyCommand[RdfToJellyOptions]: | ||
| override def group = "rdf" | ||
|
|
||
| override def names: List[List[String]] = List( | ||
| List("rdf", "to-jelly"), | ||
| ) | ||
|
|
||
| override def doRun(options: RdfToJellyOptions, remainingArgs: RemainingArgs): Unit = | ||
| val (inputStream, outputStream) = | ||
| matchIOStreams(remainingArgs.remaining.headOption, options.outputFile) | ||
| doConversion(inputStream, outputStream, options.inputFormat) | ||
|
|
||
| /** This method takes care of proper error handling and matches the desired output format to the | ||
| * correct conversion | ||
| * | ||
| * @param inputStream | ||
| * InputStream | ||
| * @param outputStream | ||
| * OutputStream | ||
| * @throws JellySerializationError | ||
| * @throws JenaRiotException | ||
| * @throws InvalidJellyFile | ||
| */ | ||
| private def doConversion( | ||
| inputStream: InputStream, | ||
| outputStream: OutputStream, | ||
| format: Option[String], | ||
| ): Unit = | ||
| try { | ||
| format match { | ||
| case Some(f: String) => | ||
| RdfFormatOption.find(f) match | ||
| case Some(NQuads) => nQuadToJelly(inputStream, outputStream) | ||
| case _ => | ||
| throw InvalidFormatSpecified( | ||
| f, | ||
| RdfToJellyPrint.validFormatsString, | ||
| ) // if anything else, it's an invalid option | ||
| case None => | ||
| nQuadToJelly(inputStream, outputStream) // default option if no parameter supplied | ||
| } | ||
| } catch | ||
| case e: RdfProtoSerializationError => | ||
| throw JellySerializationError(e.getMessage) | ||
| case e: RiotException => | ||
| throw JenaRiotException(e) | ||
| case e: InvalidProtocolBufferException => | ||
| throw InvalidJellyFile(e) | ||
|
|
||
| /** This method reads the NQuad file, rewrites it to Jelly and writes it to some output stream | ||
| * @param inputStream | ||
| * InputStream | ||
| * @param outputStream | ||
| * OutputStream | ||
| */ | ||
| private def nQuadToJelly(inputStream: InputStream, outputStream: OutputStream): Unit = | ||
| val jellyWriter = StreamRDFWriter.getWriterStream(outputStream, JellyLanguage.JELLY) | ||
| RDFParser.source(inputStream).lang(RDFLanguages.NQUADS).parse(jellyWriter) |
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
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.