qiuwei / jing-trang

Automatically exported from code.google.com/p/jing-trang
Other
1 stars 1 forks source link

Better error reports on overlapping names in interleave #14

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Currently when we have overlapping element names in interleave Jing gives
an error message saying:

overlapping element names in operands of "interleave"

but does not give any indication on what names overlap. The following patch
(from oNVDL) will provide error messages like:

overlapping element names in operands of "interleave" "y" from no namespace
for
<grammar
    xmlns="http://relaxng.org/ns/structure/1.0"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <element name="root">
            <interleave>
                <element><anyName/><text/></element>
                <element><name>y</name><text/></element>
            </interleave>
        </element>
    </start>
</grammar>

overlapping element names in operands of "interleave" "[any name]" from no
namespace
for
<grammar
    xmlns="http://relaxng.org/ns/structure/1.0"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <element name="root">
            <interleave>
                <element><anyName/><text/></element>
                <element><nsName/><text/></element>
            </interleave>
        </element>
    </start>
</grammar>

overlapping element names in operands of "interleave" "[any name]" from
namespace "http://test"
for
<grammar
    xmlns="http://relaxng.org/ns/structure/1.0"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <element name="root">
            <interleave>
                <element><anyName/><text/></element>
                <element><nsName ns="http://test"/><text/></element>
            </interleave>
        </element>
    </start>
</grammar>

overlapping element names in operands of "interleave" "[any name]" from
namespace "[any namespace]"
for
<grammar
    xmlns="http://relaxng.org/ns/structure/1.0"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <element name="root">
            <interleave>
                <element><anyName/><text/></element>
                <element><anyName/><text/></element>
            </interleave>
        </element>
    </start>
</grammar>

The patch changes the messages file to add a new parameter for the error
message and also specifies "from no namespace" when we have a name from no
namespace. It also adds a method in OverlapDetector to obtain a Name
describing the overlap and passes that Name to the
RestrictionViolationException in Alphabet. Please feel free to change the
patch, especially if you think the use of Name to pass any namespace/any
name information is not ok.

Index: src/com/thaiopensource/relaxng/impl/Alphabet.java
===================================================================
--- src/com/thaiopensource/relaxng/impl/Alphabet.java   (revision 1995)
+++ src/com/thaiopensource/relaxng/impl/Alphabet.java   (working copy)
@@ -22,6 +22,7 @@
     if (nameClass != null
    && a.nameClass != null
    && OverlapDetector.overlap(nameClass, a.nameClass))
-      throw new RestrictionViolationException("interleave_element_overlap");
+      throw new RestrictionViolationException("interleave_element_overlap", 
+          OverlapDetector.getOverlappingName(nameClass, a.nameClass));
   }
 }
Index: src/com/thaiopensource/relaxng/impl/OverlapDetector.java
===================================================================
--- src/com/thaiopensource/relaxng/impl/OverlapDetector.java    (revision 1995)
+++ src/com/thaiopensource/relaxng/impl/OverlapDetector.java    (working copy)
@@ -6,7 +6,8 @@
   private final NameClass nc1;
   private final NameClass nc2;
   private boolean overlaps = false;
-
+  private Name overlap = null;
+  
   private static final String IMPOSSIBLE = "\u0000";

   private OverlapDetector(NameClass nc1, NameClass nc2) {
@@ -17,8 +18,10 @@
   }

   private void probe(Name name) {
-    if (nc1.contains(name) && nc2.contains(name))
+    if (nc1.contains(name) && nc2.contains(name)) {
       overlaps = true;
+      overlap = name;
+    }
   }

   public void visitChoice(NameClass nc1, NameClass nc2) {
@@ -65,4 +68,28 @@
     }
     return new OverlapDetector(nc1, nc2).overlaps;
   }
+  
+  static Name getOverlappingName(NameClass nc1, NameClass nc2) {
+    Name name = null;
+    if (nc2 instanceof SimpleNameClass) {
+      SimpleNameClass snc = (SimpleNameClass)nc2;
+      name = nc1.contains(snc.getName())?snc.getName():null;
+    } else 
+    if (nc1 instanceof SimpleNameClass) {
+      SimpleNameClass snc = (SimpleNameClass)nc1;
+      name = nc2.contains(snc.getName())?snc.getName():null;
+    } else {
+      name = new OverlapDetector(nc1, nc2).overlap;
+      if (name != null) {
+        if (IMPOSSIBLE == name.getNamespaceUri() &&
+            IMPOSSIBLE == name.getLocalName()) {
+          name = new Name ("[any namespace]", "[any name]");
+        } else
+        if (OverlapDetector.IMPOSSIBLE == name.getLocalName()) {
+          name = new Name (name.getNamespaceUri(), "[any name]");
+        }        
+      }
+    }
+    return name; 
+  }
 }
Index: src/com/thaiopensource/relaxng/impl/resources/Messages.properties
===================================================================
--- src/com/thaiopensource/relaxng/impl/resources/Messages.properties
(revision 1995)
+++ src/com/thaiopensource/relaxng/impl/resources/Messages.properties
(working copy)
@@ -60,7 +60,7 @@
 start_contains_value=\"start\" contains \"value\"
 duplicate_attribute=duplicate attribute
 duplicate_attribute_detail=duplicate attribute {0}
-interleave_element_overlap=overlapping element names in operands of
\"interleave\"
+interleave_element_overlap=overlapping element names in operands of
\"interleave\" {0}
 list_contains_interleave=\"list\" contains \"interleave\"
 interleave_text_overlap=both operands of \"interleave\" contain \"text\"
 open_name_class_not_repeated=attribute using \"nsName\" or \"anyName\"
must be in \"oneOrMore\"
@@ -98,5 +98,5 @@
 first_id=first occurrence of ID \"{0}\"

 # Fragments
-name_absent_namespace=\"{0}\"
+name_absent_namespace=\"{0}\" from no namespace
 name_with_namespace=\"{1}\" from namespace \"{0}\"
\ No newline at end of file

Original issue reported on code.google.com by georgebina76 on 20 Oct 2008 at 10:27

GoogleCodeExporter commented 8 years ago
Fixed in r1996 and r1997.

George, please review and test. Change status to Verified if it's OK.

Original comment by jjc.jclark.com on 20 Oct 2008 at 4:08

GoogleCodeExporter commented 8 years ago
Hi James,

There is one issue when the overlap is no namespace, the error message is 

attributes from namespace can occur more than once

in case of attributes:
<grammar
    xmlns="http://relaxng.org/ns/structure/1.0"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <element name="root">
            <oneOrMore><attribute><anyName/></attribute></oneOrMore>
            <oneOrMore><attribute><nsName/></attribute></oneOrMore>
        </element>
    </start>
</grammar>

and

elements from namespace can occur in more than one operand of "interleave"

in case of interleave:
<grammar
    xmlns="http://relaxng.org/ns/structure/1.0"
    xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
    datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
    <start>
        <element name="root">
            <interleave>
                <element><anyName/><text/></element>
                <element><nsName/><text/></element>
            </interleave>
        </element>
    </start>
</grammar>

A possible solution will be to put the namespace parameter inside quotes:

Index: src/com/thaiopensource/relaxng/impl/resources/Messages.properties
===================================================================
--- src/com/thaiopensource/relaxng/impl/resources/Messages.properties
(.../D:/users/george/workspace/jingOnGoogle/src/com/thaiopensource/relaxng/impl/
resources/Messages.properties)
(revision 1997)
+++ src/com/thaiopensource/relaxng/impl/resources/Messages.properties
(.../https://jing-trang.googlecode.com/svn/trunk/src/com/thaiopensource/relaxng/
impl/resources/Messages.properties)
(working copy)
@@ -60,10 +60,10 @@
 start_contains_value=\"start\" contains \"value\"
 duplicate_attribute=duplicate attribute
 duplicate_attribute_name=duplicate attribute {0}
-duplicate_attribute_ns=attributes from namespace {0} can occur more than once
+duplicate_attribute_ns=attributes from namespace \"{0}\" can occur more than 
once
 interleave_element_overlap=overlapping element names in operands of \"interleave\"
 interleave_element_overlap_name=the element {0} can occur in more than one operand
of \"interleave\"
-interleave_element_overlap_ns=elements from namespace {0} can occur in more 
than one
operand of \"interleave\"
+interleave_element_overlap_ns=elements from namespace \"{0}\" can occur in 
more than
one operand of \"interleave\"
 list_contains_interleave=\"list\" contains \"interleave\"
 interleave_text_overlap=both operands of \"interleave\" contain \"text\"
 open_name_class_not_repeated=attribute using \"nsName\" or \"anyName\" must be in
\"oneOrMore\"

That will give 
attributes from namespace "" can occur more than once
elements from namespace "" can occur in more than one operand of "interleave"

Apart from this issue everything works ok.

Thanks,
George

Original comment by georgebina76 on 21 Oct 2008 at 6:58

GoogleCodeExporter commented 8 years ago
Thanks. Fixed in r2043.

Original comment by jjc.jclark.com on 21 Oct 2008 at 5:18

GoogleCodeExporter commented 8 years ago

Original comment by jjc.jclark.com on 22 Oct 2008 at 5:34

GoogleCodeExporter commented 8 years ago

Original comment by georgebina76 on 22 Oct 2008 at 5:39

GoogleCodeExporter commented 8 years ago

Original comment by jjc.jclark.com on 22 Oct 2008 at 5:47