jimm.util
Class XMLWriter

java.lang.Object
  extended by java.io.Writer
      extended by java.io.PrintWriter
          extended by jimm.util.XMLWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class XMLWriter
extends java.io.PrintWriter

An XML writer is a print writer that knows how to output XML elements and make the output look pretty.

Calling indent and outdent changes the indentation level. The default indentation width is 4 spaces.

Author:
Jim Menard, jimm@io.com

Nested Class Summary
(package private) static class XMLWriter.ElementInfo
           
 
Field Summary
protected static int DEFAULT_INDENTATION_WIDTH
           
protected  java.util.ArrayList elementStack
           
protected  boolean inElementStart
           
protected  int level
           
protected  boolean newline
           
protected  int width
           
 
Fields inherited from class java.io.PrintWriter
out
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
XMLWriter(java.io.OutputStream out)
          Constructor, same as the PrintWriter version.
XMLWriter(java.io.OutputStream out, boolean autoFlush, int width)
          Constructor, same as PrintWriter version without the width parameter.
XMLWriter(java.io.Writer out)
          Constructor, same as the PrintWriter version.
XMLWriter(java.io.Writer out, boolean autoFlush, int width)
          Constructor, same as the PrintWriter version.
 
Method Summary
 void attr(java.lang.String name, boolean bool)
           
 void attr(java.lang.String name, char value)
           
 void attr(java.lang.String name, java.awt.Color c)
           
 void attr(java.lang.String name, double value)
           
 void attr(java.lang.String name, int value)
           
 void attr(java.lang.String name, java.lang.Object value)
           
 void attr(java.lang.String name, java.lang.String value)
          Writes an attribute.
 void cdata(java.lang.String text)
           
 void cdataElement(java.lang.String name, java.lang.String text)
           
 void comment(java.lang.String text)
           
protected  void doIndent()
          Performs indentation by printing the correct number of tabs and spaces.
 void endElement()
          Ends an element.
protected  void finishStartElement()
          Writes the end of the start of an element.
protected  void finishStartElement(boolean outputNewline)
          Writes the end of the start of an element.
 void indent()
          Increases the indentation level by one.
protected  void init(int indentationWidth)
          Initializes some instance variables.
 void outdent()
          Decreases the indentation level by one.
protected  void parentShouldOutdent()
           
 void print(boolean b)
           
 void print(char c)
           
 void print(char[] s)
           
 void print(double d)
           
 void print(float f)
           
 void print(int i)
           
 void print(long l)
           
 void print(java.lang.Object obj)
           
 void print(java.lang.String str)
          This method does not handle newlines embedded in the string.
 void println()
           
 void println(boolean b)
           
 void println(char c)
           
 void println(char[] s)
           
 void println(double d)
           
 void println(float f)
           
 void println(int i)
           
 void println(long l)
           
 void println(java.lang.Object obj)
           
 void println(java.lang.String str)
           
 void startElement(java.lang.String name)
          Starts an element.
 void text(java.lang.String text)
           
 void textElement(java.lang.String name, java.lang.String text)
           
 void xmlDecl(java.lang.String encoding)
           
 
Methods inherited from class java.io.PrintWriter
append, append, append, checkError, clearError, close, flush, format, format, printf, printf, setError, write, write, write, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_INDENTATION_WIDTH

protected static final int DEFAULT_INDENTATION_WIDTH
See Also:
Constant Field Values

width

protected int width

level

protected int level

newline

protected boolean newline

elementStack

protected java.util.ArrayList elementStack

inElementStart

protected boolean inElementStart
Constructor Detail

XMLWriter

public XMLWriter(java.io.OutputStream out)
Constructor, same as the PrintWriter version.

Parameters:
out - an output stream

XMLWriter

public XMLWriter(java.io.OutputStream out,
                 boolean autoFlush,
                 int width)
Constructor, same as PrintWriter version without the width parameter.

Parameters:
out - an output stream
autoFlush - if true, the println() methods will flush the output bufferset to flush after every line
width - indentation width in spaces

XMLWriter

public XMLWriter(java.io.Writer out)
Constructor, same as the PrintWriter version.

Parameters:
out - a writer

XMLWriter

public XMLWriter(java.io.Writer out,
                 boolean autoFlush,
                 int width)
Constructor, same as the PrintWriter version.

Parameters:
out - a writer
autoFlush - if true, the println() methods will flush the output bufferset to flush after every line
width - indentation width in spaces
Method Detail

init

protected void init(int indentationWidth)
Initializes some instance variables. Called from constructors.

Parameters:
indentationWidth - number of spaces per indentation level

indent

public void indent()
Increases the indentation level by one.


outdent

public void outdent()
Decreases the indentation level by one.


print

public void print(boolean b)
Overrides:
print in class java.io.PrintWriter

print

public void print(char c)
Overrides:
print in class java.io.PrintWriter

print

public void print(char[] s)
Overrides:
print in class java.io.PrintWriter

print

public void print(double d)
Overrides:
print in class java.io.PrintWriter

print

public void print(float f)
Overrides:
print in class java.io.PrintWriter

print

public void print(int i)
Overrides:
print in class java.io.PrintWriter

print

public void print(long l)
Overrides:
print in class java.io.PrintWriter

print

public void print(java.lang.Object obj)
Overrides:
print in class java.io.PrintWriter

print

public void print(java.lang.String str)
This method does not handle newlines embedded in the string.

Overrides:
print in class java.io.PrintWriter
Parameters:
str - the string to output

println

public void println()
Overrides:
println in class java.io.PrintWriter

println

public void println(boolean b)
Overrides:
println in class java.io.PrintWriter

println

public void println(char c)
Overrides:
println in class java.io.PrintWriter

println

public void println(char[] s)
Overrides:
println in class java.io.PrintWriter

println

public void println(double d)
Overrides:
println in class java.io.PrintWriter

println

public void println(float f)
Overrides:
println in class java.io.PrintWriter

println

public void println(int i)
Overrides:
println in class java.io.PrintWriter

println

public void println(long l)
Overrides:
println in class java.io.PrintWriter

println

public void println(java.lang.Object obj)
Overrides:
println in class java.io.PrintWriter

println

public void println(java.lang.String str)
Overrides:
println in class java.io.PrintWriter

doIndent

protected void doIndent()
Performs indentation by printing the correct number of tabs and spaces.


xmlDecl

public void xmlDecl(java.lang.String encoding)

finishStartElement

protected void finishStartElement(boolean outputNewline)
Writes the end of the start of an element.


finishStartElement

protected void finishStartElement()
Writes the end of the start of an element.


parentShouldOutdent

protected void parentShouldOutdent()

startElement

public void startElement(java.lang.String name)
Starts an element. This may be followed by zero or more calls to attribute. The start-element will be closed by the first following call to any method other than attribute.


attr

public void attr(java.lang.String name,
                 java.lang.String value)
Writes an attribute.


attr

public void attr(java.lang.String name,
                 double value)

attr

public void attr(java.lang.String name,
                 int value)

attr

public void attr(java.lang.String name,
                 char value)

attr

public void attr(java.lang.String name,
                 boolean bool)

attr

public void attr(java.lang.String name,
                 java.awt.Color c)

attr

public void attr(java.lang.String name,
                 java.lang.Object value)

endElement

public void endElement()
Ends an element. This may output an end-tag or close the current start-tag as an empty element.


cdataElement

public void cdataElement(java.lang.String name,
                         java.lang.String text)

cdata

public void cdata(java.lang.String text)

textElement

public void textElement(java.lang.String name,
                        java.lang.String text)

text

public void text(java.lang.String text)

comment

public void comment(java.lang.String text)