You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2016/07/04 10:18:43 UTC
svn commit: r1751241 - in /sis/branches/JDK8/storage/sis-xmlstore/src:
main/java/org/apache/sis/internal/gpx/ main/java/org/apache/sis/internal/xml/
test/java/org/apache/sis/internal/gpx/
Author: desruisseaux
Date: Mon Jul 4 10:18:43 2016
New Revision: 1751241
URL: http://svn.apache.org/viewvc?rev=1751241&view=rev
Log:
Partial revert of StaxStreamReader previous commit: should not extends DataStore because StaxStreamReader is not intended to be used directly as a DataStore.
It is rather intended to be contained in a DataStore, as internal mechanic. Add a StaxStream base class with documentation about this intended design.
Added:
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java (with props)
Modified:
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter100.java
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXWriter110.java
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamReader.java
sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStreamWriter.java
sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXReaderTest.java
sis/branches/JDK8/storage/sis-xmlstore/src/test/java/org/apache/sis/internal/gpx/GPXWriterTest.java
Modified: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java?rev=1751241&r1=1751240&r2=1751241&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java [UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/gpx/GPXReader.java [UTF-8] Mon Jul 4 10:18:43 2016
@@ -87,9 +87,8 @@ public class GPXReader extends StaxStrea
* @throws IOException if input failed to be opened for any IO reason
* @throws XMLStreamException if input is not a valid XML stream
*/
- public GPXReader(final StorageConnector storage) throws DataStoreException, IOException, XMLStreamException {
- super(storage);
- final Object input = storage.getStorage();
+ public GPXReader(final Object input, final StorageConnector storage) throws DataStoreException, IOException, XMLStreamException {
+ super(input, storage);
final XMLStreamReader reader = getReader();
//search for the bound tag to generate the envelope
@@ -154,7 +153,6 @@ public class GPXReader extends StaxStrea
*
* @return Metadata or null if input is not set.
*/
- @Override
public Metadata getMetadata() {
return null; // TODO
}
@@ -163,7 +161,7 @@ public class GPXReader extends StaxStrea
* {@inheritDoc }
*/
@Override
- public void close() throws DataStoreException {
+ public void close() throws IOException, XMLStreamException {
super.close();
metadata = null;
current = null;
@@ -179,7 +177,7 @@ public class GPXReader extends StaxStrea
* @throws javax.xml.stream.XMLStreamException if xml parser encounter an invalid
* element or underlying stream caused an exception
*/
- public boolean hasNext() throws DataStoreException, XMLStreamException {
+ public boolean hasNext() throws IOException, XMLStreamException {
findNext();
return current != null;
}
@@ -191,7 +189,7 @@ public class GPXReader extends StaxStrea
* @throws javax.xml.stream.XMLStreamException if xml parser encounter an invalid
* element or underlying stream caused an exception
*/
- public Feature next() throws DataStoreException, XMLStreamException {
+ public Feature next() throws IOException, XMLStreamException {
findNext();
final Feature ele = current;
current = null;
@@ -202,7 +200,7 @@ public class GPXReader extends StaxStrea
* Search for the next feature in the stax stream.
* This method will set the current local property if there is one.
*/
- private void findNext() throws DataStoreException, XMLStreamException {
+ private void findNext() throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
if(current != null) return;
@@ -239,7 +237,7 @@ public class GPXReader extends StaxStrea
*
* @return MetaData
*/
- private MetaData parseMetaData100() throws DataStoreException, XMLStreamException {
+ private MetaData parseMetaData100() throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final MetaData metadata = new MetaData();
@@ -294,7 +292,7 @@ public class GPXReader extends StaxStrea
*
* @return MetaData
*/
- private MetaData parseMetaData110() throws DataStoreException, XMLStreamException {
+ private MetaData parseMetaData110() throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final MetaData metadata = new MetaData();
@@ -340,7 +338,7 @@ public class GPXReader extends StaxStrea
*
* @return CopyRight
*/
- private CopyRight parseCopyRight() throws DataStoreException, XMLStreamException {
+ private CopyRight parseCopyRight() throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final CopyRight copyright = new CopyRight();
copyright.setAuthor(reader.getAttributeValue(null, ATT_COPYRIGHT_AUTHOR));
@@ -378,7 +376,7 @@ public class GPXReader extends StaxStrea
*
* @return URI
*/
- private URI parseLink() throws DataStoreException, XMLStreamException {
+ private URI parseLink() throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
String text = reader.getAttributeValue(null, ATT_LINK_HREF);
String mime = null;
@@ -417,7 +415,7 @@ public class GPXReader extends StaxStrea
*
* @return Person
*/
- private Person parsePerson() throws DataStoreException, XMLStreamException {
+ private Person parsePerson() throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final Person person = new Person();
@@ -453,7 +451,7 @@ public class GPXReader extends StaxStrea
*
* @return Envelope
*/
- private Envelope parseBound() throws DataStoreException, XMLStreamException {
+ private Envelope parseBound() throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final String xmin = reader.getAttributeValue(null, ATT_BOUNDS_MINLON);
final String xmax = reader.getAttributeValue(null, ATT_BOUNDS_MAXLON);
@@ -477,7 +475,7 @@ public class GPXReader extends StaxStrea
*
* @return Feature
*/
- private Feature parseWayPoint(final int index) throws DataStoreException, XMLStreamException {
+ private Feature parseWayPoint(final int index) throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final Feature feature = TYPE_WAYPOINT.newInstance();
feature.setPropertyValue("index", index);
@@ -569,7 +567,7 @@ public class GPXReader extends StaxStrea
*
* @return Feature
*/
- private Feature parseRoute(final int index) throws DataStoreException, XMLStreamException {
+ private Feature parseRoute(final int index) throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final Feature feature = TYPE_ROUTE.newInstance();
feature.setPropertyValue("index", index);
@@ -632,7 +630,7 @@ public class GPXReader extends StaxStrea
*
* @return Feature
*/
- private Feature parseTrackSegment(final int index) throws DataStoreException, XMLStreamException {
+ private Feature parseTrackSegment(final int index) throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final Feature feature = TYPE_TRACK_SEGMENT.newInstance();
feature.setPropertyValue("index", index);
@@ -669,7 +667,7 @@ public class GPXReader extends StaxStrea
*
* @return Feature
*/
- private Feature parseTrack(final int index) throws DataStoreException, XMLStreamException {
+ private Feature parseTrack(final int index) throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
final Feature feature = TYPE_TRACK.newInstance();
feature.setPropertyValue("index", index);
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=1751241&r1=1751240&r2=1751241&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] Mon Jul 4 10:18:43 2016
@@ -60,8 +60,8 @@ public class GPXWriter100 extends StaxSt
*
* @param creator file creator
*/
- public GPXWriter100(final String creator) {
- this(GPX_NAMESPACE_V10,creator);
+ public GPXWriter100(final String creator, final Object output) throws IOException, XMLStreamException {
+ this(GPX_NAMESPACE_V10, creator, output);
}
/**
@@ -69,7 +69,8 @@ public class GPXWriter100 extends StaxSt
* @param namespace gpx namespace
* @param creator file creator
*/
- protected GPXWriter100(final String namespace,final String creator) {
+ protected GPXWriter100(final String namespace, final String creator, final Object output) throws IOException, XMLStreamException {
+ super(output);
ensureNonNull("creator", creator);
this.creator = creator;
this.namespace = namespace;
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=1751241&r1=1751240&r2=1751241&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] Mon Jul 4 10:18:43 2016
@@ -19,6 +19,7 @@ package org.apache.sis.internal.gpx;
import java.net.URI;
import java.time.temporal.Temporal;
import java.util.Collection;
+import java.io.IOException;
import javax.xml.stream.XMLStreamException;
import static org.apache.sis.internal.gpx.GPXConstants.*;
@@ -37,8 +38,8 @@ public class GPXWriter110 extends GPXWri
*
* @param creator gpx file creator
*/
- public GPXWriter110(final String creator) {
- super(GPX_NAMESPACE_V11, creator);
+ public GPXWriter110(final String creator, final Object output) throws IOException, XMLStreamException {
+ super(GPX_NAMESPACE_V11, creator, output);
}
/**
@@ -52,7 +53,7 @@ public class GPXWriter110 extends GPXWri
/**
* Write metadata gpx tag.
- *
+ *
* @param metadata not null
* @throws XMLStreamException if underlying xml stax writer encounter an error
*/
@@ -87,7 +88,7 @@ public class GPXWriter110 extends GPXWri
*/
protected void writePerson(final Person person) throws XMLStreamException {
if (person == null) return;
-
+
writer.writeStartElement(namespace, TAG_AUTHOR);
writeSimpleTag(namespace, TAG_NAME, person.getName());
writeSimpleTag(namespace, TAG_AUTHOR_EMAIL, person.getEmail());
@@ -97,7 +98,7 @@ public class GPXWriter110 extends GPXWri
/**
* Write multiple links.
- *
+ *
* @param links links to write
* @throws XMLStreamException if underlying xml stax writer encounter an error
*/
Added: 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=1751241&view=auto
==============================================================================
--- sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java (added)
+++ sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java [UTF-8] Mon Jul 4 10:18:43 2016
@@ -0,0 +1,89 @@
+/*
+ * 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.internal.xml;
+
+import java.util.Locale;
+import java.io.IOException;
+import javax.xml.stream.XMLStreamException;
+import org.apache.sis.util.Localized;
+import org.apache.sis.util.resources.Errors;
+
+
+/**
+ * Common base class for {@code StaxStreamReader} and {@code StaxStreamWriter}.
+ * {@code StaxStream} subclasses are not used directly (they are Apache SIS internal mechanic);
+ * they are rather used as helper classes for {@link org.apache.sis.storage.DataStore} implementations.
+ * Those {@code DataStore}s will typically manage {@code StaxStreamReader} and {@code StaxStreamWriter}
+ * instances on which they delegate their read and write operations.
+ *
+ * <div class="section">Multi-threading</div>
+ * This class and subclasses are not tread-safe. Synchronization shall be done by the {@code DataStore}
+ * that contains the {@code StaxStream} instances.
+ *
+ * @author Johann Sorel (Geomatys)
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.8
+ * @version 0.8
+ * @module
+ */
+abstract class StaxStream implements AutoCloseable, Localized {
+ /**
+ * The locale to use for error messages, or {@code null} for the default locale.
+ */
+ private Locale locale;
+
+ /**
+ * For sub-classes constructors.
+ */
+ StaxStream() {
+ }
+
+ /**
+ * Sets the locale to use for formatting error messages.
+ * A null value means to use the default locale.
+ *
+ * @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.
+ */
+ @Override
+ public Locale getLocale() {
+ return locale;
+ }
+
+ /**
+ * Returns the error resources in the current locale.
+ */
+ protected final Errors errors() {
+ return Errors.getResources(getLocale());
+ }
+
+ /**
+ * Closes the input or output stream and releases any resources used by this XML reader or writer.
+ * This reader or writer can not be used anymore after this method has been invoked.
+ *
+ * @throws IOException if an error occurred while closing the input or output stream.
+ * @throws XMLStreamException if an error occurred while releasing XML reader/writer resources.
+ */
+ @Override
+ public abstract void close() throws IOException, XMLStreamException;
+}
Propchange: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK8/storage/sis-xmlstore/src/main/java/org/apache/sis/internal/xml/StaxStream.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
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=1751241&r1=1751240&r2=1751241&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] Mon Jul 4 10:18:43 2016
@@ -20,10 +20,10 @@ import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;
+import java.io.EOFException;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;
-import org.apache.sis.storage.DataStore;
import org.apache.sis.storage.DataStoreException;
import org.apache.sis.storage.StorageConnector;
import org.apache.sis.internal.system.XMLInputFactory;
@@ -34,13 +34,14 @@ import org.apache.sis.util.Classes;
/**
* Base class of Apache SIS readers of XML files using STAX parser.
+ * This is a helper class for {@link org.apache.sis.storage.DataStore} implementations.
* Readers for a given specification should extend this class and provide appropriate read methods.
*
* <p>Example:</p>
- * {@preformat
+ * {@preformat java
* public class UserObjectReader extends StaxStreamReader {
* public UserObject read() throws XMLStreamException {
- * // Actual STAX reading operations.
+ * // Actual STAX read operations.
* return userObject;
* }
* }
@@ -48,20 +49,24 @@ import org.apache.sis.util.Classes;
*
* And should be used like below:
*
- * {@preformat
+ * {@preformat java
* UserObject obj;
* try (UserObjectReader reader = new UserObjectReader(input)) {
* obj = instance.read();
* }
* }
*
+ * <div class="section">Multi-threading</div>
+ * This class and subclasses are not tread-safe. Synchronization shall be done by the {@code DataStore}
+ * that contains the {@code StaxStreamReader} instance.
+ *
* @author Johann Sorel (Geomatys)
* @author Martin Desruisseaux (Geomatys)
* @since 0.8
* @version 0.8
* @module
*/
-public abstract class StaxStreamReader extends DataStore {
+public abstract class StaxStreamReader extends StaxStream {
/**
* The XML stream reader.
*/
@@ -74,67 +79,73 @@ public abstract class StaxStreamReader e
private Closeable sourceStream;
/**
- * Creates a new XML store from the given file, URL, stream or reader object.
- * This constructor invokes {@link StorageConnector#closeAllExcept(Object)},
- * keeping open only the needed resource.
- *
- * @param storage information about the storage (URL, stream, <i>etc</i>).
- * @throws DataStoreException if an error occurred while opening the XML file.
- */
- protected StaxStreamReader(final StorageConnector storage) throws DataStoreException {
- ArgumentChecks.ensureNonNull("storage", storage);
- Object obj = storage.getStorage();
- try {
- reader = XMLInputFactory.createFromAny(obj);
- if (reader != null) {
- if (obj instanceof Closeable) {
- sourceStream = (Closeable) obj;
- }
- } else {
- final InputStream in = storage.getStorageAs(InputStream.class);
- if (in == null) {
- throw new DataStoreException(Errors.format(Errors.Keys.IllegalInputTypeForReader_2, "XML", Classes.getClass(obj)));
- }
+ * 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:
+ *
+ * <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
+ * {@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.
+ * @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 {
+ 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 {
+ final InputStream in;
+ if (storage != null && (in = storage.getStorageAs(InputStream.class)) != null) {
reader = XMLInputFactory.createXMLStreamReader(in);
sourceStream = in;
- obj = in;
+ storage.closeAllExcept(in);
+ } else {
+ throw new DataStoreException(Errors.format(Errors.Keys.IllegalInputTypeForReader_2, "XML", Classes.getClass(input)));
}
- } catch (XMLStreamException e) {
- throw new DataStoreException(e);
}
- storage.closeAllExcept(obj);
- }
-
- /**
- * Returns the error resources in the current locale.
- */
- private Errors errors() {
- return Errors.getResources(getLocale());
}
/**
- * Returns the XML stream reader.
+ * Returns the XML stream reader if it is not closed.
*
* @return the XML stream reader (never null).
- * @throws DataStoreException if this XML reader has been closed.
+ * @throws IOException if this XML reader has been closed.
*/
- protected final XMLStreamReader getReader() throws DataStoreException {
+ protected final XMLStreamReader getReader() throws IOException {
if (reader != null) {
return reader;
}
- throw new DataStoreException(errors().getString(Errors.Keys.ClosedReader_1, "XML"));
+ throw new IOException(errors().getString(Errors.Keys.ClosedReader_1, "XML"));
}
/**
* Returns a XML stream reader over only a portion of the document, from given position inclusive
- * until the end of the given element exclusive. Nested elements of the same name, if any; will be
+ * until the end of the given element exclusive. Nested elements of the same name, if any, will be
* ignored.
*
* @param tagName name of the tag to close.
* @return a reader over a portion of the stream.
- * @throws DataStoreException if this XML reader has been closed.
+ * @throws IOException if this XML reader has been closed.
*/
- protected final XMLStreamReader getSubReader(final String tagName) throws DataStoreException {
+ protected final XMLStreamReader getSubReader(final String tagName) throws IOException {
return new StreamReaderDelegate(getReader()) {
/** Increased every time a nested element of the same name is found. */
private int nested;
@@ -164,10 +175,11 @@ 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 DataStoreException if end tag could not be found.
+ * @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 DataStoreException, XMLStreamException {
+ protected final void skipUntilEnd(final String tagName) throws IOException, XMLStreamException {
final XMLStreamReader reader = getReader();
int nested = 0;
while (reader.hasNext()) {
@@ -186,7 +198,7 @@ public abstract class StaxStreamReader e
}
}
}
- throw new DataStoreException(errors().getString(Errors.Keys.UnexpectedEndOfFile_1, tagName));
+ throw new EOFException(errors().getString(Errors.Keys.UnexpectedEndOfFile_1, tagName));
}
/**
@@ -208,24 +220,21 @@ public abstract class StaxStreamReader e
}
/**
- * Closes the input stream and releases any resources used by this data store.
- * This data store can not be used anymore after this method has been invoked.
+ * Closes the input stream and releases any resources used by this XML reader.
+ * This reader can not be used anymore after this method has been invoked.
*
- * @throws DataStoreException if an error occurred while closing the stream or releasing the resources.
+ * @throws IOException if an error occurred while closing the input stream.
+ * @throws XMLStreamException if an error occurred while releasing XML reader/writer resources.
*/
@Override
- public synchronized void close() throws DataStoreException {
- try {
- if (reader != null) {
- reader.close();
- reader = null;
- }
- if (sourceStream != null) {
- sourceStream.close();
- sourceStream = null;
- }
- } catch (IOException | XMLStreamException e) {
- throw new DataStoreException(e);
+ public void close() throws IOException, XMLStreamException {
+ if (reader != null) {
+ reader.close();
+ reader = null;
+ }
+ if (sourceStream != null) {
+ sourceStream.close();
+ sourceStream = null;
}
}
}
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=1751241&r1=1751240&r2=1751241&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] Mon Jul 4 10:18:43 2016
@@ -35,44 +35,41 @@ import org.apache.sis.xml.Namespaces;
/**
- * An abstract class for all stax stream writer.<br>
- * Writers for a given specification should extend this class and
- * provide appropriate write methods.<br>
- * <br>
- * Example : <br>
- * <pre>
- * {@code
- * public class UserWriter extends StaxStreamWriter{
+ * Base class of Apache SIS writers of XML files using STAX writer.
+ * This is a helper class for {@link org.apache.sis.storage.DataStore} implementations.
+ * Writers for a given specification should extend this class and provide appropriate write methods.
*
- * public void write(User user) throws XMLStreamException{
- * //casual stax writing operations
- * writer.writeStartElement(...
- * }
- * }
- * }
- * </pre>
- * And should be used like : <br>
- * <pre>
- * {@code
- * final UserWriter instance = new UserWriter();
- * try{
- * instance.setOutput(stream);
- * instance.write(aUser);
- * }finally{
- * instance.dispose();
+ * <p>Example:</p>
+ * {@preformat
+ * public class UserWriter extends StaxStreamWriter {
+ * public void write(User user) throws XMLStreamException {
+ * // Actual STAX write operations.
+ * writer.writeStartElement(\u2026);
+ * }
+ * }
* }
+ *
+ * And should be used like below:
+ *
+ * {@preformat java
+ * try (UserWriter instance = new UserWriter()) {
+ * instance.setOutput(stream);
+ * instance.write(aUser);
+ * }
* }
- * </pre>
+ *
+ * <div class="section">Multi-threading</div>
+ * This class and subclasses are not tread-safe. Synchronization shall be done by the {@code DataStore}
+ * that contains the {@code StaxStream} instances.
*
* @author Johann Sorel (Geomatys)
* @since 0.8
* @version 0.8
* @module
*/
-public abstract class StaxStreamWriter implements AutoCloseable {
-
+public abstract class StaxStreamWriter extends StaxStream {
/**
- * Underlying stax writer.
+ * The XML stream writer.
*/
protected XMLStreamWriter writer;
@@ -87,49 +84,7 @@ public abstract class StaxStreamWriter i
private final Map<String, String> unknowNamespaces = new HashMap<>();
/**
- * 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.
- *
- * @return underlying stax writer, can be null if input has not been set
- */
- public XMLStreamWriter getWriter() {
- return writer;
- }
-
- /**
- * close potentiel previous stream and cache if there are some.
- * This way the writer can be reused for a different output later.
- * The underlying stax writer will be closed.
- * @throws java.io.IOException if previous source stream caused an exception on close
- * @throws javax.xml.stream.XMLStreamException if previous stax reader caused an exception on close
- */
- public void reset() throws IOException, XMLStreamException {
- if (writer != null) {
- writer.close();
- writer = null;
- }
- if (targetStream != null) {
- targetStream.close();
- targetStream = null;
- }
- }
-
- /**
- * Release potentiel locks or opened stream.
- * Must be called when the writer is not needed anymore.
- * It should not be used after this method has been called.
- * @throws java.io.IOException if previous source stream caused an exception on close
- * @throws javax.xml.stream.XMLStreamException if previous stax reader caused an exception on close
- */
- @Override
- public void close() throws Exception{
- reset();
- }
-
- /**
+ * 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>
@@ -142,9 +97,7 @@ public abstract class StaxStreamWriter i
* @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
*/
- public void setOutput(Object output) throws IOException, XMLStreamException {
- reset();
-
+ protected StaxStreamWriter(Object output) throws IOException, XMLStreamException {
if (output instanceof XMLStreamWriter) {
writer = (XMLStreamWriter) output;
return;
@@ -165,6 +118,19 @@ public abstract class StaxStreamWriter i
}
/**
+ * 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.
+ *
+ * @return underlying stax writer, can be null if input has not been set
+ */
+ public XMLStreamWriter getWriter() {
+ return writer;
+ }
+
+ /**
* 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
@@ -251,4 +217,23 @@ public abstract class StaxStreamWriter i
this.unknow = unknow;
}
}
+
+ /**
+ * Closes the output stream and releases any resources used by this XML writer.
+ * This writer can not be used anymore after this method has been invoked.
+ *
+ * @throws IOException if an error occurred while closing the output stream.
+ * @throws XMLStreamException if an error occurred while releasing XML writer resources.
+ */
+ @Override
+ public void close() throws IOException, XMLStreamException {
+ if (writer != null) {
+ writer.close();
+ writer = null;
+ }
+ if (targetStream != null) {
+ targetStream.close();
+ targetStream = null;
+ }
+ }
}
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=1751241&r1=1751240&r2=1751241&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] Mon Jul 4 10:18:43 2016
@@ -55,7 +55,8 @@ public class GPXReaderTest {
private static GPXReader create(final String resource) throws DataStoreException, IOException, XMLStreamException {
- return new GPXReader(new StorageConnector(GPXReaderTest.class.getResource(resource)));
+ StorageConnector storage = new StorageConnector(GPXReaderTest.class.getResource(resource));
+ return new GPXReader(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=1751241&r1=1751240&r2=1751241&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] Mon Jul 4 10:18:43 2016
@@ -29,7 +29,6 @@ import com.esri.core.geometry.Point;
import org.apache.sis.geometry.GeneralEnvelope;
import org.apache.sis.geometry.ImmutableEnvelope;
import org.apache.sis.referencing.CommonCRS;
-import org.apache.sis.storage.StorageConnector;
import org.junit.Test;
import static org.apache.sis.internal.gpx.GPXConstants.*;
@@ -58,8 +57,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");
- writer.setOutput(f);
+ final GPXWriter110 writer = new GPXWriter110("Apache SIS", f);
final Person person = new Person();
person.setName("Jean-Pierre");
@@ -90,7 +88,7 @@ public final strictfp class GPXWriterTes
writer.writeEndDocument();
writer.close();
- try (GPXReader reader = new GPXReader(new StorageConnector(f))) {
+ try (GPXReader reader = new GPXReader(f, null)) {
assertEquals(metaData, reader.metadata);
}
@@ -108,8 +106,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");
- writer.setOutput(f);
+ final GPXWriter110 writer = new GPXWriter110("Apache SIS", f);
//way points -----------------------------------------------------------
Feature point1 = TYPE_WAYPOINT.newInstance();
@@ -250,7 +247,7 @@ public final strictfp class GPXWriterTes
writer.writeEndDocument();
writer.close();
- final GPXReader reader = new GPXReader(new StorageConnector(f));
+ final GPXReader reader = new GPXReader(f, null);
//testing on toString since JTS geometry always fail on equals method.
assertEquals(point1.toString(), reader.next().toString());