You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@daffodil.apache.org by sl...@apache.org on 2019/05/14 12:09:06 UTC

[incubator-daffodil] branch master updated: Do not abort when an import/include fails

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

slawrence pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-daffodil.git


The following commit(s) were added to refs/heads/master by this push:
     new 3e648b3  Do not abort when an import/include fails
3e648b3 is described below

commit 3e648b3b94a169a84386069cbc696fd856ec6375
Author: Steve Lawrence <sl...@apache.org>
AuthorDate: Fri May 10 09:55:29 2019 -0400

    Do not abort when an import/include fails
    
    When using XML Catalogs, it's very easy for import/includes to
    resolve to URI's that do not exist. This results in an IOException
    when trying to open the stream. There are also other cases related
    to IO that could causing opening of the stream to fail. When these
    failures happen, we currently abort. This is wrong, since these
    exception are expected to be able to happen.
    
    Instead of aborting with an assert when an exception occurs, we should
    instead just return null. Xerces then figures out what the problem is
    and returns an error that Daffodil handles and converts to and SDE.
    
    DAFFODIL-2126
---
 .../daffodil/CLI/xcatalog_import_failure.dfdl.xsd  | 27 +++++++++++++++++++
 .../org/apache/daffodil/CLI/xcatalog_invalid.xml   | 21 +++++++++++++++
 .../apache/daffodil/parsing/TestCLIParsing.scala   | 31 ++++++++++++++++++++++
 .../apache/daffodil/xml/DaffodilXMLLoader.scala    | 15 +++++------
 4 files changed, 86 insertions(+), 8 deletions(-)

diff --git a/daffodil-cli/src/it/resources/org/apache/daffodil/CLI/xcatalog_import_failure.dfdl.xsd b/daffodil-cli/src/it/resources/org/apache/daffodil/CLI/xcatalog_import_failure.dfdl.xsd
new file mode 100644
index 0000000..a12285d
--- /dev/null
+++ b/daffodil-cli/src/it/resources/org/apache/daffodil/CLI/xcatalog_import_failure.dfdl.xsd
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+
+<xs:schema
+  xmlns:xs="http://www.w3.org/2001/XMLSchema"
+  xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/"
+  xmlns:tns="http://example.com" 
+  targetNamespace="http://example.com">
+
+  <xs:import namespace="http://example.com/xcatalog" />
+
+</xs:schema>
diff --git a/daffodil-cli/src/it/resources/org/apache/daffodil/CLI/xcatalog_invalid.xml b/daffodil-cli/src/it/resources/org/apache/daffodil/CLI/xcatalog_invalid.xml
new file mode 100644
index 0000000..07a2e11
--- /dev/null
+++ b/daffodil-cli/src/it/resources/org/apache/daffodil/CLI/xcatalog_invalid.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  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.
+-->
+
+<catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog">
+  <uri name="http://example.com/xcatalog" uri="file:/this/path/does/not/exist" />
+</catalog>
diff --git a/daffodil-cli/src/it/scala/org/apache/daffodil/parsing/TestCLIParsing.scala b/daffodil-cli/src/it/scala/org/apache/daffodil/parsing/TestCLIParsing.scala
index fce9caf..9ba3a83 100644
--- a/daffodil-cli/src/it/scala/org/apache/daffodil/parsing/TestCLIParsing.scala
+++ b/daffodil-cli/src/it/scala/org/apache/daffodil/parsing/TestCLIParsing.scala
@@ -989,4 +989,35 @@ class TestCLIparsing {
     }
   }
 
+  @Test def test_CLI_Parsing_XCatalog_Resolution_Failure() {
+    val schemaFile = Util.daffodilPath("daffodil-cli/src/it/resources/org/apache/daffodil/CLI/xcatalog_import_failure.dfdl.xsd")
+    val testSchemaFile = if (Util.isWindows) Util.cmdConvert(schemaFile) else schemaFile
+
+    val xcatalogFile = Util.daffodilPath("daffodil-cli/src/it/resources/org/apache/daffodil/CLI/xcatalog_invalid.xml")
+    val testXcatalogFile = if (Util.isWindows) Util.cmdConvert(xcatalogFile) else xcatalogFile
+
+    val DAFFODIL_JAVA_OPTS = Map("DAFFODIL_JAVA_OPTS" -> ("-Dxml.catalog.files=" + testXcatalogFile + " -Xms256m -Xmx2048m -Djline.terminal=jline.UnsupportedTerminal -Dfile.encoding=UTF-8"))
+
+    val shell = Util.startIncludeErrors("", envp = DAFFODIL_JAVA_OPTS)
+
+    try {
+      val cmd = String.format(Util.echoN("X") + "| %s parse -s %s", Util.binPath, testSchemaFile)
+      shell.sendLine(cmd)
+
+      shell.expectIn(1, contains("Schema Definition Error"))
+      if (Util.isWindows) {
+        shell.expectIn(1, contains("\\this\\path\\does\\not\\exist"))
+        shell.expectIn(1, contains("The system cannot find the path specified"))
+      } else {
+        shell.expectIn(1, contains("/this/path/does/not/exist"))
+        shell.expectIn(1, contains("No such file or directory"))
+      }
+
+      shell.sendLine("exit")
+      shell.expect(eof)
+    } finally {
+      shell.close()
+    }
+  }
+
 }
diff --git a/daffodil-lib/src/main/scala/org/apache/daffodil/xml/DaffodilXMLLoader.scala b/daffodil-lib/src/main/scala/org/apache/daffodil/xml/DaffodilXMLLoader.scala
index 3433d52..5830f41 100644
--- a/daffodil-lib/src/main/scala/org/apache/daffodil/xml/DaffodilXMLLoader.scala
+++ b/daffodil-lib/src/main/scala/org/apache/daffodil/xml/DaffodilXMLLoader.scala
@@ -237,14 +237,13 @@ class DFDLCatalogResolver private ()
     optURI match {
       case None => null
       case Some(uri) => {
-        val resourceAsStream =
-          try {
-            uri.toURL.openStream() // This will work.
-          } catch {
-            case _: java.io.IOException => Assert.invariantFailed("found resource but couldn't open")
-          }
-        val input = new Input(publicId, uri.toString, new BufferedInputStream(resourceAsStream))
-        input
+        try {
+          val resourceAsStream = uri.toURL.openStream()
+          val input = new Input(publicId, uri.toString, new BufferedInputStream(resourceAsStream))
+          input
+        } catch {
+          case _: java.io.IOException => null
+        }
       }
     }
   }