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.XMLStreamWriterA wrapper forXMLStreamWriterand 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 classFreeColXMLWriter.WriteScopestatic classFreeColXMLWriter.WriteScopeTypeThe scope of a FreeCol object write.
-
Field Summary
Fields Modifier and Type Field Description private static java.util.logging.Loggerloggerprivate java.io.WriteroutputWriterThe writer that receives the final output.private java.io.StringWriterstringWriterAn internal writer to accumulate XML into.private javax.xml.transform.TransformertransformerAn optional transformer to handle indentation.private FreeColXMLWriter.WriteScopewriteScopeA write scope to use for FreeCol object writes.private javax.xml.stream.XMLStreamWriterxmlStreamWriterThe 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 voidclose()voidflush()java.lang.StringBufferflushBuffer()Internal flush, returning what was written.PlayergetClientPlayer()private javax.xml.stream.XMLOutputFactorygetFactory()Get theXMLOutputFactoryto create the output stream with.javax.xml.namespace.NamespaceContextgetNamespaceContext()java.lang.StringgetPrefix(java.lang.String uri)java.lang.ObjectgetProperty(java.lang.String name)FreeColXMLWriter.WriteScopegetWriteScope()Get the write scope prevailing on this stream.FreeColXMLWriter.WriteScopereplaceScope(FreeColXMLWriter.WriteScope newWriteScope)Replace the scope.voidsetDefaultNamespace(java.lang.String uri)voidsetNamespaceContext(javax.xml.namespace.NamespaceContext context)voidsetPrefix(java.lang.String prefix, java.lang.String uri)voidsetWriteScope(FreeColXMLWriter.WriteScope writeScope)Set the write scope prevailing on this stream.booleanvalidFor(Player player)booleanvalidForSave()voidwriteAttribute(java.lang.String attributeName, boolean value)Write a boolean attribute to the stream.voidwriteAttribute(java.lang.String attributeName, float value)Write a float attribute to the stream.voidwriteAttribute(java.lang.String attributeName, int value)Write an integer attribute to the stream.voidwriteAttribute(java.lang.String attributeName, long value)Write a long attribute to the stream.voidwriteAttribute(java.lang.String attributeName, java.lang.Enum<?> value)Write an enum attribute to the stream.voidwriteAttribute(java.lang.String attributeName, java.lang.Object value)Write an Object attribute to the stream.voidwriteAttribute(java.lang.String localName, java.lang.String value)voidwriteAttribute(java.lang.String namespaceURI, java.lang.String localName, java.lang.String value)voidwriteAttribute(java.lang.String prefix, java.lang.String namespaceURI, java.lang.String localName, java.lang.String value)voidwriteAttribute(java.lang.String attributeName, FreeColObject value)Write the identifier attribute of a non-null FreeColObject to the stream.voidwriteCData(java.lang.String data)voidwriteCharacters(char[] text, int start, int len)voidwriteCharacters(java.lang.String text)voidwriteComment(java.lang.String data)voidwriteDefaultNamespace(java.lang.String namespaceURI)voidwriteDTD(java.lang.String dtd)voidwriteEmptyElement(java.lang.String localName)voidwriteEmptyElement(java.lang.String namespaceURI, java.lang.String localName)voidwriteEmptyElement(java.lang.String prefix, java.lang.String localName, java.lang.String namespaceURI)voidwriteEndDocument()voidwriteEndElement()voidwriteEntityRef(java.lang.String name)voidwriteLocationAttribute(java.lang.String attributeName, Location value)Write the identifier attribute of a non-null Location to the stream.voidwriteNamespace(java.lang.String prefix, java.lang.String namespaceURI)voidwriteProcessingInstruction(java.lang.String target)voidwriteProcessingInstruction(java.lang.String target, java.lang.String data)voidwriteStartDocument()voidwriteStartDocument(java.lang.String version)voidwriteStartDocument(java.lang.String encoding, java.lang.String version)voidwriteStartElement(java.lang.String localName)voidwriteStartElement(java.lang.String namespaceURI, java.lang.String localName)voidwriteStartElement(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.IOExceptionCreates a newFreeColXMLWriter.- Parameters:
outputStream- TheOutputStreamto create anFreeColXMLWriterfor.scope- TheWriteScopeto 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.IOExceptionCreates a newFreeColXMLWriter.- Parameters:
writer- AWriterto create anFreeColXMLWriterfor.- 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.IOExceptionCreates a newFreeColXMLWriter.- Parameters:
writer- AWriterto create anFreeColXMLWriterfor.scope- TheWriteScopeto 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.IOExceptionCreates a newFreeColXMLWriter.- Parameters:
writer- AWriterto create anFreeColXMLWriterfor.scope- TheWriteScopeto 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 theXMLOutputFactoryto 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- TheWriteScopeto push.- Returns:
- The previous
WriteScope.
-
flushBuffer
public java.lang.StringBuffer flushBuffer() throws javax.xml.stream.XMLStreamExceptionInternal 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:
flushin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfacejavax.xml.stream.XMLStreamWriter
-
writeAttribute
public void writeAttribute(java.lang.String attributeName, boolean value) throws javax.xml.stream.XMLStreamExceptionWrite 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.XMLStreamExceptionWrite 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.XMLStreamExceptionWrite 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.XMLStreamExceptionWrite 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.XMLStreamExceptionWrite an enum attribute to the stream.- Parameters:
attributeName- The attribute name.value- TheEnumto 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.XMLStreamExceptionWrite an Object attribute to the stream.- Parameters:
attributeName- The attribute name.value- TheObjectto 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.XMLStreamExceptionWrite the identifier attribute of a non-null FreeColObject to the stream.- Parameters:
attributeName- The attribute name.value- TheFreeColObjectto 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.XMLStreamExceptionWrite the identifier attribute of a non-null Location to the stream.- Parameters:
attributeName- The attribute name.value- TheLocationto 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:
getNamespaceContextin interfacejavax.xml.stream.XMLStreamWriter
-
getPrefix
public java.lang.String getPrefix(java.lang.String uri) throws javax.xml.stream.XMLStreamException- Specified by:
getPrefixin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
getProperty
public java.lang.Object getProperty(java.lang.String name)
- Specified by:
getPropertyin interfacejavax.xml.stream.XMLStreamWriter
-
setDefaultNamespace
public void setDefaultNamespace(java.lang.String uri) throws javax.xml.stream.XMLStreamException- Specified by:
setDefaultNamespacein 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:
setNamespaceContextin 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:
setPrefixin 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:
writeAttributein 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:
writeAttributein 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:
writeAttributein interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeCData
public void writeCData(java.lang.String data) throws javax.xml.stream.XMLStreamException- Specified by:
writeCDatain 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:
writeCharactersin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeCharacters
public void writeCharacters(java.lang.String text) throws javax.xml.stream.XMLStreamException- Specified by:
writeCharactersin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeComment
public void writeComment(java.lang.String data) throws javax.xml.stream.XMLStreamException- Specified by:
writeCommentin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeDefaultNamespace
public void writeDefaultNamespace(java.lang.String namespaceURI) throws javax.xml.stream.XMLStreamException- Specified by:
writeDefaultNamespacein interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeDTD
public void writeDTD(java.lang.String dtd) throws javax.xml.stream.XMLStreamException- Specified by:
writeDTDin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeEmptyElement
public void writeEmptyElement(java.lang.String localName) throws javax.xml.stream.XMLStreamException- Specified by:
writeEmptyElementin 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:
writeEmptyElementin 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:
writeEmptyElementin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeEndDocument
public void writeEndDocument() throws javax.xml.stream.XMLStreamException- Specified by:
writeEndDocumentin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeEndElement
public void writeEndElement() throws javax.xml.stream.XMLStreamException- Specified by:
writeEndElementin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeEntityRef
public void writeEntityRef(java.lang.String name) throws javax.xml.stream.XMLStreamException- Specified by:
writeEntityRefin 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:
writeNamespacein interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeProcessingInstruction
public void writeProcessingInstruction(java.lang.String target) throws javax.xml.stream.XMLStreamException- Specified by:
writeProcessingInstructionin 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:
writeProcessingInstructionin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeStartDocument
public void writeStartDocument() throws javax.xml.stream.XMLStreamException- Specified by:
writeStartDocumentin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeStartDocument
public void writeStartDocument(java.lang.String version) throws javax.xml.stream.XMLStreamException- Specified by:
writeStartDocumentin 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:
writeStartDocumentin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
writeStartElement
public void writeStartElement(java.lang.String localName) throws javax.xml.stream.XMLStreamException- Specified by:
writeStartElementin 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:
writeStartElementin 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:
writeStartElementin interfacejavax.xml.stream.XMLStreamWriter- Throws:
javax.xml.stream.XMLStreamException
-
-