sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1750763 - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/xml/XML.java storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/Store.java
Date Thu, 30 Jun 2016 09:56:20 GMT
Author: desruisseaux
Date: Thu Jun 30 09:56:20 2016
New Revision: 1750763

URL: http://svn.apache.org/viewvc?rev=1750763&view=rev
Log:
Add support for Path (from NIO) in XML (un)marshalling. Opportunist comment formatting on
XML-related class.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/Store.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java?rev=1750763&r1=1750762&r2=1750763&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java [UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/xml/XML.java [UTF-8] Thu
Jun 30 09:56:20 2016
@@ -22,6 +22,7 @@ import java.util.TimeZone;
 import java.util.logging.LogRecord; // For javadoc
 import java.net.URL;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.StringReader;
@@ -33,6 +34,7 @@ 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.resources.Errors;
 import org.apache.sis.util.logging.WarningListener;
 import org.apache.sis.internal.system.Modules;
 import org.apache.sis.internal.system.SystemListener;
@@ -40,6 +42,11 @@ import org.apache.sis.internal.jaxb.Type
 
 import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
 
+// Branch-dependent imports
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+
 
 /**
  * Provides convenience methods for marshalling and unmarshalling SIS objects.
@@ -63,7 +70,7 @@ import static org.apache.sis.util.Argume
  * @author  Cédric Briançon (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.4
+ * @version 0.8
  * @module
  */
 public final class XML extends Static {
@@ -353,7 +360,7 @@ public final class XML extends Static {
      *
      * @param  object The root of content tree to be marshalled.
      * @return The XML representation of the given object.
-     * @throws JAXBException If an error occurred during the marshalling.
+     * @throws JAXBException if an error occurred during the marshalling.
      */
     public static String marshal(final Object object) throws JAXBException {
         ensureNonNull("object", object);
@@ -370,7 +377,7 @@ public final class XML extends Static {
      *
      * @param  object The root of content tree to be marshalled.
      * @param  output The stream where to write.
-     * @throws JAXBException If an error occurred during the marshalling.
+     * @throws JAXBException if an error occurred during the marshalling.
      */
     public static void marshal(final Object object, final OutputStream output) throws JAXBException
{
         ensureNonNull("object", object);
@@ -386,7 +393,7 @@ public final class XML extends Static {
      *
      * @param  object The root of content tree to be marshalled.
      * @param  output The file to be written.
-     * @throws JAXBException If an error occurred during the marshalling.
+     * @throws JAXBException if an error occurred during the marshalling.
      */
     public static void marshal(final Object object, final File output) throws JAXBException
{
         ensureNonNull("object", object);
@@ -398,6 +405,26 @@ public final class XML extends Static {
     }
 
     /**
+     * Marshall the given object into a path.
+     *
+     * @param  object The root of content tree to be marshalled.
+     * @param  output The file to be written.
+     * @throws JAXBException if an error occurred during the marshalling.
+     */
+    public static void marshal(final Object object, final Path output) throws JAXBException
{
+        ensureNonNull("object", object);
+        ensureNonNull("output", output);
+        try (OutputStream out = Files.newOutputStream(output, StandardOpenOption.CREATE,
StandardOpenOption.WRITE)) {
+            final MarshallerPool pool = getPool();
+            final Marshaller marshaller = pool.acquireMarshaller();
+            marshaller.marshal(object, out);
+            pool.recycle(marshaller);
+        } catch (IOException e) {
+            throw new JAXBException(Errors.format(Errors.Keys.CanNotOpen_1, output), e);
+        }
+    }
+
+    /**
      * 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
@@ -409,7 +436,7 @@ public final class XML extends Static {
      * @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.
+     * @throws JAXBException if a property has an illegal value, or if an error occurred
during the marshalling.
      *
      * @since 0.4
      */
@@ -434,7 +461,7 @@ public final class XML extends Static {
      *
      * @param  xml The XML representation of an object.
      * @return The object unmarshalled from the given input.
-     * @throws JAXBException If an error occurred during the unmarshalling.
+     * @throws JAXBException if an error occurred during the unmarshalling.
      */
     public static Object unmarshal(final String xml) throws JAXBException {
         ensureNonNull("input", xml);
@@ -451,7 +478,7 @@ public final class XML extends Static {
      *
      * @param  input The stream from which to read a XML representation.
      * @return The object unmarshalled from the given input.
-     * @throws JAXBException If an error occurred during the unmarshalling.
+     * @throws JAXBException if an error occurred during the unmarshalling.
      */
     public static Object unmarshal(final InputStream input) throws JAXBException {
         ensureNonNull("input", input);
@@ -467,7 +494,7 @@ public final class XML extends Static {
      *
      * @param  input The URL from which to read a XML representation.
      * @return The object unmarshalled from the given input.
-     * @throws JAXBException If an error occurred during the unmarshalling.
+     * @throws JAXBException if an error occurred during the unmarshalling.
      */
     public static Object unmarshal(final URL input) throws JAXBException {
         ensureNonNull("input", input);
@@ -483,7 +510,7 @@ public final class XML extends Static {
      *
      * @param  input The file from which to read a XML representation.
      * @return The object unmarshalled from the given input.
-     * @throws JAXBException If an error occurred during the unmarshalling.
+     * @throws JAXBException if an error occurred during the unmarshalling.
      */
     public static Object unmarshal(final File input) throws JAXBException {
         ensureNonNull("input", input);
@@ -495,6 +522,27 @@ public final class XML extends Static {
     }
 
     /**
+     * Unmarshall an object from the given path.
+     *
+     * @param  input The path from which to read a XML representation.
+     * @return The object unmarshalled from the given input.
+     * @throws JAXBException if an error occurred during the unmarshalling.
+     */
+    public static Object unmarshal(final Path input) throws JAXBException {
+        ensureNonNull("input", input);
+        final Object object;
+        try (InputStream in = Files.newInputStream(input, StandardOpenOption.READ)) {
+            final MarshallerPool pool = getPool();
+            final Unmarshaller unmarshaller = pool.acquireUnmarshaller();
+            object = unmarshaller.unmarshal(in);
+            pool.recycle(unmarshaller);
+        } catch (IOException e) {
+            throw new JAXBException(Errors.format(Errors.Keys.CanNotRead_1, input), e);
+        }
+        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
@@ -506,7 +554,7 @@ public final class XML extends Static {
      * @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.
+     * @throws JAXBException if a property has an illegal value, or if an error occurred
during the unmarshalling.
      *
      * @since 0.4
      */

Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/Store.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/Store.java?rev=1750763&r1=1750762&r2=1750763&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/Store.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/xml/Store.java
[UTF-8] Thu Jun 30 09:56:20 2016
@@ -144,7 +144,7 @@ final class Store extends DataStore {
     private void unmarshal() throws DataStoreException {
         final StreamSource s = source;
         final Closeable in = input(s);
-        source = null; // Cleared first in case of error.
+        source = null;                          // Cleared first in case of error.
         if (in != null) try {
             try {
                 object = XML.unmarshal(s, properties());
@@ -195,7 +195,7 @@ final class Store extends DataStore {
     public void close() throws DataStoreException {
         object = null;
         final Closeable in = input(source);
-        source = null; // Cleared first in case of failure.
+        source = null;                          // Cleared first in case of failure.
         if (in != null) try {
             in.close();
         } catch (IOException e) {



Mime
View raw message