org.cdlib.xtf.util
Class XMLFormatter

Object
  extended by XMLFormatter

public class XMLFormatter
extends Object

This class provides a simple way to produce indented XML with matched begin and end tags.

To use this class, you basically do the following:

XMLFormatter formatter = new XMLFormatter(); formatter.tabSize(4); // (Defaults to 2 spaces if not specified) formatter.procInstr( "xml version=\"1.0\" encoding=\"utf-8\"" ) formatter.beginTag( "tagName", "tagAttr=\"value\"" ) formatter.text( "A bunch of text within a tag." ) + newLine(2) formatter.endTag(); String result = formatter.toString();

This will produce an XML string with the following contents:

<?xml version="1.0" encoding="utf-8"?> <tagName tagAttr="value"> A bunch of text within a tag. </tagName>


Field Summary
private  StringBuffer buf
          Buffer to accumulate the results in
 int defaultTabSize
          Default amount to indent when tab() is called.
private  boolean mBlankLineAfterTag
          Automatically insert blank lines after tags
private static String newLines
          Used for multiple blank line output
private static String spaces
          Used for tabbing
private  int tabCount
          Current tab level for this thread
private  int tabSize
          Amount to indent when tab() is called.
private  LinkedList tagStack
          Stack of current tag nestings
private  boolean tagStartOpen
          Is there currently a begin tag open?
 
Constructor Summary
XMLFormatter()
           
 
Method Summary
 void attr(String attName, float attValue)
          Format a tag attribute from an attribute name and an associated floating-point value.
 void attr(String attName, int attValue)
          Format a tag attribute from an attribute name and an associated integer value.
 void attr(String attName, String attValue)
          Format a tag attribute from an attribute name and an associated string value.
 void beginTag(String tagName)
          Add a string containing a properly indented begin tag consisting only of the tag name.
 void beginTag(String tagName, String tagAtts)
          Format a string containing a properly indented begin tag consisting of a tag name and a list of attributes.
 boolean blankLineAfterTag()
          Return whether or not a blank line will automatically be inserted after each new tag.
 boolean blankLineAfterTag(boolean enable)
          Set whether or not a blank line will automatically be inserted after each new tag.
private  void closeTagStart()
          If there has been a beginTag(), we need to be sure and add the closing ">" before doing anything else.
 void endAllTags()
          Add a string containing properly indented end tags for any remaining open tags.
 void endTag()
          Add a string containing a properly indented end tag for the closest open tag (if any.)
static String escapeText(String text)
          Change any XML-special characters to their ampersand equivalents.
 void newLine()
          Add a single new-line.
 void newLine(int lineCount)
          Add a specified number of new-lines.
 void newLineAfterText()
          Adds a string containing one or two new-lines depending on whether the user wants blank lines after tags or not.
 void procInstr(String procStr)
          Format a processing instruction tag at the current level of indentation.
 void rawText(String str)
          Adds a text string, unformatted and unescaped, directly to the buffer.
private  void tab()
          Indent by the current tab size for all subsequent calls to FormatXML output functions.
 int tabCount()
          Get the current tab indent level (in spaces).
 int tabSize()
          Return the current tab size used for indenting nested tags.
 int tabSize(int newTabSize)
          Set the tab size used for indenting nested tags.
 void tag(String tagStr)
          Format an element tag.
 void text(String str)
          Format a string of text at the current level of indentation.
 void text(String str, int maxWidth)
          Format a string containing text broken across multiple indented lines less than or equal to a maximum line length.
 NodeInfo toNode(Configuration config)
          Get the results as a Saxon NodeInfo.
 Source toSource()
          Get the results as a Saxon-compatible Source.
 String toString()
          Get the formatted results as a string.
private  void untab()
          Un-indent by the current tab size for all subsequent calls to FormatXML output functions.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

buf

private StringBuffer buf
Buffer to accumulate the results in


defaultTabSize

public int defaultTabSize
Default amount to indent when tab() is called.


spaces

private static final String spaces
Used for tabbing

See Also:
Constant Field Values

newLines

private static final String newLines
Used for multiple blank line output

See Also:
Constant Field Values

tagStack

private LinkedList tagStack
Stack of current tag nestings


tabCount

private int tabCount
Current tab level for this thread


tagStartOpen

private boolean tagStartOpen
Is there currently a begin tag open?


mBlankLineAfterTag

private boolean mBlankLineAfterTag
Automatically insert blank lines after tags


tabSize

private int tabSize
Amount to indent when tab() is called.

Constructor Detail

XMLFormatter

public XMLFormatter()
Method Detail

blankLineAfterTag

public boolean blankLineAfterTag()
Return whether or not a blank line will automatically be inserted after each new tag.

The default behavior is to insert a blank line after each tag. To change this behavior, call the blankLineAfterTag(boolean enable) function.

Returns:
true - Blank lines will be inserted after each new tag. false - No blank lines will be inserted after each new tag.

blankLineAfterTag

public boolean blankLineAfterTag(boolean enable)
Set whether or not a blank line will automatically be inserted after each new tag.

Parameters:
enable - Enable (true) or disable (false) automatic blank line insertion after each tag.
Returns:
The value of the blank line flag just prior to this call.

tabSize

public int tabSize()
Return the current tab size used for indenting nested tags.

By default, the tab size is set to 2 spaces. To change the indent size, call the tabSize(int newTabSize) method.

Returns:
The number of spaces each a nested line will be indented from its container.

tabSize

public int tabSize(int newTabSize)
Set the tab size used for indenting nested tags.

Parameters:
newTabSize - The new tab size (in spaces) to indent a nested tag from its containing tag.
Returns:
The number of spaces each a nested line will be indented from its container.

beginTag

public void beginTag(String tagName)
Add a string containing a properly indented begin tag consisting only of the tag name.

Parameters:
tagName - The name of the tag to create.

closeTagStart

private void closeTagStart()
If there has been a beginTag(), we need to be sure and add the closing ">" before doing anything else.


attr

public void attr(String attName,
                 String attValue)
Format a tag attribute from an attribute name and an associated string value.


attr

public void attr(String attName,
                 int attValue)
Format a tag attribute from an attribute name and an associated integer value.


attr

public void attr(String attName,
                 float attValue)
Format a tag attribute from an attribute name and an associated floating-point value.


beginTag

public void beginTag(String tagName,
                     String tagAtts)
Format a string containing a properly indented begin tag consisting of a tag name and a list of attributes.

Parameters:
tagName - The name of the tag to create.
tagAtts - A string of attributes to tadd to the tag.
Notes:
Use the attr() method and its cousins to simplify constructing attribute name/value pairs.

endTag

public void endTag()
Add a string containing a properly indented end tag for the closest open tag (if any.)


endAllTags

public void endAllTags()
Add a string containing properly indented end tags for any remaining open tags.


tag

public void tag(String tagStr)
Format an element tag.

Parameters:
tagStr - The string to place in the tag.

procInstr

public void procInstr(String procStr)
Format a processing instruction tag at the current level of indentation.

Parameters:
procStr - The processing instruction string to place in the tag.

text

public void text(String str)
Format a string of text at the current level of indentation.

Parameters:
str - The text to indent.

text

public void text(String str,
                 int maxWidth)
Format a string containing text broken across multiple indented lines less than or equal to a maximum line length.

Parameters:
str - The string to break across multiple lines.
maxWidth - The maximum width for each line.

rawText

public void rawText(String str)
Adds a text string, unformatted and unescaped, directly to the buffer.

Parameters:
str - The string to add to the buffer.

newLine

public void newLine()
Add a single new-line.


newLine

public void newLine(int lineCount)
Add a specified number of new-lines.

Parameters:
lineCount - The number of new-lines to add.

newLineAfterText

public void newLineAfterText()
Adds a string containing one or two new-lines depending on whether the user wants blank lines after tags or not.


tabCount

public int tabCount()
Get the current tab indent level (in spaces).


tab

private void tab()
Indent by the current tab size for all subsequent calls to FormatXML output functions.


untab

private void untab()
Un-indent by the current tab size for all subsequent calls to FormatXML output functions.


escapeText

public static String escapeText(String text)
Change any XML-special characters to their ampersand equivalents.

Parameters:
text - Text to scan
Returns:
Modified version with escaped characters.

toString

public String toString()
Get the formatted results as a string.

Overrides:
toString in class Object

toSource

public Source toSource()
Get the results as a Saxon-compatible Source.


toNode

public NodeInfo toNode(Configuration config)
Get the results as a Saxon NodeInfo.