public class XMLFormatter
extends Object
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();
Modifier and Type | Field and Description |
---|---|
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 and Description |
---|
XMLFormatter() |
Modifier and Type | Method and Description |
---|---|
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.
|
private StringBuffer buf
public int defaultTabSize
tab()
is called.private static final String spaces
private static final String newLines
private LinkedList tagStack
private int tabCount
private boolean tagStartOpen
private boolean mBlankLineAfterTag
private int tabSize
tab()
is called.public boolean blankLineAfterTag()
blankLineAfterTag(boolean enable)
function. true
- Blank lines will be inserted after each new tag.
false
- No blank lines will be inserted after each new tag.public boolean blankLineAfterTag(boolean enable)
enable
- Enable (true
) or disable (false
)
automatic blank line insertion after each tag.public int tabSize()
tabSize(int newTabSize)
method.public int tabSize(int newTabSize)
newTabSize
- The new tab size (in spaces) to indent a nested tag
from its containing tag.public void beginTag(String tagName)
tagName
- The name of the tag to create.private void closeTagStart()
public void attr(String attName, String attValue)
public void attr(String attName, int attValue)
public void attr(String attName, float attValue)
public void beginTag(String tagName, String tagAtts)
tagName
- The name of the tag to create.tagAtts
- A string of attributes to tadd to the tag.attr()
method
and its cousins to simplify constructing attribute name/value
pairs.public void endTag()
public void endAllTags()
public void tag(String tagStr)
tagStr
- The string to place in the tag.public void procInstr(String procStr)
procStr
- The processing instruction string to place in the tag.public void text(String str)
str
- The text to indent.public void text(String str, int maxWidth)
str
- The string to break across multiple lines.maxWidth
- The maximum width for each line.public void rawText(String str)
str
- The string to add to the buffer.public void newLine()
public void newLine(int lineCount)
lineCount
- The number of new-lines to add.public void newLineAfterText()
public int tabCount()
private void tab()
private void untab()
public static String escapeText(String text)
text
- Text to scanpublic String toString()
toString
in class Object
public Source toSource()
public NodeInfo toNode(Configuration config)