sugar4xml
Class Sugar4Xml<T extends Sugar4Xml<?>>

java.lang.Object
  extended by sugar4xml.Sugar4Xml<T>
All Implemented Interfaces:
Creator
Direct Known Subclasses:
Sugar4Xml.Default, XHtml, Xsd

public abstract class Sugar4Xml<T extends Sugar4Xml<?>>
extends Object
implements Creator

Writes arbitrary xml to a Writer destination using only the two methods element() and end().

element(String, Object...) starts an element, end() ends the last written element.

element() expects its parameters as follows:

For nested elements, it is suggested to nest the element() and end() calls as well.

Example:

 element("html");
 {
   element("body");
   {
      element("h1", "hello world").end();
   }
   end();
 }
 end();
 
 -->
 
 <page><body><h1>hello world</h1></body></page>
 
 

Version:
$Id: Sugar4Xml.java,v 1.9 2008/02/13 23:27:27 krizzdewizz Exp $
Author:
krizzdewizz

Nested Class Summary
static class Sugar4Xml.Default
          Default, type-argument less implementation.
 
Field Summary
static String xmlns
          The xmlns attribute used for namespace declaration with an empty prefix.
 
Constructor Summary
Sugar4Xml()
          Constructs with a null writer.
Sugar4Xml(Writer writer)
          Constructs with the given writer
 
Method Summary
 T characters(char[] ch, int start, int length)
          Outputs the given characters.
 T create()
          Creates this xml.
 T element(String name, Object... attributesAndOptionalValue)
          Starts an element (namespace element with an empty prefix), its attributes and an optional value.
 T embedd(Sugar4Xml<?> xml)
           
 T end()
          Ends the last written element.
 String getDefaultElementNsPrefix()
          Returns the default prefix for all elements written if no prefix is given in nselement().
 Writer getWriter()
          Returns the output destination for this xml.
static String nsattr(String prefix, String name)
          Returns the attribute name for the given namespace prefix.
 T nselement(String nsPrefix, String name, Object... attributesAndOptionalValue)
          Starts a namespace prefixed element, its attributes and an optional value.
 T nsembedd(String nsPrefix, Sugar4Xml<?> xml)
           
 void setDefaultElementNsPrefix(String defaultElementNsPrefix)
          Sets the default prefix for all elements written if no prefix is given in nselement().
 void setWriter(Writer writer)
          Sets the writer to which to output the xml.
 T text(String text)
          Outputs the given text using a character() call.
static String xmlns(Object prefix)
          Returns an xmlns attribute for the given prefix used for a namespace declaration.
 

Field Detail

xmlns

public static final String xmlns
The xmlns attribute used for namespace declaration with an empty prefix.

Example:

 element("schema", xmlns, "http://www.w3.org/2001/XMLSchema").end();
 
 -->
 
 <schema xmlns='http://www.w3.org/2001/XMLSchema'/>
 

See Also:
xmlns(Object), Constant Field Values
Constructor Detail

Sugar4Xml

public Sugar4Xml()
Constructs with a null writer.

Note that setWriter(Writer) must be called before the xml is created or else a Sugar4XmlException will occur.


Sugar4Xml

public Sugar4Xml(Writer writer)
Constructs with the given writer

Parameters:
writer - Where to write this xml to
Method Detail

xmlns

public static String xmlns(Object prefix)
Returns an xmlns attribute for the given prefix used for a namespace declaration.

Example:

 nselement("xs", "schema", xmlns("xs"), "http://www.w3.org/2001/XMLSchema").end();
 
 -->
 
 <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'/>
 

Parameters:
prefix -
Returns:
String

nsattr

public static String nsattr(String prefix,
                            String name)
Returns the attribute name for the given namespace prefix.

Example:

 element("content", xmlns("a"), "com.acme", nsattr("a", "id"), "zx500").end();
 
 -->
 
 <content xmlns:a='com.acme' a:id='zx500'/>
 

Parameters:
prefix -
name -
Returns:
String

create

public T create()
Creates this xml.

Returns:
this
Throws:
Sugar4XmlException - if element() and end() calls do not match (mal-formed xml output).

text

public T text(String text)
Outputs the given text using a character() call.

Parameters:
text -
Returns:
this

characters

public T characters(char[] ch,
                    int start,
                    int length)
Outputs the given characters.

Parameters:
ch -
start -
length -
Returns:
this
See Also:
ContentHandler.characters(char[], int, int)

element

public T element(String name,
                 Object... attributesAndOptionalValue)
Starts an element (namespace element with an empty prefix), its attributes and an optional value.

Parameters:
name - The name of the element
attributesAndOptionalValue - attribute [name, value] pairs, optionally followed by a single value. If the length of the array is odd, the last element designates the value for the element. May be empty. An attribute name may be namespace-prefixed.
Returns:
this
See Also:
nselement(String, String, Object...)

nselement

public T nselement(String nsPrefix,
                   String name,
                   Object... attributesAndOptionalValue)
Starts a namespace prefixed element, its attributes and an optional value.

Parameters:
nsPrefix - Namespace prefix
name - The name of the element
attributesAndOptionalValue - attribute [name, value] pairs, optionally followed by a single value. If the length of the array is odd, the last element designates the value for the element. May be empty. An attribute name may be namespace-prefixed.
Returns:
this

end

public T end()
Ends the last written element.

Returns:
this

embedd

public T embedd(Sugar4Xml<?> xml)

nsembedd

public T nsembedd(String nsPrefix,
                  Sugar4Xml<?> xml)

getWriter

public Writer getWriter()
Returns the output destination for this xml.

Returns:
Writer

setWriter

public void setWriter(Writer writer)
Sets the writer to which to output the xml.

This method should not be called within the creation of the xml.

Parameters:
writer -

getDefaultElementNsPrefix

public String getDefaultElementNsPrefix()
Returns the default prefix for all elements written if no prefix is given in nselement().

Returns:
Prefix

setDefaultElementNsPrefix

public void setDefaultElementNsPrefix(String defaultElementNsPrefix)
Sets the default prefix for all elements written if no prefix is given in nselement().

Parameters:
defaultElementNsPrefix -