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:ddf.catalog.source.opensearch.impl.OpenSearchSource.java

private InputTransformer getInputTransformer(InputStream inputStream) {
    XMLStreamReader xmlStreamReader = null;
    try {/*from w ww . j a  v  a2s .  co 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);
        }
    }
    return null;
}

From source file:de.uzk.hki.da.model.ObjectPremisXmlWriter.java

/**
 * Integrate jhove data.//from www .j  a v  a2s  .  c  o m
 *
 * @param jhoveFilePath the jhove file path
 * @param tab the tab
 * @throws XMLStreamException the xML stream exception
 * @author Thomas Kleinke
 * @throws FileNotFoundException 
 */
private void integrateJhoveData(String jhoveFilePath, int tab)
        throws XMLStreamException, FileNotFoundException {
    File jhoveFile = new File(jhoveFilePath);
    if (!jhoveFile.exists())
        throw new FileNotFoundException("file does not exist. " + jhoveFile);

    FileInputStream inputStream = null;

    inputStream = new FileInputStream(jhoveFile);

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader = inputFactory.createXMLStreamReader(inputStream);

    boolean textElement = false;

    while (streamReader.hasNext()) {
        int event = streamReader.next();

        switch (event) {

        case XMLStreamConstants.START_ELEMENT:
            writer.writeDTD("\n");
            indent(tab);
            tab++;

            String prefix = streamReader.getPrefix();

            if (prefix != null && !prefix.equals("")) {
                writer.setPrefix(prefix, streamReader.getNamespaceURI());
                writer.writeStartElement(streamReader.getNamespaceURI(), streamReader.getLocalName());
            } else
                writer.writeStartElement(streamReader.getLocalName());

            for (int i = 0; i < streamReader.getNamespaceCount(); i++)
                writer.writeNamespace(streamReader.getNamespacePrefix(i), streamReader.getNamespaceURI(i));

            for (int i = 0; i < streamReader.getAttributeCount(); i++) {
                QName qname = streamReader.getAttributeName(i);
                String attributeName = qname.getLocalPart();
                String attributePrefix = qname.getPrefix();
                if (attributePrefix != null && !attributePrefix.equals(""))
                    attributeName = attributePrefix + ":" + attributeName;

                writer.writeAttribute(attributeName, streamReader.getAttributeValue(i));
            }

            break;

        case XMLStreamConstants.CHARACTERS:
            if (!streamReader.isWhiteSpace()) {
                writer.writeCharacters(streamReader.getText());
                textElement = true;
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            tab--;

            if (!textElement) {
                writer.writeDTD("\n");
                indent(tab);
            }

            writer.writeEndElement();
            textElement = false;
            break;

        default:
            break;
        }
    }

    streamReader.close();
    try {
        inputStream.close();
    } catch (IOException e) {
        throw new RuntimeException("Failed to close input stream", e);
    }
}

From source file:ddf.catalog.source.solr.DynamicSchemaResolver.java

/**
 * Given xml as a string, this method will parse out element text and CDATA text. It separates
 * each by one space character./*from   w  w w  . j  a v  a2 s. c o  m*/
 *
 * @param xmlDatas List of XML as {@code String}
 * @return parsed CDATA and element text
 */
protected List<String> parseTextFrom(List<Serializable> xmlDatas) {

    StringBuilder builder = new StringBuilder();
    List<String> parsedTexts = new ArrayList<>();
    XMLStreamReader xmlStreamReader = null;
    StringReader sr = null;
    long starttime = System.currentTimeMillis();

    try {
        for (Serializable xmlData : xmlDatas) {
            // xml parser does not handle leading whitespace
            sr = new StringReader(xmlData.toString());
            xmlStreamReader = XML_INPUT_FACTORY.createXMLStreamReader(sr);

            while (xmlStreamReader.hasNext()) {
                int event = xmlStreamReader.next();

                if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) {

                    String text = xmlStreamReader.getText();

                    if (StringUtils.isNotBlank(text)) {
                        builder.append(" ").append(text.trim());
                    }

                }
                if (event == XMLStreamConstants.START_ELEMENT) {
                    for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) {

                        String text = xmlStreamReader.getAttributeValue(i);

                        if (StringUtils.isNotBlank(text)) {
                            builder.append(" ").append(text.trim());
                        }
                    }
                }
            }
            parsedTexts.add(builder.toString());
            builder.setLength(0);
        }
    } catch (XMLStreamException e1) {
        LOGGER.warn("Failure occurred in parsing the xml data. No data has been stored or indexed.", e1);
    } finally {
        IOUtils.closeQuietly(sr);
        if (xmlStreamReader != null) {
            try {
                xmlStreamReader.close();
            } catch (XMLStreamException e) {
                LOGGER.debug("Exception closing XMLStreamReader", e);
            }
        }
    }
    long endTime = System.currentTimeMillis();

    LOGGER.debug("Parsing took {} ms", endTime - starttime);

    return parsedTexts;
}

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

private void handleItems(XMLStreamReader reader) throws XMLStreamException {
    while (reader.hasNext() && !XMLStreamUtil.isEndTag(reader, responseCollectionName)) {
        reader.next();/*from  w w w.  jav a2  s.c  o m*/
        if (XMLStreamUtil.isStartTag(reader)) {
            responseItems.add(handleItem(reader));
        }
    }

}

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

protected void handleEmailAddresses(XMLStreamReader reader, Item item) throws XMLStreamException {
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "EmailAddresses"))) {
        reader.next();/* w w  w  . jav  a2 s.c  om*/
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("Entry".equals(tagLocalName)) {
                item.put(reader.getAttributeValue(null, "Key"), XMLStreamUtil.getElementText(reader));
            }
        }
    }
}

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

protected void handleModifiedOccurrences(XMLStreamReader reader, Item item) throws XMLStreamException {
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "ModifiedOccurrences"))) {
        reader.next();//from   w w  w .  ja v  a  2s.  com
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("Occurrence".equals(tagLocalName)) {
                handleOccurrence(reader, item);
            }
        }
    }
}

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

protected void handleAttendees(XMLStreamReader reader, Item item, String attendeeType)
        throws XMLStreamException {
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, attendeeType))) {
        reader.next();/*w  w w . java2 s.c o  m*/
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("Attendee".equals(tagLocalName)) {
                handleAttendee(reader, item, attendeeType);
            }
        }
    }
}

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();// w  w  w. j  a v  a 2 s  .  com
        if (reader.getEventType() == XMLStreamConstants.CHARACTERS) {
            return reader.getText();
        }
    }
    // 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 void handleOccurrence(XMLStreamReader reader, Item item) throws XMLStreamException {
    Occurrence occurrence = new Occurrence();
    while (reader.hasNext() && !(XMLStreamUtil.isEndTag(reader, "Occurrence"))) {
        reader.next();/*from w ww . j a  v  a2  s .  co m*/
        if (XMLStreamUtil.isStartTag(reader)) {
            String tagLocalName = reader.getLocalName();
            if ("ItemId".equals(tagLocalName)) {
                occurrence.itemId = new ItemId("ItemId", getAttributeValue(reader, "Id"),
                        getAttributeValue(reader, "ChangeKey"));
            }
            if ("OriginalStart".equals(tagLocalName)) {
                occurrence.originalStart = XMLStreamUtil.getElementText(reader);
            }
        }
    }
    item.addOccurrence(occurrence);
}

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();//from   www  . j  av  a  2s.c  o  m
        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);
            }
        }
    }
    return fileAttachment;
}