sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1818422 - in /sis/branches/ISO-19115-3/core: sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/ sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/ sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/r...
Date Sat, 16 Dec 2017 15:27:20 GMT
Author: desruisseaux
Date: Sat Dec 16 15:27:19 2017
New Revision: 1818422

URL: http://svn.apache.org/viewvc?rev=1818422&view=rev
Log:
Replace Context.isLegacyMetadata() by FilterByVersion.LEGACY_METADATA.accept().

Added:
    sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/FilterByVersion.java
  (with props)
Modified:
    sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java
    sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/CI_Responsibility.java
    sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ReferenceSystemMetadata.java
    sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java
    sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java

Modified: sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java?rev=1818422&r1=1818421&r2=1818422&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java
[UTF-8] (original)
+++ sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java
[UTF-8] Sat Dec 16 15:27:19 2017
@@ -24,6 +24,7 @@ import javax.xml.bind.annotation.XmlType
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.adapters.XmlAdapter;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
+import org.apache.sis.internal.jaxb.LegacyNamespaces;
 import org.apache.sis.internal.jaxb.Context;
 import org.apache.sis.internal.jaxb.gmd.Country;
 import org.apache.sis.internal.jaxb.gmd.LanguageCode;
@@ -111,6 +112,11 @@ public final class PT_Locale extends Xml
         Charset characterEncoding;
 
         /**
+         * {@code true} if marshalling ISO 19139:2007, or {@code false} if marshalling ISO
19115-3.
+         */
+        private boolean isLegacyMetadata;
+
+        /**
          * Empty constructor for JAXB only.
          */
         public Wrapper() {
@@ -121,17 +127,18 @@ public final class PT_Locale extends Xml
          */
         Wrapper(final Locale locale) {
             final Context context = Context.current();
-            languageCode = LanguageCode.create(context, locale);
-            country      = Country     .create(context, locale);
+            isLegacyMetadata = Context.isFlagSet(context, Context.LEGACY_METADATA);
+            languageCode     = LanguageCode.create(context, locale);
+            country          = Country     .create(context, locale);
             // The characterEncoding field will be initialized at marshalling time (see method
below).
         }
 
         /**
          * Gets the language code for this PT_Locale. Used in ISO 19139.
          */
-        @XmlElement(name = "languageCode")
+        @XmlElement(name = "languageCode", namespace = LegacyNamespaces.GMD)
         private LanguageCode getLanguageCode() {
-            return Context.isLegacyMetadata() ? languageCode : null;
+            return isLegacyMetadata ? languageCode : null;
         }
 
         /**
@@ -147,7 +154,7 @@ public final class PT_Locale extends Xml
          */
         @XmlElement(name = "language")
         private LanguageCode getLanguage() {
-            return Context.isLegacyMetadata() ? null : languageCode;
+            return isLegacyMetadata ? null : languageCode;
         }
 
         /**

Modified: sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/CI_Responsibility.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/CI_Responsibility.java?rev=1818422&r1=1818421&r2=1818422&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/CI_Responsibility.java
[UTF-8] (original)
+++ sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/CI_Responsibility.java
[UTF-8] Sat Dec 16 15:27:19 2017
@@ -21,7 +21,7 @@ import org.opengis.metadata.citation.Res
 import org.apache.sis.metadata.iso.citation.DefaultResponsibility;
 import org.apache.sis.metadata.iso.citation.DefaultResponsibleParty;
 import org.apache.sis.internal.jaxb.gco.PropertyType;
-import org.apache.sis.internal.jaxb.Context;
+import org.apache.sis.internal.jaxb.FilterByVersion;
 
 
 /**
@@ -82,10 +82,10 @@ public final class CI_Responsibility ext
     @XmlElementRef
     @SuppressWarnings("deprecation")
     public DefaultResponsibility getElement() {
-        if (Context.isLegacyMetadata()) {
-            return DefaultResponsibleParty.castOrCopy(metadata);
-        } else {
+        if (FilterByVersion.CURRENT_METADATA.accept()) {
             return DefaultResponsibility.castOrCopy(metadata);
+        } else {
+            return DefaultResponsibleParty.castOrCopy(metadata);
         }
     }
 

Modified: sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ReferenceSystemMetadata.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ReferenceSystemMetadata.java?rev=1818422&r1=1818421&r2=1818422&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ReferenceSystemMetadata.java
[UTF-8] (original)
+++ sis/branches/ISO-19115-3/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ReferenceSystemMetadata.java
[UTF-8] Sat Dec 16 15:27:19 2017
@@ -17,6 +17,7 @@
 package org.apache.sis.internal.jaxb.metadata.replace;
 
 import java.util.Objects;
+import javax.xml.bind.Marshaller;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@@ -25,7 +26,8 @@ import org.opengis.referencing.Reference
 import org.apache.sis.internal.jaxb.metadata.MD_Identifier;
 import org.apache.sis.internal.jaxb.metadata.RS_Identifier;
 import org.apache.sis.internal.simple.SimpleIdentifiedObject;
-import org.apache.sis.internal.jaxb.Context;
+import org.apache.sis.internal.jaxb.FilterByVersion;
+import org.apache.sis.internal.jaxb.LegacyNamespaces;
 import org.apache.sis.util.ComparisonMode;
 
 
@@ -57,6 +59,11 @@ public class ReferenceSystemMetadata ext
     private static final long serialVersionUID = 2810145397032096087L;
 
     /**
+     * {@code true} if marshalling ISO 19139:2007, or {@code false} if marshalling ISO 19115-3.
+     */
+    private boolean isLegacyMetadata;
+
+    /**
      * Creates a reference system without identifier.
      * This constructor is mainly for JAXB.
      */
@@ -82,6 +89,13 @@ public class ReferenceSystemMetadata ext
     }
 
     /**
+     * Invoked by JAXB {@link javax.xml.bind.Marshaller} before this object is marshalled
to XML.
+     */
+    private void beforeMarshal(final Marshaller marshaller) {
+        isLegacyMetadata = !FilterByVersion.CURRENT_METADATA.accept();
+    }
+
+    /**
      * Returns the primary name by which this object is identified.
      * This method can be invoked during ISO 19115-3 marshalling.
      *
@@ -91,7 +105,7 @@ public class ReferenceSystemMetadata ext
     @XmlElement(name = "referenceSystemIdentifier")
     @XmlJavaTypeAdapter(MD_Identifier.class)
     public final Identifier getName() {
-        return Context.isLegacyMetadata() ? null : super.getName();
+        return isLegacyMetadata ? null : super.getName();
     }
 
     /**
@@ -107,10 +121,10 @@ public class ReferenceSystemMetadata ext
      * Gets the name for this reference system metadata (used in ISO 19139 format).
      * This method can be invoked during ISO 19139 marshalling.
      */
-    @XmlElement(name = "referenceSystemIdentifier")
+    @XmlElement(name = "referenceSystemIdentifier", namespace = LegacyNamespaces.GMD)
     @XmlJavaTypeAdapter(RS_Identifier.class)
     private Identifier getLegacyName() {
-        return Context.isLegacyMetadata() ? super.getName() : null;
+        return isLegacyMetadata ? super.getName() : null;
     }
 
     /**

Modified: sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java?rev=1818422&r1=1818421&r2=1818422&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java
[UTF-8] (original)
+++ sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/Context.java
[UTF-8] Sat Dec 16 15:27:19 2017
@@ -99,7 +99,7 @@ public final class Context extends Marsh
      * Whether we are (un)marshalling legacy metadata as defined in 2003 (ISO 19139).
      * If this flag is not set, then we assume latest metadata as defined in 2014 (ISO 19115:2014).
      */
-    private static final int LEGACY_METADATA = 0x40;
+    public static final int LEGACY_METADATA = 0x40;
 
     /**
      * The thread-local context. Elements are created in the constructor, and removed in
a
@@ -114,9 +114,17 @@ public final class Context extends Marsh
     public static final Logger LOGGER = Logging.getLogger(Loggers.XML);
 
     /**
+     * Notifies {@code FilterByVersion} that (un)marshalling process may happen.
+     * This is used for avoiding classes loading before this possibility exists.
+     */
+    static {
+        FilterByVersion.enable();
+    }
+
+    /**
      * Various boolean attributes determines by the above static constants.
      */
-    private int bitMasks;
+    final int bitMasks;
 
     /**
      * The locale to use for marshalling, or an empty queue if no locale were explicitly
specified.
@@ -229,7 +237,6 @@ public final class Context extends Marsh
         if (versionMetadata != null && versionMetadata.compareTo(LegacyNamespaces.ISO_19115_3)
< 0) {
             bitMasks |= LEGACY_METADATA;
         }
-        this.bitMasks          = bitMasks;
         this.locales           = new LinkedList<>();
         this.timezone          = timezone;
         this.schemas           = schemas;               // No clone, because this class is
internal.
@@ -243,17 +250,18 @@ public final class Context extends Marsh
             locales.add(locale);
         }
         previous = CURRENT.get();
-        CURRENT.set(this);
         if ((bitMasks & MARSHALLING) != 0) {
             /*
-             * Set global semaphore last after we are sure that construction will not fail
-             * (e.g. with an OutOfMemoryError). This is necessary for allowing the caller
-             * to invoke finish() in a finally block.
+             * Set global semaphore last after our best effort to ensure that construction
+             * will not fail with an OutOfMemoryError. This is preferable for allowing the
+             * caller to invoke finish() in a finally block.
              */
             if (!Semaphores.queryAndSet(Semaphores.NULL_COLLECTION)) {
-                this.bitMasks |= CLEAR_SEMAPHORE;
+                bitMasks |= CLEAR_SEMAPHORE;
             }
         }
+        this.bitMasks = bitMasks;
+        CURRENT.set(this);
     }
 
     /**
@@ -390,17 +398,6 @@ public final class Context extends Marsh
     }
 
     /**
-     * Returns {@code true} if we are (un)marshalling the legacy ISO 19139:2007 metadata
format.
-     * A value of {@code false} means that we are (un)marshalling the more recent ISO 19115-3
format,
-     * or that no (un)marshalling process is under way.
-     *
-     * @return whether the (un)marshalling process is for legacy ISO 19139 metadata format.
-     */
-    public static boolean isLegacyMetadata() {
-        return isFlagSet(current(), LEGACY_METADATA);
-    }
-
-    /**
      * Returns the base URL of ISO 19139 (or other standards) schemas.
      * The valid values are documented in the {@link org.apache.sis.xml.XML#SCHEMAS} property.
      * If the returned value is not empty, then this method guarantees it ends with {@code
'/'}.

Added: sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/FilterByVersion.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/FilterByVersion.java?rev=1818422&view=auto
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/FilterByVersion.java
(added)
+++ sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/FilterByVersion.java
[UTF-8] Sat Dec 16 15:27:19 2017
@@ -0,0 +1,96 @@
+/*
+ * 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.sis.internal.jaxb;
+
+
+/**
+ * An enumeration of metadata or GML versions which determine which XML elements to include
or exclude
+ * during XML marshalling. If no marshalling is in progress, then all elements are considered
included.
+ * This enumeration is used in getter methods for XML elements that exist only in some versions
of ISO
+ * standards. They may be either deprecated elements to marshal only in legacy XML document
formats,
+ * or new elements to marshal only in new XML document formats.
+ *
+ * @author  Cullen Rombach (Image Matters)
+ * @author  Martin Desruisseaux (Geomatys)
+ * @version 1.0
+ * @since   1.0
+ * @module
+ */
+public enum FilterByVersion {
+    /**
+     * {@code accept() == true} if marshalling of an ISO 19139:2007 document in in progress,
+     * or if no marshalling in underway.
+     */
+    LEGACY_METADATA(Context.MARSHALLING | Context.LEGACY_METADATA,
+                    Context.MARSHALLING),
+
+    /**
+     * {@code accept() == true} if marshalling of an ISO 19139:2014 document in in progress,
+     * or if no marshalling in underway.
+     */
+    CURRENT_METADATA(Context.MARSHALLING | Context.LEGACY_METADATA,
+                     Context.MARSHALLING | Context.LEGACY_METADATA);
+
+    /**
+     * {@code true} if the {@link Context} class has been initialized.
+     * This is used as a filter for the common case where XML marshalling is not used at
all.
+     */
+    private static volatile boolean enabled;
+
+    /**
+     * Mask to apply on {@link Context#bitMasks} in order to determine the version of the
XML document
+     * being marshalled.
+     */
+    private final int mask;
+
+    /**
+     * {@code accept() == false} if the XML document version being marshalled is this version.
+     * We use exclusion instead than inclusion as an opportunistic way to get {@code true}
if
+     * no marshalling is in progress. This strategy works only if we have only two versions
to
+     * support and will need to be changed when we will have more versions.
+     */
+    private final int exclude;
+
+    /**
+     * Creates an enumeration value.
+     */
+    private FilterByVersion(final int mask, final int exclude) {
+        this.mask    = mask;
+        this.exclude = exclude;
+    }
+
+    /**
+     * Invoked by {@link Context} class initializer for notifying this {@code FilterByVersion}
class
+     * that XML marshalling may happen at any time in the future.
+     */
+    static void enable() {
+        enabled = true;
+    }
+
+    /**
+     * Returns {@code true} if we are marshalling the metadata or GML format identified by
this constant,
+     * or if no marshalling is in progress.
+     *
+     * @return {@code false} if the caller should omit XML element specific to the standard
identified
+     *         by this enumeration value.
+     */
+    public boolean accept() {
+        if (!enabled) return true;
+        final Context context = Context.current();
+        return (context == null) || (context.bitMasks & mask) != exclude;
+    }
+}

Propchange: sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/FilterByVersion.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/FilterByVersion.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
URL: http://svn.apache.org/viewvc/sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java?rev=1818422&r1=1818421&r2=1818422&view=diff
==============================================================================
--- sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
[UTF-8] (original)
+++ sis/branches/ISO-19115-3/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
[UTF-8] Sat Dec 16 15:27:19 2017
@@ -21,6 +21,8 @@ import javax.xml.bind.annotation.XmlType
 import javax.xml.bind.annotation.XmlElement;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.internal.jaxb.Context;
+import org.apache.sis.internal.jaxb.FilterByVersion;
+import org.apache.sis.internal.jaxb.LegacyNamespaces;
 import org.apache.sis.internal.jaxb.gco.GO_CharacterString;
 import org.apache.sis.internal.jaxb.gco.CharSequenceAdapter;
 import org.apache.sis.util.resources.Errors;
@@ -58,6 +60,11 @@ public final class Country extends GO_Ch
     private CodeListUID identifier;
 
     /**
+     * {@code true} if marshalling ISO 19139:2007, or {@code false} if marshalling ISO 19115-3.
+     */
+    private boolean isLegacyMetadata;
+
+    /**
      * Empty constructor for JAXB only.
      */
     private Country() {
@@ -69,6 +76,7 @@ public final class Country extends GO_Ch
      */
     private Country(final CharSequence code) {
         super(code);
+        detectVersion();
     }
 
     /**
@@ -82,14 +90,22 @@ public final class Country extends GO_Ch
      */
     private Country(final Context context, final String codeListValue, final String codeSpace,
final String value) {
         identifier = new CodeListUID(context, "Country", codeListValue, codeSpace, value);
+        detectVersion();
+    }
+
+    /**
+     * Determines if we are marshalling ISO 19139:2007 or ISO 19115-3 documents.
+     */
+    private void detectVersion() {
+        isLegacyMetadata = !FilterByVersion.CURRENT_METADATA.accept();
     }
 
     /**
      * Gets the value of the Country code using ISO 19139 element name.
      */
-    @XmlElement(name = "Country")
+    @XmlElement(name = "Country", namespace = LegacyNamespaces.GMD)
     private CodeListUID getCountry() {
-        return Context.isLegacyMetadata() ? identifier : null;
+        return isLegacyMetadata ? identifier : null;
     }
 
     /**
@@ -105,7 +121,7 @@ public final class Country extends GO_Ch
      */
     @XmlElement(name = "CountryCode")
     private CodeListUID getCountryCode() {
-        return Context.isLegacyMetadata() ? null : identifier;
+        return isLegacyMetadata ? null : identifier;
     }
 
     /**



Mime
View raw message