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 2022/12/22 13:22:49 UTC

[daffodil] branch main updated: Require the first schema to include the DFDL namespace.

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

slawrence 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 c1a737ebe Require the first schema to include the DFDL namespace.
c1a737ebe is described below

commit c1a737ebe894911fbc57d29f3eec834093342f81
Author: Mike McGann <mm...@owlcyberdefense.com>
AuthorDate: Fri Dec 16 09:29:16 2022 -0500

    Require the first schema to include the DFDL namespace.
    
    If a schema document is passed to Daffodil that does not contain a
    DFDL namespace, it is currently ignored and a warning is emitted.
    If this is the only document then no operation actually occurs and
    this condition can be masked when warnings are disabled. Enforce
    the first schema to include the DFDL namespace but emit warnings
    on subsequence imports or inclusions.
    
    DAFFODIL-2441
---
 .../dsom/SchemaSetIncludesAndImportsMixins.scala   | 11 ++++--
 .../section00/general/otherAnnotationLanguage.xsd  |  5 +++
 ...onLanguage.xsd => otherAnnotationLanguage2.xsd} | 10 +++---
 ...Language.xsd => schemaWithoutDFDLNamespace.xsd} | 22 ++----------
 .../general/testSchemaWithoutDFDLNamespace.tdml    | 37 ++++++++++++++++++++
 .../general/TestSchemaWithoutDFDLNamespace.scala   | 39 ++++++++++++++++++++++
 6 files changed, 98 insertions(+), 26 deletions(-)

diff --git a/daffodil-core/src/main/scala/org/apache/daffodil/dsom/SchemaSetIncludesAndImportsMixins.scala b/daffodil-core/src/main/scala/org/apache/daffodil/dsom/SchemaSetIncludesAndImportsMixins.scala
index 6d8e8b6aa..3ea9528ca 100644
--- a/daffodil-core/src/main/scala/org/apache/daffodil/dsom/SchemaSetIncludesAndImportsMixins.scala
+++ b/daffodil-core/src/main/scala/org/apache/daffodil/dsom/SchemaSetIncludesAndImportsMixins.scala
@@ -57,13 +57,20 @@ trait SchemaSetIncludesAndImportsMixin { self: SchemaSet =>
   lazy val allSchemaFiles = {
     val fd = fakeXMLSchemaDocument //bootstrap
     val sa = fd.seenAfter
+    val first = sa.value.head._2
     val sfl = sa.value.flatMap {
       case (_, ii) => {
         val sf = ii.iiSchemaFileMaybe // maybe not if we've already seen this file for the same namespace.
+        // Require the first schema file to have a DFDL namespace. Other included or imported schemas can be
+        // standard XSD schemas but emit a warning that the schema is being ignored.
         sf.filter { f =>
-          if (f.isDFDLSchemaFile)
+          if (f.isDFDLSchemaFile) {
             true
-          else {
+          } else if (f eq first) {
+            f.SDE("Non-DFDL Schema file. Does not have DFDL namespace definition on schema root element.\n" +
+              "Add xmlns:dfdl='%s' to the root element.", XMLUtils.DFDL_NAMESPACE)
+            false
+          } else {
             f.SDW(WarnID.IgnoreImport, "Non-DFDL Schema file ignored. Does not have DFDL namespace definition on schema root element.\n" +
               "Add xmlns:dfdl='%s' to the root element if this file must be part of the DFDL schema.", XMLUtils.DFDL_NAMESPACE)
             false
diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd
index 4a7061c41..7759bbb4e 100644
--- a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd
+++ b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd
@@ -23,6 +23,11 @@
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:fn="http://www.w3.org/2005/xpath-functions">
 
+  <!--
+  Check that other language schemas can be properly nested.
+  -->
+  <xs:import namespace="urn:otherAnnotationLanguage2" schemaLocation="otherAnnotationLanguage2.xsd" />
+
   <element name="otherAnnotation">
     <complexType>
        <attribute ref="tns:otherAnnotationAttribute"/>
diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage2.xsd
similarity index 84%
copy from daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd
copy to daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage2.xsd
index 4a7061c41..e24101197 100644
--- a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd
+++ b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage2.xsd
@@ -17,15 +17,15 @@
 -->
 
 <schema xmlns="http://www.w3.org/2001/XMLSchema"
-  targetNamespace="urn:otherAnnotationLanguage" 
-  xmlns:tns="urn:otherAnnotationLanguage"
+  targetNamespace="urn:otherAnnotationLanguage2"
+  xmlns:tns="urn:otherAnnotationLanguage2"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   xmlns:fn="http://www.w3.org/2005/xpath-functions">
 
-  <element name="otherAnnotation">
+  <element name="otherAnnotation2">
     <complexType>
-       <attribute ref="tns:otherAnnotationAttribute"/>
+       <attribute ref="tns:otherAnnotationAttribute2"/>
     </complexType>
   </element>
 
@@ -34,6 +34,6 @@
   have attributes. These attributes should be legal in DFDL, as they're not describing data, they're
   just describing annotations on the schema that a DFDL processor should be ignoring.
    -->
-  <attribute name="otherAnnotationAttribute" type="string"/>
+  <attribute name="otherAnnotationAttribute2" type="string"/>
 
 </schema>
diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/schemaWithoutDFDLNamespace.xsd
similarity index 55%
copy from daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd
copy to daffodil-test/src/test/resources/org/apache/daffodil/section00/general/schemaWithoutDFDLNamespace.xsd
index 4a7061c41..094640bf8 100644
--- a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/otherAnnotationLanguage.xsd
+++ b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/schemaWithoutDFDLNamespace.xsd
@@ -17,23 +17,7 @@
 -->
 
 <schema xmlns="http://www.w3.org/2001/XMLSchema"
-  targetNamespace="urn:otherAnnotationLanguage" 
-  xmlns:tns="urn:otherAnnotationLanguage"
-  xmlns:xs="http://www.w3.org/2001/XMLSchema"
-  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
-  xmlns:fn="http://www.w3.org/2005/xpath-functions">
-
-  <element name="otherAnnotation">
-    <complexType>
-       <attribute ref="tns:otherAnnotationAttribute"/>
-    </complexType>
-  </element>
-
-  <!-- 
-  Some other annotation elements that are not DFDL annotations may also
-  have attributes. These attributes should be legal in DFDL, as they're not describing data, they're
-  just describing annotations on the schema that a DFDL processor should be ignoring.
-   -->
-  <attribute name="otherAnnotationAttribute" type="string"/>
-
+        targetNamespace="http://example.com">
+  <element name="root"/>
 </schema>
+
diff --git a/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/testSchemaWithoutDFDLNamespace.tdml b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/testSchemaWithoutDFDLNamespace.tdml
new file mode 100644
index 000000000..510696794
--- /dev/null
+++ b/daffodil-test/src/test/resources/org/apache/daffodil/section00/general/testSchemaWithoutDFDLNamespace.tdml
@@ -0,0 +1,37 @@
+<?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.
+-->
+
+<tdml:testSuite suiteName="SchemaWithoutDFDLNamespace"
+                description="Tests that an error is generated if the top-level document is not in the DFDL namespace"
+                xmlns:tdml="http://www.ibm.com/xmlns/dfdl/testData"
+                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+                xmlns:xs="http://www.w3.org/2001/XMLSchema"
+                xmlns:ex="http://example.com"
+                xmlns:tns="urn:foo"
+                xmlns:daf="urn:ogf:dfdl:2013:imp:daffodil.apache.org:2018:ext"
+                xmlns:oth="urn:otherAnnotationLanguage">
+
+
+  <tdml:parserTestCase name="schemaWithoutDFDLNamespace" root="root" model="schemaWithoutDFDLNamespace.xsd">
+    <tdml:document>foo</tdml:document>
+    <tdml:errors>
+      <tdml:error>Non-DFDL Schema file</tdml:error>
+    </tdml:errors>
+  </tdml:parserTestCase>
+
+</tdml:testSuite>
diff --git a/daffodil-test/src/test/scala/org/apache/daffodil/section00/general/TestSchemaWithoutDFDLNamespace.scala b/daffodil-test/src/test/scala/org/apache/daffodil/section00/general/TestSchemaWithoutDFDLNamespace.scala
new file mode 100644
index 000000000..2ae4af4be
--- /dev/null
+++ b/daffodil-test/src/test/scala/org/apache/daffodil/section00/general/TestSchemaWithoutDFDLNamespace.scala
@@ -0,0 +1,39 @@
+/*
+ * 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.section00.general
+
+import org.apache.daffodil.tdml.Runner
+import org.junit.AfterClass
+import org.junit.Test
+
+object TestSchemaWithoutDFDLNamespace {
+  val testDir = "/org/apache/daffodil/section00/general/"
+  val runner: Runner = Runner(testDir, "testSchemaWithoutDFDLNamespace.tdml")
+
+  @AfterClass def shutDown(): Unit = {
+    runner.reset()
+  }
+}
+
+class TestSchemaWithoutDFDLNamespace {
+
+  import TestSchemaWithoutDFDLNamespace._
+
+  @Test def test_schemaWithoutDFDLNamespace(): Unit = { runner.runOneTest("schemaWithoutDFDLNamespace") }
+
+}