sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1774013 [2/2] - in /sis/branches/JDK8: core/sis-utility/src/main/java/org/apache/sis/internal/system/ core/sis-utility/src/main/java/org/apache/sis/util/resources/ core/sis-utility/src/main/java/org/apache/sis/xml/ core/sis-utility/src/tes...
Date Tue, 13 Dec 2016 13:38:26 GMT
Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java?rev=1774013&r1=1774012&r2=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -28,8 +28,10 @@ import java.time.temporal.Temporal;
 import java.util.Collection;
 import java.util.Iterator;
 import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
 import org.opengis.metadata.extent.GeographicBoundingBox;
 import org.apache.sis.internal.xml.StaxStreamWriter;
+import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.util.collection.BackingStoreException;
 
 import static org.apache.sis.util.ArgumentChecks.*;
@@ -50,6 +52,7 @@ import org.opengis.feature.Property;
 public class GPXWriter100 extends StaxStreamWriter {
 
     private final String creator;
+
     /**
      * GPX file namespace
      */
@@ -59,8 +62,10 @@ public class GPXWriter100 extends StaxSt
      *
      * @param creator file creator
      */
-    public GPXWriter100(final String creator, final Object output) throws IOException, XMLStreamException
{
-        this(Tags.NAMESPACE_V10, creator, output);
+    public GPXWriter100(final GPXStore owner, final String creator, final Object output,
final String encoding)
+            throws IOException, XMLStreamException, DataStoreException
+    {
+        this(owner, Tags.NAMESPACE_V10, creator, output, encoding);
     }
 
     /**
@@ -68,8 +73,10 @@ public class GPXWriter100 extends StaxSt
      * @param namespace gpx namespace
      * @param creator file creator
      */
-    protected  GPXWriter100(final String namespace, final String creator, final Object output)
throws IOException, XMLStreamException {
-        super(output);
+    GPXWriter100(final GPXStore owner, final String namespace, final String creator, final
Object output, final String encoding)
+            throws DataStoreException, XMLStreamException, IOException
+    {
+        super(owner, output, encoding);
         ensureNonNull("creator", creator);
         this.creator = creator;
         this.namespace = namespace;
@@ -89,6 +96,7 @@ public class GPXWriter100 extends StaxSt
      * @throws XMLStreamException if underlying xml stax writer encounter an error
      */
     public void writeStartDocument() throws XMLStreamException {
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartDocument("UTF-8", "1.0");
         writer.flush();
     }
@@ -98,6 +106,7 @@ public class GPXWriter100 extends StaxSt
      * @throws XMLStreamException if underlying xml stax writer encounter an error
      */
     public void writeEndDocument() throws XMLStreamException {
+        final XMLStreamWriter writer = getWriter();
         writer.writeEndDocument();
         writer.flush();
     }
@@ -107,6 +116,7 @@ public class GPXWriter100 extends StaxSt
      * @throws XMLStreamException if underlying xml stax writer encounter an error
      */
     public void writeGPXTag() throws XMLStreamException {
+        final XMLStreamWriter writer = getWriter();
         writer.setDefaultNamespace(namespace);
         writer.writeStartElement(namespace, Tags.GPX);
         writer.writeAttribute(Constants.ATT_GPX_VERSION, getVersion());
@@ -193,6 +203,7 @@ public class GPXWriter100 extends StaxSt
             }
         }
 
+        final XMLStreamWriter writer = getWriter();
         writer.writeEndElement();
     }
 
@@ -220,6 +231,7 @@ public class GPXWriter100 extends StaxSt
 
         writeSimpleTag(namespace, Tags.KEYWORDS, metadata.keywords);
         writeBounds(metadata.bounds);
+        final XMLStreamWriter writer = getWriter();
         writer.flush();
     }
 
@@ -232,7 +244,7 @@ public class GPXWriter100 extends StaxSt
      */
     public void writeWayPoint(final Feature feature, final String tagName) throws XMLStreamException
{
         if (feature == null) return;
-
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, tagName);
 
         final Point pt = (Point) feature.getProperty("@geometry").getValue();
@@ -270,6 +282,7 @@ public class GPXWriter100 extends StaxSt
     public void writeRoute(final Feature feature) throws XMLStreamException {
         if (feature == null) return;
 
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.ROUTES);
 
         writeProperty(Tags.NAME,         feature.getProperty(Tags.NAME));
@@ -296,6 +309,7 @@ public class GPXWriter100 extends StaxSt
     public void writeTrack(final Feature feature) throws XMLStreamException {
         if (feature == null) return;
 
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.TRACKS);
 
         writeProperty(Tags.NAME,         feature.getProperty(Tags.NAME));
@@ -321,6 +335,7 @@ public class GPXWriter100 extends StaxSt
      */
     protected void writeTrackSegment(final Feature feature) throws XMLStreamException {
         if (feature == null) return;
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.TRACK_SEGMENTS);
 
         for (Feature prop : (Collection<Feature>)feature.getPropertyValue(Tags.TRACK_POINTS))
{
@@ -352,6 +367,7 @@ public class GPXWriter100 extends StaxSt
     protected void writeLink(final Link link) throws XMLStreamException {
         if (link == null) return;
 
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.LINK);
         writer.writeAttribute(Constants.ATT_LINK_HREF, link.uri.toASCIIString());
         writer.writeEndElement();
@@ -366,6 +382,7 @@ public class GPXWriter100 extends StaxSt
     protected void writeBounds(final GeographicBoundingBox env) throws XMLStreamException
{
         if (env == null) return;
 
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.BOUNDS);
 
         writer.writeAttribute(Constants.ATT_BOUNDS_MINLAT, Double.toString(env.getSouthBoundLatitude()));

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java?rev=1774013&r1=1774012&r2=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -20,6 +20,8 @@ import java.time.temporal.Temporal;
 import java.util.Collection;
 import java.io.IOException;
 import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+import org.apache.sis.storage.DataStoreException;
 
 
 /**
@@ -35,8 +37,10 @@ public class GPXWriter110 extends GPXWri
      *
      * @param creator gpx file creator
      */
-    public GPXWriter110(final String creator, final Object output) throws IOException, XMLStreamException
{
-        super(Tags.NAMESPACE_V11, creator, output);
+    public GPXWriter110(final GPXStore owner, final String creator, final Object output,
final String encoding)
+            throws IOException, XMLStreamException, DataStoreException
+    {
+        super(owner, Tags.NAMESPACE_V11, creator, output, encoding);
     }
 
     /**
@@ -56,6 +60,7 @@ public class GPXWriter110 extends GPXWri
      */
     @Override
     public void write(final Metadata metadata) throws XMLStreamException {
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.METADATA);
         writeSimpleTag(namespace, Tags.NAME, metadata.name);
         writeSimpleTag(namespace, Tags.DESCRIPTION, metadata.description);
@@ -86,6 +91,7 @@ public class GPXWriter110 extends GPXWri
     protected void writePerson(final Person person) throws XMLStreamException {
         if (person == null) return;
 
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.AUTHOR);
         writeSimpleTag(namespace, Tags.NAME, person.getName());
         writeSimpleTag(namespace, Tags.EMAIL, person.email);
@@ -118,6 +124,7 @@ public class GPXWriter110 extends GPXWri
     protected void writeLink(final Link link) throws XMLStreamException {
         if (link == null) return;
 
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.LINK);
         writer.writeAttribute(Constants.ATT_LINK_HREF, link.uri.toASCIIString());
         writer.writeEndElement();
@@ -132,6 +139,7 @@ public class GPXWriter110 extends GPXWri
     public void writeCopyright(final Copyright copyRight) throws XMLStreamException {
         if (copyRight == null) return;
 
+        final XMLStreamWriter writer = getWriter();
         writer.writeStartElement(namespace, Tags.COPYRIGHT);
         final String author = copyRight.author;
         if (author != null) {

Copied: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxDataStore.java
(from r1773750, sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/XMLStoreProvider.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxDataStore.java?p2=sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxDataStore.java&p1=sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/XMLStoreProvider.java&r1=1773750&r2=1774013&rev=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/XMLStoreProvider.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxDataStore.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -16,11 +16,13 @@
  */
 package org.apache.sis.internal.xml;
 
-import org.apache.sis.storage.DataStoreProvider;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import org.apache.sis.storage.DataStore;
 
 
 /**
- * The provider of {@link StaxStreamReader} instances.
+ * Base class of XML data stores based on the STAX framework.
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
@@ -28,10 +30,54 @@ import org.apache.sis.storage.DataStoreP
  * @version 0.8
  * @module
  */
-public abstract class XMLStoreProvider extends DataStoreProvider {
+public abstract class StaxDataStore extends DataStore {
     /**
-     * Creates a new provider.
+     * The STAX readers factory, created when first needed.
+     *
+     * @see #inputFactory()
      */
-    protected XMLStoreProvider() {
+    private XMLInputFactory inputFactory;
+
+    /**
+     * The STAX writers factory, created when first needed.
+     *
+     * @see #outputFactory()
+     */
+    private XMLOutputFactory outputFactory;
+
+    /**
+     * Creates a new data store.
+     */
+    protected StaxDataStore() {
+    }
+
+    /**
+     * Returns the short name (abbreviation) of the format being read or written.
+     * This is used for error messages.
+     *
+     * @return short name of format being read or written.
+     */
+    public abstract String getFormatName();
+
+    /**
+     * Returns the factory for STAX readers.
+     */
+    final synchronized XMLInputFactory inputFactory() {
+        if (inputFactory == null) {
+            inputFactory = XMLInputFactory.newInstance();
+            // TODO: register listeners here.
+        }
+        return inputFactory;
+    }
+
+    /**
+     * Returns the factory for STAX writers.
+     */
+    final synchronized XMLOutputFactory outputFactory() {
+        if (outputFactory == null) {
+            outputFactory = XMLOutputFactory.newInstance();
+            outputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
+        }
+        return outputFactory;
     }
 }

Copied: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxDataStoreProvider.java
(from r1774012, sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/XMLStoreProvider.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxDataStoreProvider.java?p2=sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxDataStoreProvider.java&p1=sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/XMLStoreProvider.java&r1=1774012&r2=1774013&rev=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/XMLStoreProvider.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxDataStoreProvider.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -28,10 +28,10 @@ import org.apache.sis.storage.DataStoreP
  * @version 0.8
  * @module
  */
-public abstract class XMLStoreProvider extends DataStoreProvider {
+public abstract class StaxDataStoreProvider extends DataStoreProvider {
     /**
      * Creates a new provider.
      */
-    protected XMLStoreProvider() {
+    protected StaxDataStoreProvider() {
     }
 }

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java?rev=1774013&r1=1774012&r2=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -16,10 +16,10 @@
  */
 package org.apache.sis.internal.xml;
 
-import java.util.Locale;
+import java.io.Closeable;
 import java.io.IOException;
 import javax.xml.stream.XMLStreamException;
-import org.apache.sis.util.Localized;
+import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 
 
@@ -40,41 +40,42 @@ import org.apache.sis.util.resources.Err
  * @version 0.8
  * @module
  */
-abstract class StaxStream implements AutoCloseable, Localized {
+abstract class StaxStream implements AutoCloseable {
     /**
-     * The locale to use for error messages, or {@code null} for the default locale.
+     * The data store for which this reader or writer has been created.
      */
-    private Locale locale;
+    final StaxDataStore owner;
 
     /**
-     * For sub-classes constructors.
+     * The underlying stream to close when this {@code StaxStream} reader or writer is closed,
+     * or {@code null} if none.
      */
-    StaxStream() {
-    }
+    private Closeable stream;
 
     /**
-     * Sets the locale to use for formatting error messages.
-     * A null value means to use the default locale.
+     * For sub-classes constructors.
      *
-     * @param locale the locale to use for formatting error messages (can be null).
-     */
-    public void setLocale(final Locale locale) {
-        this.locale = locale;
-    }
-
-    /**
-     * Returns the locale to use for formatting error messages.
+     * @param owner  the data store for which this reader or writer is created.
      */
-    @Override
-    public Locale getLocale() {
-        return locale;
+    StaxStream(final StaxDataStore owner) {
+        ArgumentChecks.ensureNonNull("owner", owner);
+        this.owner = owner;
     }
 
     /**
-     * Returns the error resources in the current locale.
+     * Notifies this {@code StaxStream} that the given stream will need to be closed by the
{@link #close()} method.
+     * This method can be invoked at most once. This method does nothing if the given object
does not implement the
+     * {@link Closeable} interface.
+     *
+     * @param stream the stream to be closed when {@link #close()} will be invoked.
      */
-    protected final Errors errors() {
-        return Errors.getResources(getLocale());
+    protected final void initCloseable(final Object stream) {
+        if (this.stream != null) {
+            throw new IllegalStateException();
+        }
+        if (stream instanceof Closeable) {
+            this.stream = (Closeable) stream;
+        }
     }
 
     /**
@@ -85,5 +86,17 @@ abstract class StaxStream implements Aut
      * @throws XMLStreamException if an error occurred while releasing XML reader/writer
resources.
      */
     @Override
-    public abstract void close() throws IOException, XMLStreamException;
+    public void close() throws IOException, XMLStreamException {
+        if (stream != null) {
+            stream.close();
+            stream = null;
+        }
+    }
+
+    /**
+     * Returns the error resources in the current locale.
+     */
+    final Errors errors() {
+        return Errors.getResources(owner.getLocale());
+    }
 }

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java?rev=1774013&r1=1774012&r2=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -16,17 +16,26 @@
  */
 package org.apache.sis.internal.xml;
 
-import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.NoSuchElementException;
 import java.io.EOFException;
+import java.io.Reader;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.util.StreamReaderDelegate;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stax.StAXSource;
+import org.xml.sax.InputSource;
+import org.w3c.dom.Node;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.StorageConnector;
-import org.apache.sis.internal.system.XMLInputFactory;
+import org.apache.sis.storage.UnsupportedStorageException;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.Classes;
@@ -66,74 +75,81 @@ import org.apache.sis.util.Classes;
  * @version 0.8
  * @module
  */
-public abstract class StaxStreamReader extends StaxStream {
+public abstract class StaxStreamReader extends StaxStream implements XMLStreamConstants {
     /**
      * The XML stream reader.
      */
     private XMLStreamReader reader;
 
     /**
-     * The underlying reader or input stream to close when this {@code StaxStreamReader}
is closed,
-     * or {@code null} if none.
-     */
-    private Closeable sourceStream;
-
-    /**
      * Creates a new XML reader from the given file, URL, stream or reader object.
-     * This constructor is used in two ways depending on whether the optional {@code storage}
argument is null or not:
+     * This constructor is used in two ways depending on whether the optional {@code connector}
argument is null or not:
      *
      * <ul class="verbose">
-     *   <li>If the {@code storage} argument is {@code null}, then the {@code input}
argument shall be an instance of
-     *       {@link javax.xml.stream.XMLStreamReader}, {@link javax.xml.stream.XMLEventReader},
-     *       {@link org.xml.sax.InputSource}, {@link java.io.InputStream}, {@link java.io.Reader},
-     *       {@link javax.xml.transform.Source} or {@link org.w3c.dom.Node},
-     *       otherwise a {@link DataStoreException} will be thrown.</li>
-     *
-     *   <li>If the {@code storage} argument is {@code null}, then the {@code input}
argument should be the value of
-     *       {@link StorageConnector#getStorage()} (i.e. the input object as given by the
user). If that value is not
-     *       recognized, then this constructor will fallback on {@link StorageConnector#getStorageAs(Class)}
with the
-     *       {@link InputStream} type. In any cases, this constructor invokes
+     *   <li>If the {@code connector} argument is {@code null}, then the {@code input}
argument shall be an instance of
+     *       {@link XMLStreamReader}, {@link XMLEventReader}, {@link InputSource}, {@link
InputStream}, {@link Reader},
+     *       {@link Source} or {@link Node}, otherwise a {@link DataStoreException} will
be thrown.</li>
+     *
+     *   <li>If the {@code connector} argument is not {@code null}, then the {@code
input} argument should be the
+     *       value of {@link StorageConnector#getStorage()} (i.e. the input object as given
by the user). If that
+     *       value is not recognized, then this constructor will fallback on {@link StorageConnector#getStorageAs(Class)}
+     *       with the {@link InputStream} type. In any cases, this constructor invokes
      *       {@link StorageConnector#closeAllExcept(Object)} after the input has been set.</li>
      * </ul>
      *
-     * @param  input   value of {@code storage.getStorage()}.
-     * @param  storage information about the storage (URL, stream, <i>etc</i>),
or {@code null} if unknown.
+     * @param  owner      the data store for which this reader is created.
+     * @param  input      value of {@code storage.getStorage()}.
+     * @param  connector  information about the storage (URL, stream, <i>etc</i>),
or {@code null} if unknown.
      * @throws DataStoreException if the input type is not recognized.
      * @throws XMLStreamException if an error occurred while opening the XML file.
      */
-    protected StaxStreamReader(final Object input, final StorageConnector storage) throws
DataStoreException, XMLStreamException {
+    protected StaxStreamReader(final StaxDataStore owner, final Object input, final StorageConnector
connector)
+            throws DataStoreException, XMLStreamException
+    {
+        super(owner);
         ArgumentChecks.ensureNonNull("input", input);
-        reader = XMLInputFactory.createFromAny(input);
-        if (reader != null) {
-            if (input instanceof Closeable) {
-                sourceStream = (Closeable) input;
-            }
-            if (storage != null) {
-                storage.closeAllExcept(input);
-            }
-        } else {
+        if      (input instanceof XMLStreamReader) reader = (XMLStreamReader) input;
+        else if (input instanceof XMLEventReader)  reader = factory().createXMLStreamReader(new
StAXSource((XMLEventReader) input));
+        else if (input instanceof InputSource)     reader = factory().createXMLStreamReader(new
SAXSource((InputSource) input));
+        else if (input instanceof InputStream)     reader = factory().createXMLStreamReader((InputStream)
input);
+        else if (input instanceof Reader)          reader = factory().createXMLStreamReader((Reader)
input);
+        else if (input instanceof Source)          reader = factory().createXMLStreamReader((Source)
input);
+        else if (input instanceof Node)            reader = factory().createXMLStreamReader(new
DOMSource((Node) input));
+        else {
             final InputStream in;
-            if (storage != null && (in = storage.getStorageAs(InputStream.class))
!= null) {
-                reader = XMLInputFactory.createXMLStreamReader(in);
-                sourceStream = in;
-                storage.closeAllExcept(in);
-            } else {
-                throw new DataStoreException(Errors.format(Errors.Keys.IllegalInputTypeForReader_2,
"XML", Classes.getClass(input)));
+            if (connector == null || (in = connector.getStorageAs(InputStream.class)) ==
null) {
+                throw new UnsupportedStorageException(errors().getString(Errors.Keys.IllegalInputTypeForReader_2,
+                                                      owner.getFormatName(), Classes.getClass(input)));
             }
+            reader = factory().createXMLStreamReader(in);
+            connector.closeAllExcept(in);
+            initCloseable(in);
+            return;
         }
+        initCloseable(input);
+        if (connector != null) {
+            connector.closeAllExcept(input);
+        }
+    }
+
+    /**
+     * Convenience method invoking {@link StaxDataStore#inputFactory()}.
+     */
+    private XMLInputFactory factory() {
+        return owner.inputFactory();
     }
 
     /**
      * Returns the XML stream reader if it is not closed.
      *
      * @return the XML stream reader (never null).
-     * @throws IOException if this XML reader has been closed.
+     * @throws XMLStreamException if this XML reader has been closed.
      */
-    protected final XMLStreamReader getReader() throws IOException {
+    protected final XMLStreamReader getReader() throws XMLStreamException {
         if (reader != null) {
             return reader;
         }
-        throw new IOException(errors().getString(Errors.Keys.ClosedReader_1, "XML"));
+        throw new XMLStreamException(errors().getString(Errors.Keys.ClosedReader_1, "XML"));
     }
 
     /**
@@ -143,9 +159,9 @@ public abstract class StaxStreamReader e
      *
      * @param  tagName name of the tag to close.
      * @return a reader over a portion of the stream.
-     * @throws IOException if this XML reader has been closed.
+     * @throws XMLStreamException if this XML reader has been closed.
      */
-    protected final XMLStreamReader getSubReader(final String tagName) throws IOException
{
+    protected final XMLStreamReader getSubReader(final String tagName) throws XMLStreamException
{
         return new StreamReaderDelegate(getReader()) {
             /** Increased every time a nested element of the same name is found. */
             private int nested;
@@ -175,11 +191,10 @@ public abstract class StaxStreamReader e
      * Nested tags of the same name, if any, are also skipped.
      *
      * @param  tagName name of the tag to close.
-     * @throws IOException if this XML reader has been closed.
      * @throws EOFException if end tag could not be found.
      * @throws XMLStreamException if an error occurred while reading the XML stream.
      */
-    protected final void skipUntilEnd(final String tagName) throws IOException, XMLStreamException
{
+    protected final void skipUntilEnd(final String tagName) throws EOFException, XMLStreamException
{
         final XMLStreamReader reader = getReader();
         int nested = 0;
         while (reader.hasNext()) {
@@ -232,9 +247,6 @@ public abstract class StaxStreamReader e
             reader.close();
             reader = null;
         }
-        if (sourceStream != null) {
-            sourceStream.close();
-            sourceStream = null;
-        }
+        super.close();
     }
 }

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamWriter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamWriter.java?rev=1774013&r1=1774012&r2=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamWriter.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamWriter.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -16,21 +16,31 @@
  */
 package org.apache.sis.internal.xml;
 
+import java.util.Map;
+import java.util.HashMap;
 import java.io.BufferedOutputStream;
-import java.io.File;
-import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.Writer;
 import java.nio.file.Files;
 import java.nio.file.Path;
-import java.nio.file.StandardOpenOption;
-import java.util.HashMap;
-import java.util.Map;
+import javax.xml.stream.XMLEventWriter;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.Result;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.sax.SAXResult;
+import javax.xml.transform.stax.StAXResult;
+import org.w3c.dom.Node;
+import org.xml.sax.ContentHandler;
+import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.storage.UnsupportedStorageException;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.Classes;
+import org.apache.sis.util.ObjectConverters;
+import org.apache.sis.util.UnconvertibleObjectException;
+import org.apache.sis.util.resources.Errors;
 import org.apache.sis.xml.Namespaces;
 
 
@@ -63,6 +73,7 @@ import org.apache.sis.xml.Namespaces;
  * that contains the {@code StaxStream} instances.
  *
  * @author  Johann Sorel (Geomatys)
+ * @author  Martin Desruisseaux (Geomatys)
  * @since   0.8
  * @version 0.8
  * @module
@@ -71,74 +82,82 @@ public abstract class StaxStreamWriter e
     /**
      * The XML stream writer.
      */
-    protected XMLStreamWriter writer;
+    private XMLStreamWriter writer;
 
     /**
-     * Store the output stream if it was generated by the parser itself.
-     * It will closed on the dispose method or when a new input is set.
+     * The namespaces for which no prefix are mapped. In such case, default prefixes will
be created.
+     * Keys are namespaces URL and values are prefixes.
      */
-    private OutputStream targetStream;
-
-    private int lastUnknowPrefix = 0;
-
-    private final Map<String, String> unknowNamespaces = new HashMap<>();
+    private final Map<String,String> unknownNamespaces = new HashMap<>();
 
     /**
-     * Creates a new XML writer from the given file, URL, stream or reader object.
-     * Set the output for this writer.<br>
-     * Handle types are :<br>
-     * - java.io.File<br>
-     * - java.io.Writer<br>
-     * - java.io.OutputStream<br>
-     * - javax.xml.stream.XMLStreamWriter<br>
-     * - javax.xml.transform.Result<br>
+     * Creates a new XML writer from the given file, URL, stream or writer object.
+     * The {@code output} argument shall be an instance of {@link XMLStreamWriter}, {@link
XMLEventWriter},
+     * {@link ContentHandler}, {@link OutputStream}, {@link Writer}, {@link Result}, {@link
Node} or an
+     * object convertible to {@link Path}, otherwise a {@link DataStoreException} will be
thrown.
      *
-     * @param output output object
-     * @throws IOException if output is not supported or caused an error
-     * @throws XMLStreamException if output is not a valid XML stream, or closing previous
stream caused an error
-     */
-    protected StaxStreamWriter(Object output) throws IOException, XMLStreamException {
-        if (output instanceof XMLStreamWriter) {
-            writer = (XMLStreamWriter) output;
+     * @param  owner     the data store for which this writer is created.
+     * @param  output    where to write the XML file.
+     * @param  encoding  the document encoding (usually {@code "UTF-8"}). May be ignored.
+     * @throws DataStoreException if the output type is not recognized.
+     * @throws IOException if an error occurred while creating the file.
+     * @throws XMLStreamException if an error occurred while opening the XML file.
+     */
+    protected StaxStreamWriter(final StaxDataStore owner, final Object output, final String
encoding)
+            throws DataStoreException, IOException, XMLStreamException
+    {
+        super(owner);
+        ArgumentChecks.ensureNonNull("output", output);
+        if      (output instanceof XMLStreamWriter) writer = (XMLStreamWriter) output;
+        else if (output instanceof XMLEventWriter)  writer = factory().createXMLStreamWriter(new
StAXResult((XMLEventWriter) output));
+        else if (output instanceof ContentHandler)  writer = factory().createXMLStreamWriter(new
SAXResult((ContentHandler) output));
+        else if (output instanceof OutputStream)    writer = factory().createXMLStreamWriter((OutputStream)
output, encoding);
+        else if (output instanceof Writer)          writer = factory().createXMLStreamWriter((Writer)
output);
+        else if (output instanceof Result)          writer = factory().createXMLStreamWriter((Result)
output);
+        else if (output instanceof Node)            writer = factory().createXMLStreamWriter(new
DOMResult((Node) output));
+        else try {
+            final Path path = ObjectConverters.convert(output, Path.class);
+            final BufferedOutputStream out = new BufferedOutputStream(Files.newOutputStream(path));
+            writer = factory().createXMLStreamWriter(out, encoding);
+            initCloseable(out);
             return;
+        } catch (UnconvertibleObjectException e) {
+            throw new UnsupportedStorageException(errors().getString(Errors.Keys.IllegalOutputTypeForWriter_2,
+                                                  owner.getFormatName(), Classes.getClass(output)),
e);
         }
+        initCloseable(output);
+    }
 
-        if (output instanceof File) {
-            targetStream = new FileOutputStream((File)output);
-            final BufferedOutputStream bout = new BufferedOutputStream(targetStream);
-            output = bout;
-        }
-        if (output instanceof Path) {
-            targetStream = Files.newOutputStream((Path) output, StandardOpenOption.CREATE,
StandardOpenOption.WRITE);
-            final BufferedOutputStream bout = new BufferedOutputStream(targetStream);
-            output = bout;
-        }
-
-        writer = toWriter(output);
+    /**
+     * Convenience method invoking {@link StaxDataStore#outputFactory()}.
+     */
+    private XMLOutputFactory factory() {
+        return owner.outputFactory();
     }
 
     /**
-     * Acces the underlying stax writer.
-     * This method is used when several writer are wrapping a single writer.
-     * Like when an Symbology Encoding writer wraps a Filter writer.
-     * <br>
-     * It can also be used to write tag before or after this writer is used.
+     * Returns the XML stream writer if it is not closed.
      *
-     * @return underlying stax writer, can be null if input has not been set
+     * @return the XML stream writer (never null).
+     * @throws XMLStreamException if this XML writer has been closed.
      */
-    public XMLStreamWriter getWriter() {
-        return writer;
+    protected final XMLStreamWriter getWriter() throws XMLStreamException {
+        if (writer != null) {
+            return writer;
+        }
+        throw new XMLStreamException(errors().getString(Errors.Keys.ClosedWriter_1, "XML"));
     }
 
     /**
-     * Write a new tag with the text corresponding to the given value.
-     * The tag won't be written if the value is null.
-     * @param namespace : namespace of the wanted tag
-     * @param localName : local name of the wanted tag
-     * @param value : text value to write
-     * @throws XMLStreamException if underlying stax stream raised an error
+     * Writes a new tag with the given value and no attribute.
+     * If the given value is null, then this method does nothing.
+     *
+     * @param  namespace  namespace (URL) of the tag to write.
+     * @param  localName  local name of the tag to write.
+     * @param  value      text to write inside the tag.
+     * @throws XMLStreamException if the underlying STAX writer raised an error.
      */
-    protected void writeSimpleTag(final String namespace, final String localName, final Object
value) throws XMLStreamException {
+    protected final void writeSimpleTag(final String namespace, final String localName, final
Object value) throws XMLStreamException {
         if (value != null) {
             writer.writeStartElement(namespace, localName);
             writer.writeCharacters(value.toString());
@@ -147,75 +166,23 @@ public abstract class StaxStreamWriter e
     }
 
     /**
-     * Creates a new XMLStreamWriter.
-     * @param output output object
-     * @return XMLStreamWriter
-     * @throws XMLStreamException if the output is not handled
-     */
-    private static XMLStreamWriter toWriter(final Object output) throws XMLStreamException
{
-        final XMLOutputFactory XMLfactory = XMLOutputFactory.newInstance();
-        XMLfactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, Boolean.TRUE);
-
-        if (output instanceof OutputStream) {
-            return XMLfactory.createXMLStreamWriter((OutputStream)output,"UTF-8");
-        } else if(output instanceof Result) {
-            return XMLfactory.createXMLStreamWriter((Result)output);
-        } else if(output instanceof Writer) {
-            return XMLfactory.createXMLStreamWriter((Writer)output);
-        } else {
-            throw new XMLStreamException("Output type is not supported : "+ output);
-        }
-    }
-
-    /**
-     * Returns the prefix for the given namespace.
+     * Returns the prefix for the given namespace. If the given namespace is one of the
+     * {@linkplain Namespaces well-known namespaces}, then this method returns the prefix
+     * commonly used for that namespace. Otherwise a new prefix is generated.
      *
-     * @param namespace The namespace for which we want the prefix.
-     * @return namespace prefix
+     * @param  namespace  the namespace (URL) for which to get the prefix.
+     * @return prefix for the given namespace.
      */
-    protected Prefix getPrefix(final String namespace) {
+    protected final String getPrefix(final String namespace) {
         String prefix = Namespaces.getPreferredPrefix(namespace, null);
-        /*
-         * temporary hack todo remove
-         */
-        if ("http://www.opengis.net/gml/3.2".equals(namespace)) {
-            return new Prefix(false, "gml");
-        }
-        boolean unknow = false;
         if (prefix == null) {
-            prefix = unknowNamespaces.get(namespace);
+            prefix = unknownNamespaces.get(namespace);
             if (prefix == null) {
-                prefix = "ns" + lastUnknowPrefix;
-                lastUnknowPrefix++;
-                unknow = true;
-                unknowNamespaces.put(namespace, prefix);
+                prefix = "ns" + (unknownNamespaces.size() + 1);
+                unknownNamespaces.put(namespace, prefix);
             }
         }
-        return new Prefix(unknow, prefix);
-    }
-
-    /**
-     * Inner class for handling prefix and if it is already known.
-     */
-    protected final class Prefix {
-        /**
-         * True if prefix in unknown, not mapped to any namespace
-         */
-        public boolean unknow;
-        /**
-         * Namespace prefix.
-         */
-        public String prefix;
-
-        /**
-         *
-         * @param unknow if prefix is mapped to a namespace
-         * @param prefix prefix text
-         */
-        public Prefix(final boolean unknow, final String prefix) {
-            this.prefix = prefix;
-            this.unknow = unknow;
-        }
+        return prefix;
     }
 
     /**
@@ -231,9 +198,6 @@ public abstract class StaxStreamWriter e
             writer.close();
             writer = null;
         }
-        if (targetStream != null) {
-            targetStream.close();
-            targetStream = null;
-        }
+        super.close();
     }
 }

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java?rev=1774013&r1=1774012&r2=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -54,7 +54,7 @@ public class GPXReaderTest extends TestC
 
     private static GPXReader create(final String resource) throws DataStoreException, IOException,
XMLStreamException {
         StorageConnector storage = new StorageConnector(GPXReaderTest.class.getResource(resource));
-        return new GPXReader(storage.getStorage(), storage);
+        return new GPXReader(new GPXStore(storage), storage.getStorage(), storage);
     }
 
     /**

Modified: sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java?rev=1774013&r1=1774012&r2=1774013&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java
[UTF-8] Tue Dec 13 13:38:26 2016
@@ -50,9 +50,13 @@ import org.opengis.feature.Feature;
 public final strictfp class GPXWriterTest extends TestCase{
 
 
-    private static GPXReader create(final File resource) throws DataStoreException, IOException,
XMLStreamException {
+    private static GPXReader reader(final File resource) throws DataStoreException, IOException,
XMLStreamException {
         StorageConnector storage = new StorageConnector(resource);
-        return new GPXReader(storage.getStorage(), storage);
+        return new GPXReader(new GPXStore(storage), storage.getStorage(), storage);
+    }
+
+    private static GPXWriter110 writer(final File f) throws DataStoreException, IOException,
XMLStreamException {
+        return new GPXWriter110(new GPXStore(new StorageConnector("dummy")), "Apache SIS",
f, "UTF-8");
     }
 
     /**
@@ -65,7 +69,7 @@ public final strictfp class GPXWriterTes
         final File f = new File("output.xml");
         f.deleteOnExit();
         if (f.exists()) f.delete();
-        final GPXWriter110 writer = new GPXWriter110("Apache SIS", f);
+        final GPXWriter110 writer = writer(f);
 
         final Person person = new Person();
         person.name = "Jean-Pierre";
@@ -94,7 +98,7 @@ public final strictfp class GPXWriterTes
         writer.writeEndDocument();
         writer.close();
 
-        try (GPXReader reader = create(f)) {
+        try (GPXReader reader = reader(f)) {
             assertEquals(metaData, reader.getMetadata());
         }
 
@@ -113,7 +117,7 @@ public final strictfp class GPXWriterTes
         final File f = new File("output.xml");
         f.deleteOnExit();
         if (f.exists()) f.delete();
-        final GPXWriter110 writer = new GPXWriter110("Apache SIS", f);
+        final GPXWriter110 writer = writer(f);
 
         //way points -----------------------------------------------------------
         Feature point1 = types.wayPoint.newInstance();
@@ -254,7 +258,7 @@ public final strictfp class GPXWriterTes
         writer.writeEndDocument();
         writer.close();
 
-        final GPXReader reader = create(f);
+        final GPXReader reader = reader(f);
 
         //testing on toString since JTS geometry always fail on equals method.
         assertEquals(point1.toString(), reader.next().toString());



Mime
View raw message