stevedlawrence commented on code in PR #878:
URL: https://github.com/apache/daffodil/pull/878#discussion_r1033961185
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -1236,8 +1266,77 @@ object Main {
}
}
- // Required to avoid "match may not be exhaustive", but should never
happen
- case _ => Assert.impossible()
+ case Some(conf.exi) => {
+ var rc = ExitCode.Success
+ val exiOpts = conf.exi
+ val channel = exiOpts.output.toOption match {
+ case Some("-") | None => Channels.newChannel(STDOUT)
+ case Some(file) => new FileOutputStream(file).getChannel()
+ }
+ val output = Channels.newOutputStream(channel)
+
+ val inputStream = exiOpts.infile.toOption match {
+ case Some("-") | None => STDIN
+ case Some(file) => {
+ val f = new File(file)
+ new FileInputStream(f)
+ }
+ }
+ val input = new InputSource(inputStream)
+
+ val exiFactory = DefaultEXIFactory.newInstance
+ if (exiOpts.schema.isDefined) try {
+ val grammarFactory = GrammarFactory.newInstance
+ val grammar =
grammarFactory.createGrammars(exiOpts.schema.toOption.get.toString,
DFDLCatalogResolver.get)
+ exiFactory.setGrammars(grammar)
+ } catch {
+ case e: EXIException => {
+ Logger.log.error(s"Error creating EXI grammar for the supplied
schema: ${ Misc.getSomeMessage(e).get }")
+ rc = ExitCode.Failure
+ }
+ }
+
+ (exiOpts.decode.toOption.get, rc) match {
+ case (true, ExitCode.Success) => { // Decoding
+ val exiSource = new EXISource(exiFactory)
+ exiSource.setInputSource(input)
+
+ val result = new StreamResult(output)
+ val tf = TransformerFactory.newInstance()
+ val transformer = tf.newTransformer
+ try {
+ transformer.transform(exiSource, result)
+ } catch {
+ case e: Exception => {
+ Logger.log.error(s"Error decoding EXI input: ${
Misc.getSomeMessage(e).get }")
+ rc = ExitCode.Failure
+ }
+ }
+ }
+ case (false, ExitCode.Success) => { // Encoding
+ val exiResult = new EXIResult(exiFactory)
+ exiResult.setOutputStream(output)
+
+ val reader = XMLReaderFactory.createXMLReader()
+ reader.setContentHandler(exiResult.getHandler)
+ try {
+ reader.parse(input)
+ } catch {
+ case s: org.xml.sax.SAXException => {
+ Logger.log.error(s"Error parsing input XML: ${
Misc.getSomeMessage(s).get }")
+ rc = ExitCode.Failure
+ }
+ }
+ output.close
+ }
+ case (_, _) => // Hit an exception creating exiFactory, rc already
set
+ }
+
+ rc
+ }
+
+ // Required to avoid "match may not be exhaustive", but should never
happen
+ case _ => Assert.impossible()
Review Comment:
I think the indentation is off with this case?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]