Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

org.progeeks.meta.xml
Class XmlPrintWriter  view XmlPrintWriter download XmlPrintWriter.java

java.lang.Object
  extended byjava.io.Writer
      extended byjava.io.PrintWriter
          extended byorg.progeeks.meta.xml.IndentPrintWriter
              extended byorg.progeeks.meta.xml.XmlPrintWriter

public class XmlPrintWriter
extends IndentPrintWriter

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.

Version:
$Revision: 1.4 $

Field Summary
private  int context
          Holds the current context mode to keep track of whether or not we are in a tag, a comment, a CDATA block, or just normal body text.
private static int CTX_BODY
           
private static int CTX_COMMENT
           
private static int CTX_DATA
           
private static int CTX_TAG
           
private  boolean hasAttributes
          True if attributes have been written for the current tag.
private  java.lang.String openTag
          The name of the currently open tag or null if there is no open tag.
private  boolean singleTag
          Set to true if the current tag is in single tag form, ie: This is necessary for auto-closing the tag.
private  java.util.List tags
           
 
Fields inherited from class org.progeeks.meta.xml.IndentPrintWriter
 
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 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.
protected  void closeTag()
          Closes the current tag if open.
private static java.lang.String encodeXml(java.lang.String value, boolean quoted)
          Returns an encoded safe string for the specified value.
protected  boolean isEncodable(int c)
          Returns true if the specified character should be encoded.
static void main(java.lang.String[] args)
           
 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.
 void printComment(java.lang.String comment)
          Prints the specified string as an enclosed comment.
 void printTag(java.lang.String tag)
          Prints the specified tag in single-element form, ie:
 void pushTag(java.lang.String tag)
          Prints the tag start and sets up the indent for nesting.
 void startComment()
          Starts a comment block.
 void startDataBlock()
          Starts a CDATA block.
 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(int c)
          Writes the specified value out as an encoded string if necessary.
 
Methods inherited from class org.progeeks.meta.xml.IndentPrintWriter
endLine, popIndent, println, pushIndent, pushIndent, setOverrideIndent
 
Methods inherited from class java.io.PrintWriter
checkError, flush, print, print, print, print, print, print, print, print, print, 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
 

Field Detail

tags

private java.util.List tags

CTX_BODY

private static final int CTX_BODY
See Also:
Constant Field Values

CTX_TAG

private static final int CTX_TAG
See Also:
Constant Field Values

CTX_COMMENT

private static final int CTX_COMMENT
See Also:
Constant Field Values

CTX_DATA

private static final int CTX_DATA
See Also:
Constant Field Values

context

private int context
Holds the current context mode to keep track of whether or not we are in a tag, a comment, a CDATA block, or just normal body text.


openTag

private java.lang.String openTag
The name of the currently open tag or null if there is no open tag.


hasAttributes

private boolean hasAttributes
True if attributes have been written for the current tag.


singleTag

private boolean singleTag
Set to true if the current tag is in single tag form, ie: This is necessary for auto-closing the tag.

Constructor Detail

XmlPrintWriter

public XmlPrintWriter(java.io.Writer out)
Method Detail

pushTag

public void pushTag(java.lang.String tag)
Prints the tag start and sets up the indent for nesting.


printTag

public void printTag(java.lang.String tag)
Prints the specified tag in single-element form, ie:


printAttribute

public void printAttribute(java.lang.String name,
                           java.lang.String value)
Writes the specified attribute to the current tag if it is still open. Otherwise it will throw an IllegalStateException. The name will be verified for XML attribute name restrictions and the value will be encoded as necessary for attribute values.


popTag

public java.lang.String popTag()
Prints the closing element for the current tag and pops the indent.


popTag

public void popTag(java.lang.String tag)
Prints the closing elements for the stacked tags until the specified tag is reached and popped. If the tag is never found than an illegal state exception is thrown.


printComment

public void printComment(java.lang.String comment)
Prints the specified string as an enclosed comment.


startComment

public void startComment()
Starts a comment block.


closeComment

public void closeComment()
Closes the comment block.


startDataBlock

public void startDataBlock()
Starts a CDATA block.


closeDataBlock

public void closeDataBlock()
Closes a CDATA block.


closeBlock

protected void closeBlock()
Closes the current tag or the current comment block depending on what block is currently open.


closeTag

protected void closeTag()
Closes the current tag if open. Otherwise this is a noop. Note this is not to be confused with popTag() which actually writes a closing tag in many cases.


close

public void close()
Automatically finished writing any in-progess tags and then closes the file.


write

public void write(int c)
Overridden to provide XML encoding when appropriate.

Overrides:
write in class IndentPrintWriter

write

public void write(char[] buff,
                  int off,
                  int len)
Overridden to provide XML encoding when appropriate.

Overrides:
write in class IndentPrintWriter

write

public void write(java.lang.String s,
                  int off,
                  int len)
Overridden to provide XML encoding when appropriate.

Overrides:
write in class IndentPrintWriter

isEncodable

protected boolean isEncodable(int c)
Returns true if the specified character should be encoded.


writeEncoded

protected void writeEncoded(int c)
Writes the specified value out as an encoded string if necessary.


encodeXml

private static java.lang.String encodeXml(java.lang.String value,
                                          boolean quoted)
Returns an encoded safe string for the specified value. If quoted is true then it is also safe to encode quotes and <> signs.


main

public static void main(java.lang.String[] args)
                 throws java.io.IOException