Author: desruisseaux
Date: Fri May 11 17:44:26 2018
New Revision: 1831436

URL: http://svn.apache.org/viewvc?rev=1831436&view=rev
Log:
Enable lenient unmarshalling only from XML.unmarshal(...) methods, for avoiding 
surprising behavior when a user create his own UnmarshallerPool.
This restore a behavior closer to the one we had before the "upgrade metadata 
to ISO 19115-3:2016" work.

Modified:
    
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/Context.java
    
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/TypeRegistration.java
    
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/Pooled.java
    
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/XML.java
    
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/lan/LanguageCodeTest.java
    
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/XMLTestCase.java
    
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java

Modified: 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/Context.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/Context.java?rev=1831436&r1=1831435&r2=1831436&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/Context.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/Context.java
 [UTF-8] Fri May 11 17:44:26 2018
@@ -98,9 +98,16 @@ public final class Context extends Marsh
     public static final int LEGACY_METADATA = 0x20;
 
     /**
+     * Whether the unmarshalling process should accept any metadata or GML 
version if the user did not
+     * specified an explicit version. Accepting any version may have 
surprising results since namespace
+     * substitutions may be performed on the fly.
+     */
+    public static final int LENIENT_UNMARSHAL = 0x40;
+
+    /**
      * Bit where to store whether {@link #finish()} shall invoke {@code 
Semaphores.clear(Semaphores.NULL_COLLECTION)}.
      */
-    private static final int CLEAR_SEMAPHORE = 0x40;
+    private static final int CLEAR_SEMAPHORE = 0x80;
 
     /**
      * The thread-local context. Elements are created in the constructor, and 
removed in a

Modified: 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/TypeRegistration.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/TypeRegistration.java?rev=1831436&r1=1831435&r2=1831436&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/TypeRegistration.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/TypeRegistration.java
 [UTF-8] Fri May 11 17:44:26 2018
@@ -194,7 +194,7 @@ public abstract class TypeRegistration {
      * Completes the given properties with an entry for {@link #ROOT_ADAPTERS} 
if not already present.
      * If a {@code ROOT_ADAPTERS} entry is already present, then the map is 
returned unchanged.
      *
-     * <p>This method store a direct reference to the internal {@code 
TypeRegistration[]} array in the given map.
+     * <p>This method stores a reference to the internal {@code 
TypeRegistration[]} array in a copy of the given map.
      * <strong>That array shall not be modified.</strong> This method is 
currently for Apache SIS internal usage only,
      * because the {@code TypeRegistration} class is not part of public API. 
However if we add this functionality in a
      * future SIS release (probably as an interface rather than exposing 
{@code TypeRegistration} itself), then we may

Modified: 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/Pooled.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/Pooled.java?rev=1831436&r1=1831435&r2=1831436&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/Pooled.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/Pooled.java
 [UTF-8] Fri May 11 17:44:26 2018
@@ -250,11 +250,11 @@ abstract class Pooled {
      */
     final TransformVersion getTransformVersion() {
         /*
-         * If no version is specified, then the default behavior will be:
+         * If no version is specified and unmarshalling is lenient, then the 
default behavior will be:
          *   - enable namespace replacement on unmarshalling (in order to 
accept all versions)
          *   - disable namespace replacement on marshalling (in order to use 
latest version).
          */
-        final boolean byDefault = (bitMasks & Context.MARSHALLING) == 0;
+        final boolean byDefault = (bitMasks & (Context.MARSHALLING | 
Context.LENIENT_UNMARSHAL)) == Context.LENIENT_UNMARSHAL;
         /*
          * Bitwise combination of legacy schemas to support:
          *   1: namespace replacement needed for GML
@@ -348,6 +348,14 @@ abstract class Pooled {
                     converter = (ValueConverter) value;
                     return;
                 }
+                case XML.LENIENT_UNMARSHAL: {
+                    if ((value instanceof CharSequence) ? 
Boolean.parseBoolean(value.toString()) : (Boolean) value) {
+                        bitMasks |= Context.LENIENT_UNMARSHAL;
+                    } else {
+                        bitMasks &= ~Context.LENIENT_UNMARSHAL;
+                    }
+                    return;
+                }
                 case XML.STRING_SUBSTITUTES: {
                     bitMasks &= ~(Context.SUBSTITUTE_LANGUAGE |
                                   Context.SUBSTITUTE_COUNTRY  |
@@ -402,14 +410,15 @@ abstract class Pooled {
     @SuppressWarnings("ReturnOfCollectionOrArrayField")     // Because 
unmodifiable.
     public final Object getProperty(String name) throws PropertyException {
         switch (name) {
-            case XML.LOCALE:           return locale;
-            case XML.TIMEZONE:         return timezone;
-            case XML.SCHEMAS:          return schemas;
-            case XML.GML_VERSION:      return versionGML;
-            case XML.METADATA_VERSION: return versionMetadata;
-            case XML.RESOLVER:         return resolver;
-            case XML.CONVERTER:        return converter;
-            case XML.WARNING_LISTENER: return warningListener;
+            case XML.LOCALE:            return locale;
+            case XML.TIMEZONE:          return timezone;
+            case XML.SCHEMAS:           return schemas;
+            case XML.GML_VERSION:       return versionGML;
+            case XML.METADATA_VERSION:  return versionMetadata;
+            case XML.RESOLVER:          return resolver;
+            case XML.CONVERTER:         return converter;
+            case XML.WARNING_LISTENER:  return warningListener;
+            case XML.LENIENT_UNMARSHAL: return (bitMasks & 
Context.LENIENT_UNMARSHAL) != 0;
             case XML.STRING_SUBSTITUTES: {
                 int n = 0;
                 final String[] substitutes = new String[4];

Modified: 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/XML.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/XML.java?rev=1831436&r1=1831435&r2=1831436&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/XML.java 
[UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/xml/XML.java 
[UTF-8] Fri May 11 17:44:26 2018
@@ -19,6 +19,7 @@ package org.apache.sis.xml;
 import java.util.Map;
 import java.util.Locale;
 import java.util.TimeZone;
+import java.util.Collections;
 import java.util.logging.LogRecord;             // For javadoc
 import java.net.URL;
 import java.io.File;
@@ -229,6 +230,13 @@ public final class XML extends Static {
     public static final String METADATA_VERSION = 
"org.apache.sis.xml.version.metadata";
 
     /**
+     * Whether the unmarshalling process should accept any metadata or GML 
version if the user did not
+     * specified an explicit version. Accepting any version may have 
surprising results since namespace
+     * substitutions may be performed on the fly.
+     */
+    static final String LENIENT_UNMARSHAL = 
"org.apache.sis.xml.version.lenient";
+
+    /**
      * Allows client code to replace {@code xlink} or {@code uuidref} 
attributes by the actual objects to use.
      * The value for this property shall be an instance of {@link 
ReferenceResolver}.
      *
@@ -398,7 +406,7 @@ public final class XML extends Static {
             synchronized (XML.class) {
                 pool = POOL;                            // Double-check idiom: 
see javadoc.
                 if (pool == null) {
-                    POOL = pool = new MarshallerPool(null);
+                    POOL = pool = new 
MarshallerPool(Collections.singletonMap(LENIENT_UNMARSHAL, Boolean.TRUE));
                 }
             }
         }

Modified: 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/lan/LanguageCodeTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/lan/LanguageCodeTest.java?rev=1831436&r1=1831435&r2=1831436&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/lan/LanguageCodeTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/internal/jaxb/lan/LanguageCodeTest.java
 [UTF-8] Fri May 11 17:44:26 2018
@@ -92,6 +92,7 @@ public final strictfp class LanguageCode
         final Map<String,Object> properties = new HashMap<>(4);
         assertNull(properties.put(XML.LOCALE, Locale.UK));
         assertNull(properties.put(XML.TIMEZONE, UTC));
+        assertNull(properties.put("org.apache.sis.xml.version.lenient", 
Boolean.TRUE));   // XML.LENIENT_UNMARSHAL
         pool = new MarshallerPool(JAXBContext.newInstance(MetadataMock.class), 
properties);
     }
 

Modified: 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/XMLTestCase.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/XMLTestCase.java?rev=1831436&r1=1831435&r2=1831436&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/XMLTestCase.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/test/XMLTestCase.java
 [UTF-8] Fri May 11 17:44:26 2018
@@ -138,6 +138,7 @@ public abstract strictfp class XMLTestCa
             final Map<String,Object> properties = new HashMap<>(4);
             assertNull(properties.put(XML.LOCALE, Locale.UK));
             assertNull(properties.put(XML.TIMEZONE, TIMEZONE));
+            assertNull(properties.put("org.apache.sis.xml.version.lenient", 
Boolean.TRUE));   // XML.LENIENT_UNMARSHAL
             defaultPool = new MarshallerPool(properties);
         }
         return defaultPool;

Modified: 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java?rev=1831436&r1=1831435&r2=1831436&view=diff
==============================================================================
--- 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/core/sis-metadata/src/test/java/org/apache/sis/util/iso/NameMarshallingTest.java
 [UTF-8] Fri May 11 17:44:26 2018
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.util.iso;
 
+import java.util.Collections;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.JAXBContext;
@@ -64,7 +65,8 @@ public final strictfp class NameMarshall
      */
     private String marshal(final GenericName name) throws JAXBException {
         if (pool == null) {
-            pool = new 
MarshallerPool(JAXBContext.newInstance(IdentifiedObjectMock.class), null);
+            pool = new 
MarshallerPool(JAXBContext.newInstance(IdentifiedObjectMock.class),
+                    
Collections.singletonMap("org.apache.sis.xml.version.lenient", Boolean.TRUE));
         }
         final Marshaller marshaller = pool.acquireMarshaller();
         marshaller.setProperty(XML.METADATA_VERSION, VERSION_2007);


Reply via email to