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

SAX2StAXEventWriter

public class SAX2StAXEventWriter extends SAX2StAXBaseWriter
author
Sunitha Reddy

Fields Summary
private XMLEventWriter
writer
private XMLEventFactory
eventFactory
private List
namespaceStack
private boolean
needToCallStartDocument
Constructors Summary
public SAX2StAXEventWriter()

   

      

        eventFactory = XMLEventFactory.newInstance();

    
public SAX2StAXEventWriter(XMLEventWriter writer)


        this.writer = writer;
        eventFactory = XMLEventFactory.newInstance();

    
public SAX2StAXEventWriter(XMLEventWriter writer, XMLEventFactory factory)


        this.writer = writer;
        if (factory != null) {

            this.eventFactory = factory;

        } else {

            eventFactory = XMLEventFactory.newInstance();

        }

    
Methods Summary
public voidcharacters(char[] ch, int start, int length)


        super.characters(ch, start, length);

        try {

            if (!isCDATA) {

                eventFactory.setLocation(getCurrentLocation());
                writer.add(eventFactory.createCharacters(new String(ch,
                        start, length)));

            }

        } catch (XMLStreamException e) {

            throw new SAXException(e);

        }

    
public voidcomment(char[] ch, int start, int length)


        super.comment(ch, start, length);

        eventFactory.setLocation(getCurrentLocation());
        try {

            writer.add(eventFactory.createComment(new String(ch, start,
                    length)));

        } catch (XMLStreamException e) {

            throw new SAXException(e);

        }

    
protected javax.xml.stream.events.NamespacecreateNamespace(java.lang.String prefix, java.lang.String uri)


        if (prefix == null || prefix.length() == 0) {

            return eventFactory.createNamespace(uri);

        } else {

            return eventFactory.createNamespace(prefix, uri);

        }

    
protected voidcreateStartEvents(org.xml.sax.Attributes attributes, java.util.Collection[] events)


        Map nsMap = null;
        List attrs = null;

        // create namespaces
        if (namespaces != null) {

            final int nDecls = namespaces.size();
                for (int i = 0; i < nDecls; i++) {
                    final String prefix = (String) namespaces.elementAt(i);
                    String uri = (String) namespaces.elementAt(i++);
                Namespace ns = createNamespace(prefix, uri);
                if (nsMap == null) {

                    nsMap = new HashMap();

                }
                nsMap.put(prefix, ns);

                
            }

        }

        // create attributes
        String[] qname = {null, null};
        for (int i = 0, s = attributes.getLength(); i < s; i++) {

            parseQName(attributes.getQName(i), qname);

            String attrPrefix = qname[0];
            String attrLocal = qname[1];

            String attrQName = attributes.getQName(i);
            String attrValue = attributes.getValue(i);
            String attrURI = attributes.getURI(i);

            if ("xmlns".equals(attrQName) || "xmlns".equals(attrPrefix)) {

                // namespace declaration disguised as an attribute. If the
                // namespace has already been declared, skip it, otherwise
                // write it as an namespace

                if (!nsMap.containsKey(attrPrefix)) {

                    Namespace ns = createNamespace(attrPrefix, attrValue);
                    if (nsMap == null) {

                        nsMap = new HashMap();

                    }
                    nsMap.put(attrPrefix, ns);

                }

            } else {

                Attribute attribute;
                if (attrPrefix.length() > 0) {

                    attribute = eventFactory.createAttribute(attrPrefix,
                            attrURI, attrLocal, attrValue);

                } else {

                    attribute = eventFactory.createAttribute(attrLocal,
                            attrValue);

                }

                if (attrs == null) {

                    attrs = new ArrayList();

                }
                attrs.add(attribute);

            }

        }

        events[0] = (nsMap == null ? Collections.EMPTY_LIST : nsMap.values());
        events[1] = (attrs == null ? Collections.EMPTY_LIST : attrs);

    
public voidendCDATA()


        eventFactory.setLocation(getCurrentLocation());
        try {

            writer.add(eventFactory.createCData(CDATABuffer.toString()));

        } catch (XMLStreamException e) {

            throw new SAXException(e);

        }

        super.endCDATA();

    
public voidendDocument()


        eventFactory.setLocation(getCurrentLocation());

        try {

            writer.add(eventFactory.createEndDocument());

        } catch (XMLStreamException e) {

            throw new SAXException(e);

        }

        super.endDocument();

        // clear the namespaces
        namespaceStack.clear();

    
public voidendElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)


        super.endElement(uri, localName, qName);

        eventFactory.setLocation(getCurrentLocation());

        // parse name
        String[] qname = {null, null};
        parseQName(qName, qname);

        // get namespaces
        Collection nsList = (Collection) namespaceStack.remove(namespaceStack.size() - 1);
        Iterator nsIter = nsList.iterator();

        try {

            writer.add(eventFactory.createEndElement(qname[0], uri, qname[1],
                    nsIter));

        } catch (XMLStreamException e) {

            throw new SAXException(e);

        }

    
public javax.xml.stream.XMLEventFactorygetEventFactory()


        return eventFactory;

    
public javax.xml.stream.XMLEventWritergetEventWriter()


        return writer;

    
public voidignorableWhitespace(char[] ch, int start, int length)


        super.ignorableWhitespace(ch, start, length);
        characters(ch, start, length);

    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)


        super.processingInstruction(target, data);
        try {

            writer.add(eventFactory.createProcessingInstruction(target, data));

        } catch (XMLStreamException e) {

            throw new SAXException(e);

        }

    
public voidsetEventFactory(javax.xml.stream.XMLEventFactory factory)


        this.eventFactory = factory;

    
public voidsetEventWriter(javax.xml.stream.XMLEventWriter writer)


        this.writer = writer;

    
public voidstartDocument()


        super.startDocument();

        namespaceStack.clear();

        eventFactory.setLocation(getCurrentLocation());
        
        // Encoding and version info will be available only after startElement 
        // is called for first time. So, defer START_DOCUMENT event of StAX till
        // that point of time.
        needToCallStartDocument = true;
        
    
public voidstartElement(java.lang.String uri, java.lang.String localName, java.lang.String qName, org.xml.sax.Attributes attributes)

        
        if (needToCallStartDocument){
            try {
                
                if (docLocator == null)
                    writer.add(eventFactory.createStartDocument());
                else {
                    try{
                        writer.add(eventFactory.createStartDocument(((Locator2)docLocator).getEncoding(),((Locator2)docLocator).getXMLVersion()));
                    }catch(ClassCastException e){
                        writer.add(eventFactory.createStartDocument());
                    }
                }
                
            } catch (XMLStreamException e) {

                throw new SAXException(e);

            }
            needToCallStartDocument = false;
        }
        
        // set document location
        eventFactory.setLocation(getCurrentLocation());

        // create attribute and namespace events
        Collection[] events = {null, null};
        createStartEvents(attributes, events);


        namespaceStack.add(events[0]);

        try {

            String[] qname = {null, null};
            parseQName(qName, qname);

            writer.add(eventFactory.createStartElement(qname[0], uri,
                    qname[1], events[1].iterator(), events[0].iterator()));

        } catch (XMLStreamException e) {

            throw new SAXException(e);

        } finally {

            super.startElement(uri, localName, qName, attributes);

        }