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.xml.security.stax.impl.processor.input.AbstractDecryptInputProcessor.java

private void forwardToWrapperElement(XMLStreamReader xmlStreamReader) throws XMLStreamException {
    do {/*from w w w  . j a v  a 2  s  .  c  o m*/
        if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT
                && xmlStreamReader.getName().equals(wrapperElementName)) {
            xmlStreamReader.next();
            break;
        }
        xmlStreamReader.next();
    } while (xmlStreamReader.hasNext());
}

From source file:org.atomserver.core.validators.SimpleXMLContentValidator.java

private void touchEvent(XMLStreamReader reader) throws XMLStreamException {
    int eventCode = reader.next();
    String val = null;
    switch (eventCode) {
    case XMLStreamReader.START_ELEMENT:
        val = reader.getLocalName();
        if (log.isTraceEnabled()) {
            log.trace("event = START_ELEMENT");
            log.trace("Localname = " + val);
        }// ww w .j a v a  2s.com
        break;
    case XMLStreamReader.END_ELEMENT:
        val = reader.getLocalName();
        if (log.isTraceEnabled()) {
            log.trace("event = END_ELEMENT");
            log.trace("Localname = " + val);
        }
        break;
    case XMLStreamReader.PROCESSING_INSTRUCTION:
        val = reader.getPIData();
        if (log.isTraceEnabled()) {
            log.trace("event = PROCESSING_INSTRUCTION");
            log.trace("PIData = " + val);
        }
        break;
    case XMLStreamReader.CHARACTERS:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = CHARACTERS");
            log.trace("Characters = " + val);
        }
        break;
    case XMLStreamReader.COMMENT:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = COMMENT");
            log.trace("Comment = " + val);
        }
        break;
    case XMLStreamReader.SPACE:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = SPACE");
            log.trace("Space = " + val);
        }
        break;
    case XMLStreamReader.START_DOCUMENT:
        if (log.isTraceEnabled()) {
            log.trace("event = START_DOCUMENT");
            log.trace("Document Started.");
        }
        break;
    case XMLStreamReader.END_DOCUMENT:
        if (log.isTraceEnabled()) {
            log.trace("event = END_DOCUMENT");
            log.trace("Document Ended");
        }
        break;
    case XMLStreamReader.ENTITY_REFERENCE:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = ENTITY_REFERENCE");
            log.trace("Text = " + val);
        }
        break;
    case XMLStreamReader.DTD:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = DTD");
            log.trace("DTD = " + val);
        }
        break;
    case XMLStreamReader.CDATA:
        val = reader.getText();
        if (log.isTraceEnabled()) {
            log.trace("event = CDATA");
            log.trace("CDATA = " + val);
        }
        break;
    }
}

From source file:org.auraframework.impl.factory.SVGParser.java

@Override
public SVGDef getDefinition(DefDescriptor<SVGDef> descriptor, TextSource<SVGDef> source)
        throws SVGParserException, QuickFixException {
    if (descriptor.getDefType() == DefType.SVG) {
        XMLStreamReader reader = null;
        String contents = source.getContents();
        //If the file is too big throw before we parse the whole thing.
        SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition();
        try (StringReader stringReader = new StringReader(contents)) {
            reader = xmlInputFactory.createXMLStreamReader(stringReader);
            if (reader != null) {
                LOOP: while (reader.hasNext()) {
                    int type = reader.next();
                    switch (type) {
                    case XMLStreamConstants.END_DOCUMENT:
                        break LOOP;
                    //This is plain text inside the file
                    case XMLStreamConstants.CHARACTERS:
                        if (DISSALOWED_LIST.matcher(reader.getText()).matches()) {
                            throw new InvalidDefinitionException(
                                    String.format("Text contains disallowed symbols: %s", reader.getText()),
                                    XMLParser.getLocation(reader, source));
                        }/*  ww  w . j  a v a  2  s .  c o  m*/
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        String name = reader.getName().toString().toLowerCase();
                        if (!SVG_TAG_WHITELIST.contains(name)) {
                            throw new InvalidDefinitionException(
                                    String.format("Invalid SVG tag specified: %s", name),
                                    XMLParser.getLocation(reader, source));
                        }
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            QName qAttr = reader.getAttributeName(i);
                            String attr = qAttr.getLocalPart();
                            if (SVG_ATTR_BLACKLIST.contains(attr)) {
                                throw new InvalidDefinitionException(
                                        String.format("Invalid SVG attribute specified: %s", attr),
                                        XMLParser.getLocation(reader, source));
                            }
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                    case XMLStreamConstants.COMMENT:
                    case XMLStreamConstants.DTD:
                    case XMLStreamConstants.SPACE:
                        continue;
                    default:
                        throw new InvalidDefinitionException(String.format("Found unexpected element in xml."),
                                XMLParser.getLocation(reader, source));
                    }
                }
            }
        } catch (XMLStreamException e) {
            throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage()));
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (XMLStreamException e) {
                    //Well I tried to play nicely
                }
            }
        }
        return ret;
    }
    return null;
}

From source file:org.auraframework.impl.root.parser.handler.IncludeDefRefHandlerTest.java

private XMLStreamReader getReader(Source<?> source) throws XMLStreamException {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
    XMLStreamReader xmlReader = xmlInputFactory.createXMLStreamReader(source.getSystemId(),
            source.getHashingReader());//from  w  w  w.j  a va  2s . co m
    xmlReader.next();
    return xmlReader;
}

From source file:org.auraframework.impl.svg.parser.SVGParser.java

@Override
public SVGDef parse(DefDescriptor<SVGDef> descriptor, Source<SVGDef> source)
        throws SVGParserException, QuickFixException {
    if (descriptor.getDefType() == DefType.SVG) {
        XMLStreamReader reader = null;
        String contents = source.getContents();
        //If the file is too big throw before we parse the whole thing.
        SVGDef ret = new SVGDefHandler<>(descriptor, source).createDefinition();
        try (StringReader stringReader = new StringReader(contents)) {
            reader = xmlInputFactory.createXMLStreamReader(stringReader);
            if (reader != null) {
                LOOP: while (reader.hasNext()) {
                    int type = reader.next();
                    switch (type) {
                    case XMLStreamConstants.END_DOCUMENT:
                        break LOOP;
                    //This is plain text inside the file
                    case XMLStreamConstants.CHARACTERS:
                        if (DISSALOWED_LIST.matcher(reader.getText()).matches()) {
                            throw new InvalidDefinitionException(
                                    String.format("Text contains disallowed symbols: %s", reader.getText()),
                                    XMLParser.getLocation(reader, source));
                        }//ww w .j ava 2  s  .c o m
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        String name = reader.getName().toString().toLowerCase();
                        if (!SVG_TAG_WHITELIST.contains(name)) {
                            throw new InvalidDefinitionException(
                                    String.format("Invalid SVG tag specified: %s", name),
                                    XMLParser.getLocation(reader, source));
                        }
                        for (int i = 0; i < reader.getAttributeCount(); i++) {
                            QName qAttr = reader.getAttributeName(i);
                            String attr = qAttr.getLocalPart();
                            if (SVG_ATTR_BLACKLIST.contains(attr)) {
                                throw new InvalidDefinitionException(
                                        String.format("Invalid SVG attribute specified: %s", attr),
                                        XMLParser.getLocation(reader, source));
                            }
                        }
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                    case XMLStreamConstants.COMMENT:
                    case XMLStreamConstants.DTD:
                    case XMLStreamConstants.SPACE:
                        continue;
                    default:
                        throw new InvalidDefinitionException(String.format("Found unexpected element in xml."),
                                XMLParser.getLocation(reader, source));
                    }
                }
            }
        } catch (XMLStreamException e) {
            throw new SVGParserException(StringEscapeUtils.escapeHtml4(e.getMessage()));
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (XMLStreamException e) {
                    //Well I tried to play nicely
                }
            }
        }
        return ret;
    }
    return null;
}

From source file:org.codice.ddf.opensearch.source.OpenSearchSource.java

private InputTransformer getInputTransformer(InputStream inputStream) throws UnsupportedQueryException {
    XMLStreamReader xmlStreamReader = null;
    try {/*from w  ww  .  j a  v  a2 s  .  c o  m*/
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream);
        while (xmlStreamReader.hasNext()) {
            int next = xmlStreamReader.next();
            if (next == XMLStreamConstants.START_ELEMENT) {
                String namespaceUri = xmlStreamReader.getNamespaceURI();
                InputTransformer transformerReference = lookupTransformerReference(namespaceUri);
                if (transformerReference != null) {
                    return transformerReference;
                }
            }
        }
    } catch (XMLStreamException | InvalidSyntaxException e) {
        LOGGER.debug("Failed to parse transformer namespace", e);
    } finally {
        try {
            if (xmlStreamReader != null) {
                xmlStreamReader.close();
            }
        } catch (XMLStreamException e) {
            LOGGER.debug("Failed to close namespace reader", e);
        }
    }

    throw new UnsupportedQueryException(
            "Unable to find applicable InputTransformer for metacard content from Atom feed.");
}

From source file:org.commonjava.maven.ext.manip.util.PomPeek.java

private void parseCoordElements(final File pom) {
    Reader reader = null;//from w ww  .  j a  v  a2  s.  c om
    XMLStreamReader xml = null;
    try {
        reader = new FileReader(pom);
        xml = XMLInputFactory.newFactory().createXMLStreamReader(reader);

        final Stack<String> path = new Stack<String>();
        while (xml.hasNext()) {
            final int evt = xml.next();
            switch (evt) {
            case START_ELEMENT: {
                final String elem = xml.getLocalName();
                path.push(elem);
                if (captureValue(elem, path, xml)) {
                    // seems like xml.getElementText() traverses the END_ELEMENT event...
                    path.pop();
                }
                break;
            }
            case END_ELEMENT: {
                final String elem = xml.getLocalName();
                if (MODULES_ELEM.equals(elem)) {
                    modulesDone = true;
                }

                path.pop();
                break;
            }
            default: {
            }
            }

            if (foundAll()) {
                return;
            }
        }
    } catch (final IOException e) {
        logger.warn("Failed to peek at POM coordinate for: " + pom + " Reason: " + e.getMessage()
                + "\nThis POM will NOT be available as an ancestor to other models during effective-model building.",
                e);
    } catch (final XMLStreamException e) {
        logger.warn("Failed to peek at POM coordinate for: " + pom + " Reason: " + e.getMessage()
                + "\nThis POM will NOT be available as an ancestor to other models during effective-model building.",
                e);
    } catch (final FactoryConfigurationError e) {
        logger.warn("Failed to peek at POM coordinate for: " + pom + " Reason: " + e.getMessage()
                + "\nThis POM will NOT be available as an ancestor to other models during effective-model building.",
                e);
    } finally {
        if (xml != null) {
            try {
                xml.close();
            } catch (final XMLStreamException e) {
            }
        }

        try {
            reader.close();
        } catch (final IOException ioe) {
        }
    }
}

From source file:org.commonjava.maven.galley.maven.parse.PomPeek.java

private void parseCoordElements() {
    InputStream in = null;/*from   ww  w. j ava  2s . c o  m*/
    XMLStreamReader xml = null;
    try {
        if (pom != null) {
            in = new FileInputStream(pom);
        } else if (transfer != null) {
            in = transfer.openInputStream(false);
        } else {
            in = stream;
        }

        xml = XMLInputFactory.newFactory().createXMLStreamReader(in);

        final Stack<String> path = new Stack<>();
        while (xml.hasNext()) {
            final int evt = xml.next();
            switch (evt) {
            case START_ELEMENT: {
                final String elem = xml.getLocalName();
                if (captureValue(elem, path, xml)) {
                    // seems like xml.getElementText() traverses the END_ELEMENT event...
                    path.pop();
                }
                break;
            }
            case END_ELEMENT: {
                path.pop();
                break;
            }
            default: {
            }
            }

            if (foundAll()) {
                return;
            }
        }
    } catch (final IOException | FactoryConfigurationError | XMLStreamException e) {
        logger.warn("Failed to peek at POM coordinate for: " + pom + " Reason: " + e.getMessage()
                + "\nThis POM will NOT be available as an ancestor to other models during effective-model building.",
                e);
    } finally {
        if (xml != null) {
            try {
                xml.close();
            } catch (final XMLStreamException e) {
                logger.warn("Failed to close XMLStreamReader: " + e.getMessage(), e);
            }
        }

        closeQuietly(in);
    }
}

From source file:org.deegree.client.core.renderer.OutputXMLRenderer.java

private void encodeXML(ResponseWriter writer, String value) throws IOException {
    int depth = 0;
    try {//from  w  w  w . j  a  va2  s .c  om
        XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(value));
        boolean lastWasEndElement = false;
        boolean lastWasComment = false;
        while (reader.hasNext()) {
            switch (reader.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                if (!lastWasComment) {
                    writer.startElement("br", null);
                    writer.endElement("br");
                }
                writer.write(getSpaces(depth));
                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&lt;");
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "tag", null);
                String prefix = reader.getPrefix();
                writer.write(
                        (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName());
                writer.endElement("span");

                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "attributeName", null);
                    writer.write("&#160;");
                    String attributePrefix = reader.getAttributePrefix(i);
                    writer.write(
                            (attributePrefix != null && attributePrefix.length() > 0 ? attributePrefix + ":"
                                    : "") + reader.getAttributeLocalName(i));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("=\"");
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(encodeString(reader.getAttributeValue(i)));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("\"");
                    writer.endElement("span");
                }

                for (int i = 0; i < reader.getNamespaceCount(); i++) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "attributeName", null);
                    writer.write("&#160;");
                    String nsPrefix = reader.getNamespacePrefix(i);
                    writer.write("xmlns");
                    if (nsPrefix != null && !nsPrefix.isEmpty()) {
                        writer.write(":" + nsPrefix);
                    }
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("=\"");
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(reader.getNamespaceURI(i));
                    writer.endElement("span");

                    writer.startElement("span", null);
                    writer.writeAttribute("class", "sign", null);
                    writer.write("\"");
                    writer.endElement("span");
                }

                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&gt;");
                writer.endElement("span");
                depth++;
                lastWasEndElement = false;
                lastWasComment = false;
                break;
            case XMLStreamConstants.CHARACTERS:
                String text = reader.getText();
                if (text.trim().length() > 0) {
                    writer.startElement("span", null);
                    writer.writeAttribute("class", "text", null);
                    writer.write(encodeString(text));
                    writer.endElement("span");
                    lastWasEndElement = false;
                    lastWasComment = false;
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                depth--;
                if (lastWasEndElement) {
                    writer.startElement("br", null);
                    writer.endElement("br");
                    writer.write(getSpaces(depth));
                }
                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&lt;/");
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "tag", null);
                prefix = reader.getPrefix();
                writer.write(
                        (prefix != null && prefix.length() > 0 ? prefix + ":" : "") + reader.getLocalName());
                writer.endElement("span");

                writer.startElement("span", null);
                writer.writeAttribute("class", "sign", null);
                writer.write("&gt;");
                writer.endElement("span");
                lastWasEndElement = true;
                lastWasComment = false;
                break;
            case XMLStreamConstants.COMMENT:
                writer.startElement("div", null);
                writer.writeAttribute("class", "comment", null);
                writer.write("&lt;/!--" + reader.getText() + "--&gt;");
                writer.endElement("div");
                lastWasEndElement = false;
                lastWasComment = true;
                break;
            default:
                break;
            }
            reader.next();

        }
        reader.close();
    } catch (Throwable e) {
        if (depth == 0) {
            writer.writeText("Response could not parsed as XML.", null);
        } else {
            writer.writeText("... (if you want the complete document, please click the download button)", null);
        }
    }
}

From source file:org.deegree.maven.ithelper.ServiceIntegrationTestHelper.java

public void testCapabilities(String service) throws MojoFailureException {
    String address = createBaseURL() + "services/" + service.toLowerCase() + "?request=GetCapabilities&service="
            + service;// ww  w  .  j a  va 2 s  .  c  o  m
    try {
        log.info("Reading capabilities from " + address);
        String input = IOUtils.toString(new URL(address).openStream(), "UTF-8");
        XMLInputFactory fac = XMLInputFactory.newInstance();
        XMLStreamReader in = fac.createXMLStreamReader(new StringReader(input));
        in.next();
        if (in.getLocalName().toLowerCase().contains("exception")) {
            log.error("Actual response was:");
            log.error(input);
            throw new MojoFailureException("Retrieving capabilities from " + address + " failed.");
        }
    } catch (Throwable e) {
        log.debug("Failed to retrieve capabilities.", e);
        throw new MojoFailureException(
                "Retrieving capabilities for " + service + " failed: " + e.getLocalizedMessage(), e);
    }
}