sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1415955 - in /sis/branches/JDK7: sis-build-helper/src/main/java/org/apache/sis/internal/taglet/ sis-utility/src/main/java/org/apache/sis/internal/util/ sis-utility/src/main/java/org/apache/sis/xml/
Date Sat, 01 Dec 2012 09:23:47 GMT
Author: desruisseaux
Date: Sat Dec  1 09:23:46 2012
New Revision: 1415955

URL: http://svn.apache.org/viewvc?rev=1415955&view=rev
Log:
Ported ObjectConverters.

Added:
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ObjectConverters.java 
 (with props)
Modified:
    sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/taglet/Preformat.java
    sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java

Modified: sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/taglet/Preformat.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/taglet/Preformat.java?rev=1415955&r1=1415954&r2=1415955&view=diff
==============================================================================
--- sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/taglet/Preformat.java
(original)
+++ sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/internal/taglet/Preformat.java
Sat Dec  1 09:23:46 2012
@@ -27,6 +27,10 @@ import com.sun.tools.doclets.formats.htm
  * The first word after the tag must be the format name ("java", "math", "wkt" or "text").
  * The remaining is the text to format.
  *
+ * <p>This taglet will automatically replace {@code &}, {@code <} and {@code
>} by their HTML entities.
+ * The only exception is {@code &#64;}, which is converted to the original {@code @}
character because
+ * we can't use that character directly inside this taglet.</p>
+ *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.00)
  * @version 0.3
@@ -37,10 +41,10 @@ public final class Preformat extends Inl
      * Special characters to replace by HTML entities.
      */
     private static final String[] SPECIAL_CHARS = new String[] {
-        "&", "&amp;",
-        "<", "&lt;",
-        ">", "&gt;",
-        "↑", "&uarr;"
+        "&#64;", "@", // Because we can't use @ directly in {@preformat}.
+        "&",     "&amp;",
+        "<",     "&lt;",
+        ">",     "&gt;"
     };
 
     /**

Modified: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java?rev=1415955&r1=1415954&r2=1415955&view=diff
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java
(original)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/internal/util/ReferenceQueueConsumer.java
Sat Dec  1 09:23:46 2012
@@ -39,7 +39,7 @@ import org.apache.sis.util.logging.Loggi
  *             assert ReferenceQueueConsumer.DEFAULT.isAlive();
  *         }
  *
- *         @Override
+ *         &#64;Override
  *         public void dispose() {
  *             // Perform here some cleaning work that must be done when the referent has
  *             // been garbage-collected. Remember that get() returns null from this point.

Added: sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ObjectConverters.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ObjectConverters.java?rev=1415955&view=auto
==============================================================================
--- sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ObjectConverters.java (added)
+++ sis/branches/JDK7/sis-utility/src/main/java/org/apache/sis/xml/ObjectConverters.java Sat
Dec  1 09:23:46 2012
@@ -0,0 +1,297 @@
+/*
+ * 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.net.URI;
+import java.net.URL;
+import java.net.URISyntaxException;
+import java.net.MalformedURLException;
+import java.util.Locale;
+import java.util.UUID;
+import javax.measure.unit.Unit;
+import org.apache.sis.measure.Units;
+import org.apache.sis.util.Locales;
+
+import static org.apache.sis.util.CharSequences.trimWhitespaces;
+
+
+/**
+ * Performs conversions of objects encountered during XML (un)marshalling. Each method in
this
+ * class is a converter and can be invoked at (un)marshalling time. The default implementation
+ * is straightforward and documented in the javadoc of each method.
+ *
+ * <p>This class provides a way to handle the errors which may exist in some XML documents.
+ * For example a URL in the document may be malformed, causing a {@link MalformedURLException}
+ * to be thrown. If this error is not handled, it will cause the (un)marshalling of the entire
+ * document to fail. An application may want to change this behavior by replacing URLs that
+ * are known to be erroneous by fixed versions of those URLs. Example:</p>
+ *
+ * {@preformat java
+ *     class URLFixer extends ObjectConverters {
+ *         &#64;Override
+ *         public URL toURL(URI uri) throws MalformedURLException {
+ *             try {
+ *                 return super.toURL(uri);
+ *             } catch (MalformedURLException e) {
+ *                 if (uri.equals(KNOWN_ERRONEOUS_URI) {
+ *                     return FIXED_URL;
+ *                 } else {
+ *                     throw e;
+ *                 }
+ *             }
+ *         }
+ *     }
+ * }
+ *
+ * See the {@link XML#CONVERTERS} javadoc for an example of registering a custom
+ * {@code ObjectConverters} to a (un)marshaller.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.07)
+ * @version 0.3
+ * @module
+ */
+public class ObjectConverters {
+    /**
+     * The default, thread-safe and immutable instance. This instance defines the
+     * converters used during every (un)marshalling if no {@code ObjectConverters}
+     * was explicitly set.
+     */
+    public static final ObjectConverters DEFAULT = new ObjectConverters();
+
+    /**
+     * Creates a default {@code ObjectConverters}. This is for subclasses only,
+     * since new instances are useful only if at least one method is overridden.
+     */
+    protected ObjectConverters() {
+    }
+
+    /**
+     * Invoked when an exception occurred in any {@code toXXX(…)} method. The default
implementation
+     * does nothing and return {@code false}, which will cause the (un)marshalling process
of the
+     * whole XML document to fail.
+     *
+     * <p>This method provides a single hook that subclasses can override in order
to provide their
+     * own error handling for every methods defined in this class, like the example documented
in
+     * the {@link XML#CONVERTERS} javadoc. Subclasses also have the possibility to override
individual
+     * {@code toXXX(…)} methods, like the example provided in this <a href="#skip-navbar_top">class
+     * javadoc</a>.</p>
+     *
+     * @param  <T> The compile-time type of the {@code sourceType} argument.
+     * @param  value The value that can't be converted.
+     * @param  sourceType The base type of the value to convert. This is determined by the
argument
+     *         type of the method that caught the exception. For example the source type
is always
+     *         {@code URI.class} if the exception has been caught by the {@link #toURL(URI)}
method.
+     * @param  targetType The expected type of the converted object.
+     * @param  exception The exception that occurred during the conversion attempt.
+     * @return {@code true} if the (un)marshalling process should continue despite this error,
+     *         or {@code false} (the default) if the exception should be propagated, thus
causing
+     *         the (un)marshalling to fail.
+     */
+    protected <T> boolean exceptionOccured(T value, Class<T> sourceType, Class<?>
targetType, Exception exception) {
+        return false;
+    }
+
+    /**
+     * Converts the given string to a locale. The string is the language code either as the
2
+     * letters or the 3 letters ISO code. It can optionally be followed by the {@code '_'}
+     * character and the country code (again either as 2 or 3 letters), optionally followed
+     * by {@code '_'} and the variant.
+     *
+     * @param  value The string to convert to a locale, or {@code null}.
+     * @return The converted locale, or {@code null} if the given value was null or empty,
or
+     *         if an exception was thrown and {@code exceptionOccured(…)} returned {@code
true}.
+     * @throws IllegalArgumentException If the given string can not be converted to a locale.
+     */
+    public Locale toLocale(String value) throws IllegalArgumentException {
+        value = trimWhitespaces(value);
+        if (value != null && !value.isEmpty()) try {
+            return Locales.parse(value);
+        } catch (IllegalArgumentException e) {
+            if (!exceptionOccured(value, String.class, Locale.class, e)) {
+                throw e;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Converts the given string to a unit. The default implementation is as below, omitting
+     * the check for null value and the call to {@link #exceptionOccured exceptionOccured(…)}
+     * in case of error:
+     *
+     * {@preformat java
+     *     return Units.valueOf(value);
+     * }
+     *
+     * @param  value The string to convert to a unit, or {@code null}.
+     * @return The converted unit, or {@code null} if the given value was null or empty,
or
+     *         if an exception was thrown and {@code exceptionOccured(…)} returned {@code
true}.
+     * @throws IllegalArgumentException If the given string can not be converted to a unit.
+     *
+     * @see Units#valueOf(String)
+     */
+    public Unit<?> toUnit(String value) throws IllegalArgumentException {
+        value = trimWhitespaces(value);
+        if (value != null && !value.isEmpty()) try {
+            return Units.valueOf(value);
+        } catch (IllegalArgumentException e) {
+            if (!exceptionOccured(value, String.class, Unit.class, e)) {
+                throw e;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Converts the given string to a Universal Unique Identifier. The default implementation
+     * is as below, omitting the check for null value and the call to {@link #exceptionOccured
+     * exceptionOccured(…)} in case of error:
+     *
+     * {@preformat java
+     *     return UUID.fromString(value);
+     * }
+     *
+     * @param  value The string to convert to a UUID, or {@code null}.
+     * @return The converted UUID, or {@code null} if the given value was null or empty,
or
+     *         if an exception was thrown and {@code exceptionOccured(…)} returned {@code
true}.
+     * @throws IllegalArgumentException If the given string can not be converted to a UUID.
+     *
+     * @see UUID#fromString(String)
+     */
+    public UUID toUUID(String value) throws IllegalArgumentException {
+        value = trimWhitespaces(value);
+        if (value != null && !value.isEmpty()) try {
+            return UUID.fromString(value);
+        } catch (IllegalArgumentException e) {
+            if (!exceptionOccured(value, String.class, UUID.class, e)) {
+                throw e;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Converts the given string to a URI. The default performs the following work
+     * (omitting the check for null value and the call to {@link #exceptionOccured
+     * exceptionOccured(…)} in case of error):
+     *
+     * {@preformat java
+     *     return new URI(value);
+     * }
+     *
+     * @param  value The string to convert to a URI, or {@code null}.
+     * @return The converted URI, or {@code null} if the given value was null or empty, or
if
+     *         an exception was thrown and {@code exceptionOccured(…)} returned {@code
true}.
+     * @throws URISyntaxException If the given string can not be converted to a URI.
+     *
+     * @see URI#URI(String)
+     */
+    public URI toURI(String value) throws URISyntaxException {
+        value = trimWhitespaces(value);
+        if (value != null && !value.isEmpty()) try {
+            return new URI(value);
+        } catch (URISyntaxException e) {
+            if (!exceptionOccured(value, String.class, URI.class, e)) {
+                throw e;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Converts the given URL to a URI. The default implementation is as below, omitting
+     * the check for null value and the call to {@link #exceptionOccured exceptionOccured(…)}
+     * in case of error:
+     *
+     * {@preformat java
+     *     return value.toURI();
+     * }
+     *
+     * @param  value The URL to convert to a URI, or {@code null}.
+     * @return The converted URI, or {@code null} if the given value was null or if an
+     *         exception was thrown and {@code exceptionOccured(…)} returned {@code
true}.
+     * @throws URISyntaxException If the given URL can not be converted to a URI.
+     *
+     * @see URL#toURI()
+     */
+    public URI toURI(final URL value) throws URISyntaxException {
+        if (value != null) try {
+            return value.toURI();
+        } catch (URISyntaxException e) {
+            if (!exceptionOccured(value, URL.class, URI.class, e)) {
+                throw e;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Converts the given URI to a URL. The default implementation is as below, omitting
+     * the check for null value and the call to {@link #exceptionOccured exceptionOccured(…)}
+     * in case of error:
+     *
+     * {@preformat java
+     *     return value.toURL();
+     * }
+     *
+     * @param  value The URI to convert to a URL, or {@code null}.
+     * @return The converted URL, or {@code null} if the given value was null or if an
+     *         exception was thrown and {@code exceptionOccured(…)} returned {@code
true}.
+     * @throws MalformedURLException If the given URI can not be converted to a URL.
+     *
+     * @see URI#toURL()
+     */
+    public URL toURL(final URI value) throws MalformedURLException {
+        if (value != null) try {
+            return value.toURL();
+        } catch (MalformedURLException | IllegalArgumentException e) {
+            if (!exceptionOccured(value, URI.class, URL.class, e)) {
+                throw e;
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Converts the given string to a {@code NilReason}. The default implementation is as
below,
+     * omitting the check for null value and the call to {@link #exceptionOccured exceptionOccured(…)}
+     * in case of error:
+     *
+     * {@preformat java
+     *     return NilReason.valueOf(value);
+     * }
+     *
+     * @param  value The string to convert to a nil reason, or {@code null}.
+     * @return The converted nil reason, or {@code null} if the given value was null or empty,
or
+     *         if an exception was thrown and {@code exceptionOccured(…)} returned {@code
true}.
+     * @throws URISyntaxException If the given string can not be converted to a nil reason.
+     *
+     * @see NilReason#valueOf(String)
+     */
+    public NilReason toNilReason(String value) throws URISyntaxException {
+        value = trimWhitespaces(value);
+        if (value != null && !value.isEmpty()) try {
+            return NilReason.valueOf(value);
+        } catch (URISyntaxException e) {
+            if (!exceptionOccured(value, String.class, URI.class, e)) {
+                throw e;
+            }
+        }
+        return null;
+    }
+}

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

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



Mime
View raw message