You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by ja...@apache.org on 2022/12/20 02:34:08 UTC

[daffodil] branch main updated: Add custom error handler for CLI EXI encode/decode

This is an automated email from the ASF dual-hosted git repository.

jadams pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/daffodil.git


The following commit(s) were added to refs/heads/main by this push:
     new 0638f3e39 Add custom error handler for CLI EXI encode/decode
0638f3e39 is described below

commit 0638f3e390f648525c806cafce465f09f8e7dfb5
Author: Josh Adams <ja...@tresys.com>
AuthorDate: Sun Dec 18 18:15:54 2022 -0500

    Add custom error handler for CLI EXI encode/decode
    
    Adding custom error handler that implements SAX ErrorHandler and
    Transform ErrorListener helps contain some of the error messages that
    were getting printed to stderr and were polluting the output of our
    integration tests.
    
    There is still an issue with one of the tests causing a stack trace
    from an IllegalArgumentException to be printed that occurs when decoding a
    schema aware encoded EXI file with a non-schema aware decoder. This appears to
    be getting printed directly to stderr from within Exificient. The ticket
    https://github.com/EXIficient/exificient/issues/33 is tracking the issue
    on Exificient's side.
    
    DAFFODIL-2750
---
 .../src/main/scala/org/apache/daffodil/Main.scala  | 34 +++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala b/daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala
index a5659fd68..3afea040a 100644
--- a/daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala
+++ b/daffodil-cli/src/main/scala/org/apache/daffodil/Main.scala
@@ -29,6 +29,7 @@ import java.nio.file.Paths
 import java.util.Scanner
 import java.util.concurrent.Executors
 import javax.xml.transform.TransformerFactory
+import javax.xml.transform.TransformerException
 import javax.xml.transform.stream.StreamResult
 
 import scala.concurrent.Await
@@ -51,6 +52,7 @@ import org.rogach.scallop.ValueConverter
 import org.rogach.scallop.exceptions.GenericScallopException
 
 import org.xml.sax.InputSource
+import org.xml.sax.SAXParseException
 import org.xml.sax.helpers.XMLReaderFactory
 
 import com.siemens.ct.exi.core.EXIFactory
@@ -1300,13 +1302,15 @@ object Main {
             val result = new StreamResult(output)
             val tf = TransformerFactory.newInstance()
             val transformer = tf.newTransformer
+            transformer.setErrorListener(new EXIErrorHandler)
             try {
               transformer.transform(exiSource, result)
             } catch {
               /* We catch a generic Exception here as Exificient will attempt
                * to decode anything and will throw very generic errors, such as
                * an IllegalArgumentException when it runs into a series of bytes
-               * that aren't a Unicode codepoint. */
+               * that aren't a Unicode codepoint. This should be removed once
+               * https://github.com/EXIficient/exificient/issues/33 is fixed.*/
               case e: Exception => {
                 Logger.log.error(s"Error decoding EXI input: ${ Misc.getSomeMessage(e).get }")
                 rc = ExitCode.Failure
@@ -1319,6 +1323,7 @@ object Main {
 
             val reader = XMLReaderFactory.createXMLReader()
             reader.setContentHandler(exiResult.getHandler)
+            reader.setErrorHandler(new EXIErrorHandler)
             try {
               reader.parse(input)
             } catch {
@@ -1480,3 +1485,30 @@ object Main {
     System.exit(exitCode.id)
   }
 }
+
+class EXIErrorHandler extends org.xml.sax.ErrorHandler with javax.xml.transform.ErrorListener {
+
+  // SAX ErrorHandler methods
+  def error(e: SAXParseException): Unit = {
+    Logger.log.error(e.getMessage)
+  }
+
+  def fatalError(e: SAXParseException): Unit = {
+    Logger.log.error(e.getMessage)
+  }
+
+  def warning(e: SAXParseException): Unit = {
+    Logger.log.warn(e.getMessage)
+  }
+
+  // Transformer ErrorListener methods
+  def error(e: TransformerException): Unit = {
+    Logger.log.error(e.getMessage)
+  }
+  def fatalError(e: TransformerException): Unit = {
+    Logger.log.error(e.getMessage)
+  }
+  def warning(e: TransformerException): Unit = {
+    Logger.log.warn(e.getMessage)
+  }
+}