BEA Systems, Inc.

WebLogic Server 6.0sp2 API Reference

weblogic.apache.xalan.xpath.xml
Class FormatterToXML

java.lang.Object
  |
  +--weblogic.apache.xalan.xpath.xml.FormatterToXML
Direct Known Subclasses:
FormatterToHTML

public class FormatterToXML
extends java.lang.Object
implements org.xml.sax.DocumentHandler, org.xml.sax.ext.LexicalHandler, RawCharacterHandler

FormatterToXML formats SAX-style events into XML. Warning: this class will be replaced by the Xerces Serializer classes.

This class is based on an implementation from the Apache XML Project. In future releases the XML parser, XSLT processor, and associated classes will likely be updated to be based on a later version of the Apache implementations. Since Apache does not guarantee backwards compatibility between versions of their software, we cannot guarantee backwards compatibility of any of the classes contained in the weblogic.apache package or sub-packages.

Author:
Copyright © 2001 BEA Systems, Inc. All Rights Reserved.

Field Summary
 int indent
          Amount to indent.
protected  int level
          Current level of indent.
 char[] m_attrCharsMap
           
 char[] m_attrSpecialChars
          These are characters that will be escaped in the output.
protected  byte[] m_byteBuf
           
protected  char[] m_charBuf
           
 char[] m_charsMap
           
protected  int m_currentIndent
          Flag to keep track of the indent amount.
protected  boolean m_doIndent
          Flag to tell if indenting (pretty-printing) is on.
protected  BoolStack m_elemStack
          A stack of Boolean objects that tell if the given element has children.
protected  java.lang.String m_encoding
          The character encoding.
 boolean m_escapeCData
          If true, characters in cdata sections are escaped, instead of being writted out as cdata sections.
protected  boolean m_inCData
           
protected  boolean m_inEntityRef
          Tells if we're in an EntityRef event.
protected  boolean m_ispreserve
          State flag to tell if preservation of whitespace is important.
protected  boolean m_isprevtext
          State flag that tells if the previous node processed was text, so we can tell if we should preserve whitespace.
protected  java.lang.String m_lineSep
          Use the system line seperator to write line breaks.
protected  int m_lineSepLen
          The length of the line seperator, since the write is done one character at a time.
protected  java.io.OutputStream m_outputStream
           
protected  int m_pos
           
protected  BoolStack m_preserves
          Stack to keep track of whether or not we need to preserve whitespace.
 boolean m_shouldNotWriteXMLHeader
          Tells if we should write the XML declaration.
 boolean m_spaceBeforeClose
          Add space before '/>' for XHTML.
 boolean m_stripCData
          If true, cdata sections are simply stripped of their CDATA brackets, without escaping.
 java.lang.String m_version
          Tells the XML version, for writing out to the XML decl.
protected  java.io.Writer m_writer
          The writer where the XML will be written.
protected static java.util.Hashtable s_revsize
           
 
Constructor Summary
FormatterToXML()
          Default constructor.
FormatterToXML(FormatterToXML xmlListener)
          Constructor using a writer.
FormatterToXML(java.io.OutputStream os)
          Constructor using an output stream, and a simple OutputFormat.
FormatterToXML(java.io.Writer writer)
          Constructor using a writer.
 
Method Summary
protected  void accum(byte b)
          Append a byte to the buffer.
protected  void accum(char b)
          Append a character to the buffer.
protected  void accum(char[] chars, int start, int length)
          Append a character to the buffer.
protected  void accum(java.lang.String s)
          Append a character to the buffer.
 void cdata(char[] ch, int start, int length)
          Receive notification of cdata.
 void characters(char[] chars, int start, int length)
          Receive notification of character data.
 void charactersRaw(char[] ch, int start, int length)
          If available, when the disable-output-escaping attribute is used, output raw text without escaping.
protected  boolean childNodesWereAdded()
          Tell if child nodes have been added to the current element.
 void comment(char[] ch, int start, int length)
          Report an XML comment anywhere in the document.
static java.lang.String convertJava2MimeEncoding(java.lang.String encoding)
          Convert a Java encoding name to MIME charset name.
static java.lang.String convertMime2JavaEncoding(java.lang.String mimeCharsetName)
          Convert a MIME charset name, also known as an XML encoding name, to a Java encoding name.
 void endCDATA()
          Report the end of a CDATA section.
 void endDocument()
          Receive notification of the end of a document.
 void endDTD()
          Report the end of DTD declarations.
 void endElement(java.lang.String name)
          Receive notification of the end of an element.
 void endEntity(java.lang.String name)
          Report the end of an entity.
 void entityReference(java.lang.String name)
          Receive notivication of a entityReference.
 void flush()
           
 void flushWriter()
          Flush the formatter's result stream.
 java.io.Writer getWriter()
          Return the Writer.
 void ignorableWhitespace(char[] ch, int start, int length)
          Receive notification of ignorable whitespace in element content.
 void indent(int n)
          Prints a newline character and n spaces.
 void init(java.io.OutputStream output, OutputFormat format)
          Initialize the serializer with the specified output stream and output format.
 void init(java.io.Writer writer, OutputFormat format)
          Initialize the serializer with the specified writer and output format.
protected  void initAttrCharsMap()
          Set the attribute characters what will require special mapping.
protected  void initCharsMap()
          Set the characters what will require special mapping.
static void initEncodings()
           
protected  void openElementForChildren()
          Flag the current element as not yet having any children.
protected  void outputLineSep()
          Output a system-dependent line break.
 void printSpace(int n)
          Prints n spaces.
protected  void processAttribute(java.lang.String name, java.lang.String value)
          Process an attribute.
 void processingInstruction(java.lang.String target, java.lang.String data)
          Receive notification of a processing instruction.
 void setDocumentLocator(org.xml.sax.Locator locator)
          Receive an object for locating the origin of SAX document events.
protected  boolean shouldIndent()
           
 void startCDATA()
          Report the start of a CDATA section.
 void startDocument()
          Receive notification of the beginning of a document.
 void startDTD(java.lang.String name, java.lang.String publicId, java.lang.String systemId)
          Report the start of DTD declarations, if any.
 void startElement(java.lang.String name, org.xml.sax.AttributeList atts)
          Receive notification of the beginning of an element.
 void startEntity(java.lang.String name)
          Report the beginning of an entity.
 void writeAttrString(java.lang.String string, java.lang.String encoding)
          Returns the specified string after substituting specials, and UTF-16 surrogates for chracter references &#xnn.
protected  void writeParentTagEnd()
          Check to see if a parent's ">" has been written, and, if it has not, write it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_writer

protected java.io.Writer m_writer
The writer where the XML will be written.

m_outputStream

protected java.io.OutputStream m_outputStream

m_stripCData

public boolean m_stripCData
If true, cdata sections are simply stripped of their CDATA brackets, without escaping.

m_escapeCData

public boolean m_escapeCData
If true, characters in cdata sections are escaped, instead of being writted out as cdata sections.

m_encoding

protected java.lang.String m_encoding
The character encoding. Must match the encoding used for the printWriter.

m_shouldNotWriteXMLHeader

public boolean m_shouldNotWriteXMLHeader
Tells if we should write the XML declaration.

m_version

public java.lang.String m_version
Tells the XML version, for writing out to the XML decl.

m_elemStack

protected BoolStack m_elemStack
A stack of Boolean objects that tell if the given element has children.

m_lineSep

protected final java.lang.String m_lineSep
Use the system line seperator to write line breaks.

m_lineSepLen

protected final int m_lineSepLen
The length of the line seperator, since the write is done one character at a time.

m_ispreserve

protected boolean m_ispreserve
State flag to tell if preservation of whitespace is important.

m_preserves

protected BoolStack m_preserves
Stack to keep track of whether or not we need to preserve whitespace.

m_isprevtext

protected boolean m_isprevtext
State flag that tells if the previous node processed was text, so we can tell if we should preserve whitespace.

m_doIndent

protected boolean m_doIndent
Flag to tell if indenting (pretty-printing) is on.

m_currentIndent

protected int m_currentIndent
Flag to keep track of the indent amount.

indent

public int indent
Amount to indent.

level

protected int level
Current level of indent.

m_inEntityRef

protected boolean m_inEntityRef
Tells if we're in an EntityRef event.

m_attrSpecialChars

public char[] m_attrSpecialChars
These are characters that will be escaped in the output.

m_attrCharsMap

public char[] m_attrCharsMap

m_charsMap

public char[] m_charsMap

m_spaceBeforeClose

public boolean m_spaceBeforeClose
Add space before '/>' for XHTML.

m_inCData

protected boolean m_inCData

m_charBuf

protected char[] m_charBuf

m_byteBuf

protected byte[] m_byteBuf

m_pos

protected int m_pos

s_revsize

protected static java.util.Hashtable s_revsize
Constructor Detail

FormatterToXML

public FormatterToXML()
Default constructor.

FormatterToXML

public FormatterToXML(java.io.Writer writer)
Constructor using a writer.

Parameters:
writer - The character output stream to use.

FormatterToXML

public FormatterToXML(java.io.OutputStream os)
               throws java.io.UnsupportedEncodingException
Constructor using an output stream, and a simple OutputFormat.

Parameters:
writer - The character output stream to use.

FormatterToXML

public FormatterToXML(FormatterToXML xmlListener)
Constructor using a writer.

Parameters:
writer - The character output stream to use.
Method Detail

getWriter

public java.io.Writer getWriter()
Return the Writer.


outputLineSep

protected final void outputLineSep()
                            throws org.xml.sax.SAXException
Output a system-dependent line break.


initAttrCharsMap

protected void initAttrCharsMap()
Set the attribute characters what will require special mapping.


initCharsMap

protected void initCharsMap()
Set the characters what will require special mapping.


init

public void init(java.io.Writer writer,
                 OutputFormat format)
Initialize the serializer with the specified writer and output format. Must be called before calling any of the serialize methods.

Parameters:
writer - The writer to use
format - The output format

init

public void init(java.io.OutputStream output,
                 OutputFormat format)
          throws java.io.UnsupportedEncodingException
Initialize the serializer with the specified output stream and output format. Must be called before calling any of the serialize methods.

Parameters:
output - The output stream to use
format - The output format
Throws:
java.io.UnsupportedEncodingException - The encoding specified in the output format is not supported

setDocumentLocator

public void setDocumentLocator(org.xml.sax.Locator locator)
Receive an object for locating the origin of SAX document events.
Specified by:
setDocumentLocator in interface org.xml.sax.DocumentHandler

Parameters:
locator - An object that can return the location of any SAX document event.
See Also:
Locator

startDocument

public void startDocument()
                   throws org.xml.sax.SAXException
Receive notification of the beginning of a document.
Specified by:
startDocument in interface org.xml.sax.DocumentHandler

Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

endDocument

public void endDocument()
                 throws org.xml.sax.SAXException
Receive notification of the end of a document.
Specified by:
endDocument in interface org.xml.sax.DocumentHandler

Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

startDTD

public void startDTD(java.lang.String name,
                     java.lang.String publicId,
                     java.lang.String systemId)
              throws org.xml.sax.SAXException
Report the start of DTD declarations, if any. Any declarations are assumed to be in the internal subset unless otherwise indicated.
Specified by:
startDTD in interface org.xml.sax.ext.LexicalHandler

Parameters:
name - The document type name.
publicId - The declared public identifier for the external DTD subset, or null if none was declared.
systemId - The declared system identifier for the external DTD subset, or null if none was declared.
Throws:
org.xml.sax.SAXException - The application may raise an exception.
See Also:
endDTD(), startEntity(java.lang.String)

endDTD

public void endDTD()
            throws org.xml.sax.SAXException
Report the end of DTD declarations.
Specified by:
endDTD in interface org.xml.sax.ext.LexicalHandler

Throws:
org.xml.sax.SAXException - The application may raise an exception.
See Also:
startDTD(java.lang.String, java.lang.String, java.lang.String)

startElement

public void startElement(java.lang.String name,
                         org.xml.sax.AttributeList atts)
                  throws org.xml.sax.SAXException
Receive notification of the beginning of an element.
Specified by:
startElement in interface org.xml.sax.DocumentHandler

Parameters:
name - The element type name.
atts - The attributes attached to the element, if any.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.
See Also:
endElement(java.lang.String), AttributeList

writeParentTagEnd

protected void writeParentTagEnd()
                          throws org.xml.sax.SAXException
Check to see if a parent's ">" has been written, and, if it has not, write it.


openElementForChildren

protected void openElementForChildren()
Flag the current element as not yet having any children.


childNodesWereAdded

protected boolean childNodesWereAdded()
Tell if child nodes have been added to the current element. Must be called in balance with openElementForChildren().


endElement

public void endElement(java.lang.String name)
                throws org.xml.sax.SAXException
Receive notification of the end of an element.
Specified by:
endElement in interface org.xml.sax.DocumentHandler

Parameters:
name - The element type name
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

processAttribute

protected void processAttribute(java.lang.String name,
                                java.lang.String value)
                         throws org.xml.sax.SAXException
Process an attribute.

Parameters:
name - The name of the attribute.
value - The value of the attribute.

processingInstruction

public void processingInstruction(java.lang.String target,
                                  java.lang.String data)
                           throws org.xml.sax.SAXException
Receive notification of a processing instruction.
Specified by:
processingInstruction in interface org.xml.sax.DocumentHandler

Parameters:
target - The processing instruction target.
data - The processing instruction data, or null if none was supplied.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.

comment

public void comment(char[] ch,
                    int start,
                    int length)
             throws org.xml.sax.SAXException
Report an XML comment anywhere in the document. This callback will be used for comments inside or outside the document element, including comments in the external DTD subset (if read).
Specified by:
comment in interface org.xml.sax.ext.LexicalHandler

Parameters:
ch - An array holding the characters in the comment.
start - The starting position in the array.
length - The number of characters to use from the array.
Throws:
org.xml.sax.SAXException - The application may raise an exception.

startCDATA

public void startCDATA()
                throws org.xml.sax.SAXException
Report the start of a CDATA section.
Specified by:
startCDATA in interface org.xml.sax.ext.LexicalHandler

Throws:
org.xml.sax.SAXException - The application may raise an exception.
See Also:
endCDATA()

endCDATA

public void endCDATA()
              throws org.xml.sax.SAXException
Report the end of a CDATA section.
Specified by:
endCDATA in interface org.xml.sax.ext.LexicalHandler

Throws:
org.xml.sax.SAXException - The application may raise an exception.
See Also:
startCDATA()

cdata

public void cdata(char[] ch,
                  int start,
                  int length)
           throws org.xml.sax.SAXException
Receive notification of cdata.

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information.

The application must not attempt to read from the array outside of the specified range.

Note that some parsers will report whitespace using the ignorableWhitespace() method rather than this one (validating parsers must do so).

Parameters:
ch - The characters from the XML document.
start - The start position in the array.
length - The number of characters to read from the array.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.
See Also:
ignorableWhitespace(char[], int, int), Locator

accum

protected final void accum(byte b)
                    throws org.xml.sax.SAXException
Append a byte to the buffer.


accum

protected final void accum(char b)
                    throws org.xml.sax.SAXException
Append a character to the buffer.


accum

protected final void accum(char[] chars,
                           int start,
                           int length)
                    throws org.xml.sax.SAXException
Append a character to the buffer.


accum

protected final void accum(java.lang.String s)
                    throws org.xml.sax.SAXException
Append a character to the buffer.


flushWriter

public final void flushWriter()
                       throws org.xml.sax.SAXException
Flush the formatter's result stream.


flush

public final void flush()
                 throws org.xml.sax.SAXException


characters

public void characters(char[] chars,
                       int start,
                       int length)
                throws org.xml.sax.SAXException
Receive notification of character data.

The Parser will call this method to report each chunk of character data. SAX parsers may return all contiguous character data in a single chunk, or they may split it into several chunks; however, all of the characters in any single event must come from the same external entity, so that the Locator provides useful information.

The application must not attempt to read from the array outside of the specified range.

Note that some parsers will report whitespace using the ignorableWhitespace() method rather than this one (validating parsers must do so).

Specified by:
characters in interface org.xml.sax.DocumentHandler

Parameters:
chars - The characters from the XML document.
start - The start position in the array.
length - The number of characters to read from the array.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.
See Also:
ignorableWhitespace(char[], int, int), Locator

charactersRaw

public void charactersRaw(char[] ch,
                          int start,
                          int length)
                   throws org.xml.sax.SAXException
If available, when the disable-output-escaping attribute is used, output raw text without escaping.
Specified by:
charactersRaw in interface RawCharacterHandler


ignorableWhitespace

public void ignorableWhitespace(char[] ch,
                                int start,
                                int length)
                         throws org.xml.sax.SAXException
Receive notification of ignorable whitespace in element content. Not sure how to get this invoked quite yet.
Specified by:
ignorableWhitespace in interface org.xml.sax.DocumentHandler

Parameters:
ch - The characters from the XML document.
start - The start position in the array.
length - The number of characters to read from the array.
Throws:
org.xml.sax.SAXException - Any SAX exception, possibly wrapping another exception.
See Also:
characters(char[], int, int)

startEntity

public void startEntity(java.lang.String name)
                 throws org.xml.sax.SAXException
Report the beginning of an entity. The start and end of the document entity are not reported. The start and end of the external DTD subset are reported using the pseudo-name "[dtd]". All other events must be properly nested within start/end entity events.
Specified by:
startEntity in interface org.xml.sax.ext.LexicalHandler

Parameters:
name - The name of the entity. If it is a parameter entity, the name will begin with '%'.
Throws:
org.xml.sax.SAXException - The application may raise an exception.
See Also:
endEntity(java.lang.String), org.xml.sax.misc.DeclHandler#internalEntityDecl, org.xml.sax.misc.DeclHandler#externalEntityDecl

endEntity

public void endEntity(java.lang.String name)
               throws org.xml.sax.SAXException
Report the end of an entity.
Specified by:
endEntity in interface org.xml.sax.ext.LexicalHandler

Parameters:
name - The name of the entity that is ending.
Throws:
org.xml.sax.SAXException - The application may raise an exception.
See Also:
startEntity(java.lang.String)

entityReference

public void entityReference(java.lang.String name)
                     throws org.xml.sax.SAXException
Receive notivication of a entityReference.


writeAttrString

public void writeAttrString(java.lang.String string,
                            java.lang.String encoding)
                     throws org.xml.sax.SAXException
Returns the specified string after substituting specials, and UTF-16 surrogates for chracter references &#xnn.

Parameters:
string - String to convert to XML format.
specials - Chracters, should be represeted in chracter referenfces.
encoding - CURRENTLY NOT IMPLEMENTED.
Returns:
XML-formatted string.
See Also:
#backReference

shouldIndent

protected boolean shouldIndent()


printSpace

public void printSpace(int n)
                throws org.xml.sax.SAXException
Prints n spaces.

Parameters:
pw - The character output stream to use.
n - Number of spaces to print.
Throws:
java.io.IOException - Thrown if pw is invalid.

indent

public void indent(int n)
            throws org.xml.sax.SAXException
Prints a newline character and n spaces.

Parameters:
pw - The character output stream to use.
n - Number of spaces to print.
Throws:
java.io.IOException - Thrown if pw is invalid.

initEncodings

public static void initEncodings()


convertMime2JavaEncoding

public static java.lang.String convertMime2JavaEncoding(java.lang.String mimeCharsetName)
                                                 throws java.io.UnsupportedEncodingException
Convert a MIME charset name, also known as an XML encoding name, to a Java encoding name.

Parameters:
mimeCharsetName - Case insensitive MIME charset name: UTF-8, US-ASCII, ISO-8859-1, ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6, ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-2022-JP, Shift_JIS, EUC-JP, GB2312, Big5, EUC-KR, ISO-2022-KR, KOI8-R, EBCDIC-CP-US, EBCDIC-CP-CA, EBCDIC-CP-NL, EBCDIC-CP-DK, EBCDIC-CP-NO, EBCDIC-CP-FI, EBCDIC-CP-SE, EBCDIC-CP-IT, EBCDIC-CP-ES, EBCDIC-CP-GB, EBCDIC-CP-FR, EBCDIC-CP-AR1, EBCDIC-CP-HE, EBCDIC-CP-CH, EBCDIC-CP-ROECE, EBCDIC-CP-YU, EBCDIC-CP-IS and EBCDIC-CP-AR2.
Returns:
Java encoding name, or null if mimeCharsetName is unknown.
See Also:
#reverse

convertJava2MimeEncoding

public static java.lang.String convertJava2MimeEncoding(java.lang.String encoding)
Convert a Java encoding name to MIME charset name. Available values of encoding are "UTF8", "ISO8859_1", "ISO8859_2", "ISO8859_3", "ISO8859_4", "ISO8859_5", "ISO8859_6", "ISO8859_7", "ISO8859_8", "ISO8859_9", "JIS", "SJIS", "EUCJIS", "GB2312", "BIG5", "KSC5601", "ISO2022KR", "KOI8_R", "CP037", "CP277", "CP278", "CP280", "CP284", "CP285", "CP297", "CP420", "CP424", "CP500", "CP870", "CP871" and "CP918".

Parameters:
encoding - Case insensitive Java encoding name: UTF8, ISO8859_1, ISO8859_2, ISO8859_3, ISO8859_4, ISO8859_5, ISO8859_6, ISO8859_7, ISO8859_8, ISO8859_9, JIS, SJIS, EUCJIS, GB2312, BIG5, KSC5601, ISO2022KR, KOI8_R, CP037, CP277, CP278, CP280, CP284, CP285, CP297, CP420, CP424, CP500, CP870, CP871 and CP918.
Returns:
MIME charset name, or null if encoding is unknown.
See Also:
#convert

Documentation is available at
http://download.oracle.com/docs/cd/E13222_01/wls/docs60