Framework Professional Edition Package


com.prosyst.util.xml
Class XmlSerializer

java.lang.Object
  extended by com.prosyst.util.xml.XmlSerializer

public class XmlSerializer
extends java.lang.Object

XmlSerializer is an utility class, that simplifies creation of XML files. It is designed to be similar to the XML Pull API serializer.

Author:
Valentin Valchev

Constructor Summary
XmlSerializer()
           
 
Method Summary
 XmlSerializer attribute(java.lang.String localName, java.lang.String value)
          Write an attribute out for the current entity.
 XmlSerializer cdsect(java.lang.String text)
          Writes raw, CDATA section
 XmlSerializer comment(java.lang.String text)
          Writes XML comment code
 XmlSerializer docdecl(java.lang.String text)
          Writes DOCTYPE declaration.
 void endDocument()
          Close this writer.
 XmlSerializer endTag()
          Deprecated. see endTag(String)
 XmlSerializer endTag(java.lang.String aName)
          End the current entity.
 int getDepth()
          Returns the current depth of the element.
 void setIndent(boolean indent)
          Enables/disables indentation
 void setOutput(java.io.OutputStream os, java.lang.String encoding)
          Set to use binary output stream with given encoding.
 XmlSerializer setOutput(java.io.Writer writer)
          Opens and initializes XML output on top of existing Writer.
 XmlSerializer startDocument(java.lang.String encoding, java.lang.Boolean standalone)
          Writes XML prolog code:
 XmlSerializer startTag(java.lang.String name)
          Begin to output an entity.
 XmlSerializer text(java.lang.String text)
          Output body text.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlSerializer

public XmlSerializer()
Method Detail

setOutput

public XmlSerializer setOutput(java.io.Writer writer)
Opens and initializes XML output on top of existing Writer.

Parameters:
writer - the output writer
Returns:
self

setOutput

public void setOutput(java.io.OutputStream os,
                      java.lang.String encoding)
               throws java.io.IOException
Set to use binary output stream with given encoding.

Parameters:
os - the output stream
encoding - the output encoding
Throws:
java.io.IOException
java.lang.NullPointerException - is output stream is null
See Also:
setOutput(Writer)

startDocument

public XmlSerializer startDocument(java.lang.String encoding,
                                   java.lang.Boolean standalone)
                            throws java.io.IOException
Writes XML prolog code:
        <?xml version="1.0"?>
 

Parameters:
encoding - the selected encoding. If null encoding attribute is not written.
standalone - if the XML is stand-alone, no DTD or schema
Returns:
self
Throws:
java.io.IOException - on I/O error
java.lang.IllegalStateException - if prolog code is already written

endDocument

public void endDocument()
Close this writer. It does not close the underlying writer, but does throw an exception if there are as yet unclosed tags.

Throws:
java.lang.IllegalStateException - if tags are not closed

getDepth

public int getDepth()
Returns the current depth of the element. Outside the root element, the depth is 0. The depth is incremented by 1 when startTag() is called. The depth is decremented after the call to endTag() event was observed.
        <!-- outside -->     0
        <root>               1
          sometext                 1
            <foobar>         2
            </foobar>        2
        </root>              1
        <!-- outside -->     0
 

Returns:
the current level

startTag

public XmlSerializer startTag(java.lang.String name)
                       throws java.io.IOException
Begin to output an entity.

Parameters:
name - name of entity.
Returns:
self
Throws:
java.io.IOException - on I/O error

endTag

public XmlSerializer endTag()
                     throws java.io.IOException
Deprecated. see endTag(String)

End the current entity silently without validating if correct tag is closed.

Returns:
self
Throws:
java.io.IOException

endTag

public XmlSerializer endTag(java.lang.String aName)
                     throws java.io.IOException
End the current entity. This will throw an exception if it is called when there is not a currently open entity.

Parameters:
aName - tag to close. This is used mostly for validation
Returns:
self
Throws:
java.io.IOException - on I/O error
java.lang.IllegalStateException - if no entity waits to be closed
java.lang.IllegalArgumentException - if expected element name is not the same

attribute

public XmlSerializer attribute(java.lang.String localName,
                               java.lang.String value)
                        throws java.io.IOException
Write an attribute out for the current entity. Any XML characters in the value are escaped. Calls to attribute() MUST follow a call to startTag() immediately.

Parameters:
localName - name of attribute.
value - value of attribute.
Returns:
self
Throws:
java.io.IOException - on I/O error
java.lang.IllegalStateException - if opening tag is closed

text

public XmlSerializer text(java.lang.String text)
                   throws java.io.IOException
Output body text. Any XML characters are escaped.

Parameters:
text - the body text
Returns:
self
Throws:
java.io.IOException - on I/O error

cdsect

public XmlSerializer cdsect(java.lang.String text)
                     throws java.io.IOException
Writes raw, CDATA section

Parameters:
text - the data
Returns:
self
Throws:
java.io.IOException - on I/O error

comment

public XmlSerializer comment(java.lang.String text)
                      throws java.io.IOException
Writes XML comment code

Parameters:
text - the comment
Returns:
self
Throws:
java.io.IOException - on I/O error

docdecl

public XmlSerializer docdecl(java.lang.String text)
                      throws java.io.IOException
Writes DOCTYPE declaration. The document must be open prior calling that method.

Parameters:
text - declaration
Returns:
self
Throws:
java.io.IOException - on I/O error
java.lang.IllegalStateException - if document is not open or start tag is already started

setIndent

public void setIndent(boolean indent)
Enables/disables indentation

Parameters:
indent - an indentation string or null to disable indentation.

Framework Professional Edition Package


Copyright © 1999-2007 ProSyst Software GmbH. All Rights Reserved