|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.io.Writer
java.io.PrintWriter
org.progeeks.util.IndentPrintWriter
org.progeeks.util.xml.XmlPrintWriter
public class XmlPrintWriter
Extends the functionality of the IndentPrintWriter to keep track of tag state and provide attribute support, etc.. Also, all output is encoded as appropriate for its context.
It is important to note that this class only keeps rudimentary state and cannot (is not designed to) validate the XML on the way out. It is a helper for programmatically generating declarative XML in a way that's often easier then constructing a full DOM hierarchy.
Field Summary |
---|
Fields inherited from class java.io.PrintWriter |
---|
out |
Fields inherited from class java.io.Writer |
---|
lock |
Constructor Summary | |
---|---|
XmlPrintWriter(java.io.Writer out)
|
Method Summary | |
---|---|
void |
clearPushedAttributes()
Clears all pushed attributes. |
void |
close()
Automatically finished writing any in-progess tags and then closes the file. |
protected void |
closeBlock()
Closes the current tag or the current comment block depending on what block is currently open. |
void |
closeComment()
Closes the comment block. |
void |
closeDataBlock()
Closes a CDATA block. |
void |
closeDocType()
Closes the open DOCTYPE element. |
void |
closeMarkupDeclaration()
Closes any open markup declaration. |
void |
closeProcessingInstruction()
Closes the current processing instruction block. |
void |
closeRawText()
Closes a section of raw text output. |
protected void |
closeTag()
Closes the current tag if open. |
boolean |
getSingleLineAttributes()
Returns true if attributes are currently rendered on a single line. |
int |
getTagStackDepth()
Returns the current tag stack depth. |
static void |
main(java.lang.String[] args)
Used for testing. |
void |
popAllTags()
Pops all current tags from this XmlPrintWriter effectively returning to the root state. |
java.lang.String |
popTag()
Prints the closing element for the current tag and pops the indent. |
void |
popTag(java.lang.String tag)
Prints the closing elements for the stacked tags until the specified tag is reached and popped. |
void |
printAttribute(java.lang.String name,
java.lang.String value)
Writes the specified attribute to the current tag if it is still open. |
protected void |
printBypass(java.lang.String s)
Bypasses the automatic block termination by sending the data directly to the super class. |
void |
printComment(java.lang.String comment)
Prints the specified string as an enclosed comment. |
void |
println()
Resets the new line state and outputs a line separator. |
protected void |
printlnBypass(java.lang.String s)
Bypasses the automatic block termination by sending the data directly to the super class. |
void |
printProcessingInstruction(java.lang.String target)
Prints the specified processing instruction and opens it for additional writing with printAttribute() or free form printing. |
protected void |
printPushedAttributes()
Iterates over the accumulated pushed attributes and prints them. |
void |
printRawText(java.lang.String comment)
Prints the specified string as raw text without any encoding being performed. |
void |
printTag(java.lang.String tag)
Prints the specified tag in single-element form, ie: <tag /> This is different that the normal pushTag() because if another tag or any text is written then the printed tag will automatically be closed with the &/gt; instead of nesting the tags or text. |
void |
pushAttribute(java.lang.String name,
java.lang.String value)
Adds an attribute to the set of attributes that will be automatically applied to the next rendered tag. |
void |
pushTag(java.lang.String tag)
Prints the tag start and sets up the indent for nesting. |
void |
setSingleLineAttributes(boolean flag)
Set to true to print each attribute on its own line. |
void |
startComment()
Starts a comment block. |
void |
startDataBlock()
Starts a CDATA block. |
void |
startDocType(java.lang.String root,
java.lang.String systemLiteral,
java.lang.String publicId)
Prints the XML DOCTYPE declaration. |
void |
startMarkupDeclaration(java.lang.String markup)
Starts a markup declaration such as ENTITY, ATTLIST and so on. |
void |
startRawText()
Starts a section of raw text output. |
void |
write(char[] buff,
int off,
int len)
Overridden to provide XML encoding when appropriate. |
void |
write(int c)
Overridden to provide XML encoding when appropriate. |
void |
write(java.lang.String s,
int off,
int len)
Overridden to provide XML encoding when appropriate. |
protected void |
writeEncoded(char[] buff,
int offset,
int len)
|
protected void |
writeEncoded(int c)
Writes the specified value out as an encoded string if necessary. |
protected void |
writeEncoded(java.lang.String s,
int offset,
int len)
|
protected void |
writeInvalidChar(int c)
Called to write out characters that are invalid in XML. |
Methods inherited from class org.progeeks.util.IndentPrintWriter |
---|
endLine, getDefaultIndent, isNewLine, popIndent, pushIndent, pushIndent, setDefaultIndent, setOverrideIndent |
Methods inherited from class java.io.PrintWriter |
---|
append, append, append, checkError, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, setError, write, write |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public XmlPrintWriter(java.io.Writer out)
Method Detail |
---|
public void setSingleLineAttributes(boolean flag)
public boolean getSingleLineAttributes()
public void pushTag(java.lang.String tag)
public void printTag(java.lang.String tag)
public void printProcessingInstruction(java.lang.String target)
public void closeProcessingInstruction()
public void printAttribute(java.lang.String name, java.lang.String value)
name
- The name of the attribute.value
- The value to be written. If the value is null then
the attribute will not be written.public void pushAttribute(java.lang.String name, java.lang.String value)
public void clearPushedAttributes()
protected void printPushedAttributes()
public java.lang.String popTag()
public void popTag(java.lang.String tag)
public void startDocType(java.lang.String root, java.lang.String systemLiteral, java.lang.String publicId)
Example: startDocType( "root", null, null ); Generates: <!DOCTYPE root> Example: startDocType( "root", "foo.dtd", null ); Generates: <!DOCTYPE root SYSTEM "foo.dtd" > Example: startDocType( "root", "foo.dtd", "foo" ); Generates: <!DOCTYPE root PUBLIC "foo" "foo.dtd" >
public void closeDocType()
public void startMarkupDeclaration(java.lang.String markup)
public void closeMarkupDeclaration()
public int getTagStackDepth()
public void printComment(java.lang.String comment)
public void startComment()
public void closeComment()
public void startDataBlock()
public void closeDataBlock()
public void printRawText(java.lang.String comment)
public void startRawText()
public void closeRawText()
protected void closeBlock()
protected void closeTag()
public void popAllTags()
public void close()
close
in interface java.io.Closeable
close
in class java.io.PrintWriter
public void write(int c)
write
in class IndentPrintWriter
public void println()
IndentPrintWriter
println
in class IndentPrintWriter
public void write(char[] buff, int off, int len)
write
in class IndentPrintWriter
public void write(java.lang.String s, int off, int len)
write
in class IndentPrintWriter
protected void printBypass(java.lang.String s)
protected void printlnBypass(java.lang.String s)
protected void writeEncoded(java.lang.String s, int offset, int len)
protected void writeEncoded(char[] buff, int offset, int len)
protected void writeInvalidChar(int c)
protected void writeEncoded(int c)
public static void main(java.lang.String[] args) throws java.io.IOException
java.io.IOException
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |