FileDocCategorySizeDatePackage
ToTextStream.javaAPI DocJava SE 5 API21398Fri Aug 26 14:56:02 BST 2005com.sun.org.apache.xml.internal.serializer

ToTextStream

public class ToTextStream extends ToStream
author
Santiago Pericas-Geertsen

Fields Summary
Constructors Summary
public ToTextStream()
Default constructor.

    super();
  
Methods Summary
public voidaddAttribute(java.lang.String uri, java.lang.String localName, java.lang.String rawName, java.lang.String type, java.lang.String value)

see
com.sun.org.apache.xml.internal.serializer.ExtendedContentHandler#addAttribute(String, String, String, String, String)

        // do nothing, just forget all about the attribute
    
public voidaddAttribute(java.lang.String name, java.lang.String value)
From XSLTC

        // do nothing, forget about the attribute
    
public voidaddUniqueAttribute(java.lang.String qName, java.lang.String value, int flags)
Add a unique attribute

        // do nothing, forget about the attribute 
    
public voidcdata(char[] ch, int start, int length)
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).

param
ch The characters from the XML document.
param
start The start position in the array.
param
length The number of characters to read from the array.
throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
see
#ignorableWhitespace
see
org.xml.sax.Locator

    try
    {
        writeNormalizedChars(ch, start, length, false, m_lineSepUse);
        if (m_tracer != null)
            super.fireCDATAEvent(ch, start, length);              
    }
    catch(IOException ioe)
    {
      throw new SAXException(ioe);
    }
  
public voidcharacters(java.lang.String characters)
From XSLTC

 
        final int length = characters.length();
        if (length > m_charsBuff.length)
        {
            m_charsBuff = new char[length*2 + 1];
        }
        characters.getChars(0, length, m_charsBuff, 0);
        characters(m_charsBuff, 0, length); 
    
public voidcharacters(char[] ch, int start, int length)
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).

param
ch The characters from the XML document.
param
start The start position in the array.
param
length The number of characters to read from the array.
throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
see
#ignorableWhitespace
see
org.xml.sax.Locator


    // this.accum(ch, start, length);
    flushPending();    
    
    try
    {
        writeNormalizedChars(ch, start, length, false, m_lineSepUse);
        if (m_tracer != null)
            super.fireCharEvent(ch, start, length);      
    }
    catch(IOException ioe)
    {
      throw new SAXException(ioe);
    }
  
public voidcharactersRaw(char[] ch, int start, int length)
If available, when the disable-output-escaping attribute is used, output raw text without escaping.

param
ch The characters from the XML document.
param
start The start position in the array.
param
length The number of characters to read from the array.
throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.


    try
    {
      writeNormalizedChars(ch, start, length, false, m_lineSepUse);
    }
    catch(IOException ioe)
    {
      throw new SAXException(ioe);
    }
  
public voidcomment(java.lang.String data)
Called when a Comment is to be constructed. Note that Xalan will normally invoke the other version of this method. %REVIEW% In fact, is this one ever needed, or was it a mistake?

param
data The comment data.
throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.

      final int length = data.length();
      if (length > m_charsBuff.length)
      {
          m_charsBuff = new char[length*2 + 1];
      }
      data.getChars(0, length, m_charsBuff, 0);
      comment(m_charsBuff, 0, length);
  
public voidcomment(char[] ch, int start, int length)
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).

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


    flushPending();
    if (m_tracer != null)
        super.fireCommentEvent(ch, start, length);
  
public voidendCDATA()

see
org.xml.sax.ext.LexicalHandler#endCDATA()

        // do nothing
    
public voidendDocument()
Receive notification of the end of a document.

The SAX parser will invoke this method only once, and it will be the last method invoked during the parse. The parser shall not invoke this method until it has either abandoned parsing (because of an unrecoverable error) or reached the end of input.

throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
throws
org.xml.sax.SAXException

    flushPending();
    flushWriter();
    if (m_tracer != null)
        super.fireEndDoc();
  
public voidendElement(java.lang.String elemName)

see
com.sun.org.apache.xml.internal.serializer.ExtendedContentHandler#endElement(String)

        if (m_tracer != null)
            super.fireEndElem(elemName);                       
    
public voidendElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String name)
Receive notification of the end of an element.

The SAX parser will invoke this method at the end of every element in the XML document; there will be a corresponding startElement() event for every endElement() event (even when the element is empty).

If the element name has a namespace prefix, the prefix will still be attached to the name.

param
namespaceURI The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
param
localName The local name (without prefix), or the empty string if Namespace processing is not being performed.
param
name The qualified name (with prefix), or the empty string if qualified names are not available.
param
name The element type name
throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
throws
org.xml.sax.SAXException

        if (m_tracer != null)
            super.fireEndElem(name);           
  
public voidentityReference(java.lang.String name)
Receive notivication of a entityReference.

param
name non-null reference to the name of the entity.
throws
org.xml.sax.SAXException

        if (m_tracer != null)
            super.fireEntityReference(name);    
  
public voidflushPending()

            if (m_needToCallStartDocument)
            {
                startDocumentInternal();
                m_needToCallStartDocument = false;
            }
    
public voidignorableWhitespace(char[] ch, int start, int length)
Receive notification of ignorable whitespace in element content.

Validating Parsers must use this method to report each chunk of ignorable whitespace (see the W3C XML 1.0 recommendation, section 2.10): non-validating parsers may also use this method if they are capable of parsing and using content models.

SAX parsers may return all contiguous whitespace 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.

param
ch The characters from the XML document.
param
start The start position in the array.
param
length The number of characters to read from the array.
throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
see
#characters
throws
org.xml.sax.SAXException


    try
    {
      writeNormalizedChars(ch, start, length, false, m_lineSepUse);
    }
    catch(IOException ioe)
    {
      throw new SAXException(ioe);
    }
  
public voidnamespaceAfterStartElement(java.lang.String prefix, java.lang.String uri)

        // no namespace support for HTML
    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)
Receive notification of a processing instruction.

The Parser will invoke this method once for each processing instruction found: note that processing instructions may occur before or after the main document element.

A SAX parser should never report an XML declaration (XML 1.0, section 2.8) or a text declaration (XML 1.0, section 4.3.1) using this method.

param
target The processing instruction target.
param
data The processing instruction data, or null if none was supplied.
throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
throws
org.xml.sax.SAXException

    // flush anything pending first
    flushPending();  
    
    if (m_tracer != null)
        super.fireEscapingEvent(target, data);  
  
protected voidstartDocumentInternal()
Receive notification of the beginning of a document.

The SAX parser will invoke this method only once, before any other methods in this interface or in DTDHandler (except for setDocumentLocator).

throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
throws
org.xml.sax.SAXException

    super.startDocumentInternal();

    m_needToCallStartDocument = false;

    // No action for the moment.
  
public voidstartElement(java.lang.String elementNamespaceURI, java.lang.String elementLocalName, java.lang.String elementName)
From XSLTC

        if (m_needToCallStartDocument)
            startDocumentInternal();        
        // time to fire off startlement event.
        if (m_tracer != null) {
            super.fireStartElem(elementName);
            this.firePseudoAttributes();
        }
        
        return;
    
public voidstartElement(java.lang.String namespaceURI, java.lang.String localName, java.lang.String name, org.xml.sax.Attributes atts)
Receive notification of the beginning of an element.

The Parser will invoke this method at the beginning of every element in the XML document; there will be a corresponding endElement() event for every startElement() event (even when the element is empty). All of the element's content will be reported, in order, before the corresponding endElement() event.

If the element name has a namespace prefix, the prefix will still be attached. Note that the attribute list provided will contain only attributes with explicit values (specified or defaulted): #IMPLIED attributes will be omitted.

param
namespaceURI The Namespace URI, or the empty string if the element has no Namespace URI or if Namespace processing is not being performed.
param
localName The local name (without prefix), or the empty string if Namespace processing is not being performed.
param
name The qualified name (with prefix), or the empty string if qualified names are not available.
param
atts The attributes attached to the element, if any.
throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
see
#endElement
see
org.xml.sax.AttributeList
throws
org.xml.sax.SAXException

    // time to fire off startElement event
    if (m_tracer != null) {
        super.fireStartElem(name);
        this.firePseudoAttributes();
    }
    return;
  
public booleanstartPrefixMapping(java.lang.String prefix, java.lang.String uri, boolean shouldFlush)

        // no namespace support for HTML
        return false;
    
public voidstartPrefixMapping(java.lang.String prefix, java.lang.String uri)

        // no namespace support for HTML
    
voidwriteNormalizedChars(char[] ch, int start, int length, boolean isCData, boolean useLineSep)
Normalize the characters, but don't escape. Different from SerializerToXML#writeNormalizedChars because it does not attempt to do XML escaping at all.

param
ch The characters from the XML document.
param
start The start position in the array.
param
length The number of characters to read from the array.
param
isCData true if a CDATA block should be built around the characters.
param
useLineSep true if the operating systems end-of-line separator should be output rather than a new-line character.
throws
IOException
throws
org.xml.sax.SAXException

    final java.io.Writer writer = m_writer;
    final int end = start + length;

    /* copy a few "constants" before the loop for performance */
    final char S_LINEFEED = CharInfo.S_LINEFEED;
    final int M_MAXCHARACTER = this.m_maxCharacter;

    if (isCData)
    {
        // This for() loop always increments i by one at the end
        // of the loop.  Additional increments of i adjust for when
        // two input characters are processed.
        for (int i = start; i < end; i++)
        {
            final char c = ch[i];

            if (S_LINEFEED == c && useLineSep)
            {
                writer.write(m_lineSep, 0, m_lineSepLen);
            }
            else if (c > M_MAXCHARACTER)
            {
                if (i != 0)
                    closeCDATA();

                // This needs to go into a function...
                if (isUTF16Surrogate(c))
                {
                    writeUTF16Surrogate(c, ch, i, end);
                    i++; // two input characters processed
                }
                else
                {
                    writer.write(c);
                }

                if ((i != 0) && (i < (end - 1)))
                {
                    writer.write(CDATA_DELIMITER_OPEN);
                    m_cdataTagOpen = true;
                }
            }
            else if (
                ((i < (end - 2))
                    && (']" == c)
                    && (']" == ch[i + 1])
                    && ('>" == ch[i + 2])))
            {
                writer.write(CDATA_CONTINUE);
                i += 2;
            }
            else
            {
                if (c <= M_MAXCHARACTER)
                {
                    writer.write(c);
                }

                else if (isUTF16Surrogate(c))
                {
                    writeUTF16Surrogate(c, ch, i, end);
                    i++; // two input characters processed
                }
                else
                {
                    /* The character is greater than the allowed 
                     * maximum value and it is not part of a UTF-16
                     * pair that would be put out as a character reference.
                     */
                    String encoding = getEncoding();
                    if (encoding != null)
                    {
                        /* The output encoding is known, 
                         * so somthing is wrong.
                         */ 
                        String integralValue = Integer.toString(c);
                        throw new SAXException(XMLMessages.createXMLMessage(
                            XMLErrorResources.ER_ILLEGAL_CHARACTER,
                            new Object[]{ integralValue, encoding}));
                    }
                    else 
                    {
                        /* The output encoding is not known,
                         * so just write it out as-is.
                         */                        
                        writer.write(c);
                    }
                }
            }
        }
    }
    else
    {
        // not in CDATA section
        for (int i = start; i < end; i++)
        {
            final char c = ch[i];

            if (S_LINEFEED == c && useLineSep)
            {
                writer.write(m_lineSep, 0, m_lineSepLen);
            }
            else if (c <= M_MAXCHARACTER)
            {
                writer.write(c);
            }
            else if (isUTF16Surrogate(c))
            {
                writeUTF16Surrogate(c, ch, i, end);
                i++; // two input characters processed
            }
            else
            {
                /* The character is greater than the allowed 
                 * maximum value and it is not part of a UTF-16
                 * pair that would be put out as a character reference.
                 */
                String encoding = getEncoding();
                if (encoding != null) 
                {
                    /* The output encoding is known, 
                     * so somthing is wrong.
                     */ 
                    String integralValue = Integer.toString(c);
                    throw new SAXException(XMLMessages.createXMLMessage(
                        XMLErrorResources.ER_ILLEGAL_CHARACTER,
                        new Object[]{ integralValue, encoding}));
                }
                else 
                {
                    /* The output encoding is not known,
                     * so just write it out as-is.
                     */                        
                    writer.write(c);
                }                
            }
        }
    }