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:davmail.exchange.ews.EWSMethod.java

protected void handleOccurrence(XMLStreamReader reader, Item item) throws XMLStreamException {
    Occurrence occurrence = new Occurrence();
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Occurrence"))) {
        reader.next();
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("ItemId".equals(tagLocalName)) {
                occurrence.itemId = new ItemId("ItemId", getAttributeValue(reader, "Id"),
                        getAttributeValue(reader, "ChangeKey"));
            }/* ww w  .  j a va  2 s. c o  m*/
            if ("OriginalStart".equals(tagLocalName)) {
                occurrence.originalStart = XMLStreamUtil.getElementText(reader);
            }
        }
    }
    item.addOccurrence(occurrence);
}

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

protected String getTagContent(XMLStreamReader reader) throws XMLStreamException {
    String tagLocalName = reader.getLocalName();
    while (reader.hasNext() && !(reader.getEventType() == XMLStreamConstants.END_ELEMENT)) {
        reader.next();
        if (reader.getEventType() == XMLStreamConstants.CHARACTERS) {
            return reader.getText();
        }/*from   w ww.j  a v a 2 s  . co  m*/
    }
    // empty tag
    if (reader.hasNext()) {
        return null;
    } else {
        throw new XMLStreamException("End element for " + tagLocalName + " not found");
    }
}

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

protected FileAttachment handleFileAttachment(XMLStreamReader reader) throws XMLStreamException {
    FileAttachment fileAttachment = new FileAttachment();
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "FileAttachment"))) {
        reader.next();
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("AttachmentId".equals(tagLocalName)) {
                fileAttachment.attachmentId = getAttributeValue(reader, "Id");
            } else if ("Name".equals(tagLocalName)) {
                fileAttachment.name = getTagContent(reader);
            } else if ("ContentType".equals(tagLocalName)) {
                fileAttachment.contentType = getTagContent(reader);
            }/*  w  ww. j a v  a 2  s.c o m*/
        }
    }
    return fileAttachment;
}

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();
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("FileAttachment".equals(tagLocalName)) {
                attachments.add(handleFileAttachment(reader));
            }/*from  w ww  . jav a2  s .c om*/
        }
    }
    return attachments;
}

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

protected void addExtendedPropertyValue(XMLStreamReader reader, Item item) throws XMLStreamException {
    String propertyTag = null;/*from  w w w  .j a  va  2 s . c  om*/
    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();
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            String value = null;//from   ww  w .j a va  2 s .  c  o  m
            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);/*www  . j  a  v  a  2s . 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:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.ddi.DDIFileReader.java

private String getElementText(XMLStreamReader xmlr) throws XMLStreamException {
    if (xmlr.getEventType() != XMLStreamConstants.START_ELEMENT) {
        throw new XMLStreamException("parser must be on START_ELEMENT to read next text", xmlr.getLocation());
    }/*from w  w  w  .ja  v a2s  .c  o m*/
    int eventType = xmlr.next();
    StringBuffer content = new StringBuffer();
    while (eventType != XMLStreamConstants.END_ELEMENT) {
        if (eventType == XMLStreamConstants.CHARACTERS || eventType == XMLStreamConstants.CDATA
                || eventType == XMLStreamConstants.SPACE
        /* || eventType == XMLStreamConstants.ENTITY_REFERENCE*/) {
            content.append(xmlr.getText());
        } else if (eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
                || eventType == XMLStreamConstants.COMMENT
                || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
            // skipping
        } else if (eventType == XMLStreamConstants.END_DOCUMENT) {
            throw new XMLStreamException("unexpected end of document when reading element text content");
        } else if (eventType == XMLStreamConstants.START_ELEMENT) {
            throw new XMLStreamException("element text content may not contain START_ELEMENT",
                    xmlr.getLocation());
        } else {
            throw new XMLStreamException("Unexpected event type " + eventType, xmlr.getLocation());
        }
        eventType = xmlr.next();
    }
    return content.toString();
}

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 {// w w w.j  av  a2 s  .  c om
            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:com.cedarsoft.serialization.test.performance.XmlParserPerformance.java

private void benchParse(javolution.xml.stream.XMLInputFactory inputFactory)
        throws XMLStreamException, javolution.xml.stream.XMLStreamException {
    for (int i = 0; i < BIG; i++) {
        javolution.xml.stream.XMLStreamReader parser = inputFactory
                .createXMLStreamReader(new StringReader(CONTENT_SAMPLE));

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

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

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getLocalName().toString());
        assertEquals(XMLStreamReader.CHARACTERS, parser.next());

        String id = parser.getText().toString();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("id", parser.getLocalName().toString());

        assertEquals(XMLStreamReader.START_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getLocalName().toString());

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

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

        String extension = parser.getText().toString();

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("extension", parser.getLocalName().toString());

        assertEquals(XMLStreamReader.END_ELEMENT, parser.nextTag());
        assertEquals("fileType", parser.getLocalName().toString());
        assertEquals(XMLStreamReader.END_DOCUMENT, parser.next());

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

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