tuxji commented on code in PR #878:
URL: https://github.com/apache/daffodil/pull/878#discussion_r1026907624
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -1482,6 +1518,74 @@ object Main {
}
}
+ case Some(conf.encodeEXI) => {
+ val encodeOpts = conf.encodeEXI
Review Comment:
I'm puzzled why we're seeing codecov check warnings that some lines are not
covered by tests, when we do have a new test.
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -1482,6 +1518,74 @@ object Main {
}
}
+ case Some(conf.encodeEXI) => {
+ val encodeOpts = conf.encodeEXI
+ val channel = encodeOpts.output.toOption match {
+ case Some("-") | None => Channels.newChannel(STDOUT)
+ case Some(file) => new FileOutputStream(file).getChannel()
+ }
+ val output = Channels.newOutputStream(channel)
+
+ val inputStream = encodeOpts.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 = {
+ if (encodeOpts.schema.isDefined)
+ getExiFactoryOpt(InfosetType.EXISA, encodeOpts.schema.toOption)
+ else
+ getExiFactoryOpt(InfosetType.EXI, encodeOpts.schema.toOption)
+ }
+ val exiResult = new EXIResult(exiFactory.get)
+ exiResult.setOutputStream(output)
+
+ val reader = XMLReaderFactory.createXMLReader()
+ reader.setContentHandler(exiResult.getHandler)
+ reader.parse(input)
+ output.close
Review Comment:
I would like to see tests which encode an invalid XML file and decode an
invalid EXI file to make sure they get an error message, not a bug stack trace.
I'm sure people will try to encode an EXI file and decode an XML file the
wrong way around and they should see an appropriate error message.
##########
daffodil-cli/src/it/scala/org/apache/daffodil/CLI/Util.scala:
##########
@@ -384,6 +385,7 @@ object Util {
def expect(matcher: Matcher[_]): Result = expect.expect(matcher)
def expect(string: String): Result = expect.expect(contains(string))
+ def expectEOF(): Result = expect.expect(eof())
Review Comment:
If you decide to stop calling expectEOF and getBefore and write to a temp
file instead as Steve suggested, then you'll want to revert your changes here.
##########
daffodil-cli/src/it/scala/org/apache/daffodil/exi/TestEXIEncodeDecode.scala:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.{Node, XML}
+
+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/section06/namespaces/multi_base_15.dfdl.xsd")
+
+ withTempFile { tempEXI =>
+
+ // Create initial XML infoset
+ var infosetXML: Node = null
+ var encodedMD5: String = null
+ runCLI(args"parse -s $schema") { cli =>
+ cli.send("test", inputDone = true)
+ val res = cli.expectEOF
+ infosetXML = XML.loadString(res.getBefore)
+ } (ExitCode.Success)
+
+ // Encode infoset to schema aware EXI
+ runCLI(args"encodeEXI -s $schema -o $tempEXI") { cli =>
+ cli.sendLine(infosetXML.toString, inputDone = true)
+ encodedMD5 = md5sum(tempEXI)
+ } (ExitCode.Success)
+
+ // Decode EXI to XML and compare against original XML infoset
+ runCLI(args"decodeEXI -s $schema $tempEXI") { cli =>
+ val res = cli.expectEOF
+ val resultNode = XML.loadString(res.getBefore)
Review Comment:
Once you write to a tempFile, you can use commons-io to compare two temp
files by calling
[FileUtils.contentsEquals](https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#contentEquals(java.io.File,%20java.io.File)).
##########
daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala:
##########
@@ -514,12 +516,46 @@ class CLIConf(arguments: Array[String]) extends
scallop.ScallopConf(arguments) {
requireSubcommand()
}
+ // Encode EXI Subcommand Options
+ object encodeEXI extends scallop.Subcommand("encodeEXI") {
+ banner("""|Usage: daffodil encodeEXI [-s <schema>] [-o <output>] [infile]
Review Comment:
Yes, I think it's better to have a single subcommand `exi` which normally
encodes XML to EXI and optionally decodes EXI to XML with the `-d` or
`--decode` options.
--
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]