stevedlawrence commented on code in PR #878:
URL: https://github.com/apache/daffodil/pull/878#discussion_r1033685351
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -31,7 +31,7 @@ import java.nio.charset.StandardCharsets
import java.util.Scanner
import java.util.concurrent.Executors
import javax.xml.parsers.DocumentBuilderFactory
-import javax.xml.transform.TransformerFactory
+import javax.xml.transform.{ TransformerFactory, TransformerException }
Review Comment:
Can you split these to two lines? I think we've started moving away from
multiple imports on a single line.
##########
daffodil-cli/src/it/scala/org/apache/daffodil/exi/TestEXIEncodeDecode.scala:
##########
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.daffodil.saving
+
+import org.junit.Test
+import scala.xml.XML
+import java.nio.file.Paths
+
+import org.apache.daffodil.CLI.Util._
+import org.apache.daffodil.Main.ExitCode
+import org.apache.daffodil.xml.XMLUtils
+
+class TestCLIEncodeDecodeEXI {
+
+ @Test def test_3017_CLI_Encode_Decode_EXI_SA(): Unit = {
+ val schema =
path("daffodil-test/src/test/resources/org/apache/daffodil/usertests/Book2.dfdl.xsd")
+ val infosetPath =
path("daffodil-test/src/test/resources/org/apache/daffodil/usertests/test_Book2.expected.xml")
+ val infosetXML = XML.loadFile(infosetPath.toFile)
+
+ withTempDir { tempDir =>
+ val tempEXI = Paths.get(tempDir.toString, "temp.exi")
+ val tempXML = Paths.get(tempDir.toString, "temp.xml")
+
+ // Encode infoset to schema aware EXI
+ runCLI(args"exi -s $schema -o $tempEXI") { cli =>
+ cli.sendLine(infosetXML.toString, inputDone = true)
+ } (ExitCode.Success)
+
+ // Decode EXI to XML and compare against original XML infoset
+ runCLI(args"exi -d -s $schema -o $tempXML $tempEXI") { cli =>
+ } (ExitCode.Success)
+
+ val resultNode = XML.loadFile(tempXML.toFile)
+ XMLUtils.compareAndReport(infosetXML, resultNode)
+ }
+ }
+
+ @Test def test_3017_CLI_Encode_Decode_EXI(): Unit = {
+ val inputXML = <person><name>Edward</name><age>42</age></person>
+
+ withTempDir { tempDir =>
+ val tempEXI = Paths.get(tempDir.toString, "temp.exi")
+ val tempXML = Paths.get(tempDir.toString, "temp.xml")
+
+ runCLI(args"exi -o $tempEXI") { cli =>
+ cli.sendLine(inputXML.toString, inputDone = true)
+ } (ExitCode.Success)
+
+ runCLI(args"exi -d -o $tempXML $tempEXI") { cli =>
+ } (ExitCode.Success)
+
+ val resultNode = XML.loadFile(tempXML.toFile)
+ XMLUtils.compareAndReport(inputXML, resultNode)
+ }
+ }
+
+ @Test def test_3017_CLI_EncodeBadFile_EXI(): Unit = {
+ val badXML =
path("daffodil-test/src/test/resources/org/apache/daffodil/usertests/Book2.csv")
+
+ runCLI(args"exi $badXML") { cli =>
+ cli.expectErr("Error parsing input XML")
+ } (ExitCode.Failure)
+ }
+
+ @Test def test_3017_CLI_DecodeBadFile_EXI(): Unit = {
+ val badEXI =
path("daffodil-test/src/test/resources/org/apache/daffodil/usertests/Book2.csv")
+
+ runCLI(args"exi -d $badEXI") { cli =>
+ cli.expectErr("No valid EXI document")
+ } (ExitCode.Failure)
+ }
+
+ @Test def test_3017_CLI_LoadBadSchema_EXI(): Unit = {
+ val badSchema =
path("daffodil-test/src/test/resources/org/apache/daffodil/usertests/Book2.csv")
+
+ runCLI(args"exi -s $badSchema") { cli =>
+ cli.expectErr("Error creating EXI grammar for the supplied schema")
+ } (ExitCode.Failure)
+ }
+}
Review Comment:
Thoughts on two additional tests/checks:
1. Show that it errors if you try to encode an XML file with the `-s`
option, but the XML doesn't match the provided schema
1. Show that it errors if you try to decode a schema aware EXI file, but
either no `-s` option was given or it was but it doesn't match what the EXI
file was encoded with
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -514,12 +517,30 @@ class CLIConf(arguments: Array[String]) extends
scallop.ScallopConf(arguments) {
requireSubcommand()
}
+ // Encode or decode EXI Subcommand Options
+ object exi extends scallop.Subcommand("exi") {
+ banner("""|Usage: daffodil exi [-d] [-s <schema>] [-o <output>] [infile]
+ |
+ |Encode/decode an XML file with EXI. If a schema is specified,
it will use schema aware encoding/decoding.
+ |
+ |EncodeEXI Options:""".stripMargin)
+
+ descr("Encode an XML file with EXI")
+ helpWidth(width)
+
+ val output = opt[String](argName = "file", descr = "Output file to write
the encoded/decoded file to.")
+ val schema = opt[URI]("schema", argName = "file", descr = "DFDL schema to
use for schema aware encoding/decoding.")(fileResourceURIConverter)
+ val infile = trailArg[String](required = false, descr = "Input XML file to
encode. If not specified, or a value of -, reads from stdin.")
Review Comment:
Can we move infile to the end of the options (i.e. after decode) like we do
with the other subcommands? Makes it more clear that it's a trailing argument.
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -1482,6 +1503,78 @@ object Main {
}
}
+ case Some(conf.exi) => {
+ 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 = try {
+ if (exiOpts.schema.isDefined)
+ getExiFactoryOpt(InfosetType.EXISA, exiOpts.schema.toOption)
+ else
+ getExiFactoryOpt(InfosetType.EXI, exiOpts.schema.toOption)
+ } catch {
+ case e: EXIException => {
+ Logger.log.error("Error creating EXI grammar for the supplied
schema: %s".format(Misc.getSomeMessage(e).get))
+ return ExitCode.Failure
+ }
+ }
+
+ if (exiOpts.decode.toOption.get) { // Decoding EXI file to XML
+ val exiSource = new EXISource(exiFactory.get)
+ exiSource.setInputSource(input)
+
+ val result = new StreamResult(output)
+ val tf = TransformerFactory.newInstance()
+ val transformer = tf.newTransformer
+ try {
+ transformer.transform(exiSource, result)
+ } catch {
+ case t: TransformerException => {
+ Logger.log.error("Error decoding EXI input:
%s".format(Misc.getSomeMessage(t).get))
Review Comment:
We've been trying to use the `s` string interpolator, it avoids issues where
the format options don't match the format string. So instead this could be
something like this:
```scala
Logger.log.error(s"Error decoding EXI input: ${ Misc.getSomeMessage(t) }")
```
Same with other log messages below.
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -1482,6 +1503,78 @@ object Main {
}
}
+ case Some(conf.exi) => {
+ 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 = try {
+ if (exiOpts.schema.isDefined)
+ getExiFactoryOpt(InfosetType.EXISA, exiOpts.schema.toOption)
+ else
+ getExiFactoryOpt(InfosetType.EXI, exiOpts.schema.toOption)
+ } catch {
+ case e: EXIException => {
+ Logger.log.error("Error creating EXI grammar for the supplied
schema: %s".format(Misc.getSomeMessage(e).get))
+ return ExitCode.Failure
Review Comment:
It might make things a bit more complicated, but we should avoid the use of
`return`. Same with other uses of return below.
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -514,12 +517,30 @@ class CLIConf(arguments: Array[String]) extends
scallop.ScallopConf(arguments) {
requireSubcommand()
}
+ // Encode or decode EXI Subcommand Options
+ object exi extends scallop.Subcommand("exi") {
+ banner("""|Usage: daffodil exi [-d] [-s <schema>] [-o <output>] [infile]
+ |
+ |Encode/decode an XML file with EXI. If a schema is specified,
it will use schema aware encoding/decoding.
+ |
+ |EncodeEXI Options:""".stripMargin)
+
+ descr("Encode an XML file with EXI")
+ helpWidth(width)
+
+ val output = opt[String](argName = "file", descr = "Output file to write
the encoded/decoded file to.")
Review Comment:
In our other `output` options we add `If not given or is -, infoset is
written to stdout.` I believe this has the same behavior.
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -1482,6 +1503,78 @@ object Main {
}
}
+ case Some(conf.exi) => {
+ 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 = try {
+ if (exiOpts.schema.isDefined)
+ getExiFactoryOpt(InfosetType.EXISA, exiOpts.schema.toOption)
+ else
+ getExiFactoryOpt(InfosetType.EXI, exiOpts.schema.toOption)
+ } catch {
+ case e: EXIException => {
+ Logger.log.error("Error creating EXI grammar for the supplied
schema: %s".format(Misc.getSomeMessage(e).get))
+ return ExitCode.Failure
+ }
+ }
+
+ if (exiOpts.decode.toOption.get) { // Decoding EXI file to XML
+ val exiSource = new EXISource(exiFactory.get)
+ exiSource.setInputSource(input)
+
+ val result = new StreamResult(output)
+ val tf = TransformerFactory.newInstance()
+ val transformer = tf.newTransformer
+ try {
+ transformer.transform(exiSource, result)
+ } catch {
+ case t: TransformerException => {
+ Logger.log.error("Error decoding EXI input:
%s".format(Misc.getSomeMessage(t).get))
+ return ExitCode.Failure
+ }
+ case s: org.xml.sax.SAXException => {
+ Logger.log.error("Error decoding EXI input:
%s".format(Misc.getSomeMessage(s).get))
+ return ExitCode.Failure
+ }
+ case e: EXIException => {
+ Logger.log.error("Error decoding EXI input:
%s".format(Misc.getSomeMessage(e).get))
+ return ExitCode.Failure
Review Comment:
It would be nice if we could have tests that hit these errors. I suggested
some that might cover some of these.
--
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]