Class 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 for XMLStreamWriter 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.
    • 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.
    • Constructor Detail

      • FreeColXMLWriter

        public FreeColXMLWriter​(java.io.OutputStream outputStream,
                                FreeColXMLWriter.WriteScope scope,
                                boolean indent)
                         throws java.io.IOException
        Creates a new FreeColXMLWriter.
        Parameters:
        outputStream - The OutputStream to create an FreeColXMLWriter for.
        scope - The WriteScope 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 the FreeColXMLWriter.
      • FreeColXMLWriter

        public FreeColXMLWriter​(java.io.Writer writer)
                         throws java.io.IOException
        Creates a new FreeColXMLWriter.
        Parameters:
        writer - A Writer to create an FreeColXMLWriter for.
        Throws:
        java.io.IOException - if there is a problem while creating the FreeColXMLWriter.
      • FreeColXMLWriter

        public FreeColXMLWriter​(java.io.Writer writer,
                                FreeColXMLWriter.WriteScope scope)
                         throws java.io.IOException
        Creates a new FreeColXMLWriter.
        Parameters:
        writer - A Writer to create an FreeColXMLWriter for.
        scope - The WriteScope to use for FreeCol objects.
        Throws:
        java.io.IOException - if there is a problem while creating the FreeColXMLWriter.
      • FreeColXMLWriter

        public FreeColXMLWriter​(java.io.Writer writer,
                                FreeColXMLWriter.WriteScope scope,
                                boolean indent)
                         throws java.io.IOException
        Creates a new FreeColXMLWriter.
        Parameters:
        writer - A Writer to create an FreeColXMLWriter for.
        scope - The WriteScope to use for FreeCol objects.
        indent - If true, produce indented output if supported.
        Throws:
        java.io.IOException - if there is a problem while creating the FreeColXMLWriter.
    • Method Detail

      • getFactory

        private javax.xml.stream.XMLOutputFactory getFactory()
        Get the XMLOutputFactory 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 new 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 interface javax.xml.stream.XMLStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface javax.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 - The Enum 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 - The Object 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 - The FreeColObject 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 - The Location 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 interface javax.xml.stream.XMLStreamWriter
      • getPrefix

        public java.lang.String getPrefix​(java.lang.String uri)
                                   throws javax.xml.stream.XMLStreamException
        Specified by:
        getPrefix in interface javax.xml.stream.XMLStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • getProperty

        public java.lang.Object getProperty​(java.lang.String name)
        Specified by:
        getProperty in interface javax.xml.stream.XMLStreamWriter
      • setDefaultNamespace

        public void setDefaultNamespace​(java.lang.String uri)
                                 throws javax.xml.stream.XMLStreamException
        Specified by:
        setDefaultNamespace in interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.xml.stream.XMLStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeEndDocument

        public void writeEndDocument()
                              throws javax.xml.stream.XMLStreamException
        Specified by:
        writeEndDocument in interface javax.xml.stream.XMLStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeEndElement

        public void writeEndElement()
                             throws javax.xml.stream.XMLStreamException
        Specified by:
        writeEndElement in interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.xml.stream.XMLStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException
      • writeStartDocument

        public void writeStartDocument()
                                throws javax.xml.stream.XMLStreamException
        Specified by:
        writeStartDocument in interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.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 interface javax.xml.stream.XMLStreamWriter
        Throws:
        javax.xml.stream.XMLStreamException