com.myjavatools.xml
Class XmlWriter

java.lang.Object
  extended byjava.io.Writer
      extended bycom.myjavatools.xml.XmlWriter

public class XmlWriter
extends java.io.Writer


Field Summary
static java.lang.String escapedChars
          escapeChars - characters that ought to be escaped in output Xml file
static java.lang.String okChars
          okChars - characters that are okay to be kept intact in output Xml file
 
Constructor Summary
XmlWriter(java.io.OutputStream out)
          Creates an XmlWriter from an OutputStream, using default UTF8 encoding.
XmlWriter(java.io.OutputStream out, java.lang.String encoding)
          Creates an XmlWriter from an OutputStream, using apwxidiws encoding.
XmlWriter(java.io.OutputStreamWriter out)
          Creates an XmlWriter from an OutputStreamWriter, using its encoding.
XmlWriter(java.io.Writer out)
          Creates an XmlWriter from a Writer, using default UTF8 encoding.
XmlWriter(java.io.Writer out, java.lang.String encoding)
          Creates an XmlWriter from a Writer, using specified encoding.
 
Method Summary
 java.lang.String attributesToString(java.lang.String[] attrs)
          Converts a list of attibutes to a string.
 java.lang.String attributeToString(java.lang.String name, java.lang.String value)
          Converts a name-value pair to a string of form name="value".
 int attrSize(java.lang.Object attrs)
          Return the number of attrubutes in the attribute list which can be a string.
 void close()
          Closes the output of Xml, dumping end tags of all elements stored in the stack.
 void closeElement()
          Closes an element in output.
 void flush()
          Flushes the output.
 java.lang.String getAttribute(java.lang.Object attrs, java.lang.String name)
          Gets a value of an attribute from an attribute list hat can be a map or a name-value pair string array.
 void openElement(java.lang.String type, java.lang.Object attrs)
          Starts outputting an element of Xml.
static java.lang.String sgmlEntity(char c)
          Encodes a character by SGML rules It can be a hex representation
static java.lang.String toSgmlEncoding(char c)
          Converts a character to its SGML numeric encoding
 void write(char[] cc, int off, int len)
          Writes characters to output stream.
static void write(java.io.File f, java.lang.Object o)
          Write the contents of object as an Xml file
 void write(java.lang.Object o)
          Writes an object as Xml content to the output.
static void write(java.io.OutputStream out, java.lang.Object o)
          Writes an object as Xml content to the output.
 void write(java.lang.String s)
          Writes a string to output.
static void write(java.lang.String filename, java.lang.Object o)
          Write the contents of object as an Xml file.
 void writeAttribute(java.lang.String name, java.lang.String value)
          Writes an attribute name-value pair to the output.
 void writeAttributes(java.util.Map attrs)
          Write attributes, name-value pairs, to the output.
 void writeAttributes(java.lang.Object attrs)
          Write attributes, name-value pairs, to the output.
 void writeAttributes(java.lang.String[] attrs)
          Writes attributes, name-value pairs, to the output.
 void writeElement(java.lang.String type, java.lang.String value)
          Writes an element that does not have any attributes or kids.
 void writeElement(java.lang.String type, java.lang.String value, java.lang.Object attrs)
          Writes an element that does not have any kids.
static java.lang.String xmlEscape(java.lang.String s)
          Converts a string to the form that is acceptable inside Xml files, escaping special characters.
 
Methods inherited from class java.io.Writer
write, write, write
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

escapedChars

public static final java.lang.String escapedChars
escapeChars - characters that ought to be escaped in output Xml file

See Also:
Constant Field Values

okChars

public static final java.lang.String okChars
okChars - characters that are okay to be kept intact in output Xml file

See Also:
Constant Field Values
Constructor Detail

XmlWriter

public XmlWriter(java.io.Writer out,
                 java.lang.String encoding)
          throws java.io.IOException
Creates an XmlWriter from a Writer, using specified encoding.

Parameters:
out - the original writer
encoding - encoding to use
Throws:
java.io.IOException

XmlWriter

public XmlWriter(java.io.Writer out)
          throws java.io.IOException
Creates an XmlWriter from a Writer, using default UTF8 encoding.

Parameters:
out - the original Writer
Throws:
java.io.IOException

XmlWriter

public XmlWriter(java.io.OutputStreamWriter out)
          throws java.io.IOException
Creates an XmlWriter from an OutputStreamWriter, using its encoding.

Parameters:
out - the original OutputStreamWriter
Throws:
java.io.IOException

XmlWriter

public XmlWriter(java.io.OutputStream out,
                 java.lang.String encoding)
          throws java.io.UnsupportedEncodingException,
                 java.io.IOException
Creates an XmlWriter from an OutputStream, using apwxidiws encoding.

Parameters:
out - the OutputStream to use
encoding - encoding to use
Throws:
java.io.IOException
java.io.UnsupportedEncodingException

XmlWriter

public XmlWriter(java.io.OutputStream out)
          throws java.io.UnsupportedEncodingException,
                 java.io.IOException
Creates an XmlWriter from an OutputStream, using default UTF8 encoding.

Parameters:
out - the OutputStream to use
Throws:
java.io.IOException
java.io.UnsupportedEncodingException
Method Detail

xmlEscape

public static java.lang.String xmlEscape(java.lang.String s)
Converts a string to the form that is acceptable inside Xml files, escaping special characters.

Parameters:
s - the string to convert
Returns:
string with certain characters replaiced with their SGML entity representations

write

public void write(char[] cc,
                  int off,
                  int len)
           throws java.io.IOException
Writes characters to output stream.

Parameters:
cc - char array to output
off - offset inside the arrray
len - number of characters to write
Throws:
java.io.IOException

write

public void write(java.lang.String s)
           throws java.io.IOException
Writes a string to output.

Parameters:
s - the strign to write
Throws:
java.io.IOException - if something went wrong

flush

public void flush()
           throws java.io.IOException
Flushes the output.

Throws:
java.io.IOException - if something went wrong

writeAttribute

public void writeAttribute(java.lang.String name,
                           java.lang.String value)
                    throws java.io.IOException
Writes an attribute name-value pair to the output.

Parameters:
name - attribute name
value - attribute value
Throws:
java.io.IOException - if something went wrong

attributeToString

public java.lang.String attributeToString(java.lang.String name,
                                          java.lang.String value)
Converts a name-value pair to a string of form name="value".

Parameters:
name - the name of the attribute
value - the value of the attribute
Returns:
a string of form <name>="<value>". If value is null or an empty string, result is an empty string.

writeAttributes

public void writeAttributes(java.lang.String[] attrs)
                     throws java.io.IOException
Writes attributes, name-value pairs, to the output.

Parameters:
attrs - String array of name-value pairs
Throws:
java.io.IOException - if somethings went wrong

writeAttributes

public void writeAttributes(java.util.Map attrs)
                     throws java.io.IOException
Write attributes, name-value pairs, to the output.

Parameters:
attrs - maps attribute names to values
Throws:
java.io.IOException - if somethings went wrong

attrSize

public int attrSize(java.lang.Object attrs)
Return the number of attrubutes in the attribute list which can be a string. array or a map.

Parameters:
attrs - a string array or a map of attributes
Returns:
the number of attributes (size of array / 2, number of keys in a map, 0 otherwise).

getAttribute

public java.lang.String getAttribute(java.lang.Object attrs,
                                     java.lang.String name)
Gets a value of an attribute from an attribute list hat can be a map or a name-value pair string array.

Parameters:
attrs - attribute list
name - the name of the attribute
Returns:
the value of the attribute, or null if not found

writeAttributes

public void writeAttributes(java.lang.Object attrs)
                     throws java.io.IOException
Write attributes, name-value pairs, to the output.

Parameters:
attrs - it is either a name-value pair string array, or a map that maps attribute names to values
Throws:
java.io.IOException - if somethings went wrong

attributesToString

public java.lang.String attributesToString(java.lang.String[] attrs)
                                    throws java.io.IOException
Converts a list of attibutes to a string.

Parameters:
attrs - a name-value pair string array
Returns:
its representaion as string, by the rules of Xml.
Throws:
java.io.IOException

openElement

public void openElement(java.lang.String type,
                        java.lang.Object attrs)
                 throws java.io.IOException
Starts outputting an element of Xml.

as a result of this, "<"type followed by attributes followed by ">" is sent to the output

Parameters:
type - element type
attrs - attributes (can be a string array or a map)
Throws:
java.io.IOException

closeElement

public void closeElement()
                  throws java.io.IOException
Closes an element in output. If the last element has a type t, the characters "</t>" are sent to the output

Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes the output of Xml, dumping end tags of all elements stored in the stack.

Throws:
java.io.IOException

writeElement

public void writeElement(java.lang.String type,
                         java.lang.String value)
                  throws java.io.IOException
Writes an element that does not have any attributes or kids.

Parameters:
type - element type
value - element value
Throws:
java.io.IOException

writeElement

public void writeElement(java.lang.String type,
                         java.lang.String value,
                         java.lang.Object attrs)
                  throws java.io.IOException
Writes an element that does not have any kids.

Parameters:
type - element type
value - element value
attrs - element attributes
Throws:
java.io.IOException

write

public void write(java.lang.Object o)
           throws java.io.IOException
Writes an object as Xml content to the output.

Parameters:
o - the data to be sent to output. If it is not XmlData, it is stringified first.
Throws:
java.io.IOException

write

public static void write(java.io.OutputStream out,
                         java.lang.Object o)
                  throws java.io.IOException
Writes an object as Xml content to the output.

Parameters:
out - the stream to output the data to
o - the data to output
Throws:
java.io.IOException

write

public static void write(java.io.File f,
                         java.lang.Object o)
                  throws java.io.IOException
Write the contents of object as an Xml file

Parameters:
f - the file to write to
o - the object to write
Throws:
java.io.IOException

write

public static void write(java.lang.String filename,
                         java.lang.Object o)
                  throws java.io.IOException
Write the contents of object as an Xml file.

Parameters:
filename - the name of the file to write to
o - the object to write
Throws:
java.io.IOException

toSgmlEncoding

public static java.lang.String toSgmlEncoding(char c)
Converts a character to its SGML numeric encoding

Parameters:
c - the character
Returns:
a string with the representation of c as "Numeric Character Reference" in SGMLese

Example:
  • toSgmlEncoding('\n') returns "&#10;".

  • sgmlEntity

    public static java.lang.String sgmlEntity(char c)
    Encodes a character by SGML rules It can be a hex representation

    Parameters:
    c - the character
    Returns:
    the string with either Predefined Entity, Numeric Character Reference, or null if no entity could be found

    Examples:
  • sgmlEntity('\60ab') returns "&#24747;" (that is, Numeric Character Reference);
  • sgmlEntity('<') returns "&lt;" (that is, Predefined Entity);
  • sgmlEntity('&') returns "&lt;" (that is, Predefined Entity);
  • sgmlEntity('X') returns null";
  • sgmlEntity('\n') returns null".