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

StAXEvent2SAX

public class StAXEvent2SAX extends Object implements Locator, XMLReader
author
Suresh Kumar
author
Sunitha Reddy
since
1.6

Fields Summary
private final XMLEventReader
staxEventReader
private ContentHandler
_sax
private LexicalHandler
_lex
private SAXImpl
_saxImpl
private String
version
private String
encoding
Constructors Summary
public StAXEvent2SAX(XMLEventReader staxCore)

        

       
	staxEventReader = staxCore;
    
Methods Summary
private voidbridge()


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

            XMLEvent event = staxEventReader.peek();

            if (!event.isStartDocument() && !event.isStartElement()) {
                throw new IllegalStateException();
            }

            // if the parser is on START_DOCUMENT, skip ahead to the first element
            while( !event.isStartElement() ) {
                if (event.getEventType() == XMLStreamConstants.START_DOCUMENT){
                    version = ((StartDocument)event).getVersion();
                    if (((StartDocument)event).encodingSet())
                        encoding = ((StartDocument)event).getCharacterEncodingScheme();
                }
                
                event = staxEventReader.nextEvent();
            }
           
            handleStartDocument(event);

            do {
                // These are all of the events listed in the javadoc for
                // XMLEvent.
                // The spec only really describes 11 of them.
                switch (event.getEventType()) {
                    case XMLStreamConstants.START_ELEMENT :
                        depth++;
                        handleStartElement(event.asStartElement());
                        break;
                    case XMLStreamConstants.END_ELEMENT :
                        handleEndElement(event.asEndElement());
                        depth--;
                        break;
                    case XMLStreamConstants.CHARACTERS :
                        handleCharacters(event.asCharacters());
                        break;
                    case XMLStreamConstants.ENTITY_REFERENCE :
                        handleEntityReference();
                        break;
                    case XMLStreamConstants.PROCESSING_INSTRUCTION :
                        handlePI((ProcessingInstruction)event);
                        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=staxEventReader.nextEvent();
            } while (depth!=0);

            handleEndDocument();
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    
private org.xml.sax.AttributesgetAttributes(javax.xml.stream.events.StartElement event)
Get the attributes associated with the given START_ELEMENT StAXevent.

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

        AttributesImpl attrs = new AttributesImpl();

        if ( !event.isStartElement() ) {
            throw new InternalError(
                "getAttributes() attempting to process: " + event);
        }
        
        // 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 (Iterator i = event.getAttributes(); i.hasNext();) {
            Attribute staxAttr = (javax.xml.stream.events.Attribute)i.next();
            
            String uri = staxAttr.getName().getNamespaceURI();
            if (uri == null) {
                uri = "";
            }
            String localName = staxAttr.getName().getLocalPart();
            String prefix = staxAttr.getName().getPrefix();
            String qName;
            if (prefix == null || prefix.length() == 0) {
                qName = localName;
            } else {
                qName = prefix + ':" + localName;
            }
            String type = staxAttr.getDTDType();
            String value = staxAttr.getValue();
            
            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(javax.xml.stream.events.Characters event)

        try {
            _sax.characters(
                event.getData().toCharArray(),
                0,
                event.getData().length());
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    
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(javax.xml.stream.events.EndElement event)

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

            // end namespace bindings
            for( Iterator i = event.getNamespaces(); i.hasNext();) {
                String prefix = (String)i.next();
                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(javax.xml.stream.events.ProcessingInstruction event)

        try {
            _sax.processingInstruction(
                event.getTarget(),
                event.getData());
        } 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(javax.xml.stream.events.XMLEvent event)

        _sax.setDocumentLocator(new Locator2() {
            public int getColumnNumber() {
                return event.getLocation().getColumnNumber();
            }
            public int getLineNumber() {
                return event.getLocation().getLineNumber();
            }
            public String getPublicId() {
                return event.getLocation().getPublicId();
            }
            public String getSystemId() {
                return event.getLocation().getSystemId();
            }
            public String getXMLVersion(){
                return version;
            }
            public String getEncoding(){
                return encoding;
            }
            
        });
        _sax.startDocument();
    
private voidhandleStartElement(javax.xml.stream.events.StartElement event)

        try {
            // start namespace bindings
            for (Iterator i = event.getNamespaces(); i.hasNext();) {
                String prefix = ((Namespace)i.next()).getPrefix();
                if (prefix == null) { // true for default namespace
                    prefix = "";
                }
                _sax.startPrefixMapping(
                    prefix,
                    event.getNamespaceURI(prefix));
            }

            // fire startElement
            QName qName = event.getName();
            String prefix = qName.getPrefix();
            String rawname;
            if (prefix == null || prefix.length() == 0) {
                rawname = qName.getLocalPart();
            } else {
                rawname = prefix + ':" + qName.getLocalPart();
            }
            
            Attributes saxAttrs = getAttributes(event);
            _sax.startElement(
                qName.getNamespaceURI(),
                qName.getLocalPart(),
                rawname,
                saxAttrs);
        } catch (SAXException e) {
            throw new XMLStreamException(e);
        }
    
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 voidparse(org.xml.sax.InputSource unused)

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

        bridge();
    
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.