sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1523676 - in /sis/branches/JDK7/core: sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/ sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/ sis-metadata/src/test/java/org/apache/sis/xml/ sis-utility/src/test/j...
Date Mon, 16 Sep 2013 14:33:22 GMT
Author: desruisseaux
Date: Mon Sep 16 14:33:22 2013
New Revision: 1523676

URL: http://svn.apache.org/r1523676
Log:
The DOM builder needs to be namespace-aware in all tests.
This requires the prefixes to be bind to namespaces in XML tests.

Modified:
    sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java
    sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java
    sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFractionTest.java
    sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java
    sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/xml/ImageryMarshallingTest.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/Assert.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java

Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java?rev=1523676&r1=1523675&r2=1523676&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/gml/TimePeriodTest.java
[UTF-8] Mon Sep 16 14:33:22 2013
@@ -25,6 +25,7 @@ import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 import javax.xml.datatype.DatatypeConfigurationException;
 import org.apache.sis.xml.XML;
+import org.apache.sis.xml.Namespaces;
 import org.apache.sis.xml.MarshallerPool;
 import org.apache.sis.internal.jaxb.XmlUtilities;
 import org.apache.sis.test.XMLTestCase;
@@ -111,7 +112,7 @@ public final strictfp class TimePeriodTe
         final TimeInstant instant = createTimeInstant("1992-01-01 00:00:00");
         final String actual = marshal(marshaller, instant);
         assertXmlEquals(
-                "<gml:TimeInstant>\n" +
+                "<gml:TimeInstant xmlns:gml=\"" + Namespaces.GML + "\">\n" +
                 "  <gml:timePosition>1992-01-01T01:00:00.000+01:00</gml:timePosition>\n"
+
                 "</gml:TimeInstant>\n", actual, "xmlns:*");
 
@@ -135,7 +136,7 @@ public final strictfp class TimePeriodTe
         final TimePeriodBound end   = new TimePeriodBound.GML2(new DummyInstant(date("2007-12-31
00:00:00")));
         clearContext();
         testPeriod(begin, end,
-                "<gml:TimePeriod>\n" +
+                "<gml:TimePeriod xmlns:gml=\"" + Namespaces.GML + "\">\n" +
                 "  <gml:begin>\n" +
                 "    <gml:TimeInstant>\n" +
                 "      <gml:timePosition>1992-01-01T01:00:00+01:00</gml:timePosition>\n"
+
@@ -188,7 +189,7 @@ public final strictfp class TimePeriodTe
         final TimePeriodBound end   = new TimePeriodBound.GML3(new DummyInstant(date("2007-12-31
00:00:00")), "after");
         clearContext();
         testPeriod(begin, end,
-                "<gml:TimePeriod>\n" +
+                "<gml:TimePeriod xmlns:gml=\"" + Namespaces.GML + "\">\n" +
                 "  <gml:beginPosition>1992-01-01T01:00:00+01:00</gml:beginPosition>\n"
+
                 "  <gml:endPosition>2007-12-31T01:00:00+01:00</gml:endPosition>\n"
+
                 "</gml:TimePeriod>\n", true);
@@ -207,7 +208,7 @@ public final strictfp class TimePeriodTe
         final TimePeriodBound end   = new TimePeriodBound.GML3(new DummyInstant(date("2007-12-30
23:00:00")), "after");
         clearContext();
         testPeriod(begin, end,
-                "<gml:TimePeriod>\n" +
+                "<gml:TimePeriod xmlns:gml=\"" + Namespaces.GML + "\">\n" +
                 "  <gml:beginPosition>1992-01-02</gml:beginPosition>\n" +
                 "  <gml:endPosition>2007-12-31</gml:endPosition>\n" +
                 "</gml:TimePeriod>\n", false);
@@ -226,7 +227,7 @@ public final strictfp class TimePeriodTe
         final TimePeriodBound end   = new TimePeriodBound.GML3(new DummyInstant(date("2007-12-30
23:00:00")), "after");
         clearContext();
         testPeriod(begin, end,
-                "<gml:TimePeriod>\n" +
+                "<gml:TimePeriod xmlns:gml=\"" + Namespaces.GML + "\">\n" +
                 "  <gml:beginPosition indeterminatePosition=\"before\"/>\n" +
                 "  <gml:endPosition>2007-12-31</gml:endPosition>\n" +
                 "</gml:TimePeriod>\n", false);
@@ -245,7 +246,7 @@ public final strictfp class TimePeriodTe
         final TimePeriodBound end   = new TimePeriodBound.GML3(null, "after");
         clearContext();
         testPeriod(begin, end,
-                "<gml:TimePeriod>\n" +
+                "<gml:TimePeriod xmlns:gml=\"" + Namespaces.GML + "\">\n" +
                 "  <gml:beginPosition>1992-01-02</gml:beginPosition>\n" +
                 "  <gml:endPosition indeterminatePosition=\"after\"/>\n" +
                 "</gml:TimePeriod>\n", false);

Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java?rev=1523676&r1=1523675&r2=1523676&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java
[UTF-8] Mon Sep 16 14:33:22 2013
@@ -55,7 +55,8 @@ public final strictfp class DefaultBrows
         browse.setFileType("image/tiff");
         final String xml = XML.marshal(browse);
         assertXmlEquals(
-                "<gmd:MD_BrowseGraphic>\n" +
+                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                                     " xmlns:gmx=\"" + Namespaces.GMX + "\">\n" +
                 "  <gmd:fileType>\n" +
                 "    <gmx:MimeFileType type=\"image/tiff\">image/tiff</gmx:MimeFileType>\n"
+
                 "  </gmd:fileType>\n" +
@@ -77,7 +78,8 @@ public final strictfp class DefaultBrows
         final DefaultBrowseGraphic browse = new DefaultBrowseGraphic(uri);
         final String xml = XML.marshal(browse);
         assertXmlEquals(
-                "<gmd:MD_BrowseGraphic>\n" +
+                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                                     " xmlns:gmx=\"" + Namespaces.GMX + "\">\n" +
                 "  <gmd:fileName>\n" +
                 "    <gmx:FileName src=\"file:/catalog/image.png\">image.png</gmx:FileName>\n"
+
                 "  </gmd:fileName>\n" +
@@ -120,7 +122,8 @@ public final strictfp class DefaultBrows
         browse.setFileType("image/tiff");
         final String xml = XML.marshal(browse);
         assertXmlEquals(
-                "<gmd:MD_BrowseGraphic>\n" +
+                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                                     " xmlns:gmx=\"" + Namespaces.GMX + "\">\n" +
                 "  <gmd:fileName>\n" +
                 "    <gmx:FileName src=\"file:/catalog/image.png\">image.png</gmx:FileName>\n"
+
                 "  </gmd:fileName>\n" +
@@ -151,7 +154,8 @@ public final strictfp class DefaultBrows
                 singletonMap(XML.STRING_SUBSTITUTES, new String[] {"filename", "mimetype"}));
         final String xml = buffer.toString();
         assertXmlEquals(
-                "<gmd:MD_BrowseGraphic>\n" +
+                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                                     " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
                 "  <gmd:fileName>\n" +
                 "    <gco:CharacterString>file:/catalog/image.png</gco:CharacterString>\n"
+
                 "  </gmd:fileName>\n" +

Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFractionTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFractionTest.java?rev=1523676&r1=1523675&r2=1523676&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFractionTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultRepresentativeFractionTest.java
[UTF-8] Mon Sep 16 14:33:22 2013
@@ -18,6 +18,7 @@ package org.apache.sis.metadata.iso.iden
 
 import javax.xml.bind.JAXBException;
 import org.apache.sis.xml.XML;
+import org.apache.sis.xml.Namespaces;
 import org.apache.sis.xml.IdentifierSpace;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
@@ -59,7 +60,9 @@ public final strictfp class DefaultRepre
         fraction.getIdentifierMap().putSpecialized(IdentifierSpace.ID, "scale");
         final String xml = XML.marshal(fraction);
         assertXmlEquals(
-                "<gmd:MD_RepresentativeFraction id=\"scale\">\n" +
+                "<gmd:MD_RepresentativeFraction xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                                              " xmlns:gco=\"" + Namespaces.GCO + '"' +
+                                              " id=\"scale\">\n" +
                 "  <gmd:denominator>\n" +
                 "    <gco:Integer>8</gco:Integer>\n" +
                 "  </gmd:denominator>\n" +

Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java?rev=1523676&r1=1523675&r2=1523676&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultResolutionTest.java
[UTF-8] Mon Sep 16 14:33:22 2013
@@ -18,6 +18,7 @@ package org.apache.sis.metadata.iso.iden
 
 import javax.xml.bind.JAXBException;
 import org.apache.sis.xml.XML;
+import org.apache.sis.xml.Namespaces;
 import org.apache.sis.metadata.iso.LoggingWatcher;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
@@ -131,7 +132,8 @@ public final strictfp class DefaultResol
          * an unknown reason.
          */
         assertXmlEquals(
-                "<gmd:MD_Resolution>\n" +
+                "<gmd:MD_Resolution xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                                  " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
                 "  <gmd:distance>\n" +
                 "    <gco:Distance uom=\"http://schemas.opengis.net/iso/19139/20070417/resources/uom/gmxUom.xml#xpointer(//*[@gml:id='m'])\">1000.0</gco:Distance>\n"
+
                 "  </gmd:distance>\n" +

Modified: sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/xml/ImageryMarshallingTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/xml/ImageryMarshallingTest.java?rev=1523676&r1=1523675&r2=1523676&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/xml/ImageryMarshallingTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/test/java/org/apache/sis/xml/ImageryMarshallingTest.java
[UTF-8] Mon Sep 16 14:33:22 2013
@@ -56,7 +56,8 @@ public final strictfp class ImageryMarsh
          */
         String actual = XML.marshal(lineage);
         assertXmlEquals(
-            "<gmd:LI_Lineage xmlns:gmd=\"" + Namespaces.GMD + "\">\n" +
+            "<gmd:LI_Lineage xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                           " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
             "  <gmd:source>\n" +
             "    <gmd:LI_Source>\n" +
             "      <gmd:description>\n" +
@@ -71,7 +72,9 @@ public final strictfp class ImageryMarsh
         source.setProcessedLevel(new DefaultIdentifier("DummyLevel"));
         actual = XML.marshal(lineage);
         assertXmlEquals(
-            "<gmd:LI_Lineage xmlns:gmd=\"" + Namespaces.GMD + "\">\n" +
+            "<gmd:LI_Lineage xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                           " xmlns:gmi=\"" + Namespaces.GMI + '"' +
+                           " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
             "  <gmd:source>\n" +
             "    <gmi:LE_Source>\n" +
             "      <gmd:description>\n" +

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/Assert.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/Assert.java?rev=1523676&r1=1523675&r2=1523676&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/Assert.java [UTF-8]
(original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/Assert.java [UTF-8]
Mon Sep 16 14:33:22 2013
@@ -18,7 +18,6 @@ package org.apache.sis.test;
 
 import java.util.Set;
 import java.util.Map;
-import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.LinkedHashSet;
 import java.util.LinkedHashMap;
@@ -244,6 +243,33 @@ public strictfp class Assert extends org
      *
      * This method will ignore comments and the optional attributes given in arguments.
      *
+     * {@section Ignored attributes substitution}
+     * For convenience, this method replaces some well known prefixes in the {@code ignoredAttributes}
+     * array by their full namespace URLs. For example this method replaces{@code "xsi:schemaLocation"}
+     * by {@code "http://www.w3.org/2001/XMLSchema-instance:schemaLocation"}.
+     * If such substitution is not desired, consider using {@link XMLComparator} directly
instead.
+     *
+     * <p>The current substitution map is as below (may be expanded in any future SIS
version):</p>
+     *
+     * <table class="sis">
+     *   <tr><th>Prefix</th> <th>URL</th></tr>
+     *   <tr><td>xmlns</td>  <td>{@code "http://www.w3.org/2000/xmlns"}</td></tr>
+     *   <tr><td>xlink</td>  <td>{@value org.apache.sis.xml.Namespaces#XLINK}</td></tr>
+     *   <tr><td>xsi</td>    <td>{@value org.apache.sis.xml.Namespaces#XSI}</td></tr>
+     *   <tr><td>gml</td>    <td>{@value org.apache.sis.xml.Namespaces#GML}</td></tr>
+     *   <tr><td>gmd</td>    <td>{@value org.apache.sis.xml.Namespaces#GMD}</td></tr>
+     *   <tr><td>gmx</td>    <td>{@value org.apache.sis.xml.Namespaces#GMX}</td></tr>
+     *   <tr><td>gmi</td>    <td>{@value org.apache.sis.xml.Namespaces#GMI}</td></tr>
+     *   <tr><td>gco</td>    <td>{@value org.apache.sis.xml.Namespaces#GCO}</td></tr>
+     * </table>
+     *
+     * <p>For example in order to ignore the namespace, type and schema location declaration,
+     * the following strings can be given to the {@code ignoredAttributes} argument:</p>
+     *
+     * {@preformat
+     *   "xmlns:*", "xsi:schemaLocation", "xsi:type"
+     * }
+     *
      * @param  expected The expected XML document.
      * @param  actual   The XML document to compare.
      * @param  ignoredAttributes The fully-qualified names of attributes to ignore
@@ -261,7 +287,8 @@ public strictfp class Assert extends org
      * Parses two XML tree as DOM documents, and compares the nodes with the given tolerance
      * threshold for numerical values. The inputs given to this method can be any of the
types
      * documented {@linkplain #assertXmlEquals(Object, Object, String[]) above}. This method
-     * will ignore comments and the optional attributes given in arguments.
+     * will ignore comments and the optional attributes given in arguments as documented
in the
+     * above method.
      *
      * @param  expected  The expected XML document.
      * @param  actual    The XML document to compare.
@@ -286,7 +313,9 @@ public strictfp class Assert extends org
         }
         comparator.tolerance = tolerance;
         comparator.ignoreComments = true;
-        comparator.ignoredAttributes.addAll(Arrays.asList(ignoredAttributes));
+        for (final String attribute : ignoredAttributes) {
+            comparator.ignoredAttributes.add(XMLComparator.substitutePrefix(attribute));
+        }
         comparator.compare();
     }
 

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java?rev=1523676&r1=1523675&r2=1523676&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/XMLComparator.java
[UTF-8] Mon Sep 16 14:33:22 2013
@@ -16,8 +16,10 @@
  */
 package org.apache.sis.test;
 
+import java.util.Map;
 import java.util.Set;
 import java.util.List;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.ArrayList;
 import java.net.URI;
@@ -33,13 +35,13 @@ import javax.xml.parsers.ParserConfigura
 import org.w3c.dom.Attr;
 import org.w3c.dom.CDATASection;
 import org.w3c.dom.Comment;
-import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.ProcessingInstruction;
 import org.w3c.dom.Text;
 import org.xml.sax.SAXException;
+import org.apache.sis.xml.Namespaces;
 import org.apache.sis.util.ArgumentChecks;
 
 import static java.lang.StrictMath.*;
@@ -69,7 +71,7 @@ import static org.apache.sis.util.CharSe
  * @author  Martin Desruisseaux (Geomatys)
  * @author  Guilhem Legal (Geomatys)
  * @since   0.3 (derived from geotk-3.17)
- * @version 0.3
+ * @version 0.4
  * @module
  *
  * @see XMLTestCase
@@ -77,6 +79,32 @@ import static org.apache.sis.util.CharSe
  */
 public strictfp class XMLComparator {
     /**
+     * Commonly used prefixes for namespaces. Used as shorthands for calls to
+     * {@link Assert#assertXmlEquals(Object, Object, String[])}.
+     *
+     * @see #substitutePrefix(String)
+     */
+    private static final Map<String, String> PREFIX_URL = new HashMap<>(16);
+    static {
+        final Map<String,String> map = PREFIX_URL;
+        map.put("xmlns", "http://www.w3.org/2000/xmlns"); // No trailing slash.
+        map.put("xlink", Namespaces.XLINK);
+        map.put("xsi",   Namespaces.XSI);
+        map.put("gml",   Namespaces.GML);
+        map.put("gmd",   Namespaces.GMD);
+        map.put("gmx",   Namespaces.GMX);
+        map.put("gmi",   Namespaces.GMI);
+        map.put("gco",   Namespaces.GCO);
+    }
+
+    /**
+     * The DOM factory, created when first needed.
+     *
+     * @see #newDocumentBuilder()
+     */
+    private static DocumentBuilderFactory factory;
+
+    /**
      * The expected document.
      */
     private final Node expectedDoc;
@@ -92,20 +120,42 @@ public strictfp class XMLComparator {
     public boolean ignoreComments;
 
     /**
-     * The fully-qualified name of attributes to ignore in comparisons. The name shall be
in
-     * the form {@code "namespace:name"}, or only {@code "name"} if there is no namespace.
-     * In order to ignore everything in a namespace, use {@code "namespace:*"}.
+     * The fully-qualified name of attributes to ignore in comparisons.
+     * This collection is initially empty. Users can add or remove elements in this collection
as they wish.
+     * The content of this collection will be honored by the default {@link #compareAttributes(Node,
Node)}
+     * implementation.
+     *
+     * <p>The elements shall be names in the form {@code "namespace:name"}, or only
{@code "name"} if there
+     * is no namespace. In order to ignore everything in a namespace, use {@code "namespace:*"}.</p>
+     *
+     * <p>Whether the namespace is the full URL or only the prefix depends on whether
+     * {@link DocumentBuilderFactory#setNamespaceAware(boolean)} was set to {@code true}
+     * or {@code false} respectively before the XML document has been built.
+     * For example in order to ignore the standard {@code "schemaLocation"} attribute:</p>
+     *
+     * <ul>
+     *   <li>If {@code NamespaceAware} is {@code true}, then this {@code ignoredAttributes}
collection
+     *       shall contains {@code "http://www.w3.org/2001/XMLSchema-instance:schemaLocation"}.</li>
+     *   <li>If {@code NamespaceAware} is {@code false}, then this {@code ignoredAttributes}
collection
+     *       shall contains {@code "xsi:schemaLocation"}, assuming that {@code "xsi"} is
the prefix for
+     *       {@code "http://www.w3.org/2001/XMLSchema-instance"}.</li>
+     * </ul>
+     *
+     * <p>{@code XMLComparator} is namespace aware. The second case in the above-cited
choice may happen only
+     * if the user provided {@link Node} instances to the constructor. In such case, {@code
XMLComparator} has
+     * no control on whether the nodes contain namespaces or not.</p>
      *
      * <p>For example in order to ignore the namespace, type and schema location declaration,
      * the following strings can be added in this set:</p>
      *
      * {@preformat text
-     *   "xmlns:*", "xsi:schemaLocation", "xsi:type"
+     *   "http://www.w3.org/2000/xmlns:*",
+     *   "http://www.w3.org/2001/XMLSchema-instance:schemaLocation",
+     *   "http://www.w3.org/2001/XMLSchema-instance:type"
      * }
      *
-     * This set is initially empty. Users can add or remove elements in this set as they
wish.
-     * The content of this set will be honored by the default {@link #compareAttributes(Node,
Node)}
-     * implementation.
+     * Note that for convenience, the {@link Assert#assertXmlEquals(Object, Object, String[])}
method
+     * automatically replaces some widely used prefixes by their full URL.
      */
     public final Set<String> ignoredAttributes;
 
@@ -131,21 +181,6 @@ public strictfp class XMLComparator {
     public double tolerance;
 
     /**
-     * Creates a new comparator for the given root nodes.
-     *
-     * @param expected The root node of the expected XML document.
-     * @param actual   The root node of the XML document to compare.
-     */
-    public XMLComparator(final Node expected, final Node actual) {
-        ArgumentChecks.ensureNonNull("expected", expected);
-        ArgumentChecks.ensureNonNull("actual",   actual);
-        expectedDoc       = expected;
-        actualDoc         = actual;
-        ignoredAttributes = new HashSet<>();
-        ignoredNodes      = new HashSet<>();
-    }
-
-    /**
      * Creates a new comparator for the given inputs.
      * The inputs can be any of the following types:
      *
@@ -167,24 +202,40 @@ public strictfp class XMLComparator {
     public XMLComparator(final Object expected, final Object actual)
             throws IOException, ParserConfigurationException, SAXException
     {
-        this((expected instanceof Node) ? (Node) expected : read(expected),
-               (actual instanceof Node) ? (Node) actual   : read(actual));
+        ArgumentChecks.ensureNonNull("expected", expected);
+        ArgumentChecks.ensureNonNull("actual",   actual);
+        DocumentBuilder builder = null;
+        if (expected instanceof Node) {
+            expectedDoc = (Node) expected;
+        } else {
+            builder = newDocumentBuilder();
+            try (InputStream stream = toInputStream(expected)) {
+                expectedDoc = builder.parse(stream);
+            }
+        }
+        if (actual instanceof Node) {
+            actualDoc = (Node) actual;
+        } else {
+            if (builder == null) {
+                builder = newDocumentBuilder();
+            }
+            try (InputStream stream = toInputStream(actual)) {
+                actualDoc = builder.parse(stream);
+            }
+        }
+        ignoredAttributes = new HashSet<>();
+        ignoredNodes      = new HashSet<>();
     }
 
     /**
-     * Convenience method to acquire a DOM document from an input. This convenience method
-     * uses the default JRE classes, so it may not be the fastest parsing method.
+     * Creates a new document builder.
      */
-    private static Document read(final Object input)
-            throws IOException, ParserConfigurationException, SAXException
-    {
-        final Document document;
-        try (InputStream stream = toInputStream(input)) {
-            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-            final DocumentBuilder constructeur = factory.newDocumentBuilder();
-            document = constructeur.parse(stream);
+    private synchronized static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException
{
+        if (factory == null) {
+            factory = DocumentBuilderFactory.newInstance();
+            factory.setNamespaceAware(true);
         }
-        return document;
+        return factory.newDocumentBuilder();
     }
 
     /**
@@ -201,6 +252,21 @@ public strictfp class XMLComparator {
     }
 
     /**
+     * If the given attribute name begins with one of the well known prefixes,
+     * substitutes the prefix by the full URL. Otherwise returns the name unchanged.
+     */
+    static String substitutePrefix(final String attribute) {
+        final int s = attribute.lastIndexOf(':');
+        if (s >= 0) {
+            final String url = PREFIX_URL.get(attribute.substring(0, s));
+            if (url != null) {
+                return url.concat(attribute.substring(s));
+            }
+        }
+        return attribute;
+    }
+
+    /**
      * Compares the XML document specified at construction time. Before to invoke this
      * method, users may consider to add some values to the {@link #ignoredAttributes}
      * set.
@@ -405,17 +471,22 @@ public strictfp class XMLComparator {
             String name        = expAttr.getLocalName();
             if (name == null) {
                 /*
-                 * Experience shows that the namespace and name value varies depending on
how the document
-                 * has been built. The LocalName is sometime null, in which case we have
to fetch the full
-                 * name. We observe:
+                 * The above variables may be null if the node has been built from a DOM
Level 1 API,
+                 * or if the DocumentBuilder was not namespace-aware. In the following table,
the first
+                 * column shows the usual case for "http://www.w3.org/2000/xmlns/gml". The
second column
+                 * shows the case if the DocumentBuilder was not aware of namespaces. The
last column is
+                 * a case sometime observed.
                  *
-                 * ┌───────────────────┬───────────┬─────────────────────────────────┐
-                 * │ Node method       │ Example 1 │ Example 2                    
  │
-                 * ├───────────────────┼───────────┼─────────────────────────────────┤
-                 * │ getNamespaceURI() │ "xmlns"   │ "http://www.w3.org/2000/xmlns/"
│
-                 * │ getLocalName()    │ "test"    │  null                        
  │
-                 * │ getNodeName()     │           │ "xmlns:test"                 
  │
-                 * └───────────────────┴───────────┴─────────────────────────────────┘
+                 * ┌───────────────────┬─────────────────────────────────┬──────────────┬─────────────┐
+                 * │ Node method       │ Namespace (NS) aware            │ Non NS-aware
│ Other case  │
+                 * ├───────────────────┼─────────────────────────────────┼──────────────┼─────────────┤
+                 * │ getNamespaceURI() │ "http://www.w3.org/2000/xmlns/" │  null  
     │ "xmlns"     │
+                 * │ getLocalName()    │ "gml"                           │  null  
     │ "gml"       │
+                 * │ getNodeName()     │ "xmlns:gml"                     │ "xmlns:gml"
 │             │
+                 * └───────────────────┴─────────────────────────────────┴──────────────┴─────────────┘
+                 *
+                 * By default, this block is not be executed. However if the user gave us
Nodes that are
+                 * not namespace aware, then the 'isIgnored(…)' method will try to parse
the node name.
                  */
                 name = expAttr.getNodeName();
             }
@@ -441,26 +512,45 @@ public strictfp class XMLComparator {
      */
     private static boolean isIgnored(final Set<String> ignored, String ns, final String
name) {
         if (!ignored.isEmpty()) {
-            /*
-             * Check if the fully qualified attribute name is one of the attributes to ignore.
-             * Typical example: "xsi:schemaLocation"
-             */
-            if (ignored.contains((ns != null) ? ns + ':' + name : name)) {
-                return true;
-            }
-            /*
-             * The given attribute does not appear explicitely in the set of attributes to
ignore.
-             * But maybe the user asked to ignore all attributes in the namespace.
-             * Typical example: "xmlns:*"
-             */
             if (ns == null) {
+                /*
+                 * If there is no namespace, then the 'name' argument should be the qualified
name
+                 * (with a prefix). Example: "xsi:schemaLocation". We will look first for
an exact
+                 * name match, then for a match after replacing the local name by "*".
+                 */
+                if (ignored.contains(name)) {
+                    return true;
+                }
                 final int s = name.indexOf(':');
-                if (s >= 1) {
-                    ns = name.substring(0, s);
+                if (s >= 1 && ignored.contains(name.substring(0, s+1) + '*'))
{
+                    return true;
+                }
+            } else {
+                /*
+                 * If there is a namespace (which is the usual case), perform the concatenation
+                 * with the name before to check in the collection of ignored attributes.
+                 */
+                final StringBuilder buffer = new StringBuilder(ns);
+                int length = buffer.length();
+                if (length != 0 && buffer.charAt(length - 1) == '/') {
+                    buffer.setLength(--length);
+                }
+                /*
+                 * Check if the fully qualified attribute name is one of the attributes to
ignore.
+                 * Typical example: "http://www.w3.org/2001/XMLSchema-instance:schemaLocation"
+                 */
+                if (ignored.contains(buffer.append(':').append(name).toString())) {
+                    return true;
+                }
+                /*
+                 * The given attribute does not appear explicitely in the set of attributes
to ignore.
+                 * But maybe the user asked to ignore all attributes in the namespace.
+                 * Typical example: "http://www.w3.org/2000/xmlns:*"
+                 */
+                buffer.setLength(length + 1);
+                if (ignored.contains(buffer.append('*').toString())) {
+                    return true;
                 }
-            }
-            if (ns != null && ignored.contains(ns + ":*")) {
-                return true;
             }
         }
         return false;

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java?rev=1523676&r1=1523675&r2=1523676&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java
[UTF-8] Mon Sep 16 14:33:22 2013
@@ -25,6 +25,7 @@ import org.opengis.util.LocalName;
 import org.opengis.util.GenericName;
 import org.opengis.util.NameFactory;
 import org.apache.sis.internal.system.DefaultFactories;
+import org.apache.sis.xml.Namespaces;
 import org.apache.sis.xml.MarshallerPool;
 import org.apache.sis.test.mock.IdentifiedObjectMock;
 import org.apache.sis.test.XMLTestCase;
@@ -88,7 +89,8 @@ public final strictfp class NameMarshall
         final LocalName name = factory.createLocalName(null, "An ordinary local name");
         assertEquals("An ordinary local name", name.toString());
         final String expected =
-                "<gml:IO_IdentifiedObject>\n" +
+                "<gml:IO_IdentifiedObject xmlns:gml=\"" + Namespaces.GML + '"' +
+                                        " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
                 "  <gml:alias>\n" +
                 "    <gco:LocalName>An ordinary local name</gco:LocalName>\n"
+
                 "  </gml:alias>\n" +
@@ -110,7 +112,8 @@ public final strictfp class NameMarshall
         final LocalName name = factory.createLocalName(null, "A name with & and >
and <.");
         assertEquals("A name with & and > and <.", name.toString());
         final String expected =
-                "<gml:IO_IdentifiedObject>\n" +
+                "<gml:IO_IdentifiedObject xmlns:gml=\"" + Namespaces.GML + '"' +
+                                        " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
                 "  <gml:alias>\n" +
                 "    <gco:LocalName>A name with &amp; and &gt; and &lt;.</gco:LocalName>\n"
+
                 "  </gml:alias>\n" +
@@ -131,7 +134,8 @@ public final strictfp class NameMarshall
         final TypeName name = factory.createTypeName(null, "An other local name");
         assertEquals("An other local name", name.toString());
         final String expected =
-                "<gml:IO_IdentifiedObject>\n" +
+                "<gml:IO_IdentifiedObject xmlns:gml=\"" + Namespaces.GML + '"' +
+                                        " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
                 "  <gml:alias>\n" +
                 "    <gco:TypeName>\n" +
                 "      <gco:aName>\n" +
@@ -156,7 +160,8 @@ public final strictfp class NameMarshall
         final GenericName name = factory.createGenericName(null, "myScope","myName");
         assertEquals("myScope:myName", name.toString());
         final String expected =
-                "<gml:IO_IdentifiedObject>\n" +
+                "<gml:IO_IdentifiedObject xmlns:gml=\"" + Namespaces.GML + '"' +
+                                        " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
                 "  <gml:alias>\n" +
                 "    <gco:ScopedName>myScope:myName</gco:ScopedName>\n" +
                 "  </gml:alias>\n" +



Mime
View raw message