FileDocCategorySizeDatePackage
StAXStream2SAX.javaAPI DocJava SE 6 API17002Tue Jun 10 00:22:34 BST 2008com.sun.org.apache.xalan.internal.xsltc.trax

StAXStream2SAX

public class StAXStream2SAX extends Object implements Locator, XMLReader
author
Padmaja Vedula
author
Sunitha Reddy

Fields Summary
private final XMLStreamReader
staxStreamReader
private ContentHandler
_sax
private LexicalHandler
_lex
private SAXImpl
_saxImpl
Constructors Summary
public StAXStream2SAX(XMLStreamReader staxSrc)

    //private Hashtable _nsPrefixes = new Hashtable();
    
       
            staxStreamReader = staxSrc;
    
Methods Summary
public voidbridge()


        try {
            // remembers the nest level of elements to know when we are done.
            int depth=0;

            // skip over START_DOCUMENT
            int event = staxStreamReader.getEventType();
            if (event == XMLStreamConstants.START_DOCUMENT) {
                event = staxStreamReader.next();
            }

            // If not a START_ELEMENT (e.g., a DTD), skip to next tag
            if (event != XMLStreamConstants.START_ELEMENT) {
                event = staxStreamReader.nextTag();
                // An error if a START_ELEMENT isn't found now
                if (event != XMLStreamConstants.START_ELEMENT) {
                    throw new IllegalStateException("The current event is " +
                            "not START_ELEMENT\n but" + event);
                }            
            }
                  
            handleStartDocument();

            do {
                // These are all of the events listed in the javadoc for
                // XMLEvent.
                // The spec only really describes 11 of them.
                switch (event) {
                    case XMLStreamConstants.START_ELEMENT :
                        depth++;
                        handleStartElement();
                        break;
                    case XMLStreamConstants.END_ELEMENT :
                        handleEndElement();
                        depth--;
                        break;
                    case XMLStreamConstants.CHARACTERS :
                        handleCharacters();
                        break;
                    case XMLStreamConstants.ENTITY_REFERENCE :
                        handleEntityReference();
                        break;
                    case XMLStreamConstants.PROCESSING_INSTRUCTION :
                        handlePI();
                        break;
                    case XMLStreamConstants.COMMENT :
                        handleComment();
                        break;
                    case XMLStreamConstants.DTD :
                        handleDTD();
                        break;
                    case XMLStreamConstants.ATTRIBUTE :
                        handleAttribute();
                        break;
                    case XMLStreamConstants.NAMESPACE :
                        handleNamespace();
                        break;
                    case XMLStreamConstants.CDATA :
                        handleCDATA();
                        break;
                    case XMLStreamConstants.ENTITY_DECLARATION :
                        handleEntityDecl();
                        break;
                    case XMLStreamConstants.NOTATION_DECLARATION :
                        handleNotationDecl();
                        break;
                    case XMLStreamConstants.SPACE :
                        handleSpace();
                        break;
                    default :
                        throw new InternalError("processing event: " + event);
                }
                
                event=staxStreamReader.next();
            } while (depth!=0);

            handleEndDocument();
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    
private org.xml.sax.AttributesgetAttributes()
Get the attributes associated with the given START_ELEMENT or ATTRIBUTE StAXevent.

return
the StAX attributes converted to an org.xml.sax.Attributes

        AttributesImpl attrs = new AttributesImpl();

        int eventType = staxStreamReader.getEventType();
        if (eventType != XMLStreamConstants.ATTRIBUTE
            && eventType != XMLStreamConstants.START_ELEMENT) {
            throw new InternalError(
                "getAttributes() attempting to process: " + eventType);
        }
        
        // in SAX, namespace declarations are not part of attributes by default.
        // (there's a property to control that, but as far as we are concerned
        // we don't use it.) So don't add xmlns:* to attributes.

        // gather non-namespace attrs
        for (int i = 0; i < staxStreamReader.getAttributeCount(); i++) {
            String uri = staxStreamReader.getAttributeNamespace(i);
            if(uri==null)   uri="";
            String localName = staxStreamReader.getAttributeLocalName(i);
            String prefix = staxStreamReader.getAttributePrefix(i);
            String qName;
            if(prefix==null || prefix.length()==0)
                qName = localName;
            else
                qName = prefix + ':" + localName;
            String type = staxStreamReader.getAttributeType(i);
            String value = staxStreamReader.getAttributeValue(i);

            attrs.addAttribute(uri, localName, qName, type, value);
        }

        return attrs;
    
public intgetColumnNumber()
This class is only used internally so this method should never be called.

 
	return 0; 
    
public org.xml.sax.ContentHandlergetContentHandler()

 
	return _sax;
    
public org.xml.sax.DTDHandlergetDTDHandler()
This class is only used internally so this method should never be called.

 
	return null;
    
public org.xml.sax.EntityResolvergetEntityResolver()
This class is only used internally so this method should never be called.

	return null;
    
public org.xml.sax.ErrorHandlergetErrorHandler()
This class is only used internally so this method should never be called.

	return null;
    
public booleangetFeature(java.lang.String name)
This class is only used internally so this method should never be called.

	return false;
    
public intgetLineNumber()
This class is only used internally so this method should never be called.

 
	return 0; 
    
public java.lang.ObjectgetProperty(java.lang.String name)
This class is only used internally so this method should never be called.

	return null;
    
public java.lang.StringgetPublicId()
This class is only used internally so this method should never be called.

 
	return null; 
    
public java.lang.StringgetSystemId()
This class is only used internally so this method should never be called.

 
	return null; 
    
private voidhandleAttribute()

        // no-op ???
        // attribute events don't normally occur outside of a startElement
        // or endElement
    
private voidhandleCDATA()

        // no-op ???
        // this event is listed in the javadoc, but not in the spec.
    
private voidhandleCharacters()


        // workaround for bugid 5046319 - switch over to commented section
        // below when it is fixed.
        int textLength = staxStreamReader.getTextLength();
        char[] chars = new char[textLength];

        staxStreamReader.getTextCharacters(0, chars, 0, textLength);

        try {
            _sax.characters(chars, 0, chars.length);
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }

        
//        int start = 0;
//        int len;
//        do {
//            len = staxStreamReader.getTextCharacters(start, buf, 0, buf.length);
//            start += len;
//            try {
//                _sax.characters(buf, 0, len);
//            } catch (SAXException e) {
//                throw new XMLStreamException(e);
//            }
//        } while (len == buf.length);
    
private voidhandleComment()

        // no-op ???
    
private voidhandleDTD()

        // no-op ???
        // it seems like we need to pass this info along, but how?
    
private voidhandleEndDocument()

        _sax.endDocument();
    
private voidhandleEndElement()

        QName qName = staxStreamReader.getName();

        try {
            //construct prefix:localName from qName
            String qname = "";
            if (qName.getPrefix() != null && qName.getPrefix().trim().length() != 0){
                qname = qName.getPrefix() + ":";
            }
            qname += qName.getLocalPart();
            
            // fire endElement
            _sax.endElement(
                qName.getNamespaceURI(),
                qName.getLocalPart(),
                qname); 

            // end namespace bindings
            int nsCount = staxStreamReader.getNamespaceCount();
            for (int i = nsCount - 1; i >= 0; i--) {
                String prefix = staxStreamReader.getNamespacePrefix(i);
                if (prefix == null) { // true for default namespace
                    prefix = "";
                }
                _sax.endPrefixMapping(prefix);
            }
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    
private voidhandleEntityDecl()

        // no-op ???
        // this event is listed in the javadoc, but not in the spec.
    
private voidhandleEntityReference()

        // no-op ???
    
private voidhandleNamespace()

        // no-op ???
        // namespace events don't normally occur outside of a startElement
        // or endElement
    
private voidhandleNotationDecl()

        // no-op ???
        // this event is listed in the javadoc, but not in the spec.
    
private voidhandlePI()

        try {
            _sax.processingInstruction(
                staxStreamReader.getPITarget(),
                staxStreamReader.getPIData());
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    
private voidhandleSpace()

        // no-op ???
        // this event is listed in the javadoc, but not in the spec.
    
private voidhandleStartDocument()

        _sax.setDocumentLocator(new Locator2() {
            public int getColumnNumber() {
                return staxStreamReader.getLocation().getColumnNumber();
            }
            public int getLineNumber() {
                return staxStreamReader.getLocation().getLineNumber();
            }
            public String getPublicId() {
                return staxStreamReader.getLocation().getPublicId();
            }
            public String getSystemId() {
                return staxStreamReader.getLocation().getSystemId();
            }
            public String getXMLVersion() {
                return staxStreamReader.getVersion();
            }
            public String getEncoding() {
                return staxStreamReader.getEncoding();
            }
         });
        _sax.startDocument();
    
private voidhandleStartElement()


        try {
            // start namespace bindings
            int nsCount = staxStreamReader.getNamespaceCount();
            for (int i = 0; i < nsCount; i++) {
                String prefix = staxStreamReader.getNamespacePrefix(i);
                if (prefix == null) { // true for default namespace
                    prefix = "";
                }
                _sax.startPrefixMapping(
                    prefix,
                    staxStreamReader.getNamespaceURI(i));
            }

            // fire startElement
            QName qName = staxStreamReader.getName();
            String prefix = qName.getPrefix();
            String rawname;
            if(prefix==null || prefix.length()==0)
                rawname = qName.getLocalPart();
            else
                rawname = prefix + ':" + qName.getLocalPart();
            Attributes attrs = getAttributes();
            _sax.startElement(
                qName.getNamespaceURI(),
                qName.getLocalPart(),
                rawname,
                attrs);
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    
public voidparse(org.xml.sax.InputSource unused)

        try {
            bridge();
        } catch (XMLStreamException e) {
            throw new SAXException(e);
        }
    
public voidparse()

        bridge();
    
public voidparse(java.lang.String sysId)
This class is only used internally so this method should never be called.

	throw new IOException("This method is not yet implemented.");
    
public voidsetContentHandler(org.xml.sax.ContentHandler handler)

	_sax = handler;
	if (handler instanceof LexicalHandler) {
	    _lex = (LexicalHandler) handler;
	}
	
	if (handler instanceof SAXImpl) {
	    _saxImpl = (SAXImpl)handler;
	}
    
public voidsetDTDHandler(org.xml.sax.DTDHandler handler)
This class is only used internally so this method should never be called.

    
public voidsetEntityResolver(org.xml.sax.EntityResolver resolver)
This class is only used internally so this method should never be called.

    
public voidsetErrorHandler(org.xml.sax.ErrorHandler handler)
This class is only used internally so this method should never be called.

    
public voidsetFeature(java.lang.String name, boolean value)
This class is only used internally so this method should never be called.

    
public voidsetProperty(java.lang.String name, java.lang.Object value)
This class is only used internally so this method should never be called.