## org.apache.xalan.xpath.xml
Class FormatterToXML

    java.lang.Object
      |
      +--org.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.

Field Summary
`int` `indent` Amount to indent.
`char[]` `m_attrCharsMap`
`char[]` `m_attrSpecialChars` These are characters that will be escaped in the output.
`char[]` `m_charsMap`
`boolean` `m_escapeCData` If true, characters in cdata sections are escaped, instead of being writted out as cdata sections.
`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.

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
`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.
`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, org.apache.xml.serialize.OutputFormat format)` Initialize the serializer with the specified output stream and output format.
`void` `init(java.io.Writer writer, org.apache.xml.serialize.OutputFormat format)` Initialize the serializer with the specified writer and output format.
`static void` `initEncodings()`
`void` `printSpace(int n)` Prints n spaces.
`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.
`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`.

Methods inherited from class java.lang.Object
`equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Field Detail

### 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_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.

### indent

    public int indent

  Amount to indent.

### 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.

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.

### init

    public void init(java.io.Writer writer,
                     org.apache.xml.serialize.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,
                     org.apache.xml.serialize.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`

### 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.

### 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`

### 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`

### 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`

Copyright © 2000 Apache XML Project. All Rights Reserved.