Author: desruisseaux
Date: Wed Aug  7 16:48:03 2013
New Revision: 1511388

URL: http://svn.apache.org/r1511388
Log:
Provide a XML.(un)marshall variant accepting the optional map of properties.

Modified:
    
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ISOMetadata.java
    
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/MarshallerPool.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java

Modified: 
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ISOMetadata.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ISOMetadata.java?rev=1511388&r1=1511387&r2=1511388&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ISOMetadata.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-metadata/src/main/java/org/apache/sis/metadata/iso/ISOMetadata.java
 [UTF-8] Wed Aug  7 16:48:03 2013
@@ -125,7 +125,7 @@ public class ISOMetadata extends Modifia
             return IdentifierMapWithSpecialCases.EMPTY;
         }
         /*
-         * We do not cache (for now) the IdentifierMap because it is cheap to 
create, and if were
+         * We do not cache (for now) the IdentifierMap because it is cheap to 
create, and if we were
          * caching it we would need anyway to check if 'identifiers' still 
references the same list.
          */
         return new IdentifierMapWithSpecialCases(identifiers);

Modified: 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/MarshallerPool.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/MarshallerPool.java?rev=1511388&r1=1511387&r2=1511388&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/MarshallerPool.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/MarshallerPool.java
 [UTF-8] Wed Aug  7 16:48:03 2013
@@ -413,7 +413,7 @@ public class MarshallerPool {
     }
 
     /**
-     * Creates an configure a new JAXB marshaller.
+     * Creates an configures a new JAXB marshaller.
      * This method is invoked only when no existing marshaller is available in 
the pool.
      * Subclasses can override this method if they need to change the 
marshaller configuration.
      *
@@ -446,7 +446,7 @@ public class MarshallerPool {
     }
 
     /**
-     * Creates an configure a new JAXB unmarshaller.
+     * Creates an configures a new JAXB unmarshaller.
      * This method is invoked only when no existing unmarshaller is available 
in the pool.
      * Subclasses can override this method if they need to change the 
unmarshaller configuration.
      *

Modified: 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java?rev=1511388&r1=1511387&r2=1511388&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java 
[UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java 
[UTF-8] Wed Aug  7 16:48:03 2013
@@ -29,6 +29,8 @@ import java.io.StringWriter;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.Unmarshaller;
 import javax.xml.bind.JAXBException;
+import javax.xml.transform.Source;
+import javax.xml.transform.Result;
 import org.apache.sis.util.Static;
 import org.apache.sis.util.Version;
 import org.apache.sis.util.logging.WarningListener;
@@ -60,7 +62,7 @@ import static org.apache.sis.util.Argume
  * @author  Cédric Briançon (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.00)
- * @version 0.3
+ * @version 0.4
  * @module
  */
 public final class XML extends Static {
@@ -376,6 +378,36 @@ public final class XML extends Static {
     }
 
     /**
+     * Marshall the given object to a stream, DOM or other destinations.
+     * This is the most flexible marshalling method provided in this {@code 
XML} class.
+     * The destination is specified by the {@code output} argument 
implementation, for example
+     * {@link javax.xml.transform.stream.StreamResult} for writing to a file 
or output stream.
+     * The optional {@code properties} map can contain any key documented in 
this {@code XML} class,
+     * together with the keys documented in the <cite>supported 
properties</cite> section of the the
+     * {@link Marshaller} class.
+     *
+     * @param  object The root of content tree to be marshalled.
+     * @param  output The file to be written.
+     * @param  properties An optional map of properties to give to the 
marshaller, or {@code null} if none.
+     * @throws JAXBException If a property has an illegal value, or if an 
error occurred during the marshalling.
+     *
+     * @since 0.4
+     */
+    public static void marshal(final Object object, final Result output, final 
Map<String,?> properties) throws JAXBException {
+        ensureNonNull("object", object);
+        ensureNonNull("output", output);
+        final MarshallerPool pool = getPool();
+        final Marshaller marshaller = pool.acquireMarshaller();
+        if (properties != null) {
+            for (final Map.Entry<String,?> entry : properties.entrySet()) {
+                marshaller.setProperty(entry.getKey(), entry.getValue());
+            }
+        }
+        marshaller.marshal(object, output);
+        pool.recycle(marshaller);
+    }
+
+    /**
      * Unmarshall an object from the given string.
      *
      * @param  input The XML representation of an object.
@@ -439,4 +471,36 @@ public final class XML extends Static {
         pool.recycle(unmarshaller);
         return object;
     }
+
+    /**
+     * Unmarshall an object from the given stream, DOM or other sources.
+     * This is the most flexible unmarshalling method provided in this {@code 
XML} class.
+     * The source is specified by the {@code input} argument implementation, 
for example
+     * {@link javax.xml.transform.stream.StreamSource} for reading from a file 
or input stream.
+     * The optional {@code properties} map can contain any key documented in 
this {@code XML} class,
+     * together with the keys documented in the <cite>supported 
properties</cite> section of the the
+     * {@link Unmarshaller} class.
+     *
+     * @param  input The file from which to read a XML representation.
+     * @param  properties An optional map of properties to give to the 
unmarshaller, or {@code null} if none.
+     * @return The object unmarshalled from the given input.
+     * @throws JAXBException If a property has an illegal value, or if an 
error occurred during the unmarshalling.
+     *
+     * @since 0.4
+     *
+     * @see org.apache.sis.storage.xml.XMLStore
+     */
+    public static Object unmarshal(final Source input, final Map<String,?> 
properties) throws JAXBException {
+        ensureNonNull("input", input);
+        final MarshallerPool pool = getPool();
+        final Unmarshaller unmarshaller = pool.acquireUnmarshaller();
+        if (properties != null) {
+            for (final Map.Entry<String,?> entry : properties.entrySet()) {
+                unmarshaller.setProperty(entry.getKey(), entry.getValue());
+            }
+        }
+        final Object object = unmarshaller.unmarshal(input);
+        pool.recycle(unmarshaller);
+        return object;
+    }
 }


Reply via email to