Example usage for javax.xml.stream XMLStreamConstants START_ELEMENT

List of usage examples for javax.xml.stream XMLStreamConstants START_ELEMENT

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamConstants START_ELEMENT.

Prototype

int START_ELEMENT

To view the source code for javax.xml.stream XMLStreamConstants START_ELEMENT.

Click Source Link

Document

Indicates an event is a start element

Usage

From source file:jodtemplate.pptx.io.xml.PresentationXmlRelsReader.java

@Override
public Presentation read(final String path, final Resources resources, final XMLInputFactory xmlInputFactory,
        final Presentation presentation) throws XMLStreamException, IOException {
    final Resource presentationXmlRelsRes = resources.getResource(Utils.removePrefixSeparator(path));
    try (final InputStream is = presentationXmlRelsRes.getInputStream()) {
        final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(is);
        int event = xmlStreamReader.next();
        while (event != XMLStreamConstants.END_DOCUMENT) {
            if (xmlStreamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                final String elementNS = xmlStreamReader.getName().getNamespaceURI();
                final String elementName = xmlStreamReader.getName().getLocalPart();
                if (OOXMLDocument.RELATIONSHIP_ELEMENT.equals(elementName)
                        && OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE.equals(elementNS)) {
                    final Relationship relationship = createRelationshipElement(xmlStreamReader);
                    if (Relationship.SLIDE_TYPE.equals(relationship.getType())) {
                        final Slide slide = new Slide();
                        slide.setRelationship(relationship);
                        slide.setPresentation(presentation);
                        presentation.addSlide(slide);
                    } else {
                        presentation.addOtherRelationship(relationship);
                    }//  ww  w.j  a v  a  2s. c om
                }
            }
            event = xmlStreamReader.next();
        }
    }

    return presentation;
}

From source file:org.maodian.flyingcat.xmpp.codec.InfoQueryCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {/*from ww w . ja  v a  2 s  .  c  o  m*/
        String id = xmlsr.getAttributeValue("", "id");
        String type = xmlsr.getAttributeValue("", "type");
        Builder builder = new Builder(id, type);

        String from = xmlsr.getAttributeValue("", "from");
        String to = xmlsr.getAttributeValue("", "to");
        String language = xmlsr.getAttributeValue(XMLConstants.XML_NS_URI, "lang");
        builder.from(from).to(to).language(language);

        switch (type) {
        case InfoQuery.GET:
        case InfoQuery.SET:
            if (xmlsr.nextTag() != XMLStreamConstants.START_ELEMENT) {
                throw new XmppException(StanzaErrorCondition.BAD_REQUEST);
            }
            QName key = xmlsr.getName();
            builder.payload(findDecoder(key, builder.build()).decode(xmlsr));
            break;
        case InfoQuery.RESULT:
            // do nothing
            break;
        case InfoQuery.ERROR:
            throw new IllegalStateException(
                    "Since this is a server, it should not dealing with incoming result and error");
        default:
            break;
        }
        return builder.build();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.maodian.flyingcat.xmpp.extensions.xep0077.RegistrationCodec.java

@Override
public Object decode(XMLStreamReader xmlsr) {
    try {/*from w ww .j  a v  a  2s  .c  om*/
        xmlsr.require(XMLStreamConstants.START_ELEMENT, InBandRegistration.REGISTER, "query");
        String username = null;
        String password = null;

        while (xmlsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
            if (xmlsr.getName().equals(new QName(InBandRegistration.REGISTER, "username"))) {
                username = xmlsr.getElementText();
            } else if (xmlsr.getName().equals(new QName(InBandRegistration.REGISTER, "password"))) {
                password = xmlsr.getElementText();
            }
        }
        return new Registration(username, password);
    } catch (XMLStreamException e) {
        throw new XmppException(e, StreamError.INVALID_XML);
    }
}

From source file:net.di2e.ecdr.libs.result.relevance.TextParser.java

/**
 * Given xml as a string, this method will parse out element text and CDATA text. It separates
 * each by one space character./*  ww  w.ja  va 2 s  . co  m*/
 *
 * @param xmlData
 *            XML as a {@code String}
 * @return parsed CDATA and element text
 */
protected static String parseTextFrom(String xmlData) {

    StringBuilder builder = new StringBuilder();

    XMLStreamReader xmlStreamReader;

    try {
        // xml parser does not handle leading whitespace
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xmlData));

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

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

                String text = xmlStreamReader.getText();

                if (StringUtils.isNotBlank(text)) {
                    builder.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(" " + text.trim());
                    }
                }
            }
        }
    } catch (XMLStreamException e1) {
        LOGGER.warn("Failure occurred in parsing the xml data (" + xmlData
                + "). No data has been stored or indexed.", e1);
    }

    return builder.toString();
}

From source file:net.kados.gtp.app.libs.Teryt.Subjects.Streets.java

@Override
public void extract() throws XMLStreamException, FileNotFoundException {
    FileInfo ulic = this.bfis.get(Parser.Types.ULIC.toString());

    this.factory = XMLInputFactory.newInstance();
    this.readerRaw = factory
            .createXMLStreamReader(new BufferedInputStream(new FileInputStream(ulic.getFile())));
    this.reader = factory.createFilteredReader(readerRaw, this.newLineFilter);

    while (reader.hasNext()) {
        int next = reader.next();

        switch (next) {
        case XMLStreamConstants.START_ELEMENT:
            this.newElement(reader.getLocalName());
            break;

        case XMLStreamConstants.CHARACTERS:
            tagContent = reader.getText().trim();
            break;

        case XMLStreamConstants.END_ELEMENT:
            this.endElement(reader.getLocalName());
            break;
        }//from  w  w  w . j  ava  2s . c  om

        if (streets.size() >= 1000) {
            this.save();
        }

        parserMessages.sendProgress(reader.getLocation().getLineNumber(), ulic.getLines());
    }

    this.save();
}

From source file:de.huxhorn.sulky.plist.impl.PropertyListReader.java

public PropertyList read(XMLStreamReader reader) throws XMLStreamException {
    int type = reader.getEventType();

    if (XMLStreamConstants.START_DOCUMENT == type) {
        do {// w  w w  .  j a v a 2  s . co m
            reader.next();
            type = reader.getEventType();
        } while (XMLStreamConstants.START_ELEMENT != type);
    }
    PropertyList result = new PropertyList();
    if (XMLStreamConstants.START_ELEMENT == type && PLIST_NODE.equals(reader.getLocalName())) {
        reader.nextTag();
        type = reader.getEventType();
        if (!(XMLStreamConstants.END_ELEMENT == type && PLIST_NODE.equals(reader.getLocalName()))) {
            result.setRoot(readValue(reader));
        }
        reader.require(XMLStreamConstants.END_ELEMENT, null, PLIST_NODE);
    }
    return result;
}

From source file:StAXEventTreeViewer.java

public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file)
        throws XMLStreamException, FileNotFoundException {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file));
    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        switch (event.getEventType()) {
        case XMLStreamConstants.START_DOCUMENT:
            StartDocument startDocument = (StartDocument) event;
            DefaultMutableTreeNode version = new DefaultMutableTreeNode(startDocument.getVersion());
            current.add(version);//from  w  ww . j  av  a  2  s.  c  o m

            current.add(new DefaultMutableTreeNode(startDocument.isStandalone()));
            current.add(new DefaultMutableTreeNode(startDocument.standaloneSet()));
            current.add(new DefaultMutableTreeNode(startDocument.encodingSet()));
            current.add(new DefaultMutableTreeNode(startDocument.getCharacterEncodingScheme()));
            break;
        case XMLStreamConstants.START_ELEMENT:
            StartElement startElement = (StartElement) event;
            QName elementName = startElement.getName();

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(elementName.getLocalPart());
            current.add(element);
            current = element;

            if (!elementName.getNamespaceURI().equals("")) {
                String prefix = elementName.getPrefix();
                if (prefix.equals("")) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix=" + prefix + ",URI=" + elementName.getNamespaceURI());
                current.add(namespace);
            }

            for (Iterator it = startElement.getAttributes(); it.hasNext();) {
                Attribute attr = (Attribute) it.next();
                DefaultMutableTreeNode attribute = new DefaultMutableTreeNode("Attribute (name="
                        + attr.getName().getLocalPart() + ",value=" + attr.getValue() + "')");
                String attURI = attr.getName().getNamespaceURI();
                if (!attURI.equals("")) {
                    String attPrefix = attr.getName().getPrefix();
                    if (attPrefix.equals("")) {
                        attPrefix = "[None]";
                    }
                    attribute.add(new DefaultMutableTreeNode("prefix = " + attPrefix + ", URI = " + attURI));
                }
                current.add(attribute);
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            Characters characters = (Characters) event;
            if (!characters.isIgnorableWhiteSpace() && !characters.isWhiteSpace()) {
                String data = characters.getData();
                if (data.length() != 0) {
                    current.add(new DefaultMutableTreeNode(characters.getData()));
                }
            }
            break;
        case XMLStreamConstants.DTD:
            DTD dtde = (DTD) event;
            current.add(new DefaultMutableTreeNode(dtde.getDocumentTypeDeclaration()));
        default:
            System.out.println(event.getClass().getName());
        }
    }
}

From source file:fr.inria.oak.paxquery.pact.operators.unary.NavigationOperator.java

/**
 * //w ww .j  a  v  a  2 s  .c o m
 * @param record
 * @param treePattern
 * @param collector
 */
public static void navigation(NestedMetadata inputRecordSignature, Record record, int column,
        NavigationTreePattern navigationTreePattern, Collector<Record> collector) {
    XMLInputFactory factory = XMLInputFactory.newInstance();

    try {
        StringValue value = record.getField(column, StringValue.class);
        XMLStreamReader streamReader = factory.createXMLStreamReader(new StringReader(value.getValue()));

        SingleDocumentExtractor extractor = new SingleDocumentExtractor(navigationTreePattern, streamReader);

        while (streamReader.hasNext()) {
            streamReader.next();
            if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT) {
                extractor.startElement();
            } else if (streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) {
                extractor.endElement();
            } else if (streamReader.getEventType() == XMLStreamConstants.CHARACTERS) {
                extractor.characters();
            }

            if (extractor.getRecords().size() != 0) {
                Iterator<Record> pactRecordsIterator = extractor.getRecords().iterator();
                while (pactRecordsIterator.hasNext()) {
                    Record originalRecord = record.createCopy();
                    RecordOperations.concatenate(originalRecord, pactRecordsIterator.next());
                    collector.collect(originalRecord);
                }

                extractor.getRecords().clear();
            }
        }

    } catch (XMLStreamException e) {
        logger.error("XMLStreamException", e);
    }
}

From source file:edu.indiana.d2i.htrc.portal.HTRCAgentClient.java

private Map<String, JobDetailsBean> parseJobDetailBeans(InputStream stream) throws XMLStreamException {
    Map<String, JobDetailsBean> res = new TreeMap<String, JobDetailsBean>();
    XMLStreamReader parser = factory.createXMLStreamReader(stream);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.hasName()) {
                if (parser.getLocalName().equals("job_status")) {
                    // one job status
                    JobDetailsBean detail = new JobDetailsBean();
                    Map<String, String> jobParams = new HashMap<String, String>();
                    Map<String, String> results = new HashMap<String, String>();
                    int innerEvent = parser.next();
                    while (true) {
                        if (innerEvent == XMLStreamConstants.END_ELEMENT
                                && parser.getLocalName().equals("job_status")) {
                            break;
                        }/*from www.  ja va  2 s.c  o m*/

                        if (innerEvent == XMLStreamConstants.START_ELEMENT && parser.hasName()) {
                            // single tag
                            if (parser.getLocalName().equals("job_name")) {
                                detail.setJobTitle(parser.getElementText());
                            } else if (parser.getLocalName().equals("user")) {
                                detail.setUserName(parser.getElementText());
                            } else if (parser.getLocalName().equals("algorithm")) {
                                detail.setAlgorithmName(parser.getElementText());
                            } else if (parser.getLocalName().equals("job_id")) {
                                detail.setJobId(parser.getElementText());
                            } else if (parser.getLocalName().equals("date")) {
                                detail.setLastUpdatedDate(parser.getElementText());
                            }

                            // parameters
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.ONEPARAM)) {
                                String name, value;
                                name = value = "";
                                for (int i = 0; i < 3; i++) {
                                    if (parser.getAttributeName(i).toString().equals("name"))
                                        name = parser.getAttributeValue(i);
                                    if (parser.getAttributeName(i).toString().equals("value"))
                                        value = parser.getAttributeValue(i);
                                }
                                jobParams.put(name, value);
                            }

                            // status
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.STATUS)) {
                                String status = parser.getAttributeValue(0);
                                detail.setJobStatus(status);
                            }

                            // results
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.RESULT)) {
                                String name = parser.getAttributeValue(0);
                                String value = parser.getElementText();
                                results.put(name, value);
                            }

                            // message
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.MESSAGE)) {
                                detail.setMessage(parser.getElementText());
                            }

                            // saved or unsaved
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.SAVEDORNOT)) {
                                detail.setJobSavedStr(parser.getElementText());
                            }
                        }
                        innerEvent = parser.next();
                    }
                    detail.setJobParams(jobParams);
                    detail.setResults(results);
                    res.put(detail.getJobId(), detail);
                }
            }
        }
    }
    return res;
}

From source file:StAXStreamTreeViewer.java

private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current)
        throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName());
            current.add(element);// w ww  .  j  a  va 2  s . c  om
            current = element;

            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
                current.add(namespace);
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    DefaultMutableTreeNode attribute = new DefaultMutableTreeNode(
                            "Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                                    + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode(
                                "prefix=" + attPrefix + ",URI=" + attURI);
                        attribute.add(attNamespace);
                    }
                    current.add(attribute);
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText());
                current.add(data);
            }
            break;
        case XMLStreamConstants.DTD:
            DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText());
            current.add(dtd);
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText());
            current.add(comment);
            break;
        default:
            System.out.println(type);
        }
    }
}