Package net.sf.freecol.common.io
Class FreeColXMLWriter
- java.lang.Object
-
- net.sf.freecol.common.io.FreeColXMLWriter
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,javax.xml.stream.XMLStreamWriter
public class FreeColXMLWriter extends java.lang.Object implements java.io.Closeable, javax.xml.stream.XMLStreamWriter
A wrapper forXMLStreamWriter
and potentially an underlying stream. Adds on many useful utilities for writing XML and FreeCol values. Unlike FreeColXMLReader, do not try to close the underlying stream. Sometimes items are saved with successive FreeColXMLWriters writing to the same OutputStream. Strange, there is no StreamWriterDelegate, so we are stuck with all the delegation functions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FreeColXMLWriter.WriteScope
static class
FreeColXMLWriter.WriteScopeType
The scope of a FreeCol object write.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Logger
logger
private java.io.Writer
outputWriter
The writer that receives the final output.private java.io.StringWriter
stringWriter
An internal writer to accumulate XML into.private javax.xml.transform.Transformer
transformer
An optional transformer to handle indentation.private FreeColXMLWriter.WriteScope
writeScope
A write scope to use for FreeCol object writes.private javax.xml.stream.XMLStreamWriter
xmlStreamWriter
The internal XML writer to write XML to.
-
Constructor Summary
Constructors Constructor Description FreeColXMLWriter(java.io.OutputStream outputStream, FreeColXMLWriter.WriteScope scope, boolean indent)
Creates a newFreeColXMLWriter
.FreeColXMLWriter(java.io.Writer writer)
Creates a newFreeColXMLWriter
.FreeColXMLWriter(java.io.Writer writer, FreeColXMLWriter.WriteScope scope)
Creates a newFreeColXMLWriter
.FreeColXMLWriter(java.io.Writer writer, FreeColXMLWriter.WriteScope scope, boolean indent)
Creates a newFreeColXMLWriter
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
void
flush()
java.lang.StringBuffer
flushBuffer()
Internal flush, returning what was written.Player
getClientPlayer()
private javax.xml.stream.XMLOutputFactory
getFactory()
Get theXMLOutputFactory
to create the output stream with.javax.xml.namespace.NamespaceContext
getNamespaceContext()
java.lang.String
getPrefix(java.lang.String uri)
java.lang.Object
getProperty(java.lang.String name)
FreeColXMLWriter.WriteScope
getWriteScope()
Get the write scope prevailing on this stream.FreeColXMLWriter.WriteScope
replaceScope(FreeColXMLWriter.WriteScope newWriteScope)
Replace the scope.void
setDefaultNamespace(java.lang.String uri)
void
setNamespaceContext(javax.xml.namespace.NamespaceContext context)
void
setPrefix(java.lang.String prefix, java.lang.String uri)
void
setWriteScope(FreeColXMLWriter.WriteScope writeScope)
Set the write scope prevailing on this stream.boolean
validFor(Player player)
boolean
validForSave()
void
writeAttribute(java.lang.String attributeName, boolean value)
Write a boolean attribute to the stream.void
writeAttribute(java.lang.String attributeName, float value)
Write a float attribute to the stream.void
writeAttribute(java.lang.String attributeName, int value)
Write an integer attribute to the stream.void
writeAttribute(java.lang.String attributeName, long value)
Write a long attribute to the stream.void
writeAttribute(java.lang.String attributeName, java.lang.Enum<?> value)
Write an enum attribute to the stream.void
writeAttribute(java.lang.String attributeName, java.lang.Object value)
Write an Object attribute to the stream.void
writeAttribute(java.lang.String localName, java.lang.String value)
void
writeAttribute(java.lang.String namespaceURI, java.lang.String localName, java.lang.String value)
void
writeAttribute(java.lang.String prefix, java.lang.String namespaceURI, java.lang.String localName, java.lang.String value)
void
writeAttribute(java.lang.String attributeName, FreeColObject value)
Write the identifier attribute of a non-null FreeColObject to the stream.void
writeCData(java.lang.String data)
void
writeCharacters(char[] text, int start, int len)
void
writeCharacters(java.lang.String text)
void
writeComment(java.lang.String data)
void
writeDefaultNamespace(java.lang.String namespaceURI)
void
writeDTD(java.lang.String dtd)
void
writeEmptyElement(java.lang.String localName)
void
writeEmptyElement(java.lang.String namespaceURI, java.lang.String localName)
void
writeEmptyElement(java.lang.String prefix, java.lang.String localName, java.lang.String namespaceURI)
void
writeEndDocument()
void
writeEndElement()
void
writeEntityRef(java.lang.String name)
void
writeLocationAttribute(java.lang.String attributeName, Location value)
Write the identifier attribute of a non-null Location to the stream.void
writeNamespace(java.lang.String prefix, java.lang.String namespaceURI)
void
writeProcessingInstruction(java.lang.String target)
void
writeProcessingInstruction(java.lang.String target, java.lang.String data)
void
writeStartDocument()
void
writeStartDocument(java.lang.String version)
void
writeStartDocument(java.lang.String encoding, java.lang.String version)
void
writeStartElement(java.lang.String localName)
void
writeStartElement(java.lang.String namespaceURI, java.lang.String localName)
void
writeStartElement(java.lang.String prefix, java.lang.String localName, java.lang.String namespaceURI)
<T extends FreeColObject>
voidwriteToListElement(java.lang.String tag, java.util.Collection<T> members)
Writes an XML-representation of a collection object to the given stream.
-
-
-
Field Detail
-
logger
private static final java.util.logging.Logger logger
-
xmlStreamWriter
private final javax.xml.stream.XMLStreamWriter xmlStreamWriter
The internal XML writer to write XML to.
-
stringWriter
private final java.io.StringWriter stringWriter
An internal writer to accumulate XML into.
-
transformer
private final javax.xml.transform.Transformer transformer
An optional transformer to handle indentation.
-
outputWriter
private final java.io.Writer outputWriter
The writer that receives the final output.
-
writeScope
private FreeColXMLWriter.WriteScope writeScope
A write scope to use for FreeCol object writes.
-
-
Constructor Detail
-
FreeColXMLWriter
public FreeColXMLWriter(java.io.OutputStream outputStream, FreeColXMLWriter.WriteScope scope, boolean indent) throws java.io.IOException
Creates a newFreeColXMLWriter
.- Parameters:
outputStream
- TheOutputStream
to create anFreeColXMLWriter
for.scope
- TheWriteScope
to use for FreeCol object writes.indent
- If true, produce indented output if supported.- Throws:
java.io.IOException
- if there is a problem while creating theFreeColXMLWriter
.
-
FreeColXMLWriter
public FreeColXMLWriter(java.io.Writer writer) throws java.io.IOException
Creates a newFreeColXMLWriter
.- Parameters:
writer
- AWriter
to create anFreeColXMLWriter
for.- Throws:
java.io.IOException
- if there is a problem while creating theFreeColXMLWriter
.
-
FreeColXMLWriter
public FreeColXMLWriter(java.io.Writer writer, FreeColXMLWriter.WriteScope scope) throws java.io.IOException
Creates a newFreeColXMLWriter
.- Parameters:
writer
- AWriter
to create anFreeColXMLWriter
for.scope
- TheWriteScope
to use for FreeCol objects.- Throws:
java.io.IOException
- if there is a problem while creating theFreeColXMLWriter
.
-
FreeColXMLWriter
public FreeColXMLWriter(java.io.Writer writer, FreeColXMLWriter.WriteScope scope, boolean indent) throws java.io.IOException
Creates a newFreeColXMLWriter
.- Parameters:
writer
- AWriter
to create anFreeColXMLWriter
for.scope
- TheWriteScope
to use for FreeCol objects.indent
- If true, produce indented output if supported.- Throws:
java.io.IOException
- if there is a problem while creating theFreeColXMLWriter
.
-
-
Method Detail
-
getFactory
private javax.xml.stream.XMLOutputFactory getFactory()
Get theXMLOutputFactory
to create the output stream with.- Returns:
- An
XMLOutputFactory
.
-
getWriteScope
public FreeColXMLWriter.WriteScope getWriteScope()
Get the write scope prevailing on this stream.- Returns:
- The write scope.
-
setWriteScope
public void setWriteScope(FreeColXMLWriter.WriteScope writeScope)
Set the write scope prevailing on this stream.- Parameters:
writeScope
- The newWriteScope
.
-
replaceScope
public FreeColXMLWriter.WriteScope replaceScope(FreeColXMLWriter.WriteScope newWriteScope)
Replace the scope.- Parameters:
newWriteScope
- TheWriteScope
to push.- Returns:
- The previous
WriteScope
.
-
flushBuffer
public java.lang.StringBuffer flushBuffer() throws javax.xml.stream.XMLStreamException
Internal flush, returning what was written.- Returns:
- The internal buffer containing the flushed data.
- Throws:
javax.xml.stream.XMLStreamException
- on stream error.
-
flush
public void flush() throws javax.xml.stream.XMLStreamException
- Specified by:
flush
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfacejavax.xml.stream.XMLStreamWriter
-
writeAttribute
public void writeAttribute(java.lang.String attributeName, boolean value) throws javax.xml.stream.XMLStreamException
Write a boolean attribute to the stream.- Parameters:
attributeName
- The attribute name.value
- A boolean to write.- Throws:
javax.xml.stream.XMLStreamException
- if a write error occurs.
-
writeAttribute
public void writeAttribute(java.lang.String attributeName, float value) throws javax.xml.stream.XMLStreamException
Write a float attribute to the stream.- Parameters:
attributeName
- The attribute name.value
- A float to write.- Throws:
javax.xml.stream.XMLStreamException
- if a write error occurs.
-
writeAttribute
public void writeAttribute(java.lang.String attributeName, int value) throws javax.xml.stream.XMLStreamException
Write an integer attribute to the stream.- Parameters:
attributeName
- The attribute name.value
- An integer to write.- Throws:
javax.xml.stream.XMLStreamException
- if a write error occurs.
-
writeAttribute
public void writeAttribute(java.lang.String attributeName, long value) throws javax.xml.stream.XMLStreamException
Write a long attribute to the stream.- Parameters:
attributeName
- The attribute name.value
- A long to write.- Throws:
javax.xml.stream.XMLStreamException
- if a write error occurs.
-
writeAttribute
public void writeAttribute(java.lang.String attributeName, java.lang.Enum<?> value) throws javax.xml.stream.XMLStreamException
Write an enum attribute to the stream.- Parameters:
attributeName
- The attribute name.value
- TheEnum
to write.- Throws:
javax.xml.stream.XMLStreamException
- if a write error occurs.
-
writeAttribute
public void writeAttribute(java.lang.String attributeName, java.lang.Object value) throws javax.xml.stream.XMLStreamException
Write an Object attribute to the stream.- Parameters:
attributeName
- The attribute name.value
- TheObject
to write.- Throws:
javax.xml.stream.XMLStreamException
- if a write error occurs.
-
writeAttribute
public void writeAttribute(java.lang.String attributeName, FreeColObject value) throws javax.xml.stream.XMLStreamException
Write the identifier attribute of a non-null FreeColObject to the stream.- Parameters:
attributeName
- The attribute name.value
- TheFreeColObject
to write the identifier of.- Throws:
javax.xml.stream.XMLStreamException
- if a write error occurs.
-
writeLocationAttribute
public void writeLocationAttribute(java.lang.String attributeName, Location value) throws javax.xml.stream.XMLStreamException
Write the identifier attribute of a non-null Location to the stream.- Parameters:
attributeName
- The attribute name.value
- TheLocation
to write the identifier of.- Throws:
javax.xml.stream.XMLStreamException
- if a write error occurs.
-
writeToListElement
public <T extends FreeColObject> void writeToListElement(java.lang.String tag, java.util.Collection<T> members) throws javax.xml.stream.XMLStreamException
Writes an XML-representation of a collection object to the given stream.- Type Parameters:
T
- The collection type.- Parameters:
tag
- The tag for the array.members
- The members of the array.- Throws:
javax.xml.stream.XMLStreamException
- if a problem was encountered while writing.
-
getClientPlayer
public Player getClientPlayer()
-
validForSave
public boolean validForSave()
-
validFor
public boolean validFor(Player player)
-
getNamespaceContext
public javax.xml.namespace.NamespaceContext getNamespaceContext()
- Specified by:
getNamespaceContext
in interfacejavax.xml.stream.XMLStreamWriter
-
getPrefix
public java.lang.String getPrefix(java.lang.String uri) throws javax.xml.stream.XMLStreamException
- Specified by:
getPrefix
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
getProperty
public java.lang.Object getProperty(java.lang.String name)
- Specified by:
getProperty
in interfacejavax.xml.stream.XMLStreamWriter
-
setDefaultNamespace
public void setDefaultNamespace(java.lang.String uri) throws javax.xml.stream.XMLStreamException
- Specified by:
setDefaultNamespace
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
setNamespaceContext
public void setNamespaceContext(javax.xml.namespace.NamespaceContext context) throws javax.xml.stream.XMLStreamException
- Specified by:
setNamespaceContext
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
setPrefix
public void setPrefix(java.lang.String prefix, java.lang.String uri) throws javax.xml.stream.XMLStreamException
- Specified by:
setPrefix
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeAttribute
public void writeAttribute(java.lang.String localName, java.lang.String value) throws javax.xml.stream.XMLStreamException
- Specified by:
writeAttribute
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeAttribute
public void writeAttribute(java.lang.String namespaceURI, java.lang.String localName, java.lang.String value) throws javax.xml.stream.XMLStreamException
- Specified by:
writeAttribute
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeAttribute
public void writeAttribute(java.lang.String prefix, java.lang.String namespaceURI, java.lang.String localName, java.lang.String value) throws javax.xml.stream.XMLStreamException
- Specified by:
writeAttribute
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeCData
public void writeCData(java.lang.String data) throws javax.xml.stream.XMLStreamException
- Specified by:
writeCData
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeCharacters
public void writeCharacters(char[] text, int start, int len) throws javax.xml.stream.XMLStreamException
- Specified by:
writeCharacters
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeCharacters
public void writeCharacters(java.lang.String text) throws javax.xml.stream.XMLStreamException
- Specified by:
writeCharacters
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeComment
public void writeComment(java.lang.String data) throws javax.xml.stream.XMLStreamException
- Specified by:
writeComment
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeDefaultNamespace
public void writeDefaultNamespace(java.lang.String namespaceURI) throws javax.xml.stream.XMLStreamException
- Specified by:
writeDefaultNamespace
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeDTD
public void writeDTD(java.lang.String dtd) throws javax.xml.stream.XMLStreamException
- Specified by:
writeDTD
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(java.lang.String localName) throws javax.xml.stream.XMLStreamException
- Specified by:
writeEmptyElement
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(java.lang.String namespaceURI, java.lang.String localName) throws javax.xml.stream.XMLStreamException
- Specified by:
writeEmptyElement
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(java.lang.String prefix, java.lang.String localName, java.lang.String namespaceURI) throws javax.xml.stream.XMLStreamException
- Specified by:
writeEmptyElement
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeEndDocument
public void writeEndDocument() throws javax.xml.stream.XMLStreamException
- Specified by:
writeEndDocument
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeEndElement
public void writeEndElement() throws javax.xml.stream.XMLStreamException
- Specified by:
writeEndElement
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeEntityRef
public void writeEntityRef(java.lang.String name) throws javax.xml.stream.XMLStreamException
- Specified by:
writeEntityRef
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeNamespace
public void writeNamespace(java.lang.String prefix, java.lang.String namespaceURI) throws javax.xml.stream.XMLStreamException
- Specified by:
writeNamespace
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeProcessingInstruction
public void writeProcessingInstruction(java.lang.String target) throws javax.xml.stream.XMLStreamException
- Specified by:
writeProcessingInstruction
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeProcessingInstruction
public void writeProcessingInstruction(java.lang.String target, java.lang.String data) throws javax.xml.stream.XMLStreamException
- Specified by:
writeProcessingInstruction
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeStartDocument
public void writeStartDocument() throws javax.xml.stream.XMLStreamException
- Specified by:
writeStartDocument
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeStartDocument
public void writeStartDocument(java.lang.String version) throws javax.xml.stream.XMLStreamException
- Specified by:
writeStartDocument
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeStartDocument
public void writeStartDocument(java.lang.String encoding, java.lang.String version) throws javax.xml.stream.XMLStreamException
- Specified by:
writeStartDocument
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeStartElement
public void writeStartElement(java.lang.String localName) throws javax.xml.stream.XMLStreamException
- Specified by:
writeStartElement
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeStartElement
public void writeStartElement(java.lang.String namespaceURI, java.lang.String localName) throws javax.xml.stream.XMLStreamException
- Specified by:
writeStartElement
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
writeStartElement
public void writeStartElement(java.lang.String prefix, java.lang.String localName, java.lang.String namespaceURI) throws javax.xml.stream.XMLStreamException
- Specified by:
writeStartElement
in interfacejavax.xml.stream.XMLStreamWriter
- Throws:
javax.xml.stream.XMLStreamException
-
-