soapical.likeoldsax
Class SpecificXMLWriter

java.lang.Object
  extended bysoapical.likeoldsax.SpecificXMLWriter
All Implemented Interfaces:
org.xml.sax.ContentHandler
Direct Known Subclasses:
SpecificContentHandler, XSDWriter

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

Convenience class for generating XML.

This class provides easy, SAX1-style functions for your convenience when you are generating XML in mostly one namespace:

startElement just pass in a local name and optionally an Attributes
endElement just pass in a local name
emptyElement equivilant to calling startElement then endElement
addAttributes adds unprefixed attributes


The best way to use this class is to extend it and create a simpler version of the constructor. For example:


 public class ExampleWriter extends SpecificXMLWriter {
 
   public ExampleWriter(Writer writer) {
     super(writer, EXAMPLE_NAMESPACE, EXAMPLE_NAMESPACE_PREFIX);
   }

   public ExampleWriter(Writer writer, String prefix) {
     super(writer, EXAMPLE_NAMESPACE, prefix);
   }
  
   public static final String EXAMPLE_NAMESPACE_PREFIX =  "example";
   public static final String EXAMPLE_NAMESPACE = "http://www.example.org/foo";
  
 } // end ExampleWriter
 


Here is how you would use the above code:

 ExampleWriter xmlout = new ExampleWriter(System.out);
 
 xmlout.startDocument();
 
 AttributesImpl fooAtts = new AttributesImpl();
 xmlout.addAttributes(fooAtts, "myattr", "CDATA", "myvalue");
 
 xmlout.startElement("foo", fooAtts);

 AttributesImpl barAtts = new AttributesImpl();
 xmlout.addAttributes(barAtts, "myotherattr", "CDATA", "myothervalue");
 xmlout.emptyElement("bar1", barAtts);
 
 xmlout.emptyElement("bar2");

 xmlout.endElement("foo");
 xmlout.endDocument();
 

This is the XML it would produce

 <example:foo xmlns:foo="http://www.example.org/foo" myattr="myvalue">
  <example:bar1 myotherattr="myothervalue"/>
  <example:bar2/>
 </example:foo>
 
Now, you might argue that the XML produced was much shorter than the code it took to write it. True, but the code to write this XML probably has less chance of error, and that's the real benefit of automation.


If you really want to know, this class is merely a decorator to ContentHandler and XMLWriter.

Version:
$Revision: 1.3 $
$Date: 2003/04/24 13:26:24 $
Author:
Eric Kow (kow at loria point fr)
See Also:
XSDWriter

Constructor Summary
SpecificXMLWriter(org.xml.sax.ContentHandler handler, java.lang.String namespaceURI, java.lang.String prefix)
          Creates a new SpecificXMLWriter which passes its events to another ContentHandler.
SpecificXMLWriter(java.io.Writer writer, java.lang.String namespaceURI, java.lang.String prefix)
          Creates a new SpecificXMLWriter which generates XML to the Writer parameter.
 
Method Summary
 void addAttribute(org.xml.sax.helpers.AttributesImpl attrs, java.lang.String localName, java.lang.String type, java.lang.String value)
          Add an unprefixed attribute to an attributes list.
 void characters(char[] ch, int start, int len)
           
 void emptyElement(java.lang.String localName)
          Creates an empty element in the assumed namespace, with no attributes.
 void emptyElement(java.lang.String localName, org.xml.sax.Attributes atts)
          Creates an empty element in the assumed namespace.
 void endDocument()
           
 void endElement(java.lang.String localName)
          End an element in the assumed namespace.
 void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
           
 void endPrefixMapping(java.lang.String prefix)
           
 void ignorableWhitespace(char[] ch, int start, int len)
           
 void processingInstruction(java.lang.String target, java.lang.String data)
           
 void setDocumentLocator(org.xml.sax.Locator locator)
           
 void skippedEntity(java.lang.String name)
           
 void startDocument()
           
 void startElement(java.lang.String localName)
          Start a new element in the assumed namespace, with no attributes
 void startElement(java.lang.String localName, org.xml.sax.Attributes atts)
          Start a new element in the assumed namespace
 void startElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes atts)
           
 void startPrefixMapping(java.lang.String prefix, java.lang.String uri)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SpecificXMLWriter

public SpecificXMLWriter(org.xml.sax.ContentHandler handler,
                         java.lang.String namespaceURI,
                         java.lang.String prefix)
Creates a new SpecificXMLWriter which passes its events to another ContentHandler.

Parameters:
handler - the ContentHandler to which you want this SpecificXMLWriter's events to be sent
namespaceURI - the assumed namespace for when you use SpecificXMLWriter's convenience functions
prefix - the prefix to assign to the assumed namespace

SpecificXMLWriter

public SpecificXMLWriter(java.io.Writer writer,
                         java.lang.String namespaceURI,
                         java.lang.String prefix)
Creates a new SpecificXMLWriter which generates XML to the Writer parameter.

Parameters:
namespaceURI - the assumed namespace for when you use SpecificXMLWriter's convenience functions
prefix - the prefix to assign to the assumed namespace
writer - the Writer you want to generate XML to
Method Detail

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Specified by:
startDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Specified by:
endDocument in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

startElement

public void startElement(java.lang.String uri,
                         java.lang.String localName,
                         java.lang.String qName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException
Specified by:
startElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String uri,
                       java.lang.String localName,
                       java.lang.String qName)
                throws org.xml.sax.SAXException
Specified by:
endElement in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

characters

public void characters(char[] ch,
                       int start,
                       int len)
                throws org.xml.sax.SAXException
Specified by:
characters in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int len)
                         throws org.xml.sax.SAXException
Specified by:
ignorableWhitespace in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Specified by:
processingInstruction in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Specified by:
setDocumentLocator in interface org.xml.sax.ContentHandler

endPrefixMapping

public void endPrefixMapping(java.lang.String prefix)
                      throws org.xml.sax.SAXException
Specified by:
endPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

startPrefixMapping

public void startPrefixMapping(java.lang.String prefix,
                               java.lang.String uri)
                        throws org.xml.sax.SAXException
Specified by:
startPrefixMapping in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

skippedEntity

public void skippedEntity(java.lang.String name)
                   throws org.xml.sax.SAXException
Specified by:
skippedEntity in interface org.xml.sax.ContentHandler
Throws:
org.xml.sax.SAXException

startElement

public void startElement(java.lang.String localName)
                  throws org.xml.sax.SAXException
Start a new element in the assumed namespace, with no attributes

Throws:
org.xml.sax.SAXException
See Also:
startElement(String, Attributes)

startElement

public void startElement(java.lang.String localName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException
Start a new element in the assumed namespace

Throws:
org.xml.sax.SAXException
See Also:
startElement(String, String, String, Attributes)

emptyElement

public void emptyElement(java.lang.String localName)
                  throws org.xml.sax.SAXException
Creates an empty element in the assumed namespace, with no attributes.

Throws:
org.xml.sax.SAXException
See Also:
emptyElement(String, Attributes)

emptyElement

public void emptyElement(java.lang.String localName,
                         org.xml.sax.Attributes atts)
                  throws org.xml.sax.SAXException
Creates an empty element in the assumed namespace.

Throws:
org.xml.sax.SAXException

endElement

public void endElement(java.lang.String localName)
                throws org.xml.sax.SAXException
End an element in the assumed namespace.

Throws:
org.xml.sax.SAXException

addAttribute

public void addAttribute(org.xml.sax.helpers.AttributesImpl attrs,
                         java.lang.String localName,
                         java.lang.String type,
                         java.lang.String value)
                  throws org.xml.sax.SAXException
Add an unprefixed attribute to an attributes list. Remember that unprefixed attributes belong to no namespace, *not* the default nor the assumed namespace. You most likely want to be using this; the only time to have a namespace-prefixed attribute is if you are importing it from another document.

Parameters:
attrs - the AttributesImpl to add to
Throws:
org.xml.sax.SAXException