prefuse.util.io
Class XMLWriter

java.lang.Object
  extended by prefuse.util.io.XMLWriter

public class XMLWriter
extends java.lang.Object

Utility class for writing XML files. This class provides convenience methods for creating XML documents, such as starting and ending tags, and adding content and comments. This class handles correct XML formatting and will properly escape text to ensure that the text remains valid XML.

To use this class, create a new instance with the desired PrintWriter to write the XML to. Call the begin() or begin(String, int) method when ready to start outputting XML. Then use the provided methods to generate the XML file. Finally, call either the finish() or finish(String) methods to signal the completion of the file.

Author:
jeffrey heer

Constructor Summary
XMLWriter(java.io.PrintWriter out)
          Create a new XMLWriter.
XMLWriter(java.io.PrintWriter out, int tabLength)
          Create a new XMLWriter.
 
Method Summary
 void begin()
          Begin the XML document.
 void begin(java.lang.String header, int bias)
          Begin the XML document.
 void comment(java.lang.String comment)
          Print a comment in the XML document.
 void content(java.lang.String content)
          Print content text.
 void contentTag(java.lang.String tag, java.lang.String content)
          Print a new content tag with no attributes, consisting of an open tag, content text, and a closing tag, all on one line.
 void contentTag(java.lang.String tag, java.lang.String name, java.lang.String value, java.lang.String content)
          Print a new content tag with a single attribute, consisting of an open tag, content text, and a closing tag, all on one line.
 void end()
          Close the most recently opened tag.
protected  void escapeString(java.lang.String str)
          Escape a string such that it is safe to use in an XML document.
 void finish()
          Finish the XML document.
 void finish(java.lang.String footer)
          Finish the XML document, printing the given footer text at the end of the document.
 void print(java.lang.String s)
          Print unescaped text into the XML file.
 void println()
          Print a newline into the XML file.
 void println(java.lang.String s)
          Print unescaped text into the XML file, followed by a newline.
 void spacing()
          Print the current spacing (determined by the indentation level) into the document.
 void start(java.lang.String tag)
          Print a start tag without attributes.
 void start(java.lang.String tag, java.util.ArrayList names, java.util.ArrayList values, int nattr)
          Print a start tag with attributes.
 void start(java.lang.String tag, java.lang.String[] names, java.lang.String[] values, int nattr)
          Print a start tag with attributes.
 void start(java.lang.String tag, java.lang.String name, java.lang.String value)
          Print a start tag with one attribute.
 void tag(java.lang.String tag, java.util.ArrayList names, java.util.ArrayList values, int nattr)
          Print a closed tag with attributes.
protected  void tag(java.lang.String tag, java.util.ArrayList names, java.util.ArrayList values, int nattr, boolean close)
          Internal method for printing a tag with attributes.
 void tag(java.lang.String tag, java.lang.String[] names, java.lang.String[] values, int nattr)
          Print a closed tag with attributes.
protected  void tag(java.lang.String tag, java.lang.String[] names, java.lang.String[] values, int nattr, boolean close)
          Internal method for printing a tag with attributes.
 void tag(java.lang.String tag, java.lang.String name, java.lang.String value)
          Print a closed tag with one attribute.
protected  void tag(java.lang.String tag, java.lang.String name, java.lang.String value, boolean close)
          Internal method for printing a tag with a single attribute.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLWriter

public XMLWriter(java.io.PrintWriter out)
Create a new XMLWriter.

Parameters:
out - the print writer to write the XML to

XMLWriter

public XMLWriter(java.io.PrintWriter out,
                 int tabLength)
Create a new XMLWriter.

Parameters:
out - the print writer to write the XML to
tabLength - the number of spaces to use for each level of indentation in the XML file
Method Detail

print

public void print(java.lang.String s)
Print unescaped text into the XML file. To print escaped text, use the content(String) method instead.

Parameters:
s - the text to print. This String will not be escaped.

println

public void println(java.lang.String s)
Print unescaped text into the XML file, followed by a newline. To print escaped text, use the content(String) method instead.

Parameters:
s - the text to print. This String will not be escaped.

println

public void println()
Print a newline into the XML file.


begin

public void begin()
Begin the XML document. This must be called before any other formatting methods. This method prints an XML header into the top of the output stream.


begin

public void begin(java.lang.String header,
                  int bias)
Begin the XML document. This must be called before any other formatting methods. This method prints an XML header into the top of the output stream, plus additional header text provided by the client

Parameters:
header - header text to insert into the document
bias - the spacing bias to use for all subsequent indenting

comment

public void comment(java.lang.String comment)
Print a comment in the XML document. The comment will be printed according to the current spacing and followed by a newline.

Parameters:
comment - the comment text

tag

protected void tag(java.lang.String tag,
                   java.lang.String[] names,
                   java.lang.String[] values,
                   int nattr,
                   boolean close)
Internal method for printing a tag with attributes.

Parameters:
tag - the tag name
names - the names of the attributes
values - the values of the attributes
nattr - the number of attributes
close - true to close the tag, false to leave it open and adjust the spacing

tag

public void tag(java.lang.String tag,
                java.lang.String[] names,
                java.lang.String[] values,
                int nattr)
Print a closed tag with attributes. The tag will be followed by a newline.

Parameters:
tag - the tag name
names - the names of the attributes
values - the values of the attributes
nattr - the number of attributes

start

public void start(java.lang.String tag,
                  java.lang.String[] names,
                  java.lang.String[] values,
                  int nattr)
Print a start tag with attributes. The tag will be followed by a newline, and the indentation level will be increased.

Parameters:
tag - the tag name
names - the names of the attributes
values - the values of the attributes
nattr - the number of attributes

tag

protected void tag(java.lang.String tag,
                   java.lang.String name,
                   java.lang.String value,
                   boolean close)
Internal method for printing a tag with a single attribute.

Parameters:
tag - the tag name
name - the name of the attribute
value - the value of the attribute
close - true to close the tag, false to leave it open and adjust the spacing

tag

public void tag(java.lang.String tag,
                java.lang.String name,
                java.lang.String value)
Print a closed tag with one attribute. The tag will be followed by a newline.

Parameters:
tag - the tag name
name - the name of the attribute
value - the value of the attribute

start

public void start(java.lang.String tag,
                  java.lang.String name,
                  java.lang.String value)
Print a start tag with one attribute. The tag will be followed by a newline, and the indentation level will be increased.

Parameters:
tag - the tag name
name - the name of the attribute
value - the value of the attribute

tag

protected void tag(java.lang.String tag,
                   java.util.ArrayList names,
                   java.util.ArrayList values,
                   int nattr,
                   boolean close)
Internal method for printing a tag with attributes.

Parameters:
tag - the tag name
names - the names of the attributes
values - the values of the attributes
nattr - the number of attributes
close - true to close the tag, false to leave it open and adjust the spacing

tag

public void tag(java.lang.String tag,
                java.util.ArrayList names,
                java.util.ArrayList values,
                int nattr)
Print a closed tag with attributes. The tag will be followed by a newline.

Parameters:
tag - the tag name
names - the names of the attributes
values - the values of the attributes
nattr - the number of attributes

start

public void start(java.lang.String tag,
                  java.util.ArrayList names,
                  java.util.ArrayList values,
                  int nattr)
Print a start tag with attributes. The tag will be followed by a newline, and the indentation level will be increased.

Parameters:
tag - the tag name
names - the names of the attributes
values - the values of the attributes
nattr - the number of attributes

start

public void start(java.lang.String tag)
Print a start tag without attributes. The tag will be followed by a newline, and the indentation level will be increased.

Parameters:
tag - the tag name

end

public void end()
Close the most recently opened tag. The tag will be followed by a newline, and the indentation level will be decreased.


contentTag

public void contentTag(java.lang.String tag,
                       java.lang.String name,
                       java.lang.String value,
                       java.lang.String content)
Print a new content tag with a single attribute, consisting of an open tag, content text, and a closing tag, all on one line.

Parameters:
tag - the tag name
name - the name of the attribute
value - the value of the attribute, this text will be escaped
content - the text content, this text will be escaped

contentTag

public void contentTag(java.lang.String tag,
                       java.lang.String content)
Print a new content tag with no attributes, consisting of an open tag, content text, and a closing tag, all on one line.

Parameters:
tag - the tag name
content - the text content, this text will be escaped

content

public void content(java.lang.String content)
Print content text.

Parameters:
content - the content text, this text will be escaped

finish

public void finish()
Finish the XML document.


finish

public void finish(java.lang.String footer)
Finish the XML document, printing the given footer text at the end of the document.

Parameters:
footer - the footer text, this will not be escaped

spacing

public void spacing()
Print the current spacing (determined by the indentation level) into the document. This method is used by many of the other formatting methods, and so should only need to be called in the case of custom text printing outside the mechanisms provided by this class.


escapeString

protected void escapeString(java.lang.String str)
Escape a string such that it is safe to use in an XML document.

Parameters:
str - the string to escape


Copyright © 2007 Regents of the University of California