de.ispsoft.jaxme
Class XMLSerializer

java.lang.Object
  extended byde.ispsoft.jaxme.XMLSerializer
All Implemented Interfaces:
org.xml.sax.ContentHandler

public class XMLSerializer
extends java.lang.Object
implements org.xml.sax.ContentHandler

A simple serializer for XML documents.

Author:
Jochen Wiedmann, Burkhard Vogel

Field Summary
static int EMPTY_ELEMENT_STYLE_HTML
          Constant for serializing empty elements in HTML style, like this: <elem/>
static int EMPTY_ELEMENT_STYLE_XML
          Constant for serializing empty elements in XML style, like this: <elem/>
 
Constructor Summary
XMLSerializer()
          Creates a new XmlSerializer
XMLSerializer(java.io.Writer pWriter)
          Creates a new XMLSerializer using the given Writer.
XMLSerializer(java.io.Writer pWriter, int pIndent)
          Creates a new XMLSerializer using the given Writer and indentation.
 
Method Summary
 void characters(char[] ch, int start, int length)
          Inserts a string of characters into the document.
 void endDocument()
          This method finishs the handlers action.
 void endElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName)
          Terminates an element.
 void endPrefixMapping(java.lang.String prefix)
          Terminates use of a namespace prefix.
 org.xml.sax.Locator getDocumentLocator()
          Returns the locator
 int getEmptyElementStyle()
          Returns how empty elements will be serialized.
 int getIndent()
          Returns the indent for XML formating.
 java.lang.String getLineFeed()
          Returns the line feed string for XML formating.
 java.io.Writer getWriter()
          Returns the Writer.
 void ignorableWhitespace(char[] ch, int start, int length)
          Calls the character method with the same arguments.
 void processingInstruction(java.lang.String target, java.lang.String data)
          Inserts a processing instruction.
 void setDocumentLocator(org.xml.sax.Locator l)
          Sets the locator.
 void setEmptyElementStyle(int pStyle)
          Sets how empty elements will be serialized.
 void setIndent(int pIndent)
          Sets the indent for XML formating.
 void setLineFeed(java.lang.String pLineFeed)
          Sets the line feed string for XML formating.
 void setWriter(java.io.Writer w)
          Sets the writer.
 void skippedEntity(java.lang.String ent)
          Not actually implemented, because I don't know how to skip entities.
 void startDocument()
          Starts a document.
 void startElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attr)
          Starts a new element.
 void startPrefixMapping(java.lang.String prefix, java.lang.String namespaceURI)
          Starts use of a namespace prefix.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ELEMENT_STYLE_XML

public static final int EMPTY_ELEMENT_STYLE_XML

Constant for serializing empty elements in XML style, like this:

   <elem/>
 

See Also:
getEmptyElementStyle(), setEmptyElementStyle(int), EMPTY_ELEMENT_STYLE_HTML, Constant Field Values

EMPTY_ELEMENT_STYLE_HTML

public static final int EMPTY_ELEMENT_STYLE_HTML

Constant for serializing empty elements in HTML style, like this:

   <elem/>
 

See Also:
getEmptyElementStyle(), setEmptyElementStyle(int), EMPTY_ELEMENT_STYLE_XML, Constant Field Values
Constructor Detail

XMLSerializer

public XMLSerializer()
Creates a new XmlSerializer


XMLSerializer

public XMLSerializer(java.io.Writer pWriter)
Creates a new XMLSerializer using the given Writer.

Parameters:
pWriter - A Writer for which setWriter is being called.
See Also:
setWriter(java.io.Writer)

XMLSerializer

public XMLSerializer(java.io.Writer pWriter,
                     int pIndent)
Creates a new XMLSerializer using the given Writer and indentation.

Parameters:
pWriter - A Writer for which setWriter is being called.
pIndent - The indent level to use for elements.
Method Detail

getIndent

public int getIndent()

Returns the indent for XML formating. The ident is the number of blanks being added in front of every opening element, preceded by a line feed. The behavior more or less resembles standard XML formating.

A special value is 0, which disables indentation.

Returns:
The new indentation level
See Also:
setIndent(int), getLineFeed()

setIndent

public void setIndent(int pIndent)

Sets the indent for XML formating. The ident is the number of blanks being added in front of every opening element, preceded by a line feed. The behavior more or less resembles standard XML formating.

A special value is 0, which disables indentation.

Parameters:
pIndent - The indentation level being used
See Also:
setIndent(int), getLineFeed()

getLineFeed

public java.lang.String getLineFeed()

Returns the line feed string for XML formating. Any closing tag will be followed by this String in order to provide human readable output. Examples: "\r" (Unix line terminator, default) or "\r\n" (Windows line terminator).

A special value is 0, which disables indentation.

Returns:
The line feed String currently used
See Also:
setLineFeed(java.lang.String), getIndent()

setLineFeed

public void setLineFeed(java.lang.String pLineFeed)

Sets the line feed string for XML formating. Any closing tag will be followed by this String in order to provide human readable output. Examples: "\r" (Unix line terminator, default) or "\r\n" (Windows line terminator).

A special value is 0, which disables indentation.

Parameters:
pLineFeed - The new line feed String
See Also:
getLineFeed(), setIndent(int)

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator l)
Sets the locator.

Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler
Parameters:
l - A locator for use in case of errors
See Also:
getDocumentLocator()

getDocumentLocator

public org.xml.sax.Locator getDocumentLocator()
Returns the locator

Returns:
A locator previously set with setDocumentLocator or null.
See Also:
setDocumentLocator(org.xml.sax.Locator)

setWriter

public void setWriter(java.io.Writer w)
Sets the writer. The XML document is serialized by writing into this Writer. This method must be called before calling setDocument.

Parameters:
w - The destination Writer
See Also:
getWriter()

getWriter

public java.io.Writer getWriter()
Returns the Writer. The XML document is serialized by writing into this Writer.

Returns:
A Writer previously set with setWriter
See Also:
setWriter(java.io.Writer)

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String namespaceURI)
                        throws org.xml.sax.SAXException

Starts use of a namespace prefix.

Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Parameters:
namespaceURI - The namespace URI
prefix - The prefix
Throws:
org.xml.sax.SAXException - Not actually thrown, just for compliance to the interface specification.

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws org.xml.sax.SAXException

Terminates use of a namespace prefix.

Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Parameters:
prefix - The prefix being abandoned.
Throws:
org.xml.sax.SAXException - Not actually thrown, just for compliance to the interface specification.

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException

Starts a document.

Specified by:
startDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException - Not actually thrown, just for compliance to the interface specification.

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException

This method finishs the handlers action. After calling endDocument you may start a new action by calling startDocument again.

Specified by:
endDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException - Not actually thrown, just for compliance to the interface specification.

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
Calls the character method with the same arguments.

Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Parameters:
ch - A string of whitespace characters being inserted into the document.
start - The index of the first character.
length - The number of characters.
Throws:
org.xml.sax.SAXException - Thrown in case of an IOException.

characters

public void characters(char[] ch,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Inserts a string of characters into the document.

Specified by:
characters in interface org.xml.sax.ContentHandler
Parameters:
ch - The characters being inserted. A substring, to be precise.
start - Index of the first character
length - Number of characters being inserted
Throws:
org.xml.sax.SAXException - Thrown in case of an IOException

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException

Terminates an element.

Specified by:
endElement in interface org.xml.sax.ContentHandler
Parameters:
namespaceURI - The namespace URI, if any, or null
localName - The local name, without prefix, or null
qName - The qualified name, including a prefix, or null
Throws:
org.xml.sax.SAXException - Thrown in case of an IOException.

startElement

public void startElement(java.lang.String namespaceURI,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes attr)
                  throws org.xml.sax.SAXException
Starts a new element.

Specified by:
startElement in interface org.xml.sax.ContentHandler
Parameters:
namespaceURI - The namespace URI, if any, or null
localName - The local name, without prefix, or null
qName - The qualified name, including a prefix, or null
attr - The element attributes
Throws:
org.xml.sax.SAXException - Thrown in case of an IOException.

skippedEntity

public void skippedEntity(java.lang.String ent)
                   throws org.xml.sax.SAXException
Not actually implemented, because I don't know how to skip entities.

Specified by:
skippedEntity in interface org.xml.sax.ContentHandler
Parameters:
ent - The entity being skipped.
Throws:
org.xml.sax.SAXException - Not actually thrown, just for compliance to the interface specification.

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Inserts a processing instruction.

Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Parameters:
target - The PI target
data - The PI data
Throws:
org.xml.sax.SAXException - Thrown in case of an IOException

getEmptyElementStyle

public int getEmptyElementStyle()

Returns how empty elements will be serialized.

Returns:
Either of EMPTY_ELEMENT_STYLE_XML (default) or EMPTY_ELEMENT_STYLE_HTML
See Also:
setEmptyElementStyle(int)

setEmptyElementStyle

public void setEmptyElementStyle(int pStyle)

Sets how empty elements will be serialized.

Parameters:
pStyle - Either of EMPTY_ELEMENT_STYLE_XML or EMPTY_ELEMENT_STYLE_HTML.
See Also:
getEmptyElementStyle()