Example usage for javax.xml.stream XMLStreamReader hasNext

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

Introduction

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

Prototype

public boolean hasNext() throws XMLStreamException;

Source Link

Document

Returns true if there are more parsing events and false if there are no more events.

Usage

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

protected List<FileAttachment> handleAttachments(XMLStreamReader reader) throws XMLStreamException {
    List<FileAttachment> attachments = new ArrayList<FileAttachment>();
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Attachments"))) {
        reader.next();/*from   w ww.j  av a  2s .com*/
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("FileAttachment".equals(tagLocalName)) {
                attachments.add(handleFileAttachment(reader));
            }
        }
    }
    return attachments;
}

From source file:de.tuebingen.uni.sfs.germanet.api.IliLoader.java

/**
 * Returns the <code>IliRecord</code> for which the start tag was just encountered.
 * @param parser the <code>parser</code> being used on the current file
 * @return a <code>IliRecord</code> representing the data parsed
 * @throws javax.xml.stream.XMLStreamException
 *///www  .  ja  v  a 2  s . co  m
private IliRecord processIliRecord(XMLStreamReader parser) throws XMLStreamException {
    int lexUnitId;
    String ewnRelation;
    String pwnWord;
    String pwn20Id;
    String pwn30Id;
    String pwn20paraphrase = "";
    String source;
    IliRecord curIli;
    List<String> englishSynonyms = new ArrayList<String>();
    boolean done = false;
    int event;
    String nodeName;
    lexUnitId = Integer.valueOf(parser.getAttributeValue(namespace, GermaNet.XML_LEX_UNIT_ID).substring(1));
    ewnRelation = parser.getAttributeValue(namespace, GermaNet.XML_EWN_RELATION);
    pwnWord = parser.getAttributeValue(namespace, GermaNet.XML_PWN_WORD);
    pwn20Id = parser.getAttributeValue(namespace, GermaNet.XML_PWN20_ID);
    pwn30Id = parser.getAttributeValue(namespace, GermaNet.XML_PWN30_ID);
    pwn20paraphrase = parser.getAttributeValue(namespace, GermaNet.XML_PWN20_PARAPHRASE);

    source = parser.getAttributeValue(namespace, GermaNet.XML_SOURCE);

    // process this lexUnit
    while (parser.hasNext() && !done) {
        event = parser.next();
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            nodeName = parser.getLocalName();
            if (nodeName.equals(GermaNet.XML_PWN20_SYNONYM)) {
                englishSynonyms.add(processEnglishSynonyms(parser));
            }
        case XMLStreamConstants.END_ELEMENT:
            nodeName = parser.getLocalName();
            // quit when we reach the end lexUnit tag
            if (nodeName.equals(GermaNet.XML_ILI_RECORD)) {
                done = true;
            }
            break;
        }
    }

    curIli = new IliRecord(lexUnitId, EwnRel.valueOf(ewnRelation), pwnWord, pwn20Id, pwn30Id, pwn20paraphrase,
            source);

    for (String synonym : englishSynonyms) {
        curIli.addEnglishSynonym(synonym);
    }

    return curIli;
}

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

protected void addExtendedPropertyValue(XMLStreamReader reader, Item item) throws XMLStreamException {
    String propertyTag = null;/*from  ww  w.  j  a  va2  s .c o m*/
    String propertyValue = null;
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "ExtendedProperty"))) {
        reader.next();
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("ExtendedFieldURI".equals(tagLocalName)) {
                propertyTag = getAttributeValue(reader, "PropertyTag");
                // property name is in PropertyId or PropertyName with DistinguishedPropertySetId
                if (propertyTag == null) {
                    propertyTag = getAttributeValue(reader, "PropertyId");
                }
                if (propertyTag == null) {
                    propertyTag = getAttributeValue(reader, "PropertyName");
                }
            } else if ("Value".equals(tagLocalName)) {
                propertyValue = XMLStreamUtil.getElementText(reader);
            } else if ("Values".equals(tagLocalName)) {
                StringBuilder buffer = new StringBuilder();
                while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Values"))) {
                    reader.next();
                    if (XMLStreamUtil.isStartTag(reader)) {

                        if (buffer.length() > 0) {
                            buffer.append(',');
                        }
                        String singleValue = XMLStreamUtil.getElementText(reader);
                        if (singleValue != null) {
                            buffer.append(singleValue);
                        }
                    }
                }
                propertyValue = buffer.toString();
            }
        }
    }
    if ((propertyTag != null) && (propertyValue != null)) {
        item.put(propertyTag, propertyValue);
    }
}

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

protected Item handleItem(XMLStreamReader reader) throws XMLStreamException {
    Item responseItem = new Item();
    responseItem.type = reader.getLocalName();
    while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, responseItem.type)) {
        reader.next();/*from w ww  .j a v a 2  s.  c  om*/
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            String value = null;
            if ("ExtendedProperty".equals(tagLocalName)) {
                addExtendedPropertyValue(reader, responseItem);
            } else if (tagLocalName.endsWith("MimeContent")) {
                handleMimeContent(reader, responseItem);
            } else if ("Attachments".equals(tagLocalName)) {
                responseItem.attachments = handleAttachments(reader);
            } else if ("EmailAddresses".equals(tagLocalName)) {
                handleEmailAddresses(reader, responseItem);
            } else if ("RequiredAttendees".equals(tagLocalName) || "OptionalAttendees".equals(tagLocalName)) {
                handleAttendees(reader, responseItem, tagLocalName);
            } else if ("ModifiedOccurrences".equals(tagLocalName)) {
                handleModifiedOccurrences(reader, responseItem);
            } else {
                if (tagLocalName.endsWith("Id")) {
                    value = getAttributeValue(reader, "Id");
                    // get change key
                    responseItem.put("ChangeKey", getAttributeValue(reader, "ChangeKey"));
                }
                if (value == null) {
                    value = getTagContent(reader);
                }
                if (value != null) {
                    responseItem.put(tagLocalName, value);
                }
            }
        }
    }
    return responseItem;
}

From source file:hudson.plugins.report.jck.parsers.JtregReportParser.java

private JtregBackwardCompatibileSuite parseTestSuite(XMLStreamReader in) throws XMLStreamException, Exception {

    final String name = findAttributeValue(in, "name");
    final String failuresStr = findAttributeValue(in, "failures");
    final String errorsStr = findAttributeValue(in, "errors");
    final String totalStr = findAttributeValue(in, "tests");
    final String totalSkip = findAttributeValue(in, "skipped");

    final int failures = tryParseString(failuresStr);
    final int errors = tryParseString(errorsStr);
    final int total = tryParseString(totalStr);
    final int skipped = tryParseString(totalSkip);

    JtregBackwardCompatibileSuite suite = new JtregBackwardCompatibileSuite(name, failures, errors, total,
            skipped);// ww w. j  a  v a 2  s  .c om

    String statusLine = "";
    String stdOutput = "";
    String errOutput = "";

    while (in.hasNext()) {
        int event = in.next();
        if (event == START_ELEMENT && TESTCASE.equals(in.getLocalName())) {
            JtregBackwardCompatibileTest test = parseTestcase(in);
            suite.add(test);
            continue;
        }

        if (event == START_ELEMENT && PROPERTIES.equals(in.getLocalName())) {
            statusLine = findStatusLine(in);
            continue;
        }
        if (event == START_ELEMENT && SYSTEMOUT.equals(in.getLocalName())) {
            stdOutput = captureCharacters(in, SYSTEMOUT);
            continue;
        }
        if (event == START_ELEMENT && SYSTEMERR.equals(in.getLocalName())) {
            errOutput = captureCharacters(in, SYSTEMERR);
            continue;
        }

        if (event == END_ELEMENT && TESTSUITE.equals(in.getLocalName())) {
            break;
        }
    }

    //order imortant! see revalidateTests
    List<TestOutput> outputs = Arrays.asList(new TestOutput(SYSTEMOUT, stdOutput),
            new TestOutput(SYSTEMERR, errOutput));

    suite.setStatusLine(statusLine);
    suite.setOutputs(outputs);
    return suite;

}

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

protected String handleTag(XMLStreamReader reader, String localName) throws XMLStreamException {
    String result = null;/*from  w ww .  j a  va  2  s.  co m*/
    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:com.liferay.portal.util.LocalizationImpl.java

private String _getRootAttribute(String xml, String name, String defaultValue) {

    String value = null;/*from w  ww.j  a  v a 2s .co  m*/

    XMLStreamReader xmlStreamReader = null;

    ClassLoader portalClassLoader = PortalClassLoaderUtil.getClassLoader();

    Thread currentThread = Thread.currentThread();

    ClassLoader contextClassLoader = currentThread.getContextClassLoader();

    try {
        if (contextClassLoader != portalClassLoader) {
            currentThread.setContextClassLoader(portalClassLoader);
        }

        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();

        xmlStreamReader = xmlInputFactory.createXMLStreamReader(new UnsyncStringReader(xml));

        if (xmlStreamReader.hasNext()) {
            xmlStreamReader.nextTag();

            value = xmlStreamReader.getAttributeValue(null, name);
        }
    } catch (Exception e) {
        if (_log.isWarnEnabled()) {
            _log.warn(e, e);
        }
    } finally {
        if (contextClassLoader != portalClassLoader) {
            currentThread.setContextClassLoader(contextClassLoader);
        }

        if (xmlStreamReader != null) {
            try {
                xmlStreamReader.close();
            } catch (Exception e) {
            }
        }
    }

    if (Validator.isNull(value)) {
        value = defaultValue;
    }

    return value;
}

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 va 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.dav.ExchangeDavMethod.java

@Override
protected void processResponseBody(HttpState httpState, HttpConnection httpConnection) {
    Header contentTypeHeader = getResponseHeader("Content-Type");
    if (contentTypeHeader != null && "text/xml".equals(contentTypeHeader.getValue())) {
        responses = new ArrayList<MultiStatusResponse>();
        XMLStreamReader reader;
        try {/*from   w w  w  . j  a  v a 2s .  c o  m*/
            reader = XMLStreamUtil.createXMLStreamReader(new FilterInputStream(getResponseBodyAsStream()) {
                final byte[] lastbytes = new byte[3];

                @Override
                public int read(byte[] bytes, int off, int len) throws IOException {
                    int count = in.read(bytes, off, len);
                    // patch invalid element name
                    for (int i = 0; i < count; i++) {
                        byte currentByte = bytes[off + i];
                        if ((lastbytes[0] == '<') && (currentByte >= '0' && currentByte <= '9')) {
                            // move invalid first tag char to valid range
                            bytes[off + i] = (byte) (currentByte + 49);
                        }
                        lastbytes[0] = lastbytes[1];
                        lastbytes[1] = lastbytes[2];
                        lastbytes[2] = currentByte;
                    }
                    return count;
                }

            });
            while (reader.hasNext()) {
                reader.next();
                if (XMLStreamUtil.isStartTag(reader, "response")) {
                    handleResponse(reader);
                }
            }

        } catch (IOException e) {
            LOGGER.error("Error while parsing soap response: " + e, e);
        } catch (XMLStreamException e) {
            LOGGER.error("Error while parsing soap response: " + e, e);
        }
    }
}

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 . ja v a  2 s.com
        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);
}