de.mguennewig.pobjimport
Class XmlWriter

java.lang.Object
  extended by de.mguennewig.pobjimport.XmlWriter

public class XmlWriter
extends java.lang.Object

Simple class to generate an XML file on the fly.

Author:
Michael Günnewig

Constructor Summary
XmlWriter(java.io.PrintWriter out)
          Creates a new XML writer with mixed content for all elements.
XmlWriter(java.io.PrintWriter out, java.lang.String[] elementContent)
          Creates a new XML writer with element-only content for the specified elements.
 
Method Summary
 void addProlog(java.lang.String encoding, boolean standalone, java.lang.String doctypeDecl)
          Adds the XML prolog to the stream.
 void addProlog(java.lang.String encoding, boolean standalone, java.lang.String publicId, java.lang.String systemId)
          Adds the XML prolog to the stream.
 void attr(java.lang.String name, java.lang.String value)
          Adds attribute name to the current open element.
 void cdata(java.io.InputStream in)
          Generates a CDATA section from the given stream.
 void cdata(java.io.Reader reader)
          Generates a CDATA section from the given reader.
 void cdata(java.lang.String cdata)
          Generates a CDATA section.
 void close()
          Closes the XML writer and the print writer.
 void comment(java.lang.String comment)
          Generates a COMMENT section.
 void end()
          Closes the current open tag.
 void end(java.lang.String tagName)
          Closes the current open tag tagName.
 java.io.PrintWriter getPrintWriter()
          Returns the used print writer.
 boolean isElementContent(java.lang.String name)
          Tests whether the tag name uses the element-only model.
 void newLine()
          Adds a new line into the XML document.
 void start(java.lang.String tagName)
          Opens a tag tagName.
 void start(java.lang.String tagName, boolean elementContent)
          Opens a tag tagName with specified mixed content mode.
 void text(java.lang.String text)
          Writes text data to the current open element.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlWriter

public XmlWriter(java.io.PrintWriter out)
Creates a new XML writer with mixed content for all elements.


XmlWriter

public XmlWriter(java.io.PrintWriter out,
                 java.lang.String[] elementContent)
Creates a new XML writer with element-only content for the specified elements.

Method Detail

addProlog

public void addProlog(java.lang.String encoding,
                      boolean standalone,
                      java.lang.String publicId,
                      java.lang.String systemId)
Adds the XML prolog to the stream.

NOTE: This method must only be called once and before every any other data is written.

Parameters:
encoding - the encoding used for the XML file. This does not change the used encoding, but only states it for others.
standalone - iff true no external entities are used
publicId - optional PUBLIC identifier
systemId - optional SYSTEM identifier. THis is mandatory if a PUBLIC identifier is given
See Also:
addProlog(String,boolean,String)

addProlog

public void addProlog(java.lang.String encoding,
                      boolean standalone,
                      java.lang.String doctypeDecl)
Adds the XML prolog to the stream.

NOTE: This method must only be called once and before every any other data is written.

Parameters:
encoding - states the encoding used for the XML file. This does not change the used encoding, but only states it for others.
standalone - iff true no external entities are used
doctypeDecl - optional DOCTYPE declaration

close

public void close()
Closes the XML writer and the print writer.


getPrintWriter

public final java.io.PrintWriter getPrintWriter()
Returns the used print writer.


isElementContent

public boolean isElementContent(java.lang.String name)
Tests whether the tag name uses the element-only model.

See Also:
XmlWriter(PrintWriter,String[])

start

public final void start(java.lang.String tagName,
                        boolean elementContent)
Opens a tag tagName with specified mixed content mode.

Parameters:
tagName - the tag name that should be opened
elementContent - iff true this tag uses the element-only model and indentation will be done, otherwise mixed content is assumed.

start

public final void start(java.lang.String tagName)
Opens a tag tagName.

See Also:
start(String,boolean), isElementContent(String)

attr

public final void attr(java.lang.String name,
                       java.lang.String value)
Adds attribute name to the current open element.

Throws:
java.lang.IllegalStateException - if already content has been added to the current element, like text or sub-elements.

end

public final void end()
Closes the current open tag.

Throws:
java.lang.IllegalStateException - if no open tag exists.

end

public final void end(java.lang.String tagName)
Closes the current open tag tagName.

Throws:
java.lang.IllegalStateException - if no open tag exists or is not tagName
See Also:
end()

newLine

public final void newLine()
Adds a new line into the XML document.


text

public final void text(java.lang.String text)
Writes text data to the current open element.


cdata

public final void cdata(java.lang.String cdata)
Generates a CDATA section.

Throws:
java.lang.IllegalArgumentException - if cdata is null or contains ]]>.

cdata

public final void cdata(java.io.Reader reader)
                 throws java.io.IOException
Generates a CDATA section from the given reader.

NOTE: The stream is not checked for the forbidden character sequence ]]>.

Throws:
java.io.IOException - if an IO error occurs

cdata

public final void cdata(java.io.InputStream in)
                 throws java.io.IOException
Generates a CDATA section from the given stream.

The data is encoded in hex which doubles the required size.

Throws:
java.io.IOException - if an IO error occurs

comment

public final void comment(java.lang.String comment)
Generates a COMMENT section.