sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1449552 - in /sis/branches/JDK7: ./ sis-utility/ sis-utility/src/main/java/com/ sis-utility/src/main/java/com/sun/ sis-utility/src/main/java/com/sun/xml/ sis-utility/src/main/java/com/sun/xml/internal/ sis-utility/src/main/java/com/sun/xml...
Date Sun, 24 Feb 2013 21:38:52 GMT
Author: desruisseaux
Date: Sun Feb 24 21:38:52 2013
New Revision: 1449552

URL: http://svn.apache.org/r1449552
Log:
Initial draft of a NamespacePrefixMapper implementation.

Added:
    sis/branches/JDK7/sis-utility/src/main/java/com/
    sis/branches/JDK7/sis-utility/src/main/java/com/sun/
    sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/
    sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/
    sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/
    sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/marshaller/
    sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java
  (with props)
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/OGCNamespacePrefixMapper.java
  (with props)
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/MarshallerPoolTest.java
  (with props)
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/OGCNamespacePrefixMapperTest.java
  (with props)
Modified:
    sis/branches/JDK7/pom.xml
    sis/branches/JDK7/sis-utility/pom.xml
    sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java

Modified: sis/branches/JDK7/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/pom.xml?rev=1449552&r1=1449551&r2=1449552&view=diff
==============================================================================
--- sis/branches/JDK7/pom.xml (original)
+++ sis/branches/JDK7/pom.xml Sun Feb 24 21:38:52 2013
@@ -327,7 +327,6 @@ Apache SIS is a toolkit for describing l
            INDEX.LIST file is incomplete (which seem to happen in practice).
       -->
       <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-jar-plugin</artifactId>
         <configuration>
           <archive>
@@ -466,7 +465,6 @@ Apache SIS is a toolkit for describing l
            in order to use version 2.5.1 instead than 2.5. This is necessary
            for avoiding a NullPointerException (http://jira.codehaus.org/browse/MPIR-248)
-->
       <plugin>
-        <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-project-info-reports-plugin</artifactId>
         <version>2.6</version>
       </plugin>

Modified: sis/branches/JDK7/sis-utility/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/pom.xml?rev=1449552&r1=1449551&r2=1449552&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/pom.xml (original)
+++ sis/branches/JDK7/sis-utility/pom.xml Sun Feb 24 21:38:52 2013
@@ -105,6 +105,24 @@ Miscellaneous utilities.
   <build>
     <plugins>
 
+      <!-- Delete the "com/sun/xml/..." directory after compilation. -->
+      <plugin>
+        <artifactId>maven-antrun-plugin</artifactId>
+        <executions>
+          <execution>
+            <phase>process-classes</phase>
+            <goals>
+              <goal>run</goal>
+            </goals>
+            <configuration>
+              <target>
+                <delete dir="${project.build.outputDirectory}/com"/>
+              </target>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+
       <!-- Compile properties files into resources UTF files and collect JAR files. -->
       <plugin>
         <groupId>org.apache.sis</groupId>

Added: sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java?rev=1449552&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java
(added)
+++ sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java
Sun Feb 24 21:38:52 2013
@@ -0,0 +1,45 @@
+/*
+ * 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 com.sun.xml.internal.bind.marshaller;
+
+
+/**
+ * A placeholder for an internal class from Sun/Oracle JDK 6. This class is provided only
for
+ * compilation of {@link org.apache.sis.xml.OGCNamespacePrefixMapper}, and its {@code .class}
+ * file is deleted after compilation by the {@code maven-antrun-plugin} task in the
+ * {@code sis-utility/pom.xml} file.
+ *
+ * <p>The {@code NamespacePrefixMapper} class is part of Sun/Oracle JDK since version
6,
+ * but is considered internal API. For that reason, the JDK class is intentionally hidden
+ * by {@code javac} unless the non-standard {@code -XDignore.symbol.file} option is given
+ * to the compiler. We could declare a system dependency to the {@code rt.jar} file in the
+ * {@code pom.xml}, but this is specific to the Sun/Oracle JDK and would not work with JDK
8.
+ * Providing a skeleton class here, and deleting the {@code NamespacePrefixMapper.class}
file
+ * after compilation, is the most portable approach we have found.</p>
+ *
+ * <p>Note that we do not declare any method in this class. This is not needed if we
make sure
+ * that the method signatures in {@code OGCNamespacePrefixMapper} match the ones in the JDK.
+ *
+ * @author  Cédric Briançon (Geomatys)
+ * @since   0.3 (derived from geotk-3.0)
+ * @version 0.3
+ * @module
+ *
+ * @see <a href="https://issues.apache.org/jira/browse/SIS-74">SIS-74</a>
+ */
+public abstract class NamespacePrefixMapper {
+}

Propchange: sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/main/java/com/sun/xml/internal/bind/marshaller/NamespacePrefixMapper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/OGCNamespacePrefixMapper.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/OGCNamespacePrefixMapper.java?rev=1449552&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/OGCNamespacePrefixMapper.java
(added)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/OGCNamespacePrefixMapper.java
Sun Feb 24 21:38:52 2013
@@ -0,0 +1,76 @@
+/*
+ * 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.xml;
+
+import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;
+import net.jcip.annotations.Immutable;
+import org.apache.sis.util.CharSequences;
+
+
+/**
+ * A mapper between namespace prefixes and URL they represent.
+ * This class is an alternative to the standard {@link javax.xml.bind.annotation.XmlSchema}
+ * annotation. However this class extends the non-standard {@code NamespacePrefixMapper}
class,
+ * which is bundled in Sun/Oracle JDK since version 6. We have to use this mapper because
the
+ * {@code @XmlSchema} annotation doesn't work as expected before JAXB 2.2.4, and the JDK
6 is
+ * bundled with JAXB 2.1. Even with working {@code @XmlSchema} annotations, this mapper still
+ * a convenient may to gain more control like choosing a default namespace at runtime.
+ *
+ * @author  Cédric Briançon (Geomatys)
+ * @since   0.3 (derived from geotk-2.5)
+ * @version 0.3
+ * @module
+ *
+ * @see <a href="http://java.sun.com/webservices/docs/1.5/jaxb/vendorProperties.html">JAXB
extensions</a>
+ * @see <a href="https://issues.apache.org/jira/browse/SIS-74">SIS-74</a>
+ */
+@Immutable
+final class OGCNamespacePrefixMapper extends NamespacePrefixMapper {
+    /**
+     * If non-null, this namespace will be the default namespace (the one without prefix).
+     */
+    private final String defaultNamespace;
+
+    /**
+     * Creates a new prefix mapper.
+     *
+     * @param defaultNamespace The namespace which doesn't need prefix, or {@code null} if
none.
+     */
+    public OGCNamespacePrefixMapper(final String defaultNamespace) {
+        this.defaultNamespace = defaultNamespace;
+    }
+
+    /**
+     * Returns a preferred prefix for the given namespace URI.
+     *
+     * @param  namespace  The namespace URI for which the prefix needs to be found.
+     * @param  suggestion The suggested prefix, returned if the given namespace is not recognized.
+     * @param  required   {@code true} if this method is not allowed to return the empty
string.
+     * @return The prefix inferred from the namespace URI.
+     */
+    public String getPreferredPrefix(final String namespace, final String suggestion, final
boolean required) {
+        /*
+         * If the given namespace is the one defined as default namespace, this implementation
+         * just returns an empty string. This namespace will be defined with a xmlns parameter,
+         * and all tags in this namespace will not have any namespace prefix.
+         */
+        if (!required && namespace.equals(defaultNamespace)) {
+            return "";
+        }
+        return Namespaces.getPreferredPrefix(namespace, suggestion);
+    }
+}

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/OGCNamespacePrefixMapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/OGCNamespacePrefixMapper.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1449552&r1=1449551&r2=1449552&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
(original)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
Sun Feb 24 21:38:52 2013
@@ -92,6 +92,8 @@ import org.junit.runners.Suite;
     // XML most basic types.
     org.apache.sis.xml.XLinkTest.class,
     org.apache.sis.xml.NilReasonTest.class,
+    org.apache.sis.xml.OGCNamespacePrefixMapperTest.class,
+    org.apache.sis.xml.MarshallerPoolTest.class,
     org.apache.sis.internal.jaxb.IdentifierMapAdapterTest.class,
     org.apache.sis.internal.jaxb.IdentifierMapWithSpecialCasesTest.class
 })

Added: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/MarshallerPoolTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/MarshallerPoolTest.java?rev=1449552&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/MarshallerPoolTest.java
(added)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/MarshallerPoolTest.java
Sun Feb 24 21:38:52 2013
@@ -0,0 +1,69 @@
+/*
+ * 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.xml;
+
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.JAXBException;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link MarshallerPool}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.00)
+ * @version 0.3
+ * @module
+ */
+@DependsOn(OGCNamespacePrefixMapperTest.class)
+public final strictfp class MarshallerPoolTest extends TestCase {
+    /**
+     * Tests a marshaller which is acquired, then released.
+     * The marshaller should be reset to its initial state
+     * despite the setter method we may have invoked on it.
+     *
+     * @throws JAXBException Should not happen.
+     */
+    @Test
+    public void testAcquireRelease() throws JAXBException {
+        final MarshallerPool pool = new MarshallerPool(new Class<?>[0]);
+        final Marshaller marshaller = pool.acquireMarshaller();
+        assertNotNull(marshaller);
+        /*
+         * PooledMarshaller should convert the property name from "com.sun.xml.bind.xmlHeaders"
to
+         * "com.sun.xml.internal.bind.xmlHeaders" if we are running JDK implementation of
JAXB.
+         */
+        assertNull(marshaller.getProperty("com.sun.xml.bind.xmlHeaders"));
+        marshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<DTD ...>");
+        assertEquals("<DTD ...>", marshaller.getProperty("com.sun.xml.bind.xmlHeaders"));
+        /*
+         * MarshallerPool should reset the properties to their initial state.
+         */
+        pool.release(marshaller);
+        assertSame(marshaller, pool.acquireMarshaller());
+        /*
+         * Following should be null, but has been replaced by "" under the hood
+         * for avoiding a NullPointerException in current JAXB implementation.
+         */
+        assertEquals("", marshaller.getProperty("com.sun.xml.bind.xmlHeaders"));
+        pool.release(marshaller);
+    }
+}

Propchange: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/MarshallerPoolTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/MarshallerPoolTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/OGCNamespacePrefixMapperTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/OGCNamespacePrefixMapperTest.java?rev=1449552&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/OGCNamespacePrefixMapperTest.java
(added)
+++ sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/OGCNamespacePrefixMapperTest.java
Sun Feb 24 21:38:52 2013
@@ -0,0 +1,98 @@
+/*
+ * 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.xml;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import org.apache.sis.util.Numbers;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assume.*;
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link OGCNamespacePrefixMapper}.
+ * This class performs two kind of tests:
+ *
+ * <ul>
+ *   <li>Invoke every public methods from the {@code NamespacePrefixMapper} class.
+ *       If we failed to override an abstract method, we will get an {@link AbstractMethodError}.</li>
+ *   <li>For specific methods like {@link OGCNamespacePrefixMapper#getPreferredPrefix(String,
String,
+ *       boolean)}, use some value which will prove us that the overriden method has been
invoked.</li>
+ * </ul>
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+public final strictfp class OGCNamespacePrefixMapperTest extends TestCase {
+    /**
+     * Ensures that the class overrides all abstract methods defined in the JDK class.
+     * This test invokes every public methods declared in {@code NamespacePrefixMapper},
+     * which will throw {@link AbstractMethodError} if we forgot to override an abstract
+     * method. Additionally, this test check the result of some method calls in order to
+     * ensure that the invoked method was the one defined in {@link OGCNamespacePrefixMapper}.
+     *
+     * @throws ReflectiveOperationException If an error occurred while invoking a method
by
+     *         the reflection API.
+     */
+    @Test
+    public void ensureOverrideMethods() throws ReflectiveOperationException {
+        final OGCNamespacePrefixMapper mapper;
+        try {
+            mapper = new OGCNamespacePrefixMapper(null);
+        } catch (NoClassDefFoundError e) {
+            assumeNoException(e);
+            return;
+        }
+        String preferredPrefix = "getPreferredPrefix_method_has_not_been_found";
+        for (final Method method : mapper.getClass().getSuperclass().getDeclaredMethods())
{
+            final int modifiers = method.getModifiers();
+            if (Modifier.isPublic(modifiers)) {
+                /*
+                 * Prepare an array of parameter values with all references set to null
+                 * and all primitive values set to 0 (for numbers) or false (for booleans).
+                 */
+                final Class<?>[] parameters = method.getParameterTypes();
+                final Object[] values = new Object[parameters.length];
+                for (int i=0; i<values.length; i++) {
+                    values[i] = Numbers.valueOfNil(parameters[i]);
+                }
+                /*
+                 * In the case of the getPreferredPrefix(…) method, set the namespace
argument
+                 * to "http://www.inspire.org". We have to set some values in order to avoid
a
+                 * NullPointerException. Since we are at it, we will opportunistically ensure
+                 * that we get the expected "ins" prefix.
+                 */
+                final boolean isGetPreferredPrefix = method.getName().equals("getPreferredPrefix");
+                if (isGetPreferredPrefix) {
+                    values[0] = "http://www.inspire.org";
+                    values[1] = "this_suggestion_should_not_be_retained";
+                }
+                final Object result = method.invoke(mapper, values);
+                if (isGetPreferredPrefix) {
+                    preferredPrefix = (String) result;
+                }
+            }
+        }
+        assertEquals("The getPreferredPrefix(…) method returned a wrong value. "
+                + "Is this method correctly overriden?", "ins", preferredPrefix);
+    }
+}

Propchange: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/OGCNamespacePrefixMapperTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-utility/src/test/java/org/apache/sis/xml/OGCNamespacePrefixMapperTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message