com.myjavatools.xml
Class XmlWriter

java.lang.Object
  extended by java.io.Writer
      extended by com.myjavatools.xml.XmlWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class XmlWriter
extends java.io.Writer


Field Summary
protected  java.util.Stack<java.lang.CharSequence> Elements
           
static java.lang.String escapedChars
          escapeChars - characters that ought to be escaped in output Xml file
protected  boolean isEmpty
           
static java.lang.String okChars
          okChars - characters that are okay to be kept intact in output Xml file
protected  java.lang.String prefix
           
protected  java.io.Writer writer
           
 
Fields inherited from class java.io.Writer
lock
 
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 attributeToString(java.lang.CharSequence name, java.lang.CharSequence value)
          Converts a name-value pair to a string of form name="value".
 int attrSize(java.util.Map 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.
protected  void eol()
           
 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.
protected static boolean isAlmostEmpty(java.lang.CharSequence string)
           
protected static boolean isEmpty(java.lang.CharSequence cs)
           
protected static boolean needsEscape(char c)
           
 void openElement(java.lang.String type, java.util.Map<java.lang.String,java.lang.String> 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.CharSequence name, java.lang.CharSequence value)
          Writes an attribute name-value pair to the output.
 void writeAttributes(java.util.Map<java.lang.String,java.lang.String> attrs)
          Write attributes, name-value pairs, to the output.
 void writeElement(java.lang.CharSequence 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.util.Map<java.lang.String,java.lang.String> attrs)
          Writes an element that does not have any kids.
protected  void writeXmlData(XmlData data)
           
static java.lang.CharSequence xmlEscape(java.lang.CharSequence s)
          Converts a string to the form that is acceptable inside Xml files, escaping special characters.
 
Methods inherited from class java.io.Writer
append, append, append, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

writer

protected final java.io.Writer writer

prefix

protected java.lang.String prefix

Elements

protected java.util.Stack<java.lang.CharSequence> Elements

isEmpty

protected boolean isEmpty

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

needsEscape

protected static boolean needsEscape(char c)

xmlEscape

public static java.lang.CharSequence xmlEscape(java.lang.CharSequence 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.

Specified by:
write in class java.io.Writer
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.

Overrides:
write in class java.io.Writer
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.

Specified by:
flush in interface java.io.Flushable
Specified by:
flush in class java.io.Writer
Throws:
java.io.IOException - if something went wrong

eol

protected void eol()
            throws java.io.IOException
Throws:
java.io.IOException

writeAttribute

public void writeAttribute(java.lang.CharSequence name,
                           java.lang.CharSequence 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.CharSequence name,
                                          java.lang.CharSequence 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.util.Map<java.lang.String,java.lang.String> 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.util.Map attrs)
Return the number of attrubutes in the attribute list which can be a string. array or a map.

Parameters:
attrs - 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

openElement

public void openElement(java.lang.String type,
                        java.util.Map<java.lang.String,java.lang.String> 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.

Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Writer
Throws:
java.io.IOException

writeElement

public void writeElement(java.lang.CharSequence 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.util.Map<java.lang.String,java.lang.String> 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

writeXmlData

protected void writeXmlData(XmlData data)
                     throws java.io.IOException
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

isEmpty

protected static final boolean isEmpty(java.lang.CharSequence cs)

isAlmostEmpty

protected static final boolean isAlmostEmpty(java.lang.CharSequence string)

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".