org.brains2b.saxo
Class SAXWriter

java.lang.Object
  extended byorg.brains2b.saxo.SAXWriter

public class SAXWriter
extends java.lang.Object

A SAX method like XML writer To be sure there is no such thing as a SAXWriter for SAX 2, in as much as it isn't defined in the API.

This class is provided because I needed it and because I think it would be handy to have a Writer that takes at least part of the DocumentHandler interface not to read but to write a document preventing me to have to spell out all the tags every time I need to write XML data to file.

Public Members throw SAXException, instead of IOException to keep in line with the SAX specification. Possible next versions will throw subtypes of SAXException, containing writing information and position.

Note: The Writer writes to files only if you supply it by String no URL connections will be openend. If you want to write to another target use the SAXWriter(Writer) constructor instead.

Version:
0.20 [09-01-2006]
Author:
dennis

Constructor Summary
SAXWriter()
          Default empty constructor.
SAXWriter(java.lang.String location)
          Constructor Sets the file to open a StreamWriter on.
SAXWriter(java.io.Writer writer)
          Constructor Sets the Writer where the output will go.
 
Method Summary
 void endComment()
          end a comment in an XML document any content or element written after calling start en before calling end will be written inside the comment
 void endDocument()
          ends the Document.
 void endElement(java.lang.String name)
          end Element.
 void endElement(java.lang.String namespaceURI, java.lang.String localname, java.lang.String qname)
          end Element.
 void endPrefixMapping(java.lang.String prefix)
           
 boolean getFeature(java.lang.String name)
           
 java.io.Writer getWriter()
          gets the Writer used for output
 void insertDescriptor()
          insert Descriptor.
 void insertElement(java.lang.String name, org.xml.sax.Attributes attr)
          inserts an EMPTY Element Although I would not see a purpose for this method if you do, you can leave the AttributeList empty.
 void insertElement(java.lang.String namespaceURI, java.lang.String localname, java.lang.String qname, org.xml.sax.Attributes attr)
          inserts an EMPTY Element For non-EMPTY Elements use startElement.
 void setContent(java.lang.String content)
          sets the Character Content of an Element.
 void setFeature(java.lang.String name, boolean value)
           
 void setWriter(java.io.Writer writer)
          sets the Writer used for output
 void startComment()
          start a comment in an XML document any content or element written after calling start en before calling end will be written inside the comment
 void startDocument()
          start Document.
 void startElement(java.lang.String name, org.xml.sax.Attributes attr)
          start Element.
 void startElement(java.lang.String namespaceURI, java.lang.String localname, java.lang.String qname, org.xml.sax.Attributes attr)
          start Element.
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SAXWriter

public SAXWriter()
Default empty constructor. Don't forget to set Location or Writer through the setters.


SAXWriter

public SAXWriter(java.io.Writer writer)
Constructor Sets the Writer where the output will go.

Parameters:
writer - java.io.Writer

SAXWriter

public SAXWriter(java.lang.String location)
Constructor Sets the file to open a StreamWriter on. Opening of the stream will not happen until startDocument() is called.

Parameters:
location - String
Method Detail

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
start Document. starts the document and will open de StreamWriter if it is not alreay open.

Does not insert the default processing Instruction for XML. You have to call insertDescriptor() yourself.

Throws:
org.xml.sax.SAXException
See Also:
insertDescriptor()

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
ends the Document. This will end the document and flushes and closes the Stream to the document. This method should be called once and precisely once after you called startDocument to free up resources.

Throws:
org.xml.sax.SAXException

insertDescriptor

public void insertDescriptor()
                      throws org.xml.sax.SAXException
insert Descriptor.

Inserts a descriptor of the type: <xml version="1.0" encoding="UTF-8" > into the document. If you need it. If you need it call it after startDocument to end up with a valid xml document.

Throws:
org.xml.sax.SAXException

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.startPrefixMapping(String, String)

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws org.xml.sax.SAXException
Throws:
org.xml.sax.SAXException
See Also:
ContentHandler.endPrefixMapping(String)

startComment

public void startComment()
                  throws org.xml.sax.SAXException
start a comment in an XML document

any content or element written after calling start en before calling end will be written inside the comment

Throws:
org.xml.sax.SAXException - if start cannot be written

endComment

public void endComment()
                throws org.xml.sax.SAXException
end a comment in an XML document

any content or element written after calling start en before calling end will be written inside the comment

Throws:
org.xml.sax.SAXException - if end cannot be written

startElement

public void startElement(java.lang.String name,
                         org.xml.sax.Attributes attr)
                  throws org.xml.sax.SAXException
start Element. This is the start of an element that has Content.

For EMPTY elements use insertElement.

You can insert null for the AttributeList if you don't need it. Parsers however will always return an AttributeList Object even it contains no attributes.

Parameters:
name -
attr -
Throws:
java.io.IOException
org.xml.sax.SAXException
See Also:
insertElement(String, Attributes)

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
start Element. This is the start of an element that has Content.

For EMPTY elements use insertElement.

You can insert null for the AttributeList if you don't need it. Parsers however will always return an AttributeList Object even it contains no attributes.

Parameters:
namespaceURI - String, the namespace to declare
localname - String
qname - String
attr - Attributes
Throws:
org.xml.sax.SAXException
See Also:
insertElement(String, Attributes)

endElement

public void endElement(java.lang.String name)
                throws org.xml.sax.SAXException
end Element. Ends a non-EMPTY element

For EMPTY elements use insertElement.

Do not call this after insertElement.

Parameters:
name - String, the local or qname of the element
Throws:
org.xml.sax.SAXException
See Also:
insertElement(String, Attributes)

endElement

public void endElement(java.lang.String namespaceURI,
                       java.lang.String localname,
                       java.lang.String qname)
                throws org.xml.sax.SAXException
end Element. Ends a non-EMPTY element

For EMPTY elements use insertElement.

Do not call this after insertElement.

Parameters:
namespaceURI - String
localname - String, the localname the element
qname - String, the qname of the element
Throws:
org.xml.sax.SAXException
See Also:
insertElement(String, Attributes)

insertElement

public void insertElement(java.lang.String name,
                          org.xml.sax.Attributes attr)
                   throws org.xml.sax.SAXException
inserts an EMPTY Element

Although I would not see a purpose for this method if you do, you can leave the AttributeList empty. For non-EMPTY Elements use startElement.

Parameters:
name - String, can be either a localname or qname
attr - Attributes
Throws:
org.xml.sax.SAXException
See Also:
startElement(String, Attributes)

insertElement

public void insertElement(java.lang.String namespaceURI,
                          java.lang.String localname,
                          java.lang.String qname,
                          org.xml.sax.Attributes attr)
                   throws org.xml.sax.SAXException
inserts an EMPTY Element

For non-EMPTY Elements use startElement.

Parameters:
namespaceURI - String, the namespace to declare
localname - String, can be either a localname or qname
qname - String, null if setting the default namespace
attr - Attributes, null if no attributes need to be defined
Throws:
org.xml.sax.SAXException
See Also:
startElement(String, Attributes)

setContent

public void setContent(java.lang.String content)
                throws org.xml.sax.SAXException
sets the Character Content of an Element. There is no protection against writing outside Element tags yet, so you have to build that protection into the code that uses this.

Parameters:
content -
Throws:
org.xml.sax.SAXException

getFeature

public boolean getFeature(java.lang.String name)
                   throws org.xml.sax.SAXNotRecognizedException,
                          org.xml.sax.SAXNotSupportedException
Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException

setFeature

public void setFeature(java.lang.String name,
                       boolean value)
                throws org.xml.sax.SAXNotRecognizedException,
                       org.xml.sax.SAXNotSupportedException
Throws:
org.xml.sax.SAXNotRecognizedException
org.xml.sax.SAXNotSupportedException

getWriter

public java.io.Writer getWriter()
gets the Writer used for output

Returns:
Writer

setWriter

public void setWriter(java.io.Writer writer)
sets the Writer used for output

Parameters:
writer -