com.bristle.javalib.xml
Class XMLWriter

java.lang.Object
  extended by com.bristle.javalib.xml.XMLWriter

public class XMLWriter
extends Object

This class writes formatted XML strings to a Writer. It is a class, rather than just a collection of routines, to make the calling code simpler. As a class, it can use instance variables to store things like the Writer, which would otherwise have to be passed on each method call.

 Usage:
   - The following is a typical scenario for using this class:
       XMLWriter writer = new XMLWriter(writerOut);
       writer.writeStartTag("abcd");
       writer.write("some text in the abcd tag");
       writer.writeEndTag("abcd");
 Assumptions:
 Effects:
       - None.
 Anticipated Changes:
 Notes:
 Implementation Notes:
 Portability Issues:
 Revision History:
   $Log$
 


Nested Class Summary
static class XMLWriter.Tester
          Each class contains a Tester inner class with a main() for easier unit testing.
 
Field Summary
static boolean blnWITH_NEWLINE
           
static boolean m_blnFORCE_LOG
           
private  String m_strNewLine
           
private  Writer m_writer
           
 
Constructor Summary
XMLWriter(Writer writer)
          Constructor.
 
Method Summary
 boolean checkError()
          Check for errors in the nested Writer, if it's a PrintWriter.
 String getNewLineString()
          Get the newline String.
 boolean logProgressAndCheckError(Logger logger, int intCount)
          Log the progress of the calling routine in generating its data stream, and check for errors that have occurred when the calling routine wrote to this XMLWriter.
 boolean logProgressAndCheckError(Logger logger, int intCount, boolean blnForceLog)
          Log the progress of the calling routine in generating its data stream, and check for errors that have occurred when the calling routine wrote to its XMLWriter.
 void setNewLineString(String strNew)
          Set the newline String.
 void write(String strText)
          Write the line of text to the Writer.
 void writeEndTag(String strTag)
          Write the end version of the specified XML tag to the Writer, followed by a newline.
 void writeEndTag(String strTag, boolean blnNewLine)
          Write the end version of the specified XML tag to the Writer, optionally followed by a newline.
 void writeln()
          Write a newline to the Writer.
 void writeln(String strText)
          Write the line of text to the Writer, followed by a newline.
 void writeStartTag(String strTag)
          Write the start version of the specified XML tag to the Writer, followed by a newline.
 void writeStartTag(String strTag, boolean blnNewLine)
          Write the start version of the specified XML tag to the Writer, optionally followed by a newline.
 void writeStartTagAndAttributes(String strTag, String strAttributes)
          Write the start version of the specified XML tag to the Writer, including a string containing one or more attributes, followed by a newline.
 void writeStartTagAndAttributes(String strTag, String strAttributes, boolean blnNewLine)
          Write the start version of the specified XML tag to the Writer, including a string containing one or more attributes, optionally followed by a newline.
 void writeTagAndValue(String strTag, String strValue)
          Write the specified value, enclosed in the start and end versions of the specified XML tag, to the Writer, followed by a newline.
 void writeTagAndValue(String strTag, String strValue, boolean blnNewLine)
          Write the specified value, enclosed in the start and end versions of the specified XML tag, to the Writer, optionally followed by a newline.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_writer

private Writer m_writer

m_strNewLine

private String m_strNewLine

m_blnFORCE_LOG

public static final boolean m_blnFORCE_LOG
See Also:
Constant Field Values

blnWITH_NEWLINE

public static final boolean blnWITH_NEWLINE
See Also:
Constant Field Values
Constructor Detail

XMLWriter

public XMLWriter(Writer writer)
Constructor.

Parameters:
writer - The Writer to write the XML to.
Method Detail

setNewLineString

public void setNewLineString(String strNew)
Set the newline String. Recommended values are: org.apache.xml.serialize.LineSeparator.Macintosh; org.apache.xml.serialize.LineSeparator.Unix; org.apache.xml.serialize.LineSeparator.Web; org.apache.xml.serialize.LineSeparator.Windows;

Parameters:
strNew - The new value.

getNewLineString

public String getNewLineString()
Get the newline String.

Returns:
The newline String.

write

public void write(String strText)
           throws IOException
Write the line of text to the Writer.

Parameters:
strText - String of text.
Throws:
IOException - When an error occurs writing to the Writer.

writeln

public void writeln()
             throws IOException
Write a newline to the Writer.

Throws:
IOException - When an error occurs writing to the Writer.

writeln

public void writeln(String strText)
             throws IOException
Write the line of text to the Writer, followed by a newline.

Parameters:
strText - String of text.
Throws:
IOException - When an error occurs writing to the Writer.

writeStartTag

public void writeStartTag(String strTag,
                          boolean blnNewLine)
                   throws IOException
Write the start version of the specified XML tag to the Writer, optionally followed by a newline.

Parameters:
strTag - XML tag (without angle brackets).
blnNewLine - Boolean flag indicating whether to write a newline after the tag.
Throws:
IOException - When an error occurs writing to the Writer.

writeStartTag

public void writeStartTag(String strTag)
                   throws IOException
Write the start version of the specified XML tag to the Writer, followed by a newline.

Parameters:
strTag - XML tag (without angle brackets).
Throws:
IOException - When an error occurs writing to the Writer.

writeStartTagAndAttributes

public void writeStartTagAndAttributes(String strTag,
                                       String strAttributes,
                                       boolean blnNewLine)
                                throws IOException
Write the start version of the specified XML tag to the Writer, including a string containing one or more attributes, optionally followed by a newline.

Parameters:
strTag - XML tag (without angle brackets).
strAttributes - Attributes to be included in the start tag
blnNewLine - Boolean flag indicating whether to write a newline after the tag.
Throws:
IOException - When an error occurs writing to the Writer.

writeStartTagAndAttributes

public void writeStartTagAndAttributes(String strTag,
                                       String strAttributes)
                                throws IOException
Write the start version of the specified XML tag to the Writer, including a string containing one or more attributes, followed by a newline.

Parameters:
strTag - XML tag (without angle brackets).
strAttributes - Attributes to be included in the start tag
Throws:
IOException - When an error occurs writing to the Writer.

writeEndTag

public void writeEndTag(String strTag,
                        boolean blnNewLine)
                 throws IOException
Write the end version of the specified XML tag to the Writer, optionally followed by a newline.

Parameters:
strTag - XML tag (without angle brackets or slash).
blnNewLine - Boolean flag indicating whether to write a newline after the tag.
Throws:
IOException - When an error occurs writing to the Writer.

writeEndTag

public void writeEndTag(String strTag)
                 throws IOException
Write the end version of the specified XML tag to the Writer, followed by a newline.

Parameters:
strTag - XML tag (without angle brackets).
Throws:
IOException - When an error occurs writing to the Writer.

writeTagAndValue

public void writeTagAndValue(String strTag,
                             String strValue,
                             boolean blnNewLine)
                      throws IOException
Write the specified value, enclosed in the start and end versions of the specified XML tag, to the Writer, optionally followed by a newline.

Parameters:
strTag - XML tag (without angle brackets).
strValue - String value to enclose in XML tags.
blnNewLine - Boolean flag indicating whether to write a newline after the formatted XML.
Throws:
IOException - When an error occurs writing to the Writer.

writeTagAndValue

public void writeTagAndValue(String strTag,
                             String strValue)
                      throws IOException
Write the specified value, enclosed in the start and end versions of the specified XML tag, to the Writer, followed by a newline.

Parameters:
strTag - XML tag (without angle brackets).
strValue - String value to enclose in XML tags.
Throws:
IOException - When an error occurs writing to the Writer.

checkError

public boolean checkError()
Check for errors in the nested Writer, if it's a PrintWriter.

Returns:
True if errors have occurred; False if no errors, or the nested Writer is not a PrintWriter.

logProgressAndCheckError

public boolean logProgressAndCheckError(Logger logger,
                                        int intCount,
                                        boolean blnForceLog)
Log the progress of the calling routine in generating its data stream, and check for errors that have occurred when the calling routine wrote to its XMLWriter. Typically called every row of data, but only bothers to do its various tasks (log, check for error, garbage collect) periodically at different periods. Return True if any errors have occurred; False otherwise.

Parameters:
logger - Logger to log progress messages.
intCount - Count of times called (used internally to decide whether to bother with periodic tasks).
blnForceLog - Flag to force logging at this call, instead of just periodically. Useful to log the final data row.
Returns:
True if errors have occurred; False otherwise.

logProgressAndCheckError

public boolean logProgressAndCheckError(Logger logger,
                                        int intCount)
Log the progress of the calling routine in generating its data stream, and check for errors that have occurred when the calling routine wrote to this XMLWriter. Typically called every row of data, but only bothers to do its various tasks (log, check for error, garbage collect) periodically at different periods. Return True if any errors have occurred; False otherwise.

Parameters:
logger - Logger to log progress messages.
intCount - Count of times called (used internally to decide whether to bother with periodic tasks).
Returns:
True if errors have occurred; False otherwise.