sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1820900 - in /sis/branches/ISO-19115-3/core: sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/ sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/ sis-utility/src/main/java/org/apache/sis/xml/ sis-ut...
Date Thu, 11 Jan 2018 17:07:04 GMT
Author: desruisseaux
Date: Thu Jan 11 17:07:04 2018
New Revision: 1820900

URL: http://svn.apache.org/viewvc?rev=1820900&view=rev
Log:
Adaptation of part of commit 3a5a78b520f7d1827acc6bee6c42eb91a497e6f4 from Image Matter
(included tests for ISO 19115-3 in roughly 2/3 of the test classes for ISO metadata).
We use a different strategy: convert the test case to ISO 19115-3, and perform automatic
prefix and namespace replacements for testing legacy ISO 19139:2007.
We test on DefaultBrowseGraphic at first, before to expand this approach to other classes.
All XML fragments modified in this commit have been validated.

Modified:
    sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphic.java
    sis/branches/ISO-19115-3/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java
    sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java
    sis/branches/ISO-19115-3/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java

Modified: sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphic.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphic.java?rev=1820900&r1=1820899&r2=1820900&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphic.java
[UTF-8] (original)
+++ sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphic.java
[UTF-8] Thu Jan 11 17:07:04 2018
@@ -17,7 +17,6 @@
 package org.apache.sis.metadata.iso.identification;
 
 import java.net.URI;
-import java.net.URISyntaxException;
 import java.util.Collection;
 import javax.xml.bind.annotation.XmlType;
 import javax.xml.bind.annotation.XmlElement;
@@ -29,9 +28,7 @@ import org.opengis.metadata.constraint.C
 import org.opengis.metadata.identification.BrowseGraphic;
 import org.apache.sis.metadata.iso.ISOMetadata;
 import org.apache.sis.internal.jaxb.gmx.MimeFileTypeAdapter;
-import org.apache.sis.internal.jaxb.LegacyNamespaces;
 import org.apache.sis.internal.jaxb.FilterByVersion;
-import org.apache.sis.internal.jaxb.Context;
 import org.apache.sis.xml.Namespaces;
 
 
@@ -62,8 +59,7 @@ import org.apache.sis.xml.Namespaces;
  */
 @SuppressWarnings("CloneableClassWithoutClone")                 // ModifiableMetadata needs
shallow clones.
 @XmlType(name = "MD_BrowseGraphic_Type", namespace = Namespaces.MCC, propOrder = {
-    "fileNameURL",              // "fileName" attribute marshalled in ISO 19139:2007 way.
-    "fileNameString",           // "fileName" attribute marshalled in ISO 19115-3 way.
+    "fileName",
     "fileDescription",
     "fileType",
     "linkage",
@@ -168,6 +164,7 @@ public class DefaultBrowseGraphic extend
      * @return file that contains a graphic that provides an illustration of the dataset,
or {@code null}.
      */
     @Override
+    @XmlElement(name = "fileName", required = true)
     public URI getFileName() {
         return fileName;
     }
@@ -294,47 +291,6 @@ public class DefaultBrowseGraphic extend
     //////////////////////////////////////////////////////////////////////////////////////////////////
 
     /**
-     * Invoked at marshalling time for writing URL as defined by ISO 19139:2007.
-     * That legacy standard wraps the URL in a {@code <gmx:FileName>} element.
-     */
-    @XmlElement(name = "fileName", namespace = LegacyNamespaces.GMD, required = true)
-    private URI getFileNameURL() {
-        return FilterByVersion.LEGACY_METADATA.accept() ? getFileName() : null;
-    }
-
-    /**
-     * Invoked at ISO 19139:2007 unmarshalling time for storing the value of {@code <gmd:URL>}
element.
-     */
-    @SuppressWarnings("unused")
-    private void setFileNameURL(final URI newValue) {
-        setFileName(newValue);
-    }
-
-    /**
-     * Invoked at marshalling time for writing URL as defined by ISO 19115-3.
-     * That newer standard write the URL directly, without wrapping in {@code <gmd:URL>}
element.
-     */
-    @XmlElement(name = "fileName", required = true)
-    private String getFileNameString() {
-        if (FilterByVersion.CURRENT_METADATA.accept()) {
-            final URI linkage = getFileName();
-            if (linkage != null) {
-                return linkage.toString();
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Invoked at ISO 19115-3 unmarshalling time for parsing the URL.
-     */
-    @SuppressWarnings("unused")
-    private void setFileNameString(final String newValue) throws URISyntaxException {
-        final Context context = Context.current();
-        setFileName(Context.converter(context).toURI(context, newValue));
-    }
-
-    /**
      * Invoked by JAXB at both marshalling and unmarshalling time.
      * This attribute has been added by ISO 19115:2014 standard.
      * If (and only if) marshalling an older standard version, we omit this attribute.

Modified: sis/branches/ISO-19115-3/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java?rev=1820900&r1=1820899&r2=1820900&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java
[UTF-8] (original)
+++ sis/branches/ISO-19115-3/core/sis-metadata/src/test/java/org/apache/sis/metadata/iso/identification/DefaultBrowseGraphicTest.java
[UTF-8] Thu Jan 11 17:07:04 2018
@@ -18,17 +18,20 @@ package org.apache.sis.metadata.iso.iden
 
 import java.net.URI;
 import java.io.StringReader;
-import java.io.StringWriter;
 import java.util.logging.LogRecord;
+import javax.xml.bind.Marshaller;
 import javax.xml.bind.JAXBException;
 import javax.xml.transform.stream.StreamSource;
-import javax.xml.transform.stream.StreamResult;
+import org.opengis.metadata.identification.BrowseGraphic;
+import org.apache.sis.internal.jaxb.LegacyNamespaces;
 import org.apache.sis.util.logging.WarningListener;
 import org.apache.sis.util.logging.Logging;
-import org.apache.sis.test.DependsOnMethod;
-import org.apache.sis.test.TestCase;
+import org.apache.sis.util.Version;
+import org.apache.sis.xml.MarshallerPool;
 import org.apache.sis.xml.Namespaces;
 import org.apache.sis.xml.XML;
+import org.apache.sis.test.XMLTestCase;
+import org.apache.sis.test.DependsOnMethod;
 import org.junit.Test;
 
 import static org.apache.sis.test.Assert.*;
@@ -44,9 +47,34 @@ import static java.util.Collections.sing
  * @since   0.4
  * @module
  */
-public final strictfp class DefaultBrowseGraphicTest extends TestCase {
+public final strictfp class DefaultBrowseGraphicTest extends XMLTestCase {
+    /**
+     * {@code false} if testing ISO 19115-3 document, or {@code true} if testing ISO 19139:2007
document.
+     */
+    private boolean legacy;
+
+    /**
+     * Verifies that marshalling the given metadata produces the expected XML document,
+     * then verifies that unmarshalling that document gives back the original metadata object.
+     * If {@link #legacy} is {@code true}, then this method will use ISO 19139:2007 schema.
+     */
+    private void roundtrip(final BrowseGraphic browse, String expected) throws JAXBException
{
+        final String  actual;
+        final Version version;
+        if (legacy) {
+            expected = toLegacyXML(expected);
+            version  = LegacyNamespaces.ISO_19139;
+        } else {
+            version  = LegacyNamespaces.ISO_19115_3;
+        }
+        actual = marshal(browse, version);
+        assertXmlEquals(expected, actual, "xmlns:*");
+        assertEquals(browse, unmarshal(BrowseGraphic.class, actual));
+    }
+
     /**
-     * Tests XML marshalling of {@code <gcx:MimeFileType>} inside {@code <gmd:MD_BrowseGraphic>}.
+     * Tests XML marshalling of {@code <gcx:MimeFileType>} inside {@code <mcc:MD_BrowseGraphic>}.
+     * This method uses the XML schema defined by ISO 19115-3.
      *
      * @throws JAXBException if an error occurred while (un)marshalling the {@code BrowseGraphic}.
      */
@@ -54,41 +82,56 @@ public final strictfp class DefaultBrows
     public void testMimeFileType() throws JAXBException {
         final DefaultBrowseGraphic browse = new DefaultBrowseGraphic();
         browse.setFileType("image/tiff");
-        final String xml = XML.marshal(browse);
-        assertXmlEquals(
-                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+        roundtrip(browse,
+                "<mcc:MD_BrowseGraphic xmlns:mcc=\"" + Namespaces.MCC + '"' +
                                      " xmlns:gcx=\"" + Namespaces.GCX + "\">\n" +
-                "  <gmd:fileType>\n" +
+                "  <mcc:fileType>\n" +
                 "    <gcx:MimeFileType type=\"image/tiff\">image/tiff</gcx:MimeFileType>\n"
+
-                "  </gmd:fileType>\n" +
-                "</gmd:MD_BrowseGraphic>", xml, "xmlns:*");
-        /*
-         * Unmarshal the element back to a Java object and compare to the original.
-         */
-        assertEquals(browse, XML.unmarshal(xml));
+                "  </mcc:fileType>\n" +
+                "</mcc:MD_BrowseGraphic>");
     }
 
     /**
-     * Tests XML marshalling of {@code <gcx:FileName>} inside {@code <gmd:MD_BrowseGraphic>}.
+     * Tests XML marshalling of {@code <gmx:MimeFileType>} inside {@code <gmd:MD_BrowseGraphic>}.
+     * This method uses the XML schema defined by ISO 19139:2007.
+     *
+     * @throws JAXBException if an error occurred while (un)marshalling the {@code BrowseGraphic}.
+     */
+    @Test
+    @DependsOnMethod("testMimeFileType")
+    public void testMimeFileType_Legacy() throws JAXBException {
+        legacy = true;
+        testMimeFileType();
+    }
+
+    /**
+     * Tests XML marshalling of {@code <gcx:FileName>} inside {@code <mcc:MD_BrowseGraphic>}.
+     * This method uses the XML schema defined by ISO 19115-3.
      *
      * @throws JAXBException if an error occurred while (un)marshalling the {@code BrowseGraphic}.
      */
     @Test
     public void testFileName() throws JAXBException {
-        final URI uri = URI.create("file:/catalog/image.png");
-        final DefaultBrowseGraphic browse = new DefaultBrowseGraphic(uri);
-        final String xml = XML.marshal(browse);
-        assertXmlEquals(
-                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+        roundtrip(new DefaultBrowseGraphic(URI.create("file:/catalog/image.png")),
+                "<mcc:MD_BrowseGraphic xmlns:mcc=\"" + Namespaces.MCC + '"' +
                                      " xmlns:gcx=\"" + Namespaces.GCX + "\">\n" +
-                "  <gmd:fileName>\n" +
+                "  <mcc:fileName>\n" +
                 "    <gcx:FileName src=\"file:/catalog/image.png\">image.png</gcx:FileName>\n"
+
-                "  </gmd:fileName>\n" +
-                "</gmd:MD_BrowseGraphic>", xml, "xmlns:*");
-        /*
-         * Unmarshal the element back to a Java object and compare to the original.
-         */
-        assertEquals(browse, XML.unmarshal(xml));
+                "  </mcc:fileName>\n" +
+                "</mcc:MD_BrowseGraphic>");
+    }
+
+    /**
+     * Tests XML marshalling of {@code <gmx:FileName>} inside {@code <gmd:MD_BrowseGraphic>}.
+     * This method uses the XML schema defined by ISO 19139:2007.
+     *
+     * @throws JAXBException if an error occurred while (un)marshalling the {@code BrowseGraphic}.
+     */
+    @Test
+    @DependsOnMethod("testFileName")
+    public void testFileName_Legacy() throws JAXBException {
+        legacy = true;
+        testFileName();
     }
 
     /**
@@ -99,75 +142,85 @@ public final strictfp class DefaultBrows
     @Test
     @DependsOnMethod("testFileName")
     public void testFileNameWithoutSrc() throws JAXBException {
-        final DefaultBrowseGraphic browse = (DefaultBrowseGraphic) XML.unmarshal(
-                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+        final DefaultBrowseGraphic browse = unmarshal(DefaultBrowseGraphic.class,
+                "<mcc:MD_BrowseGraphic xmlns:mcc=\"" + Namespaces.MCC + '"' +
                                      " xmlns:gcx=\"" + Namespaces.GCX + "\">\n" +
-                "  <gmd:fileName>\n" +
+                "  <mcc:fileName>\n" +
                 "    <gcx:FileName>file:/catalog/image.png</gcx:FileName>\n"
+
-                "  </gmd:fileName>\n" +
-                "</gmd:MD_BrowseGraphic>");
+                "  </mcc:fileName>\n" +
+                "</mcc:MD_BrowseGraphic>");
 
         assertEquals(URI.create("file:/catalog/image.png"), browse.getFileName());
     }
 
     /**
      * Tests XML marshalling of {@code <gcx:FileName>} and {@code <gcx:MimeFileType>}
together.
+     * This method uses the XML schema defined by ISO 19115-3.
      *
      * @throws JAXBException if an error occurred while (un)marshalling the {@code BrowseGraphic}.
      */
     @Test
     @DependsOnMethod({"testFileName", "testMimeFileType"})
     public void testFileNameAndType() throws JAXBException {
-        final URI uri = URI.create("file:/catalog/image.png");
-        final DefaultBrowseGraphic browse = new DefaultBrowseGraphic(uri);
+        final DefaultBrowseGraphic browse = new DefaultBrowseGraphic(URI.create("file:/catalog/image.png"));
         browse.setFileType("image/tiff");
-        final String xml = XML.marshal(browse);
-        assertXmlEquals(
-                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+        roundtrip(browse,
+                "<mcc:MD_BrowseGraphic xmlns:mcc=\"" + Namespaces.MCC + '"' +
                                      " xmlns:gcx=\"" + Namespaces.GCX + "\">\n" +
-                "  <gmd:fileName>\n" +
+                "  <mcc:fileName>\n" +
                 "    <gcx:FileName src=\"file:/catalog/image.png\">image.png</gcx:FileName>\n"
+
-                "  </gmd:fileName>\n" +
-                "  <gmd:fileType>\n" +
+                "  </mcc:fileName>\n" +
+                "  <mcc:fileType>\n" +
                 "    <gcx:MimeFileType type=\"image/tiff\">image/tiff</gcx:MimeFileType>\n"
+
-                "  </gmd:fileType>\n" +
-                "</gmd:MD_BrowseGraphic>", xml, "xmlns:*");
-        /*
-         * Unmarshal the element back to a Java object and compare to the original.
-         */
-        assertEquals(browse, XML.unmarshal(xml));
+                "  </mcc:fileType>\n" +
+                "</mcc:MD_BrowseGraphic>");
+    }
+
+    /**
+     * Tests XML marshalling of {@code <gmx:FileName>} and {@code <gmx:MimeFileType>}
together.
+     * This method uses the XML schema defined by ISO 19139:2007.
+     *
+     * @throws JAXBException if an error occurred while (un)marshalling the {@code BrowseGraphic}.
+     */
+    @Test
+    @DependsOnMethod({"testFileName_Legacy", "testMimeFileType_Legacy"})
+    public void testFileNameAndType_Legacy() throws JAXBException {
+        legacy = true;
+        testFileNameAndType();
     }
 
     /**
      * Tests XML marshalling of filename substituted by {@code <gco:CharacterString>}
-     * inside {@code <gmd:MD_BrowseGraphic>}.
+     * inside {@code <mcc:MD_BrowseGraphic>}.
      *
      * @throws JAXBException if an error occurred while (un)marshalling the {@code BrowseGraphic}.
      */
     @Test
     @DependsOnMethod("testFileNameAndType")
     public void testStringSubstitution() throws JAXBException {
-        final URI uri = URI.create("file:/catalog/image.png");
-        final DefaultBrowseGraphic browse = new DefaultBrowseGraphic(uri);
+        final DefaultBrowseGraphic browse = new DefaultBrowseGraphic(URI.create("file:/catalog/image.png"));
         browse.setFileType("image/tiff");
-        final StringWriter buffer = new StringWriter();
-        XML.marshal(browse, new StreamResult(buffer),
-                singletonMap(XML.STRING_SUBSTITUTES, new String[] {"filename", "mimetype"}));
-        final String xml = buffer.toString();
+
+        final MarshallerPool pool = getMarshallerPool();
+        final Marshaller marshaller = pool.acquireMarshaller();
+        marshaller.setProperty(XML.STRING_SUBSTITUTES, new String[] {"filename", "mimetype"});
+        final String xml = marshal(marshaller, browse);
+        pool.recycle(marshaller);
+
         assertXmlEquals(
-                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                "<mcc:MD_BrowseGraphic xmlns:mcc=\"" + Namespaces.MCC + '"' +
                                      " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
-                "  <gmd:fileName>\n" +
+                "  <mcc:fileName>\n" +
                 "    <gco:CharacterString>file:/catalog/image.png</gco:CharacterString>\n"
+
-                "  </gmd:fileName>\n" +
-                "  <gmd:fileType>\n" +
+                "  </mcc:fileName>\n" +
+                "  <mcc:fileType>\n" +
                 "    <gco:CharacterString>image/tiff</gco:CharacterString>\n"
+
-                "  </gmd:fileType>\n" +
-                "</gmd:MD_BrowseGraphic>", xml, "xmlns:*");
+                "  </mcc:fileType>\n" +
+                "</mcc:MD_BrowseGraphic>", xml, "xmlns:*");
         /*
          * Unmarshal the element back to a Java object and compare to the original.
          */
-        assertEquals(browse, XML.unmarshal(xml));
+        assertEquals(browse, unmarshal(BrowseGraphic.class, xml));
     }
 
     /**
@@ -181,14 +234,14 @@ public final strictfp class DefaultBrows
     public void testDuplicatedValues() throws JAXBException {
         final Warning listener = new Warning();
         final DefaultBrowseGraphic browse = listener.unmarshal(
-                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                "<mcc:MD_BrowseGraphic xmlns:mcc=\"" + Namespaces.MCC + '"' +
                                      " xmlns:gcx=\"" + Namespaces.GCX + '"' +
                                      " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
-                "  <gmd:fileName>\n" +
+                "  <mcc:fileName>\n" +
                 "    <gcx:FileName src=\"file:/catalog/image.png\">image.png</gcx:FileName>\n"
+
                 "    <gco:CharacterString>file:/catalog/image.png</gco:CharacterString>\n"
+
-                "  </gmd:fileName>\n" +
-                "</gmd:MD_BrowseGraphic>");
+                "  </mcc:fileName>\n" +
+                "</mcc:MD_BrowseGraphic>");
 
         assertEquals(URI.create("file:/catalog/image.png"), browse.getFileName());
         assertFalse("Expected no warning.", listener.receivedWarning);
@@ -219,14 +272,14 @@ public final strictfp class DefaultBrows
     private void testWarnings(final String first, final String second) throws JAXBException
{
         final Warning listener = new Warning();
         final DefaultBrowseGraphic browse = listener.unmarshal(
-                "<gmd:MD_BrowseGraphic xmlns:gmd=\"" + Namespaces.GMD + '"' +
+                "<mcc:MD_BrowseGraphic xmlns:mcc=\"" + Namespaces.MCC + '"' +
                                      " xmlns:gcx=\"" + Namespaces.GCX + '"' +
                                      " xmlns:gco=\"" + Namespaces.GCO + "\">\n" +
-                "  <gmd:fileName>\n" +
+                "  <mcc:fileName>\n" +
                 "    " + first + "\n" +
                 "    " + second + "\n" +
-                "  </gmd:fileName>\n" +
-                "</gmd:MD_BrowseGraphic>");
+                "  </mcc:fileName>\n" +
+                "</mcc:MD_BrowseGraphic>");
 
         assertEquals(URI.create("file:/catalog/image.png"), browse.getFileName());
         assertTrue("Expected a warning.", listener.receivedWarning);

Modified: sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java?rev=1820900&r1=1820899&r2=1820900&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java [UTF-8]
(original)
+++ sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java [UTF-8]
Thu Jan 11 17:07:04 2018
@@ -54,6 +54,17 @@ import static org.apache.sis.util.Argume
 
 /**
  * Provides convenience methods for marshalling and unmarshalling SIS objects.
+ * Marshalling operations use the standard versions listed below
+ * (for marshalling a document in a different version, see {@link PooledMarshaller}).
+ * Unmarshalling detects the version automatically.
+ *
+ * <table class="sis">
+ *   <caption>Versions of standards applied at marshalling time</caption>
+ *   <tr><th>Topic</th>       <th>SIS 0.3 to 0.8</th>  <th>SIS
1.0</th></tr>
+ *   <tr><td>Metadata</td>    <td>ISO 19139:2007</td>  <td>ISO
19115-3</td></tr>
+ *   <tr><td>Referencing</td> <td>ISO 19136</td>       <td>ISO
19136</td></tr>
+ * </table>
+ *
  * This class defines also some property keys that can be given to the {@link Marshaller}
  * and {@link Unmarshaller} instances created by {@link PooledMarshaller}:
  *

Modified: sis/branches/ISO-19115-3/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java?rev=1820900&r1=1820899&r2=1820900&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java
[UTF-8] (original)
+++ sis/branches/ISO-19115-3/core/sis-utility/src/test/java/org/apache/sis/test/XMLTestCase.java
[UTF-8] Thu Jan 11 17:07:04 2018
@@ -31,8 +31,11 @@ import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.JAXBException;
 import org.apache.sis.internal.jaxb.Context;
+import org.apache.sis.internal.jaxb.LegacyNamespaces;
+import org.apache.sis.internal.jaxb.Schemas;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.Version;
+import org.apache.sis.xml.Namespaces;
 import org.apache.sis.xml.MarshallerPool;
 import org.apache.sis.xml.XML;
 import org.junit.After;
@@ -373,4 +376,54 @@ public abstract strictfp class XMLTestCa
             throw new AssertionError(e);
         }
     }
+
+    /**
+     * The string substitutions to perform for downgrading an ISO 19115-3 document to ISO
19139:2007.
+     * Values at even indices are strings to search, and values at odd indices are replacements.
+     */
+    private static final String[] TO_LEGACY_XML = {
+        Namespaces.CIT, LegacyNamespaces.GMD, "cit",  "gmd",
+        Namespaces.MCC, LegacyNamespaces.GMD, "mcc",  "gmd",
+        Namespaces.GCX, LegacyNamespaces.GMX, "gcx",  "gmx",
+        Schemas.METADATA_ROOT,  Schemas.METADATA_ROOT_LEGACY,           // For code lists
+        Schemas.CODELISTS_PATH, Schemas.CODELISTS_PATH_LEGACY
+    };
+
+    /**
+     * Performs a simple ISO 19115-3 to ISO 19139:2007 translations using only search-and-replaces.
+     * For example this method replaces {@code "cit"} prefix by {@code "gmd"} and the corresponding
+     * {@value Namespaces#CIT} namespace by {@value LegacyNamespaces#GMD}. However this method
does
+     * not perform any more complex translations like attributes refactored in other classes.
 If a
+     * more complex translation is required, the test case should provide the legacy XML
verbatim
+     * in a separated string.
+     *
+     * @param  xml  an XML compliant with ISO 19115-3.
+     * @return an XML compliant with ISO 19139:2007.
+     *
+     * @since 1.0
+     */
+    protected static String toLegacyXML(final String xml) {
+        final StringBuilder buffer = new StringBuilder(xml);
+        for (int c=0; c < TO_LEGACY_XML.length;) {
+            final String toSearch  = TO_LEGACY_XML[c++];
+            final String replaceBy = TO_LEGACY_XML[c++];
+            final int length = toSearch.length();
+            int i = buffer.length();
+            while ((i = buffer.lastIndexOf(toSearch, i)) >= 0) {
+                /*
+                 * Following may throw a StringIndexOutOfBoundsException if 'toSearch' is
located at the
+                 * beginning (i == 0) or end (end == buffer.length()) of the buffer. However
those cases
+                 * should never happen in Apache SIS test cases since it would be invalid
XML.
+                 */
+                if (!Character.isUnicodeIdentifierPart(buffer.codePointBefore(i))) {
+                    final int end = i + length;
+                    if (!Character.isUnicodeIdentifierPart(buffer.codePointAt(end))) {
+                        buffer.replace(i, end, replaceBy);
+                    }
+                }
+                i -= length;
+            }
+        }
+        return buffer.toString();
+    }
 }



Mime
View raw message