Example usage for javax.xml.stream XMLStreamReader next

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

Introduction

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

Prototype

public int next() throws XMLStreamException;

Source Link

Document

Get next parsing event - a processor may return all contiguous character data in a single chunk, or it may split it into several chunks.

Usage

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

public void testGetElementTextAsStreamWithAllowedNonTextChildren() throws Exception {
    XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<a>xxx<b>yyy</b>zzz</a>"));
    reader.next();
    Reader in = XMLStreamReaderUtils.getElementTextAsStream(reader, true);
    assertEquals("xxxzzz", IOUtils.toString(in));
}

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

public void testGetElementTextAsStreamWithForbiddenNonTextChildren() throws Exception {
    XMLStreamReader reader = StAXUtils.createXMLStreamReader(new StringReader("<a>xxx<b>yyy</b>zzz</a>"));
    reader.next();
    Reader in = XMLStreamReaderUtils.getElementTextAsStream(reader, false);
    try {/*from   w  ww.j  a v a  2s. c o  m*/
        IOUtils.toString(in);
        fail("Expected exception");
    } catch (IOException ex) {
        // Expected
    }
}

From source file:org.apache.axis2.databinding.utils.ConverterUtil.java

public static Object getAnyTypeObject(XMLStreamReader xmlStreamReader, Class extensionMapperClass)
        throws XMLStreamException {
    Object returnObject = null;/*from  w  w w.  j av a2 s  . c  o m*/

    // make sure reader is at the first element.
    while (!xmlStreamReader.isStartElement()) {
        xmlStreamReader.next();
    }
    // first check whether this element is null or not
    String nillableValue = xmlStreamReader.getAttributeValue(Constants.XSI_NAMESPACE, "nil");
    if ("true".equals(nillableValue) || "1".equals(nillableValue)) {
        returnObject = null;
        xmlStreamReader.next();
    } else {
        String attributeType = xmlStreamReader.getAttributeValue(Constants.XSI_NAMESPACE, "type");
        if (attributeType != null) {
            String attributeTypePrefix = "";
            if (attributeType.indexOf(":") > -1) {
                attributeTypePrefix = attributeType.substring(0, attributeType.indexOf(":"));
                attributeType = attributeType.substring(attributeType.indexOf(":") + 1);
            }
            NamespaceContext namespaceContext = xmlStreamReader.getNamespaceContext();
            String attributeNameSpace = namespaceContext.getNamespaceURI(attributeTypePrefix);

            if (Constants.XSD_NAMESPACE.equals(attributeNameSpace)) {
                if ("base64Binary".equals(attributeType)) {
                    returnObject = XMLStreamReaderUtils.getDataHandlerFromElement(xmlStreamReader);
                } else {
                    String attribValue = xmlStreamReader.getElementText();
                    if (attribValue != null) {
                        if (attributeType.equals("string")) {
                            returnObject = attribValue;
                        } else if (attributeType.equals("int")) {
                            returnObject = new Integer(attribValue);
                        } else if (attributeType.equals("QName")) {
                            String namespacePrefix = null;
                            String localPart = null;
                            if (attribValue.indexOf(":") > -1) {
                                namespacePrefix = attribValue.substring(0, attribValue.indexOf(":"));
                                localPart = attribValue.substring(attribValue.indexOf(":") + 1);
                                returnObject = new QName(namespaceContext.getNamespaceURI(namespacePrefix),
                                        localPart);
                            }
                        } else if ("boolean".equals(attributeType)) {
                            returnObject = new Boolean(attribValue);
                        } else if ("anyURI".equals(attributeType)) {
                            try {
                                returnObject = new URI(attribValue);
                            } catch (URI.MalformedURIException e) {
                                throw new XMLStreamException("Invalid URI");
                            }
                        } else if ("date".equals(attributeType)) {
                            returnObject = ConverterUtil.convertToDate(attribValue);
                        } else if ("dateTime".equals(attributeType)) {
                            returnObject = ConverterUtil.convertToDateTime(attribValue);
                        } else if ("time".equals(attributeType)) {
                            returnObject = ConverterUtil.convertToTime(attribValue);
                        } else if ("byte".equals(attributeType)) {
                            returnObject = new Byte(attribValue);
                        } else if ("short".equals(attributeType)) {
                            returnObject = new Short(attribValue);
                        } else if ("float".equals(attributeType)) {
                            returnObject = new Float(attribValue);
                        } else if ("long".equals(attributeType)) {
                            returnObject = new Long(attribValue);
                        } else if ("double".equals(attributeType)) {
                            returnObject = new Double(attribValue);
                        } else if ("decimal".equals(attributeType)) {
                            returnObject = new BigDecimal(attribValue);
                        } else if ("unsignedLong".equals(attributeType)) {
                            returnObject = new UnsignedLong(attribValue);
                        } else if ("unsignedInt".equals(attributeType)) {
                            returnObject = new UnsignedInt(attribValue);
                        } else if ("unsignedShort".equals(attributeType)) {
                            returnObject = new UnsignedShort(attribValue);
                        } else if ("unsignedByte".equals(attributeType)) {
                            returnObject = new UnsignedByte(attribValue);
                        } else if ("positiveInteger".equals(attributeType)) {
                            returnObject = new PositiveInteger(attribValue);
                        } else if ("negativeInteger".equals(attributeType)) {
                            returnObject = new NegativeInteger(attribValue);
                        } else if ("nonNegativeInteger".equals(attributeType)) {
                            returnObject = new NonNegativeInteger(attribValue);
                        } else if ("nonPositiveInteger".equals(attributeType)) {
                            returnObject = new NonPositiveInteger(attribValue);
                        } else {
                            throw new ADBException("Unknown type ==> " + attributeType);
                        }
                    } else {
                        throw new ADBException("Attribute value is null");
                    }
                }
            } else {
                try {
                    Method getObjectMethod = extensionMapperClass.getMethod("getTypeObject",
                            new Class[] { String.class, String.class, XMLStreamReader.class });
                    returnObject = getObjectMethod.invoke(null,
                            new Object[] { attributeNameSpace, attributeType, xmlStreamReader });
                } catch (NoSuchMethodException e) {
                    throw new ADBException(
                            "Can not find the getTypeObject method in the " + "extension mapper class ", e);
                } catch (IllegalAccessException e) {
                    throw new ADBException(
                            "Can not access the getTypeObject method in the " + "extension mapper class ", e);
                } catch (InvocationTargetException e) {
                    throw new ADBException(
                            "Can not invoke the getTypeObject method in the " + "extension mapper class ", e);
                }

            }

        } else {
            throw new ADBException("Any type element type has not been given");
        }
    }
    return returnObject;
}

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

public static int skipEventsTo(int targetEvent, XMLStreamReader parser) throws XMLStreamException {
    int eventType = 0;
    while (parser.hasNext()) {
        eventType = parser.next();
        if (eventType == targetEvent)
            return eventType;
    }//from   w w w  .j av  a  2s. c o  m
    return eventType; // return END_DOCUMENT;
}

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

public OMElement create(String namespace, String localPart, OMContainer parent, XMLStreamReader reader,
        OMFactory factory) throws OMException {

    if (log.isDebugEnabled()) {
        log.debug("create namespace = " + namespace);
        log.debug("  localPart = " + localPart);
        log.debug("  reader = " + reader.getClass());
    }/*  w ww.  ja va 2s  . c  o  m*/

    if (!shouldUnmarshal(namespace, localPart)) {
        if (log.isDebugEnabled()) {
            log.debug("This element won't be unmarshalled with the custom builder");
        }
        return null;
    }

    /*
     * 1) Use the the parser to fetch the inputStream
     * 2) Use the inputStream to create a DataSource, delay reading of content as much as you can.
     * 3) Use the OMFactory to create OMSourcedElement, OMSourcedElement is backed by ParsedEntityDataSource.
     */
    try {
        ParsedEntityReaderFactory perf = (ParsedEntityReaderFactory) FactoryRegistry
                .getFactory(ParsedEntityReaderFactory.class);
        ParsedEntityReader entityReader = perf.getParsedEntityReader();
        if (log.isDebugEnabled()) {
            log.debug("ParsedEntityReader = " + entityReader);
        }
        //Do not user custom builder if Parser does not have ability to read sub content.
        if (!entityReader.isParsedEntityStreamAvailable()) {
            if (log.isDebugEnabled()) {
                log.debug("ParsedEntityStream is not available, defaulting to normal build");
            }
            return null;
        }
        // Create an OMSourcedElement backed by the ParsedData
        InputStream parsedStream = getPayloadContent(reader, entityReader);
        if (parsedStream == null) {
            //cant read content from EntityReader, returning null.
            if (log.isDebugEnabled()) {
                log.debug("Unable to read content from the entity reader, defaulting to normal build");
            }
            return null;
        }
        HashMap<String, String> nsElementDecls = getElementNamespaceDeclarations(reader);
        HashMap<String, String> attrElementDecls = getElementAttributeDeclarations(reader);

        //read the payload. Lets move the parser forward.
        if (reader.hasNext()) {
            reader.next();
        }
        if (namespace == null) {
            //lets look for ns in reader
            namespace = reader.getNamespaceURI();
            if (namespace == null) {
                //still cant find the namespace, just set it to "";
                namespace = "";
            }
        }
        OMNamespace ns = factory.createOMNamespace(namespace, reader.getPrefix());
        InputStream payload = ContextListenerUtils.createPayloadElement(parsedStream, ns, localPart, parent,
                nsElementDecls, attrElementDecls);

        ParserInputStreamDataSource ds = new ParserInputStreamDataSource(payload, encoding);
        OMSourcedElement om = null;
        if (parent instanceof SOAPHeader && factory instanceof SOAPFactory) {
            om = ((SOAPFactory) factory).createSOAPHeaderBlock(localPart, ns, ds);
        } else {
            om = factory.createOMElement(ds, localPart, ns);
        }
        //Add the new OMSourcedElement ot the parent
        parent.addChild(om);
        /*
        //Lets Mark the body as complete so Serialize calls dont fetch data from parser for body content.
        if(parent instanceof SOAPBodyImpl){
        ((SOAPBodyImpl)parent).setComplete(true);
        }
        */
        return om;
    } catch (OMException e) {
        throw e;
    } catch (Throwable t) {
        throw new OMException(t);
    }
}

From source file:org.apache.axis2.jaxws.message.util.impl.SAAJConverterImpl.java

/**
 * Build SOAPTree Either the root or the parent is null. If the root is null, a new element is
 * created under the parent using information from the reader If the parent is null, the existing
 * root is updated with the information from the reader
 *
 * @param nc         NameCreator/*from ww w.j av a 2 s.  c  om*/
 * @param root       SOAPElement (the element that represents the data in the reader)
 * @param parent     (the parent of the element represented by the reader)
 * @param reader     XMLStreamReader. the first START_ELEMENT matches the root
 * @param quitAtBody - true if quit reading after the body START_ELEMENT
 */
protected SOAPElement buildSOAPTree(NameCreator nc, SOAPElement root, SOAPElement parent,
        XMLStreamReader reader, boolean quitAtBody) throws WebServiceException {
    try {
        while (reader.hasNext()) {
            int eventID = reader.next();
            switch (eventID) {
            case XMLStreamReader.START_ELEMENT: {

                // The first START_ELEMENT defines the prefix and attributes of the root
                if (parent == null) {
                    updateTagData(nc, root, reader, false);
                    parent = root;
                } else {
                    parent = createElementFromTag(nc, parent, reader);
                    if (root == null) {
                        root = parent;
                    }
                }
                if (quitAtBody && parent instanceof SOAPBody) {
                    return root;
                }
                break;
            }
            case XMLStreamReader.ATTRIBUTE: {
                String eventName = "ATTRIBUTE";
                this._unexpectedEvent(eventName);
                break;
            }
            case XMLStreamReader.NAMESPACE: {
                String eventName = "NAMESPACE";
                this._unexpectedEvent(eventName);
                break;
            }
            case XMLStreamReader.END_ELEMENT: {
                if (parent instanceof SOAPEnvelope) {
                    parent = null;
                } else {
                    parent = parent.getParentElement();
                }
                break;
            }
            case XMLStreamReader.CHARACTERS: {
                parent.addTextNode(reader.getText());
                break;
            }
            case XMLStreamReader.CDATA: {
                parent.addTextNode(reader.getText());
                break;
            }
            case XMLStreamReader.COMMENT: {
                // SOAP really doesn't have an adequate representation for comments.
                // The defacto standard is to add the whole element as a text node.
                parent.addTextNode("<!--" + reader.getText() + "-->");
                break;
            }
            case XMLStreamReader.SPACE: {
                parent.addTextNode(reader.getText());
                break;
            }
            case XMLStreamReader.START_DOCUMENT: {
                // Ignore
                break;
            }
            case XMLStreamReader.END_DOCUMENT: {
                // Close reader and ignore
                reader.close();
                break;
            }
            case XMLStreamReader.PROCESSING_INSTRUCTION: {
                // Ignore
                break;
            }
            case XMLStreamReader.ENTITY_REFERENCE: {
                // Ignore. this is unexpected in a web service message
                break;
            }
            case XMLStreamReader.DTD: {
                // Ignore. this is unexpected in a web service message
                break;
            }
            default:
                this._unexpectedEvent("EventID " + String.valueOf(eventID));
            }
        }
    } catch (WebServiceException e) {
        throw e;
    } catch (XMLStreamException e) {
        throw ExceptionFactory.makeWebServiceException(e);
    } catch (SOAPException e) {
        throw ExceptionFactory.makeWebServiceException(e);
    }
    return root;
}

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

/**
 * Reads the database model from the given XML stream reader.
 * /*from   w  w  w .j  av a 2s .  c  o  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

/**
 * Reads table elements from the XML stream reader and adds them to the given
 * database model./*from  w ww.j a v a 2s .  co  m*/
 * 
 * @param xmlReader The reader
 * @param model     The database model to add the table objects to
 */
private void readTableElements(XMLStreamReader xmlReader, Database model)
        throws XMLStreamException, IOException {
    int eventType = XMLStreamReader.START_ELEMENT;

    while (eventType != XMLStreamReader.END_ELEMENT) {
        eventType = xmlReader.next();
        if (eventType == XMLStreamReader.START_ELEMENT) {
            if (isSameAs(xmlReader.getName(), QNAME_ELEMENT_TABLE)) {
                model.addTable(readTableElement(xmlReader));
            } else {
                readOverElement(xmlReader);
            }
        }
    }
}

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

/**
 * Reads table sub elements (column, foreign key, index) from the XML stream reader and adds
 * them to the given table.//from   www .  j  av  a2  s.co m
 * 
 * @param xmlReader The reader
 * @param table     The table
 */
private void readTableSubElements(XMLStreamReader xmlReader, Table table)
        throws XMLStreamException, IOException {
    int eventType = XMLStreamReader.START_ELEMENT;

    while (eventType != XMLStreamReader.END_ELEMENT) {
        eventType = xmlReader.next();
        if (eventType == XMLStreamReader.START_ELEMENT) {
            QName elemQName = xmlReader.getName();

            if (isSameAs(elemQName, QNAME_ELEMENT_COLUMN)) {
                table.addColumn(readColumnElement(xmlReader));
            } else if (isSameAs(elemQName, QNAME_ELEMENT_FOREIGN_KEY)) {
                table.addForeignKey(readForeignKeyElement(xmlReader));
            } else if (isSameAs(elemQName, QNAME_ELEMENT_INDEX)) {
                table.addIndex(readIndexElement(xmlReader));
            } else if (isSameAs(elemQName, QNAME_ELEMENT_UNIQUE)) {
                table.addIndex(readUniqueElement(xmlReader));
            } else {
                readOverElement(xmlReader);
            }
        }
    }
}

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

/**
 * Reads reference elements from the XML stream reader and adds them to the given
 * foreign key./*from w  ww  .  j  a va 2 s .  c  o  m*/
 * 
 * @param xmlReader  The reader
 * @param foreignKey The foreign key
 */
private void readReferenceElements(XMLStreamReader xmlReader, ForeignKey foreignKey)
        throws XMLStreamException, IOException {
    int eventType = XMLStreamReader.START_ELEMENT;

    while (eventType != XMLStreamReader.END_ELEMENT) {
        eventType = xmlReader.next();
        if (eventType == XMLStreamReader.START_ELEMENT) {
            QName elemQName = xmlReader.getName();

            if (isSameAs(elemQName, QNAME_ELEMENT_REFERENCE)) {
                foreignKey.addReference(readReferenceElement(xmlReader));
            } else {
                readOverElement(xmlReader);
            }
        }
    }
}