Revision: 18753
http://sourceforge.net/p/gate/code/18753
Author: ian_roberts
Date: 2015-06-04 13:20:25 +0000 (Thu, 04 Jun 2015)
Log Message:
-----------
Use XML 1.1 by default instead of 1.0 when writing ObjectWrapper XML. This
gives support for objects whose XML representation includes characters not
allowed in XML 1.0 such as control characters, which XML 1.1 permits as numeric
character references.
Modified Paths:
--------------
gate/trunk/src/main/gate/corpora/ObjectWrapper.java
Added Paths:
-----------
gate/trunk/src/main/gate/util/xml/
gate/trunk/src/main/gate/util/xml/XML11OutputFactory.java
gate/trunk/src/main/gate/util/xml/XML11StaxDriver.java
gate/trunk/src/main/gate/util/xml/XML11StreamWriter.java
Modified: gate/trunk/src/main/gate/corpora/ObjectWrapper.java
===================================================================
--- gate/trunk/src/main/gate/corpora/ObjectWrapper.java 2015-06-04 01:20:31 UTC
(rev 18752)
+++ gate/trunk/src/main/gate/corpora/ObjectWrapper.java 2015-06-04 13:20:25 UTC
(rev 18753)
@@ -15,6 +15,8 @@
*/
package gate.corpora;
+import gate.util.xml.XML11StaxDriver;
+
import java.io.StringWriter;
import org.apache.log4j.Logger;
@@ -73,7 +75,9 @@
*/
@Override
public String toString() {
- XStream xstream = new XStream(new StaxDriver());
+ // Use XML 1.1 in case the XML representation includes characters
+ // that 1.0 forbids (typically control characters).
+ XStream xstream = new XStream(new XML11StaxDriver());
StringWriter out = new StringWriter();
xstream.toXML(this, out);
return out.toString();
Added: gate/trunk/src/main/gate/util/xml/XML11OutputFactory.java
===================================================================
--- gate/trunk/src/main/gate/util/xml/XML11OutputFactory.java
(rev 0)
+++ gate/trunk/src/main/gate/util/xml/XML11OutputFactory.java 2015-06-04
13:20:25 UTC (rev 18753)
@@ -0,0 +1,106 @@
+/*
+ * XML11OutputFactory.java
+ *
+ * Copyright (c) 1995-2015, The University of Sheffield. See the file
+ * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ *
+ * This file is part of GATE (see http://gate.ac.uk/), and is free
+ * software, licenced under the GNU Library General Public License,
+ * Version 2, June 1991 (in the distribution as file licence.html,
+ * and also available at http://gate.ac.uk/gate/licence.html).
+ *
+ * Ian Roberts, 04/Jun/2015
+ *
+ * $Id$
+ */
+package gate.util.xml;
+
+import java.io.OutputStream;
+import java.io.Writer;
+
+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;
+
+/**
+ * Simple delegating XMLOutputFactory that produces stream writers whose
+ * <code>startDocument()</code> method delegates to
+ * <code>startDocument("1.1")</code>, thus producing XML 1.1 instead of
+ * 1.0 by default.
+ */
+public class XML11OutputFactory extends XMLOutputFactory {
+
+ private XMLOutputFactory delegate;
+
+ /**
+ * Create an output factory that generates stream writers that
+ * prefer XML 1.1 instead of 1.0 by default. The underlying
+ * writers are created from the given delegate output factory.
+ */
+ public XML11OutputFactory(XMLOutputFactory delegate) {
+ this.delegate = delegate;
+ }
+
+ /**
+ * Wrap the supplied writer in a wrapper that prefers XML 1.1 over 1.0
+ */
+ protected XMLStreamWriter wrap(XMLStreamWriter writer)
+ throws XMLStreamException {
+ return new XML11StreamWriter(writer);
+ }
+
+ public XMLStreamWriter createXMLStreamWriter(Writer stream)
+ throws XMLStreamException {
+ return wrap(delegate.createXMLStreamWriter(stream));
+ }
+
+ public XMLStreamWriter createXMLStreamWriter(OutputStream stream)
+ throws XMLStreamException {
+ return wrap(delegate.createXMLStreamWriter(stream));
+ }
+
+ public XMLStreamWriter createXMLStreamWriter(OutputStream stream,
+ String encoding) throws XMLStreamException {
+ return wrap(delegate.createXMLStreamWriter(stream, encoding));
+ }
+
+ public XMLStreamWriter createXMLStreamWriter(Result result)
+ throws XMLStreamException {
+ return wrap(delegate.createXMLStreamWriter(result));
+ }
+
+ public XMLEventWriter createXMLEventWriter(Result result)
+ throws XMLStreamException {
+ return delegate.createXMLEventWriter(result);
+ }
+
+ public XMLEventWriter createXMLEventWriter(OutputStream stream)
+ throws XMLStreamException {
+ return delegate.createXMLEventWriter(stream);
+ }
+
+ public XMLEventWriter createXMLEventWriter(OutputStream stream,
+ String encoding) throws XMLStreamException {
+ return delegate.createXMLEventWriter(stream, encoding);
+ }
+
+ public XMLEventWriter createXMLEventWriter(Writer stream)
+ throws XMLStreamException {
+ return delegate.createXMLEventWriter(stream);
+ }
+
+ public void setProperty(String name, Object value)
+ throws IllegalArgumentException {
+ delegate.setProperty(name, value);
+ }
+
+ public Object getProperty(String name) throws IllegalArgumentException {
+ return delegate.getProperty(name);
+ }
+
+ public boolean isPropertySupported(String name) {
+ return delegate.isPropertySupported(name);
+ }
+}
Property changes on: gate/trunk/src/main/gate/util/xml/XML11OutputFactory.java
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: gate/trunk/src/main/gate/util/xml/XML11StaxDriver.java
===================================================================
--- gate/trunk/src/main/gate/util/xml/XML11StaxDriver.java
(rev 0)
+++ gate/trunk/src/main/gate/util/xml/XML11StaxDriver.java 2015-06-04
13:20:25 UTC (rev 18753)
@@ -0,0 +1,36 @@
+/*
+ * XML11StaxDriver.java
+ *
+ * Copyright (c) 1995-2015, The University of Sheffield. See the file
+ * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ *
+ * This file is part of GATE (see http://gate.ac.uk/), and is free
+ * software, licenced under the GNU Library General Public License,
+ * Version 2, June 1991 (in the distribution as file licence.html,
+ * and also available at http://gate.ac.uk/gate/licence.html).
+ *
+ * Ian Roberts, 04/Jun/2015
+ *
+ * $Id$
+ */
+package gate.util.xml;
+
+import gate.corpora.ObjectWrapper;
+
+import javax.xml.stream.XMLOutputFactory;
+
+import com.thoughtworks.xstream.io.xml.StaxDriver;
+
+/**
+ * Simple extension of the XStream StaxDriver to generate XML 1.1 rather
+ * than 1.0. This is used by {@link ObjectWrapper} in case the objects
+ * being wrapped include characters that are not legal in XML 1.0.
+ */
+public class XML11StaxDriver extends StaxDriver {
+
+ @Override
+ protected XMLOutputFactory createOutputFactory() {
+ return new XML11OutputFactory(super.createOutputFactory());
+ }
+
+}
Property changes on: gate/trunk/src/main/gate/util/xml/XML11StaxDriver.java
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: gate/trunk/src/main/gate/util/xml/XML11StreamWriter.java
===================================================================
--- gate/trunk/src/main/gate/util/xml/XML11StreamWriter.java
(rev 0)
+++ gate/trunk/src/main/gate/util/xml/XML11StreamWriter.java 2015-06-04
13:20:25 UTC (rev 18753)
@@ -0,0 +1,178 @@
+/*
+ * XML11StreamWriter.java
+ *
+ * Copyright (c) 1995-2015, The University of Sheffield. See the file
+ * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ *
+ * This file is part of GATE (see http://gate.ac.uk/), and is free
+ * software, licenced under the GNU Library General Public License,
+ * Version 2, June 1991 (in the distribution as file licence.html,
+ * and also available at http://gate.ac.uk/gate/licence.html).
+ *
+ * Ian Roberts, 04/Jun/2015
+ *
+ * $Id$
+ */
+package gate.util.xml;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamWriter;
+
+class XML11StreamWriter implements XMLStreamWriter {
+
+ protected XMLStreamWriter delegate;
+
+ public XML11StreamWriter(XMLStreamWriter delegate) {
+ this.delegate = delegate;
+ }
+
+ /**
+ * Overrides the default behaviour to explicitly select XML version
+ * 1.1 if no version is supplied by the caller.
+ */
+ public void writeStartDocument() throws XMLStreamException {
+ writeStartDocument("1.1");
+ }
+
+ // all remaining methods simply pass through to the delegate
+
+ public void writeStartElement(String localName) throws XMLStreamException {
+ delegate.writeStartElement(localName);
+ }
+
+ public void writeStartElement(String namespaceURI, String localName)
+ throws XMLStreamException {
+ delegate.writeStartElement(namespaceURI, localName);
+ }
+
+ public void writeStartElement(String prefix, String localName,
+ String namespaceURI) throws XMLStreamException {
+ delegate.writeStartElement(prefix, localName, namespaceURI);
+ }
+
+ public void writeEmptyElement(String namespaceURI, String localName)
+ throws XMLStreamException {
+ delegate.writeEmptyElement(namespaceURI, localName);
+ }
+
+ public void writeEmptyElement(String prefix, String localName,
+ String namespaceURI) throws XMLStreamException {
+ delegate.writeEmptyElement(prefix, localName, namespaceURI);
+ }
+
+ public void writeEmptyElement(String localName) throws XMLStreamException {
+ delegate.writeEmptyElement(localName);
+ }
+
+ public void writeEndElement() throws XMLStreamException {
+ delegate.writeEndElement();
+ }
+
+ public void writeEndDocument() throws XMLStreamException {
+ delegate.writeEndDocument();
+ }
+
+ public void close() throws XMLStreamException {
+ delegate.close();
+ }
+
+ public void flush() throws XMLStreamException {
+ delegate.flush();
+ }
+
+ public void writeAttribute(String localName, String value)
+ throws XMLStreamException {
+ delegate.writeAttribute(localName, value);
+ }
+
+ public void writeAttribute(String prefix, String namespaceURI,
+ String localName, String value) throws XMLStreamException {
+ delegate.writeAttribute(prefix, namespaceURI, localName, value);
+ }
+
+ public void writeAttribute(String namespaceURI, String localName, String
value)
+ throws XMLStreamException {
+ delegate.writeAttribute(namespaceURI, localName, value);
+ }
+
+ public void writeNamespace(String prefix, String namespaceURI)
+ throws XMLStreamException {
+ delegate.writeNamespace(prefix, namespaceURI);
+ }
+
+ public void writeDefaultNamespace(String namespaceURI)
+ throws XMLStreamException {
+ delegate.writeDefaultNamespace(namespaceURI);
+ }
+
+ public void writeComment(String data) throws XMLStreamException {
+ delegate.writeComment(data);
+ }
+
+ public void writeProcessingInstruction(String target)
+ throws XMLStreamException {
+ delegate.writeProcessingInstruction(target);
+ }
+
+ public void writeProcessingInstruction(String target, String data)
+ throws XMLStreamException {
+ delegate.writeProcessingInstruction(target, data);
+ }
+
+ public void writeCData(String data) throws XMLStreamException {
+ delegate.writeCData(data);
+ }
+
+ public void writeDTD(String dtd) throws XMLStreamException {
+ delegate.writeDTD(dtd);
+ }
+
+ public void writeEntityRef(String name) throws XMLStreamException {
+ delegate.writeEntityRef(name);
+ }
+
+ public void writeStartDocument(String version) throws XMLStreamException {
+ delegate.writeStartDocument(version);
+ }
+
+ public void writeStartDocument(String encoding, String version)
+ throws XMLStreamException {
+ delegate.writeStartDocument(encoding, version);
+ }
+
+ public void writeCharacters(String text) throws XMLStreamException {
+ delegate.writeCharacters(text);
+ }
+
+ public void writeCharacters(char[] text, int start, int len)
+ throws XMLStreamException {
+ delegate.writeCharacters(text, start, len);
+ }
+
+ public String getPrefix(String uri) throws XMLStreamException {
+ return delegate.getPrefix(uri);
+ }
+
+ public void setPrefix(String prefix, String uri) throws XMLStreamException {
+ delegate.setPrefix(prefix, uri);
+ }
+
+ public void setDefaultNamespace(String uri) throws XMLStreamException {
+ delegate.setDefaultNamespace(uri);
+ }
+
+ public void setNamespaceContext(NamespaceContext context)
+ throws XMLStreamException {
+ delegate.setNamespaceContext(context);
+ }
+
+ public NamespaceContext getNamespaceContext() {
+ return delegate.getNamespaceContext();
+ }
+
+ public Object getProperty(String name) throws IllegalArgumentException {
+ return delegate.getProperty(name);
+ }
+
+}
Property changes on: gate/trunk/src/main/gate/util/xml/XML11StreamWriter.java
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+Id
\ No newline at end of property
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs