Example usage for javax.xml.stream XMLStreamReader getEventType

List of usage examples for javax.xml.stream XMLStreamReader getEventType

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getEventType.

Prototype

public int getEventType();

Source Link

Document

Returns an integer code that indicates the type of the event the cursor is pointing to.

Usage

From source file:org.activiti.dmn.converter.util.DmnXMLUtil.java

public static DmnExtensionElement parseExtensionElement(XMLStreamReader xtr) throws Exception {
    DmnExtensionElement extensionElement = new DmnExtensionElement();
    extensionElement.setName(xtr.getLocalName());
    if (StringUtils.isNotEmpty(xtr.getNamespaceURI())) {
        extensionElement.setNamespace(xtr.getNamespaceURI());
    }/*from  ww w .j  a  va  2s  . c  o  m*/
    if (StringUtils.isNotEmpty(xtr.getPrefix())) {
        extensionElement.setNamespacePrefix(xtr.getPrefix());
    }

    for (int i = 0; i < xtr.getAttributeCount(); i++) {
        DmnExtensionAttribute extensionAttribute = new DmnExtensionAttribute();
        extensionAttribute.setName(xtr.getAttributeLocalName(i));
        extensionAttribute.setValue(xtr.getAttributeValue(i));
        if (StringUtils.isNotEmpty(xtr.getAttributeNamespace(i))) {
            extensionAttribute.setNamespace(xtr.getAttributeNamespace(i));
        }
        if (StringUtils.isNotEmpty(xtr.getAttributePrefix(i))) {
            extensionAttribute.setNamespacePrefix(xtr.getAttributePrefix(i));
        }
        extensionElement.addAttribute(extensionAttribute);
    }

    boolean readyWithExtensionElement = false;
    while (readyWithExtensionElement == false && xtr.hasNext()) {
        xtr.next();
        if (xtr.isCharacters() || XMLStreamReader.CDATA == xtr.getEventType()) {
            if (StringUtils.isNotEmpty(xtr.getText().trim())) {
                extensionElement.setElementText(xtr.getText().trim());
            }
        } else if (xtr.isStartElement()) {
            DmnExtensionElement childExtensionElement = parseExtensionElement(xtr);
            extensionElement.addChildElement(childExtensionElement);
        } else if (xtr.isEndElement() && extensionElement.getName().equalsIgnoreCase(xtr.getLocalName())) {
            readyWithExtensionElement = true;
        }
    }
    return extensionElement;
}

From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java

/**
 * @param reader//from w w w  .ja v a 2s  .com
 * @param writer
 * @param startAtNext indicate if reading should start at next event or current event
 * @throws XMLStreamException
 */
public void serialize(XMLStreamReader reader, XMLStreamWriter writer, boolean startAtNext)
        throws XMLStreamException {

    dataHandlerReader = XMLStreamReaderUtils.getDataHandlerReader(reader);
    dataHandlerWriter = XMLStreamWriterUtils.getDataHandlerWriter(writer);

    if (reader instanceof OMStAXWrapper) {
        int event = reader.getEventType();
        if (event <= 0 || event == XMLStreamReader.PROCESSING_INSTRUCTION
                || event == XMLStreamReader.START_DOCUMENT) {
            // Since we are serializing an entire document,
            // enable the the optimized DataSource events.
            // This will allow OMDataSource elements to be serialized
            // directly without expansion.
            if (log.isDebugEnabled()) {
                log.debug("Enable OMDataSource events while serializing this document");
            }
            ((OMStAXWrapper) reader).enableDataSourceEvents(true);
        }
    }
    try {
        serializeNode(reader, writer, startAtNext);
    } finally {
        if (reader instanceof OMStAXWrapper) {
            ((OMStAXWrapper) reader).enableDataSourceEvents(false);
        }
    }

}

From source file:org.apache.axiom.om.impl.serialize.StreamingOMSerializer.java

protected void serializeNode(XMLStreamReader reader, XMLStreamWriter writer, boolean startAtNext)
        throws XMLStreamException {
    // TODO We get the StAXWriter at this point and uses it hereafter 
    // assuming that this is the only entry point to this class.
    // If there can be other classes calling methodes of this we might 
    // need to change methode signatures to OMOutputer

    // If not startAtNext, then seed the processing with the current element.
    boolean useCurrentEvent = !startAtNext;

    while (reader.hasNext() || useCurrentEvent) {
        int event = 0;
        OMDataSource ds = null;// w ww .j  a  v  a  2 s. c  o  m
        if (useCurrentEvent) {
            event = reader.getEventType();
            useCurrentEvent = false;
        } else {
            event = reader.next();
        }

        // If the reader is exposing a DataSourc
        // for this event, then simply serialize the
        // DataSource
        if (reader instanceof OMStAXWrapper) {
            ds = ((OMStAXWrapper) reader).getDataSource();
        }
        if (ds != null) {
            ds.serialize(writer);
        } else {
            switch (event) {
            case START_ELEMENT:
                serializeElement(reader, writer);
                depth++;
                break;
            case ATTRIBUTE:
                serializeAttributes(reader, writer);
                break;
            case CHARACTERS:
                if (dataHandlerReader != null && dataHandlerReader.isBinary()) {
                    serializeDataHandler();
                    break;
                }
                // Fall through
            case SPACE:
                serializeText(reader, writer);
                break;
            case COMMENT:
                serializeComment(reader, writer);
                break;
            case CDATA:
                serializeCData(reader, writer);
                break;
            case PROCESSING_INSTRUCTION:
                serializeProcessingInstruction(reader, writer);
                break;
            case END_ELEMENT:
                serializeEndElement(writer);
                depth--;
                break;
            case START_DOCUMENT:
                depth++; //if a start document is found then increment the depth
                break;
            case END_DOCUMENT:
                if (depth != 0)
                    depth--; //for the end document - reduce the depth
                try {
                    serializeEndElement(writer);
                } catch (Exception e) {
                    //TODO: log exceptions
                }
            }
        }
        if (depth == 0) {
            break;
        }
    }
}

From source file:org.apache.axiom.util.stax.XMLStreamReaderUtils.java

/**
 * Get the text content of the current element as a {@link Reader} object.
 * //w w w .  j av  a2s .  com
 * @param reader
 *            The XML stream reader to read the element text from. The reader must be positioned
 *            on a {@link XMLStreamConstants#START_ELEMENT} event.
 * @param allowNonTextChildren
 *            If set to <code>true</code>, non text child nodes are allowed and skipped. If set
 *            to <code>false</code> only text nodes are allowed and the presence of any other
 *            type of child node will trigger an exception.
 * @return The reader from which the element text can be read. After the reader has reported the
 *         end of the stream, the XML stream reader will be positioned on the
 *         {@link XMLStreamConstants#END_ELEMENT} event corresponding to the initial
 *         {@link XMLStreamConstants#START_ELEMENT} event. Calling {@link Reader#close()} on the
 *         returned reader has no effect. Any parser exception will be reported by the reader
 *         using {@link XMLStreamIOException}.
 * @throws IllegalStateException
 *             if the XML stream reader is not positioned on a
 *             {@link XMLStreamConstants#START_ELEMENT} event
 */
public static Reader getElementTextAsStream(XMLStreamReader reader, boolean allowNonTextChildren) {
    if (reader.getEventType() != XMLStreamReader.START_ELEMENT) {
        throw new IllegalStateException("Reader must be on a START_ELEMENT event");
    }
    return new TextFromElementReader(reader, allowNonTextChildren);
}

From source file:org.apache.axiom.util.stax.XMLStreamReaderUtilsTest.java

public void testGetElementTextAsStream() throws Exception {
    XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<a>test</a>"));
    reader.next();/*  w  ww  .j  av  a2 s  .  com*/
    Reader in = XMLStreamReaderUtils.getElementTextAsStream(reader, false);
    assertEquals("test", IOUtils.toString(in));
    assertEquals(XMLStreamReader.END_ELEMENT, reader.getEventType());
}

From source file:org.apache.axis2.jaxws.context.listener.ParserInputStreamCustomBuilder.java

private InputStream getPayloadContent(XMLStreamReader parser, ParsedEntityReader entityReader) {
    int event = parser.getEventType();
    //Make sure its start element event.
    if (log.isDebugEnabled()) {
        log.debug("checking if event is START_ELEMENT");
    }//from w  ww .  jav  a2 s.  c  o m
    InputStream parsedStream = null;
    if (event == XMLStreamConstants.START_ELEMENT) {
        if (log.isDebugEnabled()) {
            log.debug("event is START_ELEMENT");
        }
        parsedStream = entityReader.readParsedEntityStream(parser);
        if (parsedStream != null) {
            if (log.isDebugEnabled()) {
                log.debug("Read Parsed EntityStream");
            }
        }
    }
    return parsedStream;
}

From source file:org.apache.ddlutils.io.DatabaseIO.java

/**
 * Reads the database model from the given XML stream reader.
 * /*ww  w  . ja  va2s  .co  m*/
 * @param xmlReader The reader
 * @return The database model
 */
private Database read(XMLStreamReader xmlReader) throws DdlUtilsXMLException {
    Database model = null;

    try {
        while (xmlReader.getEventType() != XMLStreamReader.START_ELEMENT) {
            if (xmlReader.next() == XMLStreamReader.END_DOCUMENT) {
                return null;
            }
        }
        if (isSameAs(xmlReader.getName(), QNAME_ELEMENT_DATABASE)) {
            model = readDatabaseElement(xmlReader);
        }
    } catch (IOException ex) {
        throw new DdlUtilsXMLException(ex);
    } catch (XMLStreamException ex) {
        throw new DdlUtilsXMLException(ex);
    }
    if (model != null) {
        model.initialize();
    }
    return model;
}

From source file:org.apache.ddlutils.io.DatabaseIO.java

/**
 * Consumes the rest of the current element. This assumes that the current XML stream
 * event type is not START_ELEMENT./*w  ww .  ja  v  a 2 s .co  m*/
 * 
 * @param reader The xml reader
 */
private void consumeRestOfElement(XMLStreamReader reader) throws XMLStreamException {
    int eventType = reader.getEventType();

    while ((eventType != XMLStreamReader.END_ELEMENT) && (eventType != XMLStreamReader.END_DOCUMENT)) {
        eventType = reader.next();
    }
}

From source file:org.apache.ddlutils.io.DataReader.java

/**
 * Reads the data from the given XML stream reader.
 * /*from w ww.  j av  a  2 s  .c o  m*/
 * @param xmlReader The reader
 */
private void read(XMLStreamReader xmlReader) throws DdlUtilsXMLException {
    try {
        while (xmlReader.getEventType() != XMLStreamReader.START_ELEMENT) {
            if (xmlReader.next() == XMLStreamReader.END_DOCUMENT) {
                return;
            }
        }
        readDocument(xmlReader);
    } catch (XMLStreamException ex) {
        throw new DdlUtilsXMLException(ex);
    }
}

From source file:org.apache.ode.utils.DOMUtils.java

private static void parse(XMLStreamReader reader, Document doc, Node parent) throws XMLStreamException {
    int event = reader.getEventType();

    while (reader.hasNext()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            // create element
            Element e = doc.createElementNS(reader.getNamespaceURI(), reader.getLocalName());
            if (reader.getPrefix() != null && reader.getPrefix() != "") {
                e.setPrefix(reader.getPrefix());
            }/* ww  w. j a  v a  2s.  com*/
            parent.appendChild(e);

            // copy namespaces
            for (int ns = 0; ns < reader.getNamespaceCount(); ns++) {
                String uri = reader.getNamespaceURI(ns);
                String prefix = reader.getNamespacePrefix(ns);
                declare(e, uri, prefix);
            }

            // copy attributes
            for (int att = 0; att < reader.getAttributeCount(); att++) {
                String name = reader.getAttributeLocalName(att);
                String prefix = reader.getAttributePrefix(att);
                if (prefix != null && prefix.length() > 0) {
                    name = prefix + ":" + name;
                }
                Attr attr = doc.createAttributeNS(reader.getAttributeNamespace(att), name);
                attr.setValue(reader.getAttributeValue(att));
                e.setAttributeNode(attr);
            }
            // sub-nodes
            if (reader.hasNext()) {
                reader.next();
                parse(reader, doc, e);
            }
            if (parent instanceof Document) {
                while (reader.hasNext())
                    reader.next();
                return;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            return;
        case XMLStreamConstants.CHARACTERS:
            if (parent != null) {
                parent.appendChild(doc.createTextNode(reader.getText()));
            }
            break;
        case XMLStreamConstants.COMMENT:
            if (parent != null) {
                parent.appendChild(doc.createComment(reader.getText()));
            }
            break;
        case XMLStreamConstants.CDATA:
            parent.appendChild(doc.createCDATASection(reader.getText()));
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            parent.appendChild(doc.createProcessingInstruction(reader.getPITarget(), reader.getPIData()));
            break;
        case XMLStreamConstants.NAMESPACE:
        case XMLStreamConstants.ATTRIBUTE:
            break;
        default:
            break;
        }

        if (reader.hasNext()) {
            event = reader.next();
        }
    }
}