sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1548356 - in /sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis: internal/system/ xml/
Date Fri, 06 Dec 2013 01:26:11 GMT
Author: desruisseaux
Date: Fri Dec  6 01:26:11 2013
New Revision: 1548356

URL: http://svn.apache.org/r1548356
Log:
Added FilteredStreamReader and modified the other FilterFoo classes in order to have a more
uniform pattern.

Added:
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamReader.java
  (with props)
Modified:
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/XMLOutputFactory.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilterVersion.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredNamespaces.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamWriter.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledMarshaller.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledUnmarshaller.java

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/XMLOutputFactory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/XMLOutputFactory.java?rev=1548356&r1=1548355&r2=1548356&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/XMLOutputFactory.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/internal/system/XMLOutputFactory.java
[UTF-8] Fri Dec  6 01:26:11 2013
@@ -16,10 +16,6 @@
  */
 package org.apache.sis.internal.system;
 
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.FileNotFoundException;
-import java.io.BufferedOutputStream;
 import java.io.OutputStream;
 import java.io.Writer;
 import javax.xml.stream.XMLEventWriter;
@@ -32,7 +28,6 @@ import javax.xml.transform.stax.StAXResu
 import org.w3c.dom.Node;
 import org.xml.sax.ContentHandler;
 import org.apache.sis.util.Static;
-import org.apache.sis.util.resources.Errors;
 
 
 /**
@@ -57,21 +52,10 @@ public final class XMLOutputFactory exte
     private XMLOutputFactory() {
     }
 
-    /**
-     * Creates a new writer for the given file.
-     *
-     * @param  out Where to write to.
-     * @param  encoding The document encoding (usually {@code "UTF-8"}).
-     * @return The writer.
-     * @throws XMLStreamException If the writer can not be created.
+    /*
+     * Do not provide convenience method for java.io.File, because the caller needs to close
the created
+     * output stream himself (this is not done by XMLStreamWriter.close(), despite its method
name).
      */
-    public static XMLStreamWriter createXMLStreamWriter(final File out, String encoding)
throws XMLStreamException {
-        try {
-            return FACTORY.createXMLStreamWriter(new BufferedOutputStream(new FileOutputStream(out)),
encoding);
-        } catch (FileNotFoundException e) {
-            throw new XMLStreamException(Errors.format(Errors.Keys.CanNotOpen_1, out), e);
-        }
-    }
 
     /**
      * Creates a new writer for the given stream.

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilterVersion.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilterVersion.java?rev=1548356&r1=1548355&r2=1548356&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilterVersion.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilterVersion.java
[UTF-8] Fri Dec  6 01:26:11 2013
@@ -39,22 +39,27 @@ enum FilterVersion {
     GML31(Namespaces.GML, LegacyNamespaces.GML);
 
     /**
-     * The URIs to replace. Keys are the old URIs, and values are the new URIs to use instead
of the old one.
-     * This map must be immutable.
+     * The URI replacements to apply when going from the "real" data producer (JAXB marshaller)
+     * to the filtered reader/writer. Keys are the actual URIs as declared in SIS implementation,
+     * and values are the URIs read or to write instead of the actual ones.
+     *
+     * @see FilteredNamespaces#toView
      */
-    final Map<String,String> replacements;
+    final Map<String,String> toView;
 
     /**
-     * The converse of {@link #replacements}. Keys are the new URIs, and values are the old
URIs which are
-     * replaced by the new ones. This map is inferred from {@link #replacements} and must
be immutable.
+     * The URI replacements to apply when going from the filtered reader/writer to the "real"
+     * data consumer (JAXB unmarshaller). This map is the converse of {@link #toView}.
+     *
+     * @see FilteredNamespaces#toImpl
      */
-    final Map<String,String> toDelegate;
+    final Map<String,String> toImpl;
 
     /**
      * Creates a new enum for replacing only one namespace.
      */
-    private FilterVersion(final String from, final String to) {
-        this.replacements = singletonMap(from, to);
-        this.toDelegate   = singletonMap(to, from);
+    private FilterVersion(final String impl, final String view) {
+        this.toView = singletonMap(impl, view);
+        this.toImpl = singletonMap(view, impl);
     }
 }

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredNamespaces.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredNamespaces.java?rev=1548356&r1=1548355&r2=1548356&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredNamespaces.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredNamespaces.java
[UTF-8] Fri Dec  6 01:26:11 2013
@@ -65,40 +65,61 @@ final class FilteredNamespaces implement
      * @see javax.xml.stream.XMLStreamReader#getNamespaceContext()
      * @see javax.xml.stream.XMLStreamWriter#getNamespaceContext()
      */
-    final NamespaceContext delegate;
+    private final NamespaceContext context;
 
     /**
-     * The URIs to replace, as a copy of the {@link FilterVersion#replacements} reference.
+     * The URI replacements to apply when going from the wrapped context to the filtered
context.
+     *
+     * @see FilterVersion#toView
      */
-    private final Map<String,String> replacements;
+    private final Map<String,String> toView;
 
     /**
-     * The converse of {@link #replacements}, as a copy of the {@link FilterVersion#toDelegate}
reference.
+     * The URI replacements to apply when going from the filtered context to the wrapped
context.
+     * This map is the converse of {@link #toView}.
+     *
+     * @see FilterVersion#toImpl
      */
-    private final Map<String,String> toDelegate;
+    private final Map<String,String> toImpl;
 
     /**
      * Creates a new namespaces filter for the given target version.
      */
-    FilteredNamespaces(final NamespaceContext delegate, final FilterVersion version) {
-        this.delegate     = delegate;
-        this.replacements = version.replacements;
-        this.toDelegate   = version.toDelegate;
+    FilteredNamespaces(final NamespaceContext context, final FilterVersion version, final
boolean inverse) {
+        this.context = context;
+        if (!inverse) {
+            toView = version.toView;
+            toImpl = version.toImpl;
+        } else {
+            toView = version.toImpl;
+            toImpl = version.toView;
+        }
+    }
+
+    /**
+     * Wraps this {@code FilteredNamespaces} in a new instance performing the inverse of
the replacements
+     * specified by the given version.
+     */
+    NamespaceContext inverse(final FilterVersion version) {
+        if (toView == version.toView && toImpl == version.toImpl) {
+            return this;
+        }
+        return new FilteredNamespaces(this, version, true);
     }
 
     /**
      * Returns the URI to make visible to the user of this filter.
      */
-    private String replacement(final String uri) {
-        final String replacement = replacements.get(uri);
+    private String toView(final String uri) {
+        final String replacement = toView.get(uri);
         return (replacement != null) ? replacement : uri;
     }
 
     /**
-     * Returns the URI used by the {@linkplain #delegate}.
+     * Returns the URI used by the {@linkplain #context}.
      */
-    private String toDelegate(final String uri) {
-        final String replacement = toDelegate.get(uri);
+    private String toImpl(final String uri) {
+        final String replacement = toImpl.get(uri);
         return (replacement != null) ? replacement : uri;
     }
 
@@ -107,7 +128,7 @@ final class FilteredNamespaces implement
      */
     @Override
     public String getNamespaceURI(final String prefix) {
-        return toDelegate(delegate.getNamespaceURI(prefix));
+        return toView(context.getNamespaceURI(prefix));
     }
 
     /**
@@ -115,7 +136,7 @@ final class FilteredNamespaces implement
      */
     @Override
     public String getPrefix(final String namespaceURI) {
-        return delegate.getPrefix(replacement(namespaceURI));
+        return context.getPrefix(toImpl(namespaceURI));
     }
 
     /**
@@ -123,6 +144,6 @@ final class FilteredNamespaces implement
      */
     @Override
     public Iterator<String> getPrefixes(final String namespaceURI) {
-        return delegate.getPrefixes(replacement(namespaceURI));
+        return context.getPrefixes(toImpl(namespaceURI));
     }
 }

Added: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamReader.java?rev=1548356&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamReader.java
(added)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamReader.java
[UTF-8] Fri Dec  6 01:26:11 2013
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.xml;
+
+import javax.xml.namespace.QName;
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.util.StreamReaderDelegate;
+
+
+/**
+ * A filter replacing the namespaces found in a XML document by the namespaces expected by
SIS at unmarshalling time.
+ * This class forwards every method calls to the wrapped {@link XMLStreamReader}, with all
{@code namespaceURI}
+ * arguments filtered before to be delegated.
+ *
+ * See {@link FilteredNamespaces} for more information.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+final class FilteredStreamReader extends StreamReaderDelegate {
+    /**
+     * The other version to unmarshall from.
+     */
+    private final FilterVersion version;
+
+    /**
+     * Creates a new filter for the given version of the standards.
+     */
+    private FilteredStreamReader(final XMLStreamReader in, final FilterVersion version) {
+        super(in);
+        this.version = version;
+    }
+
+    /**
+     * Converts a JAXB URI to the URI seen by the consumer of this wrapper.
+     */
+    private String toView(final String uri) {
+        final String replacement = version.toView.get(uri);
+        return (replacement != null) ? replacement : uri;
+    }
+
+    /**
+     * Converts a URI read from the XML document to the URI to give to JAXB.
+     */
+    private String toImpl(final String uri) {
+        final String replacement = version.toImpl.get(uri);
+        return (replacement != null) ? replacement : uri;
+    }
+
+    /**
+     * Converts a name read from the XML document to the name to give to JAXB.
+     */
+    private QName toImpl(QName name) {
+        final String namespaceURI = name.getNamespaceURI();
+        final String replacement = toImpl(namespaceURI);
+        if (replacement != namespaceURI) { // Really identity check.
+            name = new QName(namespaceURI, name.getLocalPart(), name.getPrefix());
+        }
+        return name;
+    }
+
+    /** Replaces the given URI if needed, then forwards the call. */
+    @Override
+    public void require(final int type, final String namespaceURI, final String localName)
throws XMLStreamException {
+        super.require(type, toView(namespaceURI), localName);
+    }
+
+    /** Returns the context of the underlying reader wrapped in a filter that convert the
namespaces on the fly. */
+    @Override
+    public NamespaceContext getNamespaceContext() {
+        return new FilteredNamespaces(super.getNamespaceContext(), version, true);
+    }
+
+    /** Forwards the call, then replaces the namespace URI if needed. */
+    @Override
+    public QName getName() {
+        return toImpl(super.getName());
+    }
+
+    /** Forwards the call, then replaces the namespace URI if needed. */
+    @Override
+    public QName getAttributeName(final int index) {
+        return toImpl(super.getAttributeName(index));
+    }
+
+    /** Forwards the call, then replaces the returned URI if needed. */
+    @Override
+    public String getNamespaceURI() {
+        return toImpl(super.getNamespaceURI());
+    }
+
+    /** Forwards the call, then replaces the returned URI if needed. */
+    @Override
+    public String getNamespaceURI(int index) {
+        return toImpl(super.getNamespaceURI(index));
+    }
+
+    /** Forwards the call, then replaces the returned URI if needed. */
+    @Override
+    public String getNamespaceURI(final String prefix) {
+        return toImpl(super.getNamespaceURI(prefix));
+    }
+
+    /** Forwards the call, then replaces the returned URI if needed. */
+    @Override
+    public String getAttributeNamespace(final int index) {
+        return toImpl(super.getAttributeNamespace(index));
+    }
+
+    /** Replaces the given URI if needed, then forwards the call. */
+    @Override
+    public String getAttributeValue(final String namespaceUri, final String localName) {
+        return super.getAttributeValue(toView(namespaceUri), localName);
+    }
+}

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

Propchange: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamReader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamWriter.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamWriter.java?rev=1548356&r1=1548355&r2=1548356&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamWriter.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/FilteredStreamWriter.java
[UTF-8] Fri Dec  6 01:26:11 2013
@@ -55,44 +55,48 @@ final class FilteredStreamWriter impleme
     /**
      * Returns the URI to write in the XML document.
      */
-    private String replacement(final String uri) {
-        final String replacement = version.replacements.get(uri);
+    private String toView(final String uri) {
+        final String replacement = version.toView.get(uri);
         return (replacement != null) ? replacement : uri;
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeStartElement(String localName) throws XMLStreamException {
+    public void writeStartElement(final String localName) throws XMLStreamException {
         out.writeStartElement(localName);
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException
{
-        out.writeStartElement(replacement(namespaceURI), localName);
+    public void writeStartElement(final String namespaceURI, final String localName) throws
XMLStreamException {
+        out.writeStartElement(toView(namespaceURI), localName);
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void writeStartElement(String prefix, String localName, String namespaceURI) throws
XMLStreamException {
-        out.writeStartElement(prefix, localName, replacement(namespaceURI));
+    public void writeStartElement(final String prefix, final String localName, final String
namespaceURI)
+            throws XMLStreamException
+    {
+        out.writeStartElement(prefix, localName, toView(namespaceURI));
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException
{
-        out.writeEmptyElement(replacement(namespaceURI), localName);
+    public void writeEmptyElement(final String namespaceURI, final String localName) throws
XMLStreamException {
+        out.writeEmptyElement(toView(namespaceURI), localName);
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void writeEmptyElement(String prefix, String localName, String namespaceURI) throws
XMLStreamException {
-        out.writeEmptyElement(prefix, localName, replacement(namespaceURI));
+    public void writeEmptyElement(final String prefix, final String localName, final String
namespaceURI)
+            throws XMLStreamException
+    {
+        out.writeEmptyElement(prefix, localName, toView(namespaceURI));
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeEmptyElement(String localName) throws XMLStreamException {
+    public void writeEmptyElement(final String localName) throws XMLStreamException {
         out.writeEmptyElement(localName);
     }
 
@@ -122,67 +126,71 @@ final class FilteredStreamWriter impleme
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeAttribute(String localName, String value) throws XMLStreamException
{
+    public void writeAttribute(final String localName, final String value) throws XMLStreamException
{
         out.writeAttribute(localName, value);
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void writeAttribute(String prefix, String namespaceURI, String localName, String
value) throws XMLStreamException {
-        out.writeAttribute(prefix, replacement(namespaceURI), localName, value);
+    public void writeAttribute(final String prefix, final String namespaceURI,final  String
localName,
+            final String value) throws XMLStreamException
+    {
+        out.writeAttribute(prefix, toView(namespaceURI), localName, value);
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void writeAttribute(String namespaceURI, String localName, String value) throws
XMLStreamException {
-        out.writeAttribute(replacement(namespaceURI), localName, value);
+    public void writeAttribute(final String namespaceURI, final String localName, final String
value)
+            throws XMLStreamException
+    {
+        out.writeAttribute(toView(namespaceURI), localName, value);
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException
{
-        out.writeNamespace(prefix, replacement(namespaceURI));
+    public void writeNamespace(final String prefix, final String namespaceURI) throws XMLStreamException
{
+        out.writeNamespace(prefix, toView(namespaceURI));
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
-        out.writeDefaultNamespace(replacement(namespaceURI));
+    public void writeDefaultNamespace(final String namespaceURI) throws XMLStreamException
{
+        out.writeDefaultNamespace(toView(namespaceURI));
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeComment(String data) throws XMLStreamException {
+    public void writeComment(final String data) throws XMLStreamException {
         out.writeComment(data);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeProcessingInstruction(String target) throws XMLStreamException {
+    public void writeProcessingInstruction(final String target) throws XMLStreamException
{
         out.writeProcessingInstruction(target);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeProcessingInstruction(String target, String data) throws XMLStreamException
{
+    public void writeProcessingInstruction(final String target, final String data) throws
XMLStreamException {
         out.writeProcessingInstruction(target, data);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeCData(String data) throws XMLStreamException {
+    public void writeCData(final String data) throws XMLStreamException {
         out.writeCData(data);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeDTD(String dtd) throws XMLStreamException {
+    public void writeDTD(final String dtd) throws XMLStreamException {
         out.writeDTD(dtd);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeEntityRef(String name) throws XMLStreamException {
+    public void writeEntityRef(final String name) throws XMLStreamException {
         out.writeEntityRef(name);
     }
 
@@ -194,53 +202,53 @@ final class FilteredStreamWriter impleme
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeStartDocument(String version) throws XMLStreamException {
+    public void writeStartDocument(final String version) throws XMLStreamException {
         out.writeStartDocument(version);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeStartDocument(String encoding, String version) throws XMLStreamException
{
+    public void writeStartDocument(final String encoding, final String version) throws XMLStreamException
{
         out.writeStartDocument(encoding, version);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeCharacters(String text) throws XMLStreamException {
+    public void writeCharacters(final String text) throws XMLStreamException {
         out.writeCharacters(text);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public void writeCharacters(char[] text, int start, int len) throws XMLStreamException
{
+    public void writeCharacters(final char[] text, final int start, final int len) throws
XMLStreamException {
         out.writeCharacters(text, start, len);
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public String getPrefix(String uri) throws XMLStreamException {
-        return out.getPrefix(replacement(uri));
+    public String getPrefix(final String uri) throws XMLStreamException {
+        return out.getPrefix(toView(uri));
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void setPrefix(String prefix, String uri) throws XMLStreamException {
-        out.setPrefix(prefix, replacement(uri));
+    public void setPrefix(final String prefix, final String uri) throws XMLStreamException
{
+        out.setPrefix(prefix, toView(uri));
     }
 
     /** Replaces the given URI if needed, then forwards the call. */
     @Override
-    public void setDefaultNamespace(String uri) throws XMLStreamException {
-        out.setDefaultNamespace(replacement(uri));
+    public void setDefaultNamespace(final String uri) throws XMLStreamException {
+        out.setDefaultNamespace(toView(uri));
     }
 
     /** Unwraps the original context and forwards the call. */
     @Override
     public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
         if (context instanceof FilteredNamespaces) {
-            context = ((FilteredNamespaces) context).delegate;
+            context = ((FilteredNamespaces) context).inverse(version);
         } else {
-            context = new FilteredNamespaces(context, version);
+            context = new FilteredNamespaces(context, version, true);
         }
         out.setNamespaceContext(context);
     }
@@ -248,12 +256,12 @@ final class FilteredStreamWriter impleme
     /** Returns the context of the underlying writer wrapped in a filter that convert the
namespaces on the fly. */
     @Override
     public NamespaceContext getNamespaceContext() {
-        return new FilteredNamespaces(out.getNamespaceContext(), version);
+        return new FilteredNamespaces(out.getNamespaceContext(), version, false);
     }
 
     /** Forwards the call verbatim. */
     @Override
-    public Object getProperty(String name) throws IllegalArgumentException {
+    public Object getProperty(final String name) throws IllegalArgumentException {
         return out.getProperty(name);
     }
 }

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledMarshaller.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledMarshaller.java?rev=1548356&r1=1548355&r2=1548356&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledMarshaller.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledMarshaller.java
[UTF-8] Fri Dec  6 01:26:11 2013
@@ -19,6 +19,9 @@ package org.apache.sis.xml;
 import java.io.File;
 import java.io.Writer;
 import java.io.OutputStream;
+import java.io.FileOutputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
 import javax.xml.bind.Marshaller;
 import javax.xml.bind.JAXBException;
 import javax.xml.bind.PropertyException;
@@ -140,11 +143,13 @@ final class PooledMarshaller extends Poo
      * than the one supported natively by SIS, i.e. when {@link #getFilterVersion()} returns
      * a non-null value.
      *
+     * @param object  The object to marshall.
+     * @param output  The writer create by SIS (<b>not</b> the writer given by
the user).
      * @param version Identify the namespace substitutions to perform.
      * @param close   Whether to close the writer after marshalling.
      */
-    private void marshal(final Object object, XMLStreamWriter output, final FilterVersion
version,
-            final boolean close) throws XMLStreamException, JAXBException
+    private void marshal(final Object object, XMLStreamWriter output, final FilterVersion
version)
+            throws XMLStreamException, JAXBException
     {
         output = new FilteredStreamWriter(output, version);
         final Context context = begin();
@@ -153,11 +158,7 @@ final class PooledMarshaller extends Poo
         } finally {
             context.finish();
         }
-        if (close) {
-            output.close();
-        } else {
-            output.flush();
-        }
+        output.close(); // Despite its name, this method does not close the underlying output
stream.
     }
 
     /**
@@ -167,7 +168,7 @@ final class PooledMarshaller extends Poo
     public void marshal(final Object object, final Result output) throws JAXBException {
         final FilterVersion version = getFilterVersion();
         if (version != null) try {
-            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version, false);
+            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version);
         } catch (XMLStreamException e) {
             throw new JAXBException(e);
         } else {
@@ -188,7 +189,7 @@ final class PooledMarshaller extends Poo
     public void marshal(final Object object, final OutputStream output) throws JAXBException
{
         final FilterVersion version = getFilterVersion();
         if (version != null) try {
-            marshal(object, XMLOutputFactory.createXMLStreamWriter(output, getEncoding()),
version, false);
+            marshal(object, XMLOutputFactory.createXMLStreamWriter(output, getEncoding()),
version);
         } catch (XMLStreamException e) {
             throw new JAXBException(e);
         } else {
@@ -209,8 +210,10 @@ final class PooledMarshaller extends Poo
     public void marshal(final Object object, final File output) throws JAXBException {
         final FilterVersion version = getFilterVersion();
         if (version != null) try {
-            marshal(object, XMLOutputFactory.createXMLStreamWriter(output, getEncoding()),
version, true);
-        } catch (XMLStreamException e) {
+            try (OutputStream s = new BufferedOutputStream(new FileOutputStream(output)))
{
+                marshal(object, XMLOutputFactory.createXMLStreamWriter(s, getEncoding()),
version);
+            }
+        } catch (IOException | XMLStreamException e) {
             throw new JAXBException(e);
         } else {
             // Marshalling to the default GML version.
@@ -230,7 +233,7 @@ final class PooledMarshaller extends Poo
     public void marshal(final Object object, final Writer output) throws JAXBException {
         final FilterVersion version = getFilterVersion();
         if (version != null) try {
-            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version, false);
+            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version);
         } catch (XMLStreamException e) {
             throw new JAXBException(e);
         } else {
@@ -251,7 +254,7 @@ final class PooledMarshaller extends Poo
     public void marshal(final Object object, final ContentHandler output) throws JAXBException
{
         final FilterVersion version = getFilterVersion();
         if (version != null) try {
-            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version, false);
+            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version);
         } catch (XMLStreamException e) {
             throw new JAXBException(e);
         } else {
@@ -272,7 +275,7 @@ final class PooledMarshaller extends Poo
     public void marshal(final Object object, final Node output) throws JAXBException {
         final FilterVersion version = getFilterVersion();
         if (version != null) try {
-            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version, false);
+            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version);
         } catch (XMLStreamException e) {
             throw new JAXBException(e);
         } else {
@@ -290,20 +293,16 @@ final class PooledMarshaller extends Poo
      * Delegates the marshalling to the wrapped marshaller.
      */
     @Override
-    public void marshal(final Object object, final XMLStreamWriter output) throws JAXBException
{
+    public void marshal(final Object object, XMLStreamWriter output) throws JAXBException
{
         final FilterVersion version = getFilterVersion();
-        if (version != null) try {
-            marshal(object, output, version, false);
-        } catch (XMLStreamException e) {
-            throw new JAXBException(e);
-        } else {
-            // Marshalling to the default GML version.
-            final Context context = begin();
-            try {
-                marshaller.marshal(object, output);
-            } finally {
-                context.finish();
-            }
+        if (version != null) {
+            output = new FilteredStreamWriter(output, version);
+        }
+        final Context context = begin();
+        try {
+            marshaller.marshal(object, output);
+        } finally {
+            context.finish();
         }
     }
 
@@ -314,7 +313,7 @@ final class PooledMarshaller extends Poo
     public void marshal(final Object object, final XMLEventWriter output) throws JAXBException
{
         final FilterVersion version = getFilterVersion();
         if (version != null) try {
-            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version, false);
+            marshal(object, XMLOutputFactory.createXMLStreamWriter(output), version);
         } catch (XMLStreamException e) {
             throw new JAXBException(e);
         } else {

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledUnmarshaller.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledUnmarshaller.java?rev=1548356&r1=1548355&r2=1548356&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledUnmarshaller.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/xml/PooledUnmarshaller.java
[UTF-8] Fri Dec  6 01:26:11 2013
@@ -40,18 +40,20 @@ import org.apache.sis.internal.jaxb.Cont
 
 /**
  * Wraps a {@link Unmarshaller} in order to have some control on the modifications applied
on it.
- * This wrapper serves two purpose:
+ * This wrapper serves three purposes:
  *
  * <ul>
  *   <li>Save properties before modification, in order to restore them to their original
values
  *       when the unmarshaller is recycled.</li>
  *   <li>Constructs a SIS {@link Context} object on unmarshalling, in order to give
  *       additional information to the SIS object being unmarshalled.</li>
+ *   <li>Wraps the input stream in a {@link FilteredStreamReader} if the document GML
version
+ *       in not the SIS native GML version.</li>
  * </ul>
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3 (derived from geotk-3.00)
- * @version 0.3
+ * @version 0.4
  * @module
  */
 @Decorator(Unmarshaller.class)



Mime
View raw message