You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2013/06/20 12:59:43 UTC

svn commit: r1494922 - in /sis/branches/JDK7/core: sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/ sis-metadata/src/main/java/org/apache/sis/metadata/iso/ sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/

Author: desruisseaux
Date: Thu Jun 20 10:59:42 2013
New Revision: 1494922

URL: http://svn.apache.org/r1494922
Log:
Ported missing adapter for PT_Locale.

Added:
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java   (with props)
Modified:
    sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultMetadata.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java

Added: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java?rev=1494922&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java (added)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java [UTF-8] Thu Jun 20 10:59:42 2013
@@ -0,0 +1,201 @@
+/*
+ * 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.code;
+
+import java.util.Locale;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.PropertyException;
+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.opengis.metadata.identification.CharacterSet;
+import org.apache.sis.util.logging.Logging;
+import org.apache.sis.util.Locales;
+import org.apache.sis.util.iso.Types;
+import org.apache.sis.internal.jaxb.Context;
+import org.apache.sis.internal.jaxb.gmd.Country;
+import org.apache.sis.internal.jaxb.gmd.LanguageCode;
+
+
+/**
+ * JAXB adapter for {@link Locale}, in order to integrate the value in an element respecting
+ * the ISO-19139 standard. See package documentation for more information about the handling
+ * of {@code CodeList} in ISO-19139.
+ *
+ * <p>This adapter formats the locale like below:</p>
+ *
+ * {@preformat xml
+ *   <gmd:locale>
+ *     <gmd:PT_Locale id="locale-eng">
+ *       <gmd:languageCode>
+ *         <gmd:LanguageCode codeList="./resources/Codelists.xml#LanguageCode" codeListValue="eng">eng</gmd:LanguageCode>
+ *       </gmd:languageCode>
+ *       <gmd:country>
+ *         <gmd:Country codeList="./resources/Codelists.xml#Country" codeListValue="GB">GB</gmd:Country>
+ *       </gmd:country>
+ *       <gmd:characterEncoding>
+ *         <gmd:MD_CharacterSetCode codeList="./resources/Codelists.xml#MD_CharacterSetCode"
+ *                 codeListValue="8859part15">8859part15</gmd:MD_CharacterSetCode>
+ *       </gmd:characterEncoding>
+ *     </gmd:PT_Locale>
+ *   </gmd:locale>
+ * }
+ *
+ * For an alternative (simpler) format, see {@link org.apache.sis.internal.jaxb.gmd.LocaleAdapter}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-2.5)
+ * @version 0.3
+ * @module
+ *
+ * @see LanguageCode
+ * @see Country
+ * @see org.apache.sis.internal.jaxb.gmd.LocaleAdapter
+ */
+public final class PT_Locale extends XmlAdapter<PT_Locale, Locale> {
+    /**
+     * The attributes wrapped in a {@code "PT_Locale"} element.
+     */
+    @XmlElement(name = "PT_Locale")
+    private Wrapper element;
+
+    /**
+     * Wraps the {@code "locale"} attributes in a {@code "PT_Locale"} element.
+     */
+    @XmlType(name = "PT_Locale", propOrder = { "languageCode", "country", "characterEncoding" })
+    private static final class Wrapper {
+        /**
+         * The language code, or {@code null} if none.
+         */
+        @XmlElement(required = true)
+        LanguageCode languageCode;
+
+        /**
+         * The country code, or {@code null} if none.
+         */
+        @XmlElement
+        Country country;
+
+        /**
+         * The character encoding. The specification said:
+         *
+         * <blockquote>Indeed, an XML file can only support data expressed in a single character set, which is generally
+         * declared in the XML file header. Having all the localized strings stored in a single XML file would limit the
+         * use of a single character set such as UTF-8. In order to avoid this, the {@link LocalisedCharacterString}
+         * class is implemented specifically to allow a by-reference containment of the {@link PT_FreeText#textGroup}
+         * property, and the {@link PT_LocaleContainer} is the recommended root element to be instantiated in a
+         * dedicated XML file. The localized string related to a given locale can be stored in a corresponding locale
+         * container (i.e. XML file) and referenced from the {@link PT_FreeText#textGroup} property instances.
+         * </blockquote>
+         *
+         * @todo Current SIS implementation does not yet support {@code PT_LocaleContainer}.
+         */
+        @XmlElement(required = true)
+        @XmlJavaTypeAdapter(MD_CharacterSetCode.class)
+        CharacterSet characterEncoding;
+
+        /**
+         * Empty constructor for JAXB only.
+         */
+        public Wrapper() {
+        }
+
+        /**
+         * Creates a new wrapper for the given locale.
+         */
+        Wrapper(final Locale locale) {
+            final Context context = Context.current();
+            languageCode = LanguageCode.create(context, locale);
+            country      = Country     .create(context, locale);
+            // The characterEncoding field will be initialized at marshalling time (see method below).
+        }
+
+        /**
+         * Invoked by JAXB {@link javax.xml.bind.Marshaller} before this object is marshalled to XML.
+         * This method sets the {@link #characterEncoding} to the XML encoding.
+         */
+        public void beforeMarshal(final Marshaller marshaller) {
+            final String encoding;
+            try {
+                encoding = (String) marshaller.getProperty(Marshaller.JAXB_ENCODING);
+            } catch (PropertyException | ClassCastException e) {
+                // Should never happen. But if it happen anyway, just let the
+                // characterEncoding unitialized: it will not be marshalled.
+                Logging.unexpectedException(PT_Locale.class, "beforeMarshal", e);
+                return;
+            }
+            if (encoding != null) {
+                characterEncoding = Types.forCodeName(CharacterSet.class, encoding, true);
+            }
+        }
+    }
+
+    /**
+     * Empty constructor for JAXB only.
+     */
+    public PT_Locale() {
+    }
+
+    /**
+     * Creates a new wrapper for the given locale.
+     */
+    private PT_Locale(final Locale locale) {
+        element = new Wrapper(locale);
+    }
+
+    /**
+     * Substitutes the locale by the adapter to be marshalled into an XML file
+     * or stream. JAXB calls automatically this method at marshalling time.
+     *
+     * @param value The locale value.
+     * @return The adapter for the locale value.
+     */
+    @Override
+    public PT_Locale marshal(final Locale value) {
+        return (value != null) ? new PT_Locale(value) : null;
+    }
+
+    /**
+     * Substitutes the adapter value read from a XML stream by the object which will
+     * contains the value. JAXB calls automatically this method at unmarshalling time.
+     *
+     * @param value The adapter for this metadata value.
+     * @return A locale which represents the metadata value.
+     */
+    @Override
+    public Locale unmarshal(final PT_Locale value) {
+        if (value != null) {
+            final Wrapper element = value.element;
+            if (element != null) {
+                Locale language = LanguageCode.getLocale(Context.current(), element.languageCode, true);
+                Locale country  = Country.getLocale(element.country);
+                if (language == null) {
+                    language = country;
+                } else if (country != null) {
+                    // Merge the language and the country in a single Locale instance.
+                    final String c = country.getCountry();
+                    if (!c.equals(language.getCountry())) {
+                        language = Locales.unique(new Locale(language.getLanguage(), c));
+                    }
+                }
+                return language;
+            }
+        }
+        return null;
+    }
+}

Propchange: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/code/PT_Locale.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultMetadata.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultMetadata.java?rev=1494922&r1=1494921&r2=1494922&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultMetadata.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/DefaultMetadata.java [UTF-8] Thu Jun 20 10:59:42 2013
@@ -41,7 +41,7 @@ import org.opengis.metadata.maintenance.
 import org.opengis.metadata.quality.DataQuality;
 import org.opengis.metadata.spatial.SpatialRepresentation;
 import org.opengis.referencing.ReferenceSystem;
-//import org.apache.sis.internal.jaxb.gmd.PT_Locale; // TODO
+import org.apache.sis.internal.jaxb.code.PT_Locale;
 import org.apache.sis.internal.jaxb.Context;
 import org.apache.sis.xml.Namespaces;
 
@@ -362,7 +362,7 @@ public class DefaultMetadata extends ISO
      */
     @Override
     @XmlElement(name = "locale")
-//    @XmlJavaTypeAdapter(PT_Locale.class) // TODO
+    @XmlJavaTypeAdapter(PT_Locale.class)
     public Collection<Locale> getLocales() {
         return locales = nonNullCollection(locales, Locale.class);
     }

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java?rev=1494922&r1=1494921&r2=1494922&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/Country.java [UTF-8] Thu Jun 20 10:59:42 2013
@@ -91,7 +91,7 @@ public final class Country extends GO_Ch
      * @return The country to marshal, or {@code null} if the given locale was null
      *         or if its {@link Locale#getCountry()} attribute is the empty string.
      */
-    static Country create(final Context context, final Locale locale) {
+    public static Country create(final Context context, final Locale locale) {
         if (locale != null) {
             final String codeListValue = Context.converter(context).toCountryCode(context, locale);
             if (!codeListValue.isEmpty() && Context.isFlagSet(context, Context.SUBSTITUTE_COUNTRY)) {
@@ -131,7 +131,7 @@ public final class Country extends GO_Ch
      *
      * @see LanguageCode#getLocale(Context, LanguageCode, boolean)
      */
-    static Locale getLocale(final Country value) {
+    public static Locale getLocale(final Country value) {
         if (value != null) {
             String code = null;
             if (value.proxy != null) {

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java?rev=1494922&r1=1494921&r2=1494922&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LanguageCode.java [UTF-8] Thu Jun 20 10:59:42 2013
@@ -89,7 +89,7 @@ public final class LanguageCode extends 
      * @return The language to marshal, or {@code null} if the given locale was null
      *         or if its {@link Locale#getLanguage()} attribute is the empty string.
      */
-    static LanguageCode create(final Context context, final Locale locale) {
+    public static LanguageCode create(final Context context, final Locale locale) {
         if (locale != null) {
             final String codeListValue = Context.converter(context).toLanguageCode(context, locale);
             if (!codeListValue.isEmpty() && Context.isFlagSet(context, Context.SUBSTITUTE_LANGUAGE)) {
@@ -124,6 +124,7 @@ public final class LanguageCode extends 
     /**
      * Returns the locale for the given language (which may be null), or {@code null} if none.
      *
+     * @param context The current (un)marshalling context, or {@code null} if none.
      * @param value The wrapper for this metadata value.
      * @param useCharSequence Whatever this method should fallback on the
      *        {@code gco:CharacterString} element if no value were specified for the
@@ -132,7 +133,7 @@ public final class LanguageCode extends 
      *
      * @see Country#getLocale(Country)
      */
-    static Locale getLocale(final Context context, final LanguageCode value, final boolean useCharSequence) {
+    public static Locale getLocale(final Context context, final LanguageCode value, final boolean useCharSequence) {
         if (value != null) {
             final CodeListProxy proxy = value.proxy;
             if (proxy != null) {

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java?rev=1494922&r1=1494921&r2=1494922&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java [UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/jaxb/gmd/LocaleAdapter.java [UTF-8] Thu Jun 20 10:59:42 2013
@@ -38,8 +38,8 @@ import org.apache.sis.internal.jaxb.Cont
  * @version 0.3
  * @module
  *
- * @see org.apache.sis.internal.jaxb.gmd.LanguageCode
- * @see org.apache.sis.internal.jaxb.gmd.PT_Locale
+ * @see LanguageCode
+ * @see org.apache.sis.internal.jaxb.code.PT_Locale
  */
 public final class LocaleAdapter extends XmlAdapter<LanguageCode, Locale> {
     /**