sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1624999 - in /sis/branches/JDK8/core: sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ sis-utility/src/main/java/org/apache/sis/util/iso/ sis-utility/src/test/java/org/apache/sis/util/iso/
Date Mon, 15 Sep 2014 11:12:31 GMT
Author: desruisseaux
Date: Mon Sep 15 11:12:30 2014
New Revision: 1624999

URL: http://svn.apache.org/r1624999
Log:
Accept to parse also OGC type name in global namespace (rather than only in "OGC" namespace).
This is needed for parsing of web service parameters from XML document, because TypeName does
not marshal its scope in current standards.

Modified:
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ServiceParameter.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/TypeNames.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/NamesTest.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/TypeNamesTest.java

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ServiceParameter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ServiceParameter.java?rev=1624999&r1=1624998&r2=1624999&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ServiceParameter.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/internal/jaxb/metadata/replace/ServiceParameter.java
[UTF-8] Mon Sep 15 11:12:30 2014
@@ -39,9 +39,6 @@ import org.apache.sis.xml.Namespaces;
 import static org.apache.sis.internal.util.CollectionsExt.nonNull;
 import static org.apache.sis.internal.jaxb.gco.PropertyType.LEGACY_XML;
 
-// Branch-dependent imports
-import java.util.Objects;
-
 
 /**
  * Parameter information conform to the ISO 19115:2014 specification.
@@ -317,8 +314,8 @@ public final class ServiceParameter exte
         if (super.equals(object, mode) && object instanceof ParameterDescriptor<?>)
{
             final ParameterDescriptor<?> that = (ParameterDescriptor<?>) object;
             if (that.getUnit()         == null &&
-                that.getDefaultValue() == null /*&&
-                that.getValueClass()   == getValueClass()*/) // TODO
+                that.getDefaultValue() == null &&
+                that.getValueClass()   == getValueClass())
             {
                 if (mode.ordinal() >= ComparisonMode.IGNORE_METADATA.ordinal()) {
                     return true;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java?rev=1624999&r1=1624998&r2=1624999&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java
[UTF-8] Mon Sep 15 11:12:30 2014
@@ -158,19 +158,38 @@ public class DefaultTypeName extends Def
 
     /**
      * Returns the Java class associated to this type name.
-     * The default implementation performs the following choices:
+     * The default implementation parses this name in different ways depending on the {@linkplain
#scope() scope}:
      *
      * <ul>
-     *   <li>If the {@linkplain #scope() scope} of this name is not {@code "OGC"} or
{@code "class"},
-     *       then this method returns {@code null}.</li>
-     *   <li>Otherwise this method interprets this {@code TypeName} as documented in
the
-     *       <cite>Mapping Java classes to type names</cite> section of class
javadoc.
-     *       The result is either a non-null class or an {@code UnknownNameException}.</li>
+     *   <li>If the scope is {@code "OGC"}, then:
+     *     <ul>
+     *       <li>If the name is {@code "CharacterString"}, {@code "Integer"}, {@code
"Real"} or other recognized names
+     *           (see {@linkplain DefaultTypeName class javadoc}), then the corresponding
class is returned.</li>
+     *       <li>Otherwise {@link UnknownNameException} is thrown.</li>
+     *     </ul>
+     *   </li>
+     *   <li>Else if the scope is {@code "class"}, then:
+     *     <ul>
+     *       <li>If the name is accepted by {@link Class#forName(String)}, then that
class is returned.</li>
+     *       <li>Otherwise {@link UnknownNameException} is thrown.</li>
+     *     </ul>
+     *   </li>
+     *   <li>Else if the scope {@linkplain DefaultNameSpace#isGlobal() is global},
then:
+     *     <ul>
+     *       <li>If the name is one of the names recognized in {@code "OGC"} scope
(see above),
+     *           then the corresponding class is returned.</li>
+     *       <li>Otherwise {@code null} is returned. No exception is thrown because
names in the global namespace
+     *           could be anything, so we can not be sure that the given name was wrong.</li>
+     *     </ul>
+     *   </li>
+     *   <li>Otherwise {@code null} is returned, since this method can not check the
validity of names in other
+     *       namespaces.</li>
      * </ul>
      *
-     * @return The Java class associated to this {@code TypeName}, or {@code null} if and
only if
-     *         the {@linkplain #scope() scope} is not one of the supported namespaces.
-     * @throws UnknownNameException if the scope is one of the supported namespaces, but
the name is not recognized.
+     * @return The Java class associated to this {@code TypeName},
+     *         or {@code null} if there is no mapping from this name to a Java class.
+     * @throws UnknownNameException if a mapping from this name to a Java class was expected
to exist
+     *         (typically because of the {@linkplain #scope() scope}) but the operation failed.
      *
      * @see Names#toClass(TypeName)
      * @see DefaultNameFactory#toTypeName(Class)
@@ -193,7 +212,7 @@ public class DefaultTypeName extends Def
              * See 'valueClass' javadoc for more information.
              */
             try {
-                c = TypeNames.toClass(super.scope().name().toString(), super.toString());
+                c = TypeNames.toClass(TypeNames.namespace(super.scope()), super.toString());
             } catch (ClassNotFoundException e) {
                 throw new UnknownNameException(TypeNames.unknown(super.toFullyQualifiedName()),
e);
             }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java?rev=1624999&r1=1624998&r2=1624999&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java [UTF-8]
Mon Sep 15 11:12:30 2014
@@ -220,20 +220,39 @@ public final class Names extends Static 
      * The method performs the following choices:
      *
      * <ul>
-     *   <li>If the given type is {@code null}, then this method returns {@code null}.</li>
-     *   <li>Otherwise if the given type is an instance of {@code DefaultTypeName},
+     *   <li>If the given type name is {@code null}, then this method returns {@code
null}.</li>
+     *   <li>Else if the given type name is an instance of {@code DefaultTypeName},
      *       then this method delegates to {@link DefaultTypeName#toClass()}.</li>
-     *   <li>Otherwise if the {@linkplain #scope() scope} of the given name is not
{@code "OGC"}
-     *       or {@code "class"}, then this method returns {@code null}.</li>
-     *   <li>Otherwise this method interprets the given name as documented in the
-     *       <cite>Mapping Java classes to type names</cite> section of {@link
DefaultTypeName} javadoc.
-     *       The result is either a non-null class or an {@code UnknownNameException}.</li>
+     *   <li>Else if the type name {@linkplain DefaultTypeName#scope() scope} is {@code
"OGC"}, then:
+     *     <ul>
+     *       <li>If the name is {@code "CharacterString"}, {@code "Integer"}, {@code
"Real"} or other recognized names
+     *           (see {@link DefaultTypeName} javadoc), then the corresponding class is returned.</li>
+     *       <li>Otherwise {@link UnknownNameException} is thrown.</li>
+     *     </ul>
+     *   </li>
+     *   <li>Else if the scope is {@code "class"}, then:
+     *     <ul>
+     *       <li>If the name is accepted by {@link Class#forName(String)}, then that
class is returned.</li>
+     *       <li>Otherwise {@link UnknownNameException} is thrown.</li>
+     *     </ul>
+     *   </li>
+     *   <li>Else if the scope {@linkplain DefaultNameSpace#isGlobal() is global},
then:
+     *     <ul>
+     *       <li>If the name is one of the names recognized in {@code "OGC"} scope
(see above),
+     *           then the corresponding class is returned.</li>
+     *       <li>Otherwise {@code null} is returned. No exception is thrown because
names in the global namespace
+     *           could be anything, so we can not be sure that the given name was wrong.</li>
+     *     </ul>
+     *   </li>
+     *   <li>Otherwise {@code null} is returned, since this method can not check the
validity of names in other
+     *       namespaces.</li>
      * </ul>
      *
      * @param  type The type name from which to infer a Java class.
-     * @return The Java class associated to the given {@code TypeName}, or {@code null} if
the given type
-     *         is null or if the {@linkplain #scope() scope} is not one of the supported
namespaces.
-     * @throws UnknownNameException if the scope is one of the supported namespaces, but
the name is not recognized.
+     * @return The Java class associated to the given {@code TypeName},
+     *         or {@code null} if there is no mapping from the given name to a Java class.
+     * @throws UnknownNameException if a mapping from the given name to a Java class was
expected to exist
+     *         (typically because of the {@linkplain DefaultTypeName#scope() scope}) but
the operation failed.
      *
      * @see DefaultTypeName#toClass()
      * @see DefaultNameFactory#toTypeName(Class)
@@ -249,7 +268,7 @@ public final class Names extends Static 
             c = ((DefaultTypeName) type).toClass();
         } else {
             try {
-                c = TypeNames.toClass(type.scope().name().toString(), type.toString());
+                c = TypeNames.toClass(TypeNames.namespace(type.scope()), type.toString());
             } catch (ClassNotFoundException e) {
                 throw new UnknownNameException(TypeNames.unknown(type), e);
             }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/TypeNames.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/TypeNames.java?rev=1624999&r1=1624998&r2=1624999&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/TypeNames.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/TypeNames.java
[UTF-8] Mon Sep 15 11:12:30 2014
@@ -134,17 +134,20 @@ final class TypeNames {
     }
 
     /**
-     * Returns the class for the given name. This method is the converse of {@link #toTypeName(NameFactory,
Class)}.
+     * Returns the class for a {@code TypeName} made of the given scope and name.
+     * This method is the converse of {@link #toTypeName(NameFactory, Class)}.
      * This method returns 3 kind of values:
      *
      * <ul>
-     *   <li>{@code Void.TYPE} if {@code namespace} is unrecognized.
+     *   <li>{@code Void.TYPE} if the namespace or the name is unrecognized, without
considering that as an error.
      *       This is a sentinel value expected by {@link DefaultTypeName#toClass()} for such
case.</li>
-     *   <li>{@code null} if {@code namespace} is recognized, but not the {@code name}.</li>
+     *   <li>{@code null} if {@code namespace} is recognized, but not the {@code name}.
+     *       This will be considered as an error by {@link DefaultTypeName#toClass()}.</li>
      *   <li>Otherwise the class for the given name.</li>
      * </ul>
      *
-     * @param  namespace The namespace ("OGC" or "class"), case-insensitive.
+     * @param  namespace The namespace, case-insensitive. Can be any value, but this method
recognizes
+     *         only {@code "OGC"}, {@code "class"} and {@code null}. Other namespaces will
be ignored.
      * @param  name The name, case-sensitive.
      * @return The class, or {@code Void.TYPE} if the given namespace is not recognized,
      *         or {@code null} if the namespace is recognized but not the name.
@@ -153,10 +156,13 @@ final class TypeNames {
      */
     static Class<?> toClass(final String namespace, final String name) throws ClassNotFoundException
{
         Class<?> c;
-        if (namespace.equalsIgnoreCase("OGC")) {
+        if (namespace == null || namespace.equalsIgnoreCase("OGC")) {
             c = MAPPING.get(name);
             if (c == null) {
                 c = Types.forStandardName(name);
+                if (c == null && namespace == null) {
+                    c = Void.TYPE; // Unknown name not considered an error if not in "OGC"
namespace.
+                }
             }
         } else if (namespace.equalsIgnoreCase("class")) {
             c = Class.forName(name);
@@ -178,6 +184,19 @@ final class TypeNames {
     }
 
     /**
+     * Null-safe getter for the namespace argument to be given to {@link #toClass(String,
String)}.
+     */
+    static String namespace(final NameSpace ns) {
+        if (ns != null && !ns.isGlobal()) {
+            final GenericName name = ns.name();
+            if (name != null) {
+                return name.toString();
+            }
+        }
+        return null;
+    }
+
+    /**
      * Formats the error message for an unknown type.
      * This is a helper method for callers of {@link #toClass(String, String)}.
      */

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/NamesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/NamesTest.java?rev=1624999&r1=1624998&r2=1624999&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/NamesTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/NamesTest.java
[UTF-8] Mon Sep 15 11:12:30 2014
@@ -17,12 +17,14 @@
 package org.apache.sis.util.iso;
 
 import java.util.List;
+import java.util.Random;
 import org.opengis.util.GenericName;
 import org.opengis.util.TypeName;
 import org.opengis.util.LocalName;
 import org.opengis.util.InternationalString;
 import org.opengis.util.NameSpace;
 import org.opengis.util.ScopedName;
+import org.apache.sis.util.UnknownNameException;
 import org.apache.sis.internal.system.DefaultFactories;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
@@ -54,34 +56,69 @@ public final strictfp class NamesTest ex
     }
 
     /**
-     * Tests {@link Names#toClass(TypeName)} with a known type name.
+     * Tests {@link Names#toClass(TypeName)} with a name in the {@code "class"} scope.
+     * If the name is not recognized, then {@code toClass(TypeName)} is expected to throw
an exception.
      */
     @Test
-    public void testToClass() {
+    public void testClassFromClassname() {
+        final TypeName type = DefaultFactories.SIS_NAMES.toTypeName(Random.class);
+        assertEquals("class:java.util.Random", type.toFullyQualifiedName().toString());
+        assertValueClassEquals(Random.class, type);
+        assertValueClassEquals(DefaultNameFactoryTest.class,
+                new DefaultTypeName(type.scope(), DefaultNameFactoryTest.class.getName()));
+        assertValueClassEquals(UnknownNameException.class,
+                new DefaultTypeName(type.scope(), "org.apache.sis.Dummy"));
+    }
+
+    /**
+     * Tests {@link Names#toClass(TypeName)} with a name in the {@code "OGC"} scope.
+     * If the name is not recognized, then {@code toClass(TypeName)} is expected to throw
an exception.
+     */
+    @Test
+    public void testClassFromOGC() {
         final TypeName type = DefaultFactories.SIS_NAMES.toTypeName(String.class);
         assertEquals("OGC:CharacterString", type.toFullyQualifiedName().toString());
-
-        // Tests detection from the name.
-        assertEquals(InternationalString.class, Names.toClass(new DefaultTypeName(type.scope(),
"FreeText")));
-        assertValueClassEquals(String.class, type);
+        assertValueClassEquals(String.class,               type);
+        assertValueClassEquals(Double.class,               new DefaultTypeName(type.scope(),
"Real"));
+        assertValueClassEquals(InternationalString.class,  new DefaultTypeName(type.scope(),
"FreeText"));
+        assertValueClassEquals(UnknownNameException.class, new DefaultTypeName(type.scope(),
"Dummy"));
     }
 
     /**
-     * Tests {@link Names#toClass(TypeName)} with an unknown type name.
+     * Tests {@link Names#toClass(TypeName)} with in a scope different than {@code "OGC"}.
+     * If the name is not recognized, then {@code toClass(TypeName)} is expected to return
+     * {@code null} rather than throwing an exception because the namespace is used for too
+     * many things - we can not said that the name is wrong.
      */
     @Test
-    public void testUnknownType() {
-        assertValueClassEquals(null, Names.createTypeName("MyOrg", ":", "CharacterString"));
+    public void testClassFromOtherNamespaces() {
+        assertValueClassEquals(null,         Names.createTypeName("MyOrg", ":", "CharacterString"));
+        assertValueClassEquals(String.class, Names.createTypeName(null,    ":", "CharacterString"));
+        assertValueClassEquals(null,         Names.createTypeName(null,    ":", "Dummy"));
+    }
+
+    /**
+     * Invokes {@link Names#toClass(TypeName)}, but catch {@link UnknownNameException}.
+     * If the later exception is caught, then this method returns {@code UnknownNameException.class}.
+     */
+    private static Class<?> toClass(final TypeName type) {
+        try {
+            return Names.toClass(type);
+        } catch (UnknownNameException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains(type.toFullyQualifiedName().toString()));
+            return UnknownNameException.class;
+        }
     }
 
     /**
      * Asserts that calls to {@link Names#toClass(TypeName)} returns the expected value class.
      */
     private static void assertValueClassEquals(final Class<?> expected, final TypeName
type) {
-        assertEquals(expected, Names.toClass(type));
+        assertEquals(expected, toClass(type));
 
         // Tests detection with an implementation which is not the SIS one.
-        assertEquals(expected, Names.toClass(new TypeName() {
+        assertEquals(expected, toClass(new TypeName() {
             @Override public int                       depth()                  {return type.depth();}
             @Override public List<? extends LocalName> getParsedNames()         {return
type.getParsedNames();}
             @Override public LocalName                 head()                   {return type.head();}

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/TypeNamesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/TypeNamesTest.java?rev=1624999&r1=1624998&r2=1624999&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/TypeNamesTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/TypeNamesTest.java
[UTF-8] Mon Sep 15 11:12:30 2014
@@ -124,7 +124,10 @@ public final strictfp class TypeNamesTes
      */
     @Test
     public void testInvalidNames() throws ClassNotFoundException {
-        assertEquals("Dummy:any", Void.TYPE, TypeNames.toClass("Dummy", "any"));
-        assertNull  ("OGC:Dummy",            TypeNames.toClass("OGC", "Dummy"));
+        assertEquals("Dummy:Real", Void.TYPE,    TypeNames.toClass("Dummy", "Real"));
+        assertEquals("OGC:Real",   Double.class, TypeNames.toClass("OGC",   "Real"));
+        assertEquals("Real",       Double.class, TypeNames.toClass(null,    "Real"));
+        assertEquals("Dummy",      Void.TYPE,    TypeNames.toClass(null,    "Dummy")); //
Considered not an error.
+        assertNull  ("OGC:Dummy",                TypeNames.toClass("OGC",   "Dummy")); //
Considered an error.
     }
 }



Mime
View raw message