FileDocCategorySizeDatePackage
ToSAXHandler.javaAPI DocJava SE 6 API13006Tue Jun 10 00:23:08 BST 2008com.sun.org.apache.xml.internal.serializer

ToSAXHandler

public abstract class ToSAXHandler extends SerializerBase
This class is used to provide a base behavior to be inherited by other To...SAXHandler serializers. This class is not a public API.
xsl.usage
internal

Fields Summary
protected ContentHandler
m_saxHandler
Underlying SAX handler. Taken from XSLTC
protected LexicalHandler
m_lexHandler
Underlying LexicalHandler. Taken from XSLTC
private boolean
m_shouldGenerateNSAttribute
A startPrefixMapping() call on a ToSAXHandler will pass that call on to the wrapped ContentHandler, but should we also mirror these calls with matching attributes, if so this field is true. For example if this field is true then a call such as startPrefixMapping("prefix1","uri1") will also cause the additional internally generated attribute xmlns:prefix1="uri1" to be effectively added to the attributes passed to the wrapped ContentHandler.
protected TransformStateSetter
m_state
If this is true, then the content handler wrapped by this serializer implements the TransformState interface which will give the content handler access to the state of the transform.
Constructors Summary
public ToSAXHandler()

    
public ToSAXHandler(ContentHandler hdlr, LexicalHandler lex, String encoding)

        setContentHandler(hdlr);
        setLexHandler(lex);
        setEncoding(encoding);
    
public ToSAXHandler(ContentHandler handler, String encoding)

        setContentHandler(handler);
        setEncoding(encoding);
    
Methods Summary
public voidaddUniqueAttribute(java.lang.String qName, java.lang.String value, int flags)
Add a unique attribute

        addAttribute(qName, value); 
    
public voidcharacters(org.w3c.dom.Node node)
This method gets the node's value as a String and uses that String as if it were an input character notification.

param
node the Node to serialize
throws
org.xml.sax.SAXException

        // remember the current node
        if (m_state != null)
        {
            m_state.setCurrentNode(node);
        }
        
        // Get the node's value as a String and use that String as if
        // it were an input character notification.
        String data = node.getNodeValue();
        if (data != null) {
            this.characters(data);
        }
    
public voidcharacters(java.lang.String characters)
Receive notification of character data.

param
characters The string of characters to process.
throws
org.xml.sax.SAXException
see
ExtendedContentHandler#characters(String)

        final int len = characters.length();
        if (len > m_charsBuff.length)
        {
           m_charsBuff = new char[len*2 + 1];             
        }
        characters.getChars(0,len, m_charsBuff, 0);   
        characters(m_charsBuff, 0, len);
    
protected voidcloseCDATA()

        // Redefined in SAXXMLOutput
    
protected voidcloseStartTag()

    
public voidcomment(java.lang.String comment)
Receive notification of a comment.

see
ExtendedLexicalHandler#comment(String)

        flushPending();

        // Ignore if a lexical handler has not been set
        if (m_lexHandler != null)
        {
            final int len = comment.length();
            if (len > m_charsBuff.length)
            {
               m_charsBuff = new char[len*2 + 1];              
            }
            comment.getChars(0,len, m_charsBuff, 0);            
            m_lexHandler.comment(m_charsBuff, 0, len);
            // time to fire off comment event
            if (m_tracer != null)
                super.fireCommentEvent(m_charsBuff, 0, len);
        }

    
public voiderror(org.xml.sax.SAXParseException exc)

see
org.xml.sax.ErrorHandler#error(SAXParseException)

        super.error(exc);
        
        if (m_saxHandler instanceof ErrorHandler)
            ((ErrorHandler)m_saxHandler).error(exc);        
        
    
public voidfatalError(org.xml.sax.SAXParseException exc)

see
org.xml.sax.ErrorHandler#fatalError(SAXParseException)

        super.fatalError(exc);
        
        m_needToCallStartDocument = false;
        
        if (m_saxHandler instanceof ErrorHandler) {
            ((ErrorHandler)m_saxHandler).fatalError(exc);            
        }
    
public voidflushPending()
This method flushes any pending events, which can be startDocument() closing the opening tag of an element, or closing an open CDATA section.

    
            if (m_needToCallStartDocument)
            {
                startDocumentInternal();
                m_needToCallStartDocument = false;
            }

            if (m_elemContext.m_startTagOpen)
            {
                closeStartTag();
                m_elemContext.m_startTagOpen = false;
            }
            
            if (m_cdataTagOpen)
            {
                closeCDATA();
                m_cdataTagOpen = false;
            }

    
booleangetShouldOutputNSAttr()
Returns true if namespace declarations from calls such as startPrefixMapping("prefix1","uri1") should also be mirrored with self generated additional attributes of elements that declare the namespace, for example the attribute xmlns:prefix1="uri1"

        return m_shouldGenerateNSAttribute;
    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)
Do nothing as this is an abstract class. All subclasses will need to define their behavior if it is different.

see
org.xml.sax.ContentHandler#processingInstruction(String, String)

        // Redefined in SAXXMLOutput
    
public booleanreset()
Try's to reset the super class and reset this class for re-use, so that you don't need to create a new serializer (mostly for performance reasons).

return
true if the class was successfuly reset.
see
Serializer#reset()

        boolean wasReset = false;
        if (super.reset())
        {
            resetToSAXHandler();
            wasReset = true;
        }
        return wasReset;
    
private voidresetToSAXHandler()
Reset all of the fields owned by ToSAXHandler class

        this.m_lexHandler = null;
        this.m_saxHandler = null;
        this.m_state = null;
        this.m_shouldGenerateNSAttribute = false;
    
public voidsetCdataSectionElements(java.util.Vector URI_and_localNames)
Does nothing. The setting of CDATA section elements has an impact on stream serializers.

see
SerializationHandler#setCdataSectionElements(java.util.Vector)

        // do nothing
    
public voidsetContentHandler(org.xml.sax.ContentHandler _saxHandler)
Sets the SAX ContentHandler.

param
_saxHandler The ContentHandler to set

        this.m_saxHandler = _saxHandler;
        if (m_lexHandler == null && _saxHandler instanceof LexicalHandler)
        {
            // we are not overwriting an existing LexicalHandler, and _saxHandler
            // is also implements LexicalHandler, so lets use it
            m_lexHandler = (LexicalHandler) _saxHandler;
        }
    
public voidsetLexHandler(org.xml.sax.ext.LexicalHandler _lexHandler)
Sets the LexicalHandler.

param
_lexHandler The LexicalHandler to set

        this.m_lexHandler = _lexHandler;
    
public voidsetShouldOutputNSAttr(boolean doOutputNSAttr)
Set whether or not namespace declarations (e.g. xmlns:foo) should appear as attributes of elements

param
doOutputNSAttr whether or not namespace declarations should appear as attributes

        m_shouldGenerateNSAttribute = doOutputNSAttr;
    
public voidsetTransformState(com.sun.org.apache.xml.internal.serializer.TransformStateSetter ts)
Pass in a reference to a TransformState object, which can be used during SAX ContentHandler events to obtain information about he state of the transformation. This method will be called before each startDocument event.

param
ts A reference to a TransformState object

        this.m_state = ts;
    
public voidstartDTD(java.lang.String arg0, java.lang.String arg1, java.lang.String arg2)
Do nothing.

see
org.xml.sax.ext.LexicalHandler#startDTD(String, String, String)

        // do nothing for now
    
protected voidstartDocumentInternal()
Pass callback to the SAX Handler


               
        
    
        if (m_needToCallStartDocument)  
        {
            super.startDocumentInternal();

            m_saxHandler.startDocument();
            m_needToCallStartDocument = false;
        }
    
public voidstartElement(java.lang.String arg0, java.lang.String arg1, java.lang.String arg2, org.xml.sax.Attributes arg3)
Receive notification of the beginning of an element, although this is a SAX method additional namespace or attribute information can occur before or after this call, that is associated with this element.

throws
org.xml.sax.SAXException Any SAX exception, possibly wrapping another exception.
see
org.xml.sax.ContentHandler#startElement
see
org.xml.sax.ContentHandler#endElement
see
org.xml.sax.AttributeList
throws
org.xml.sax.SAXException
see
org.xml.sax.ContentHandler#startElement(String,String,String,Attributes)

        if (m_state != null) {
            m_state.resetState(getTransformer());
        }

        // fire off the start element event
        if (m_tracer != null)
            super.fireStartElem(arg2);
    
public voidstartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
Receives notification that an element starts, but attributes are not fully known yet.

param
uri the URI of the namespace of the element (optional)
param
localName the element name, but without prefix (optional)
param
qName the element name, with prefix, if any (required)
see
ExtendedContentHandler#startElement(String, String, String)

            
        if (m_state != null) {
            m_state.resetState(getTransformer());
        }

        // fire off the start element event
        if (m_tracer != null)
            super.fireStartElem(qName);         
    
public voidstartElement(java.lang.String qName)
An element starts, but attributes are not fully known yet.

param
qName the element name, with prefix (if any).
see
ExtendedContentHandler#startElement(String)

        if (m_state != null) {
            m_state.resetState(getTransformer());
        }        
        // fire off the start element event
        if (m_tracer != null)
            super.fireStartElem(qName);              
    
public voidwarning(org.xml.sax.SAXParseException exc)

see
org.xml.sax.ErrorHandler#warning(SAXParseException)

        super.warning(exc);
        
        if (m_saxHandler instanceof ErrorHandler)
            ((ErrorHandler)m_saxHandler).warning(exc);