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:com.cedarsoft.serialization.test.performance.XmlParserPerformance.java

private void benchParse(XMLInputFactory inputFactory, @Nonnull String contentSample) throws XMLStreamException {
    for (int i = 0; i < BIG; i++) {
        XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(contentSample));

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getLocalName());
        assertEquals("fileType", parser.getName().getLocalPart());

        boolean dependent = Boolean.parseBoolean(parser.getAttributeValue(null, "dependent"));

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getName().getLocalPart());
        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String id = parser.getText();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getName().getLocalPart());

        boolean isDefault = Boolean.parseBoolean(parser.getAttributeValue(null, "default"));
        String delimiter = parser.getAttributeValue(null, "delimiter");

        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String extension = parser.getText();
        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getName().getLocalPart());

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getName().getLocalPart());
        assertEquals(XMLStreamReader.END_DOCUMENT, parser.next());

        parser.close();//from  w ww.  j  a v a  2  s . c o  m

        FileType type = new FileType(id, new Extension(delimiter, extension, isDefault), dependent);
        assertNotNull(type);
    }
}

From source file:ddf.security.assertion.impl.SecurityAssertionImpl.java

/**
 * Parses the SecurityToken by wrapping within an AssertionWrapper.
 *
 * @param securityToken SecurityToken/*from w  w  w.j a v a  2 s .  c o  m*/
 */
private void parseToken(SecurityToken securityToken) {
    XMLStreamReader xmlStreamReader = StaxUtils.createXMLStreamReader(securityToken.getToken());

    try {
        AttrStatement attributeStatement = null;
        AuthenticationStatement authenticationStatement = null;
        Attr attribute = null;
        int attrs = 0;
        while (xmlStreamReader.hasNext()) {
            int event = xmlStreamReader.next();
            switch (event) {
            case XMLStreamConstants.START_ELEMENT: {
                String localName = xmlStreamReader.getLocalName();
                switch (localName) {
                case NameID.DEFAULT_ELEMENT_LOCAL_NAME:
                    name = xmlStreamReader.getElementText();
                    for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {
                        if (xmlStreamReader.getAttributeLocalName(i).equals(NameID.FORMAT_ATTRIB_NAME)) {
                            nameIDFormat = xmlStreamReader.getAttributeValue(i);
                            break;
                        }
                    }
                    break;
                case AttributeStatement.DEFAULT_ELEMENT_LOCAL_NAME:
                    attributeStatement = new AttrStatement();
                    attributeStatements.add(attributeStatement);
                    break;
                case AuthnStatement.DEFAULT_ELEMENT_LOCAL_NAME:
                    authenticationStatement = new AuthenticationStatement();
                    authenticationStatements.add(authenticationStatement);
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (AuthnStatement.AUTHN_INSTANT_ATTRIB_NAME.equals(name)) {
                            authenticationStatement.setAuthnInstant(DateTime.parse(value));
                        }
                    }
                    break;
                case AuthnContextClassRef.DEFAULT_ELEMENT_LOCAL_NAME:
                    if (authenticationStatement != null) {
                        String classValue = xmlStreamReader.getText();
                        classValue = classValue.trim();
                        AuthenticationContextClassRef authenticationContextClassRef = new AuthenticationContextClassRef();
                        authenticationContextClassRef.setAuthnContextClassRef(classValue);
                        AuthenticationContext authenticationContext = new AuthenticationContext();
                        authenticationContext.setAuthnContextClassRef(authenticationContextClassRef);
                        authenticationStatement.setAuthnContext(authenticationContext);
                    }
                    break;
                case Attribute.DEFAULT_ELEMENT_LOCAL_NAME:
                    attribute = new Attr();
                    if (attributeStatement != null) {
                        attributeStatement.addAttribute(attribute);
                    }
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (Attribute.NAME_ATTTRIB_NAME.equals(name)) {
                            attribute.setName(value);
                        } else if (Attribute.NAME_FORMAT_ATTRIB_NAME.equals(name)) {
                            attribute.setNameFormat(value);
                        }
                    }
                    break;
                case AttributeValue.DEFAULT_ELEMENT_LOCAL_NAME:
                    XSString xsString = new XMLString();
                    xsString.setValue(xmlStreamReader.getElementText());
                    if (attribute != null) {
                        attribute.addAttributeValue(xsString);
                    }
                    break;
                case Issuer.DEFAULT_ELEMENT_LOCAL_NAME:
                    issuer = xmlStreamReader.getElementText();
                    break;
                case Conditions.DEFAULT_ELEMENT_LOCAL_NAME:
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (Conditions.NOT_BEFORE_ATTRIB_NAME.equals(name)) {
                            notBefore = DatatypeConverter.parseDateTime(value).getTime();
                        } else if (Conditions.NOT_ON_OR_AFTER_ATTRIB_NAME.equals(name)) {
                            notOnOrAfter = DatatypeConverter.parseDateTime(value).getTime();
                        }
                    }
                    break;
                case SubjectConfirmation.DEFAULT_ELEMENT_LOCAL_NAME:
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (SubjectConfirmation.METHOD_ATTRIB_NAME.equals(name)) {
                            subjectConfirmations.add(value);
                        }
                    }
                case Assertion.DEFAULT_ELEMENT_LOCAL_NAME:
                    attrs = xmlStreamReader.getAttributeCount();
                    for (int i = 0; i < attrs; i++) {
                        String name = xmlStreamReader.getAttributeLocalName(i);
                        String value = xmlStreamReader.getAttributeValue(i);
                        if (Assertion.VERSION_ATTRIB_NAME.equals(name)) {
                            if ("2.0".equals(value)) {
                                tokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0";
                            } else if ("1.1".equals(value)) {
                                tokenType = "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1";
                            }
                        }
                    }
                }
                break;
            }
            case XMLStreamConstants.END_ELEMENT: {
                String localName = xmlStreamReader.getLocalName();
                switch (localName) {
                case AttributeStatement.DEFAULT_ELEMENT_LOCAL_NAME:
                    attributeStatement = null;
                    break;
                case Attribute.DEFAULT_ELEMENT_LOCAL_NAME:
                    attribute = null;
                    break;
                default:
                    break;
                }
                break;
            }
            }
        }
    } catch (XMLStreamException e) {
        LOGGER.error("Unable to parse security token.", e);
    } finally {
        try {
            xmlStreamReader.close();
        } catch (XMLStreamException ignore) {
            //ignore
        }
    }
}

From source file:davmail.exchange.ews.EWSMethod.java

protected String handleTag(XMLStreamReader reader, String localName) throws XMLStreamException {
    String result = null;/*from w  w  w.  j  a v a  2  s .  c om*/
    int event = reader.getEventType();
    if (event == XMLStreamConstants.START_ELEMENT && localName.equals(reader.getLocalName())) {
        while (reader.hasNext()
                && !((event == XMLStreamConstants.END_ELEMENT && localName.equals(reader.getLocalName())))) {
            event = reader.next();
            if (event == XMLStreamConstants.CHARACTERS) {
                result = reader.getText();
            }
        }
    }
    return result;
}

From source file:davmail.exchange.ews.EWSMethod.java

protected void handleAttendee(XMLStreamReader reader, Item item, String attendeeType)
        throws XMLStreamException {
    Attendee attendee = new Attendee();
    if ("RequiredAttendees".equals(attendeeType)) {
        attendee.role = "REQ-PARTICIPANT";
    } else {/*w  w w  .j a  va 2s . co m*/
        attendee.role = "OPT-PARTICIPANT";
    }
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Attendee"))) {
        reader.next();
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("EmailAddress".equals(tagLocalName)) {
                attendee.email = reader.getElementText();
            } else if ("Name".equals(tagLocalName)) {
                attendee.name = XMLStreamUtil.getElementText(reader);
            } else if ("ResponseType".equals(tagLocalName)) {
                String responseType = XMLStreamUtil.getElementText(reader);
                attendee.partstat = responseTypeToPartstat(responseType);
            }
        }
    }
    item.addAttendee(attendee);
}

From source file:com.qcadoo.model.internal.classconverter.ModelXmlToClassConverterImpl.java

private void parse(final XMLStreamReader reader, final CtClass ctClass, final String pluginIdentifier)
        throws XMLStreamException, ModelXmlCompilingException, NotFoundException {
    LOG.info("Defining class " + ctClass.getName());

    ctClass.addInterface(classPool.get(QcadooModelBean.class.getName()));

    createField(ctClass, "id", Long.class.getCanonicalName());

    List<String> fields = new ArrayList<String>();
    fields.add("id");

    if (getBooleanAttribute(reader, "activable", false)) {
        createField(ctClass, "active", Boolean.class.getCanonicalName());
    }// w w  w.  j av  a  2 s.  c  o m
    if (getBooleanAttribute(reader, "auditable", false)) {
        createField(ctClass, "createDate", Date.class.getCanonicalName());
        createField(ctClass, "updateDate", Date.class.getCanonicalName());
        createField(ctClass, "createUser", String.class.getCanonicalName());
        createField(ctClass, "updateUser", String.class.getCanonicalName());
    }

    while (reader.hasNext() && reader.next() > 0) {
        if (isTagEnded(reader, TAG_MODEL)) {
            break;
        }

        if (TAG_FIELDS.equals(getTagStarted(reader))) {
            while (reader.hasNext() && reader.next() > 0) {
                if (isTagEnded(reader, TAG_FIELDS)) {
                    break;
                }

                String tag = getTagStarted(reader);

                if (tag == null) {
                    continue;
                }

                parseField(reader, pluginIdentifier, ctClass, tag, fields);
            }
            break;
        }

    }

    buildToString(ctClass, fields);
    buildHashCode(ctClass, fields);
    buildEquals(ctClass, fields);
}

From source file:com.norconex.collector.http.sitemap.impl.StandardSitemapResolver.java

private void parseLocation(InputStream is, HttpClient httpClient, SitemapURLAdder sitemapURLAdder,
        Set<String> resolvedLocations, String location) throws XMLStreamException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
    ParseState parseState = new ParseState();

    String locationDir = StringUtils.substringBeforeLast(location, "/");
    int event = xmlReader.getEventType();
    while (true) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            String tag = xmlReader.getLocalName();
            parseStartElement(parseState, tag);
            break;
        case XMLStreamConstants.CHARACTERS:
            String value = xmlReader.getText();
            if (parseState.sitemapIndex && parseState.loc) {
                resolveLocation(value, httpClient, sitemapURLAdder, resolvedLocations);
                parseState.loc = false;// w w w  .  j a v a  2 s. c  om
            } else if (parseState.baseURL != null) {
                parseCharacters(parseState, value);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            tag = xmlReader.getLocalName();
            parseEndElement(sitemapURLAdder, parseState, locationDir, tag);
            break;
        }
        if (!xmlReader.hasNext()) {
            break;
        }
        event = xmlReader.next();
    }
}

From source file:com.norconex.collector.http.sitemap.impl.DefaultSitemapResolver.java

private void parseLocation(InputStream is, DefaultHttpClient httpClient, SitemapURLStore sitemapURLStore,
        Set<String> resolvedLocations, String location) throws XMLStreamException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setProperty(XMLInputFactory.IS_COALESCING, true);
    XMLStreamReader xmlReader = inputFactory.createXMLStreamReader(is);
    ParseState parseState = new ParseState();

    String locationDir = StringUtils.substringBeforeLast(location, "/");
    int event = xmlReader.getEventType();
    while (true) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            String tag = xmlReader.getLocalName();
            parseStartElement(parseState, tag);
            break;
        case XMLStreamConstants.CHARACTERS:
            String value = xmlReader.getText();
            if (parseState.sitemapIndex && parseState.loc) {
                resolveLocation(value, httpClient, sitemapURLStore, resolvedLocations);
                parseState.loc = false;/*w  w  w  . j ava2s . c  om*/
            } else if (parseState.baseURL != null) {
                parseCharacters(parseState, value);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            tag = xmlReader.getLocalName();
            parseEndElement(sitemapURLStore, parseState, locationDir, tag);
            break;
        }
        if (!xmlReader.hasNext()) {
            break;
        }
        event = xmlReader.next();
    }
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java

private void processCatgry(XMLStreamReader xmlr, Map<String, String> valueLabelPairs,
        List<String> missingValues, String variableName) throws XMLStreamException {

    boolean isMissing = "Y".equals(xmlr.getAttributeValue(null, "missing"));
    // (default is N, so null sets missing to false)

    // STORE -- (TODO NOW)
    //cat.setDataVariable(dv);
    //dv.getCategories().add(cat);
    String varValue = null;/*  w  ww  .j a v  a2 s . co m*/
    String valueLabel = null;

    for (int event = xmlr.next(); event != XMLStreamConstants.END_DOCUMENT; event = xmlr.next()) {
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (xmlr.getLocalName().equals("labl")) {
                valueLabel = processLabl(xmlr, LEVEL_CATEGORY);
            } else if (xmlr.getLocalName().equals("catValu")) {
                varValue = parseText(xmlr, false);
            } else if (xmlr.getLocalName().equals("catStat")) {
                // category statistics should not be present in the
                // TAB file + DDI card ingest:
                throw new XMLStreamException(
                        "catStat (Category Statistics) section found in a variable section; not supported!");
            }
        } else if (event == XMLStreamConstants.END_ELEMENT) {
            if (xmlr.getLocalName().equals("catgry")) {

                if (varValue != null && !varValue.equals("")) {
                    if (valueLabel != null && !valueLabel.equals("")) {
                        dbgLog.fine("DDI Reader: storing label " + valueLabel + " for value " + varValue);
                        valueLabelPairs.put(varValue, valueLabel);
                    }
                    if (isMissing) {
                        missingValues.add(varValue);
                    }
                }

                return;
            } else if (xmlr.getLocalName().equals("catValu")) {
                // continue;
            } else if (xmlr.getLocalName().equals("labl")) {
                // continue;
            } else {
                throw new XMLStreamException(
                        "Mismatched DDI Formatting: </catgry> expected, found " + xmlr.getLocalName());
            }
        }
    }
}

From source file:com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor.java

@Override
public void readObjectsIteratively(@NotNull ParserSource source, @NotNull ParsingContext parsingContext,
        RootXNodeHandler handler) throws SchemaException, IOException {
    InputStream is = source.getInputStream();
    XMLStreamReader stream = null;
    try {/*ww w. jav a2 s .c  om*/
        stream = XMLInputFactory.newInstance().createXMLStreamReader(is);

        int eventType = stream.nextTag();
        if (eventType != XMLStreamConstants.START_ELEMENT) {
            throw new SystemException("StAX Malfunction?");
        }
        DOMConverter domConverter = new DOMConverter();
        Map<String, String> rootNamespaceDeclarations = new HashMap<>();

        QName objectsMarker = schemaRegistry.getPrismContext().getObjectsElementName();
        if (objectsMarker != null && !QNameUtil.match(stream.getName(), objectsMarker)) {
            readSingleObjectIteratively(stream, rootNamespaceDeclarations, domConverter, handler);
        }
        for (int i = 0; i < stream.getNamespaceCount(); i++) {
            rootNamespaceDeclarations.put(stream.getNamespacePrefix(i), stream.getNamespaceURI(i));
        }
        while (stream.hasNext()) {
            eventType = stream.next();
            if (eventType == XMLStreamConstants.START_ELEMENT) {
                if (!readSingleObjectIteratively(stream, rootNamespaceDeclarations, domConverter, handler)) {
                    return;
                }
            }
        }
    } catch (XMLStreamException ex) {
        String lineInfo = stream != null ? " on line " + stream.getLocation().getLineNumber() : "";
        throw new SchemaException("Exception while parsing XML" + lineInfo + ": " + ex.getMessage(), ex);
    } finally {
        if (source.closeStreamAfterParsing()) {
            IOUtils.closeQuietly(is);
        }
    }
}

From source file:jp.co.atware.solr.geta.GETAssocComponent.java

/**
 * GETAssoc?????<code>NamedList</code>???????
 * //from  w w  w .j  av  a2 s.c  om
 * @param inputStream GETAssoc??
 * @return <code>NamedList</code>?
 * @throws FactoryConfigurationError
 * @throws IOException
 */
protected NamedList<Object> convertResult(InputStream inputStream)
        throws FactoryConfigurationError, IOException {
    NamedList<Object> result = new NamedList<Object>();
    LinkedList<NamedList<Object>> stack = new LinkedList<NamedList<Object>>();
    stack.push(result);
    try {
        XMLStreamReader xml = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        while (xml.hasNext()) {
            switch (xml.getEventType()) {
            case XMLStreamConstants.START_ELEMENT:
                NamedList<Object> element = new NamedList<Object>();
                stack.peek().add(xml.getName().toString(), element);
                stack.push(element);
                for (int i = 0; i < xml.getAttributeCount(); i++) {
                    String name = xml.getAttributeName(i).toString();
                    String value = xml.getAttributeValue(i);
                    ValueOf valueOf = valueTransMap.get(name);
                    if (valueOf != null) {
                        try {
                            element.add(name, valueOf.toValue(value));
                        } catch (NumberFormatException e) {
                            element.add(name, value);
                        }
                    } else {
                        element.add(name, value);
                    }
                }
                break;
            case XMLStreamConstants.END_ELEMENT:
                stack.pop();
                break;
            default:
                break;
            }
            xml.next();

        }
        xml.close();
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }

    LOG.debug(result.toString());
    return result;
}