sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1690098 - in /sis/branches/JDK8/core: sis-metadata/src/main/java/org/apache/sis/io/wkt/ sis-referencing/src/test/java/org/apache/sis/io/wkt/
Date Thu, 09 Jul 2015 13:22:49 GMT
Author: desruisseaux
Date: Thu Jul  9 13:22:48 2015
New Revision: 1690098

URL: http://svn.apache.org/r1690098
Log:
Allows to specify the factories to be used at WKT parsing time.

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java?rev=1690098&r1=1690097&r2=1690098&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/GeodeticObjectParser.java
[UTF-8] Thu Jul  9 13:22:48 2015
@@ -232,19 +232,22 @@ final class GeodeticObjectParser extends
         csFactory       = getFactory(CSFactory.class,    factories);
         datumFactory    = getFactory(DatumFactory.class, factories);
         referencing     = ReferencingServices.getInstance();
-        opFactory       = referencing.getCoordinateOperationFactory(null, mtFactory);
         usesCommonUnits = convention.usesCommonUnits;
         ignoreAxes      = convention == Convention.WKT1_IGNORE_AXES;
+        final Factory f = factories.get(CoordinateOperationFactory.class);
+        if (f != null) {
+            opFactory = (CoordinateOperationFactory) f;
+        } else {
+            opFactory = referencing.getCoordinateOperationFactory(null, mtFactory);
+            factories.put(CoordinateOperationFactory.class, opFactory);
+        }
     }
 
     /**
-     * Returns the factory of the given type. This method will fetch the factory from the
given
-     * map if non-null. The factory actually used is stored in the map, unless the map is
null.
+     * Returns the factory of the given type. This method fetches the factory from the given
map.
+     * The factory actually used is stored in the map.
      */
-    private static <T extends Factory> T getFactory(final Class<T> type, final
Map<Class<?>,Factory> factories) {
-        if (factories == null) {
-            return DefaultFactories.forBuildin(type);
-        }
+    static <T extends Factory> T getFactory(final Class<T> type, final Map<Class<?>,Factory>
factories) {
         T factory = type.cast(factories.get(type));
         if (factory == null) {
             factory = DefaultFactories.forBuildin(type);

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java?rev=1690098&r1=1690097&r2=1690098&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/WKTFormat.java
[UTF-8] Thu Jul  9 13:22:48 2015
@@ -19,6 +19,7 @@ package org.apache.sis.io.wkt;
 import java.util.Date;
 import java.util.Locale;
 import java.util.TimeZone;
+import java.util.Set;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.TreeMap;
@@ -34,6 +35,11 @@ import org.opengis.util.Factory;
 import org.opengis.util.InternationalString;
 import org.opengis.metadata.citation.Citation;
 import org.opengis.referencing.IdentifiedObject;
+import org.opengis.referencing.cs.CSFactory;
+import org.opengis.referencing.crs.CRSFactory;
+import org.opengis.referencing.datum.DatumFactory;
+import org.opengis.referencing.operation.MathTransformFactory;
+import org.opengis.referencing.operation.CoordinateOperationFactory;
 import org.apache.sis.io.CompoundFormat;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.ArgumentChecks;
@@ -174,6 +180,8 @@ public class WKTFormat extends CompoundF
     /**
      * WKT fragments that can be inserted in longer WKT strings, or {@code null} if none.
Keys are short identifiers
      * and values are WKT subtrees to substitute to the identifiers when they are found in
a WKT to parse.
+     *
+     * @see #fragments()
      */
     private Map<String,Element> fragments;
 
@@ -200,7 +208,11 @@ public class WKTFormat extends CompoundF
     private transient AbstractParser parser;
 
     /**
-     * The factories needed by the parser. Will be created when first needed.
+     * The factories needed by the parser. Those factories are currently not serialized (because
usually not
+     * serializable), so any value that users may have specified with {@link #setFactory(Class,
Factory)}
+     * will be lost at serialization time.
+     *
+     * @see #factories()
      */
     private transient Map<Class<?>,Factory> factories;
 
@@ -227,6 +239,26 @@ public class WKTFormat extends CompoundF
     }
 
     /**
+     * Returns the {@link #fragments} map, creating it when first needed.
+     */
+    private Map<String,Element> fragments() {
+        if (fragments == null) {
+            fragments = new TreeMap<>();
+        }
+        return fragments;
+    }
+
+    /**
+     * Returns the {@link #factories} map, creating it when first needed.
+     */
+    private Map<Class<?>,Factory> factories() {
+        if (factories == null) {
+            factories = new HashMap<>(8);
+        }
+        return factories;
+    }
+
+    /**
      * Returns the locale for the given category. This method implements the following mapping:
      *
      * <ul>
@@ -510,6 +542,68 @@ public class WKTFormat extends CompoundF
     }
 
     /**
+     * Verifies if the given type is a valid key for the {@link #factories} map.
+     */
+    private void ensureValidFactoryType(final Class<?> type) throws IllegalArgumentException
{
+        ArgumentChecks.ensureNonNull("type", type);
+        if (type != CRSFactory.class            &&
+            type != CSFactory.class             &&
+            type != DatumFactory.class          &&
+            type != MathTransformFactory.class  &&
+            type != CoordinateOperationFactory.class)
+        {
+            throw new IllegalArgumentException(Errors.getResources(getLocale())
+                    .getString(Errors.Keys.IllegalArgumentValue_2, "type", type));
+        }
+    }
+
+    /**
+     * Returns one of the factories used by this {@code WKTFormat} for parsing WKT.
+     * The given {@code type} argument can be one of the following values:
+     *
+     * <ul>
+     *   <li><code>{@linkplain CRSFactory}.class</code></li>
+     *   <li><code>{@linkplain CSFactory}.class</code></li>
+     *   <li><code>{@linkplain DatumFactory}.class</code></li>
+     *   <li><code>{@linkplain MathTransformFactory}.class</code></li>
+     *   <li><code>{@linkplain CoordinateOperationFactory}.class</code></li>
+     * </ul>
+     *
+     * @param  <T>  The compile-time type of the {@code type} argument.
+     * @param  type The factory type.
+     * @return The factory used by this {@code WKTFormat} for the given type.
+     * @throws IllegalArgumentException if the {@code type} argument is not one of the valid
values.
+     */
+    public <T extends Factory> T getFactory(final Class<T> type) {
+        ensureValidFactoryType(type);
+        return GeodeticObjectParser.getFactory(type, factories());
+    }
+
+    /**
+     * Sets one of the factories to be used by this {@code WKTFormat} for parsing WKT.
+     * The given {@code type} argument can be one of the following values:
+     *
+     * <ul>
+     *   <li><code>{@linkplain CRSFactory}.class</code></li>
+     *   <li><code>{@linkplain CSFactory}.class</code></li>
+     *   <li><code>{@linkplain DatumFactory}.class</code></li>
+     *   <li><code>{@linkplain MathTransformFactory}.class</code></li>
+     *   <li><code>{@linkplain CoordinateOperationFactory}.class</code></li>
+     * </ul>
+     *
+     * @param  <T>     The compile-time type of the {@code type} argument.
+     * @param  type    The factory type.
+     * @param  factory The factory to be used by this {@code WKTFormat} for the given type.
+     * @throws IllegalArgumentException if the {@code type} argument is not one of the valid
values.
+     */
+    public <T extends Factory> void setFactory(final Class<T> type, final T factory)
{
+        ensureValidFactoryType(type);
+        if (factories().put(type, factory) != factory) {
+            parser = null;
+        }
+    }
+
+    /**
      * Returns the type of objects formatted by this class. This method has to return {@code
Object.class}
      * since it is the only common parent to all object types accepted by this formatter.
      *
@@ -521,27 +615,17 @@ public class WKTFormat extends CompoundF
     }
 
     /**
-     * Creates an object from the given character sequence.
-     * The parsing begins at the index given by the {@code pos} argument.
+     * Returns the name of all WKT fragments known to this {@code WKTFormat}.
+     * The returned collection is initially empty.
+     * WKT fragments can be added by call to {@link #addFragment(String, String)}.
      *
-     * @param  wkt The character sequence for the object to parse.
-     * @param  pos The position where to start the parsing.
-     * @return The parsed object.
-     * @throws ParseException If an error occurred while parsing the WKT.
+     * <p>The returned collection is modifiable. In particular, a call to {@link Set#clear()}
+     * removes all fragments from this {@code WKTFormat}.</p>
+     *
+     * @return The name of all fragments known to this {@code WKTFormat}.
      */
-    @Override
-    public Object parse(final CharSequence wkt, final ParsePosition pos) throws ParseException
{
-        warnings = null;
-        sharedValues = null;
-        ArgumentChecks.ensureNonEmpty("wkt", wkt);
-        ArgumentChecks.ensureNonNull ("pos", pos);
-        final AbstractParser parser = parser();
-        Object object = null;
-        try {
-            return object = parser.parseObject(wkt.toString(), pos);
-        } finally {
-            warnings = parser.getAndClearWarnings(object);
-        }
+    public Set<String> getFragmentNames() {
+        return fragments().keySet();
     }
 
     /**
@@ -563,16 +647,18 @@ public class WKTFormat extends CompoundF
      * }
      * </div>
      *
-     * @param  identifier The name to assign to the WKT fragment. Identifiers are case-sensitive.
+     * For removing a fragment, use <code>{@linkplain #getFragmentNames()}.remove(name)</code>.
+     *
+     * @param  name The name to assign to the WKT fragment. Identifiers are case-sensitive.
      * @param  wkt The Well Know Text (WKT) fragment represented by the given identifier.
      * @throws IllegalArgumentException if the name is invalid or if a fragment is already
present for that name.
      * @throws ParseException If an error occurred while parsing the given WKT.
      */
-    public void addFragment(final String identifier, final String wkt) throws IllegalArgumentException,
ParseException {
+    public void addFragment(final String name, final String wkt) throws IllegalArgumentException,
ParseException {
         ArgumentChecks.ensureNonEmpty("wkt", wkt);
-        ArgumentChecks.ensureNonEmpty("identifier", identifier);
+        ArgumentChecks.ensureNonEmpty("name", name);
         short error = Errors.Keys.NotAUnicodeIdentifier_1;
-        if (CharSequences.isUnicodeIdentifier(identifier)) {
+        if (CharSequences.isUnicodeIdentifier(name)) {
             if (sharedValues == null) {
                 sharedValues = new HashMap<>();
             }
@@ -581,22 +667,38 @@ public class WKTFormat extends CompoundF
             final int index = CharSequences.skipLeadingWhitespaces(wkt, pos.getIndex(), wkt.length());
             if (index < wkt.length()) {
                 throw new LocalizedParseException(getLocale(), Errors.Keys.UnexpectedCharactersAfter_2,
-                        new Object[] {identifier + " = " + element.keyword + "[…]", CharSequences.token(wkt,
index)}, index);
+                        new Object[] {name + " = " + element.keyword + "[…]", CharSequences.token(wkt,
index)}, index);
             }
-            if (fragments.putIfAbsent(identifier, element) == null) {
+            // 'fragments' map has been created by 'parser()'.
+            if (fragments.putIfAbsent(name, element) == null) {
                 return;
             }
             error = Errors.Keys.ElementAlreadyPresent_1;
         }
-        throw new IllegalArgumentException(Errors.getResources(getLocale()).getString(error,
identifier));
+        throw new IllegalArgumentException(Errors.getResources(getLocale()).getString(error,
name));
     }
 
     /**
-     * Removes all fragments previously added by {@link #addFragment(String, String)}.
+     * Creates an object from the given character sequence.
+     * The parsing begins at the index given by the {@code pos} argument.
+     *
+     * @param  wkt The character sequence for the object to parse.
+     * @param  pos The position where to start the parsing.
+     * @return The parsed object.
+     * @throws ParseException If an error occurred while parsing the WKT.
      */
-    public void removeAllFragments() {
-        if (fragments != null) {
-            fragments.clear();
+    @Override
+    public Object parse(final CharSequence wkt, final ParsePosition pos) throws ParseException
{
+        warnings = null;
+        sharedValues = null;
+        ArgumentChecks.ensureNonEmpty("wkt", wkt);
+        ArgumentChecks.ensureNonNull ("pos", pos);
+        final AbstractParser parser = parser();
+        Object object = null;
+        try {
+            return object = parser.parseObject(wkt.toString(), pos);
+        } finally {
+            warnings = parser.getAndClearWarnings(object);
         }
     }
 
@@ -606,20 +708,14 @@ public class WKTFormat extends CompoundF
     private AbstractParser parser() {
         AbstractParser parser = this.parser;
         if (parser == null) {
-            if (fragments == null) {
-                fragments = new TreeMap<>();
-            }
-            if (factories == null) {
-                factories = new HashMap<>();
-            }
-            this.parser = parser = new GeodeticObjectParser(symbols, fragments,
+            this.parser = parser = new GeodeticObjectParser(symbols, fragments(),
                     (NumberFormat) getFormat(Number.class),
                     (DateFormat)   getFormat(Date.class),
                     (UnitFormat)   getFormat(Unit.class),
                     convention,
                     (transliterator != null) ? transliterator : Transliterator.DEFAULT,
                     getLocale(),
-                    factories);
+                    factories());
         }
         return parser;
     }

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java?rev=1690098&r1=1690097&r2=1690098&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/io/wkt/GeodeticObjectParserTest.java
[UTF-8] Thu Jul  9 13:22:48 2015
@@ -18,6 +18,7 @@ package org.apache.sis.io.wkt;
 
 import java.util.Collections;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Locale;
 import java.text.ParsePosition;
@@ -74,7 +75,7 @@ public final strictfp class GeodeticObje
      */
     private void newParser(final Convention convention) {
         parser = new GeodeticObjectParser(Symbols.getDefault(), Collections.emptyMap(),
-                null, null, null, convention, Transliterator.DEFAULT, null, null);
+                null, null, null, convention, Transliterator.DEFAULT, null, new HashMap<>());
         assertEquals(GeodeticObjectFactory.class.getCanonicalName(), parser.getPublicFacade());
     }
 



Mime
View raw message