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:org.flowable.bpmn.converter.child.IOSpecificationParser.java

public void parseChildElement(XMLStreamReader xtr, BaseElement parentElement, BpmnModel model)
        throws Exception {

    if (parentElement instanceof Activity == false && parentElement instanceof Process == false)
        return;//from  w w w .  jav  a2s.co  m

    IOSpecification ioSpecification = new IOSpecification();
    BpmnXMLUtil.addXMLLocation(ioSpecification, xtr);
    boolean readyWithIOSpecification = false;
    try {
        while (readyWithIOSpecification == false && xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && ELEMENT_DATA_INPUT.equalsIgnoreCase(xtr.getLocalName())) {
                DataSpec dataSpec = new DataSpec();
                BpmnXMLUtil.addXMLLocation(dataSpec, xtr);
                dataSpec.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                dataSpec.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                dataSpec.setItemSubjectRef(
                        parseItemSubjectRef(xtr.getAttributeValue(null, ATTRIBUTE_ITEM_SUBJECT_REF), model));
                ioSpecification.getDataInputs().add(dataSpec);

            } else if (xtr.isStartElement() && ELEMENT_DATA_OUTPUT.equalsIgnoreCase(xtr.getLocalName())) {
                DataSpec dataSpec = new DataSpec();
                BpmnXMLUtil.addXMLLocation(dataSpec, xtr);
                dataSpec.setId(xtr.getAttributeValue(null, ATTRIBUTE_ID));
                dataSpec.setName(xtr.getAttributeValue(null, ATTRIBUTE_NAME));
                dataSpec.setItemSubjectRef(
                        parseItemSubjectRef(xtr.getAttributeValue(null, ATTRIBUTE_ITEM_SUBJECT_REF), model));
                ioSpecification.getDataOutputs().add(dataSpec);

            } else if (xtr.isStartElement() && ELEMENT_DATA_INPUT_REFS.equalsIgnoreCase(xtr.getLocalName())) {
                String dataInputRefs = xtr.getElementText();
                if (StringUtils.isNotEmpty(dataInputRefs)) {
                    ioSpecification.getDataInputRefs().add(dataInputRefs.trim());
                }

            } else if (xtr.isStartElement() && ELEMENT_DATA_OUTPUT_REFS.equalsIgnoreCase(xtr.getLocalName())) {
                String dataOutputRefs = xtr.getElementText();
                if (StringUtils.isNotEmpty(dataOutputRefs)) {
                    ioSpecification.getDataOutputRefs().add(dataOutputRefs.trim());
                }

            } else if (xtr.isEndElement() && getElementName().equalsIgnoreCase(xtr.getLocalName())) {
                readyWithIOSpecification = true;
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Error parsing ioSpecification child elements", e);
    }

    if (parentElement instanceof Process) {
        ((Process) parentElement).setIoSpecification(ioSpecification);
    } else {
        ((Activity) parentElement).setIoSpecification(ioSpecification);
    }
}

From source file:org.flowable.cmmn.converter.CmmnXmlConverter.java

public CmmnModel convertToCmmnModel(XMLStreamReader xtr) {

    ConversionHelper conversionHelper = new ConversionHelper();
    conversionHelper.setCmmnModel(new CmmnModel());

    try {/*w w w .j a  va  2 s .  c om*/
        String currentXmlElement = null;
        while (xtr.hasNext()) {
            try {
                xtr.next();
            } catch (Exception e) {
                LOGGER.debug("Error reading CMMN XML document", e);
                throw new CmmnXMLException("Error reading XML", e);
            }

            if (xtr.isStartElement()) {
                currentXmlElement = xtr.getLocalName();
                if (elementConverters.containsKey(currentXmlElement)) {
                    elementConverters.get(currentXmlElement).convertToCmmnModel(xtr, conversionHelper);
                }

            } else if (xtr.isEndElement()) {
                currentXmlElement = null;
                if (elementConverters.containsKey(xtr.getLocalName())) {
                    elementConverters.get(xtr.getLocalName()).elementEnd(xtr, conversionHelper);
                }

            } else if (xtr.isCharacters() && currentXmlElement != null) {
                if (textConverters.containsKey(currentXmlElement)) {
                    textConverters.get(currentXmlElement).convertToCmmnModel(xtr, conversionHelper);
                }

            }
        }

    } catch (CmmnXMLException e) {
        throw e;

    } catch (Exception e) {
        LOGGER.error("Error processing CMMN XML document", e);
        throw new CmmnXMLException("Error processing CMMN XML document", e);
    }

    // Post process all elements: add instances where a reference is used
    processCmmnElements(conversionHelper);
    return conversionHelper.getCmmnModel();
}

From source file:org.flowable.cmmn.converter.ExtensionElementsXMLConverter.java

@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {

    boolean readyWithChildElements = false;
    try {/*from  w w w. j  a  v a2s . c o m*/

        while (!readyWithChildElements && xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement()) {
                if (CmmnXmlConstants.ELEMENT_COMPLETION_NEUTRAL_RULE.equals(xtr.getLocalName())) {
                    readCompletionNeutralRule(xtr, conversionHelper);

                } else if (CmmnXmlConstants.ELEMENT_FIELD.equals(xtr.getLocalName())) {
                    readFieldExtension(xtr, conversionHelper);

                } else if (CmmnXmlConstants.ELEMENT_HTTP_REQUEST_HANDLER.equals(xtr.getLocalName())) {
                    readHttpRequestHandler(xtr, conversionHelper);

                } else if (CmmnXmlConstants.ELEMENT_HTTP_RESPONSE_HANDLER.equals(xtr.getLocalName())) {
                    readHttpResponseHandler(xtr, conversionHelper);

                } else if (CmmnXmlConstants.ELEMENT_PROCESS_TASK_IN_PARAMETERS.equals(xtr.getLocalName())) {
                    readIOParameter(xtr, true, conversionHelper);

                } else if (CmmnXmlConstants.ELEMENT_PROCESS_TASK_OUT_PARAMETERS.equals(xtr.getLocalName())) {
                    readIOParameter(xtr, false, conversionHelper);

                } else if (CmmnXmlConstants.ELEMENT_TASK_LISTENER.equals(xtr.getLocalName())) {
                    readTaskListener(xtr, conversionHelper);

                } else if (CmmnXmlConstants.ELEMENT_PLAN_ITEM_LIFECYCLE_LISTENER.equals(xtr.getLocalName())) {
                    readLifecycleListener(xtr, conversionHelper);

                } else {
                    ExtensionElement extensionElement = CmmnXmlUtil.parseExtensionElement(xtr);
                    conversionHelper.getCurrentCmmnElement().addExtensionElement(extensionElement);
                }

            } else if (xtr.isEndElement()) {
                if (CmmnXmlConstants.ELEMENT_TASK_LISTENER.equalsIgnoreCase(xtr.getLocalName())
                        || CmmnXmlConstants.ELEMENT_PLAN_ITEM_LIFECYCLE_LISTENER
                                .equalsIgnoreCase(xtr.getLocalName())) {
                    conversionHelper.removeCurrentCmmnElement();
                } else if (CmmnXmlConstants.ELEMENT_EXTENSION_ELEMENTS.equalsIgnoreCase(xtr.getLocalName())) {
                    readyWithChildElements = true;
                }
            }

        }
    } catch (Exception ex) {
        LOGGER.error("Error processing CMMN document", ex);
        throw new XMLException("Error processing CMMN document", ex);
    }

    return null;
}

From source file:org.flowable.cmmn.converter.ExtensionElementsXMLConverter.java

protected void readCompletionNeutralRule(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    if (conversionHelper.getCurrentCmmnElement() instanceof PlanItemControl) {
        CompletionNeutralRule completionNeutralRule = new CompletionNeutralRule();
        completionNeutralRule.setName(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_NAME));

        PlanItemControl planItemControl = (PlanItemControl) conversionHelper.getCurrentCmmnElement();
        planItemControl.setCompletionNeutralRule(completionNeutralRule);

        readCommonXmlInfo(completionNeutralRule, xtr);

        boolean readyWithChildElements = false;
        try {/*  w w  w . j a  va  2 s  .  com*/

            while (!readyWithChildElements && xtr.hasNext()) {
                xtr.next();
                if (xtr.isStartElement()) {
                    if (CmmnXmlConstants.ELEMENT_CONDITION.equals(xtr.getLocalName())) {
                        xtr.next();
                        if (xtr.isCharacters()) {
                            completionNeutralRule.setCondition(xtr.getText());
                        }
                        break;
                    }

                } else if (xtr.isEndElement()) {
                    if (CmmnXmlConstants.ELEMENT_COMPLETION_NEUTRAL_RULE.equalsIgnoreCase(xtr.getLocalName())) {
                        readyWithChildElements = true;
                    }
                }

            }
        } catch (Exception ex) {
            LOGGER.error("Error processing CMMN document", ex);
            throw new XMLException("Error processing CMMN document", ex);
        }
    }
}

From source file:org.flowable.cmmn.converter.ExtensionElementsXMLConverter.java

protected void readFieldExtension(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    BaseElement cmmnElement = conversionHelper.getCurrentCmmnElement();

    FieldExtension extension = new FieldExtension();
    extension.setFieldName(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_NAME));

    String stringValueAttribute = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_FIELD_STRING);
    String expressionAttribute = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_FIELD_EXPRESSION);
    if (StringUtils.isNotEmpty(stringValueAttribute)) {
        extension.setStringValue(stringValueAttribute);

    } else if (StringUtils.isNotEmpty(expressionAttribute)) {
        extension.setExpression(expressionAttribute);

    } else {/*from  w ww.  java 2  s. c  o m*/
        boolean readyWithFieldExtension = false;
        try {
            while (!readyWithFieldExtension && xtr.hasNext()) {
                xtr.next();
                if (xtr.isStartElement()
                        && CmmnXmlConstants.ELEMENT_FIELD_STRING.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setStringValue(xtr.getElementText().trim());

                } else if (xtr.isStartElement()
                        && CmmnXmlConstants.ELEMENT_FIELD_EXPRESSION.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setExpression(xtr.getElementText().trim());

                } else if (xtr.isEndElement()
                        && CmmnXmlConstants.ELEMENT_FIELD.equalsIgnoreCase(xtr.getLocalName())) {
                    readyWithFieldExtension = true;
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Error parsing field extension child elements", e);
        }
    }

    CmmnElement currentCmmnElement = conversionHelper.getCurrentCmmnElement();
    if (currentCmmnElement instanceof ServiceTask) {
        ((ServiceTask) currentCmmnElement).getFieldExtensions().add(extension);

    } else if (currentCmmnElement instanceof DecisionTask) {
        ((DecisionTask) currentCmmnElement).getFieldExtensions().add(extension);
    } else if (currentCmmnElement instanceof FlowableListener) {
        ((FlowableListener) currentCmmnElement).getFieldExtensions().add(extension);

    } else {
        throw new FlowableException("Programmatic error: field added to unkown element " + currentCmmnElement);

    }
}

From source file:org.flowable.cmmn.converter.FieldExtensionXmlConverter.java

@Override
protected CmmnElement convert(XMLStreamReader xtr, ConversionHelper conversionHelper) {
    CmmnElement cmmnElement = conversionHelper.getCurrentCmmnElement();
    if (!(cmmnElement instanceof ServiceTask || cmmnElement instanceof DecisionTask)) {
        return null;
    }//from   ww w .  j av  a  2 s.c  o  m

    TaskWithFieldExtensions serviceTask = (TaskWithFieldExtensions) cmmnElement;

    FieldExtension extension = new FieldExtension();
    extension.setFieldName(xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_NAME));

    String stringValueAttribute = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_FIELD_STRING);
    String expressionAttribute = xtr.getAttributeValue(null, CmmnXmlConstants.ATTRIBUTE_FIELD_EXPRESSION);
    if (StringUtils.isNotEmpty(stringValueAttribute)) {
        extension.setStringValue(stringValueAttribute);

    } else if (StringUtils.isNotEmpty(expressionAttribute)) {
        extension.setExpression(expressionAttribute);

    } else {
        boolean readyWithFieldExtension = false;
        try {
            while (!readyWithFieldExtension && xtr.hasNext()) {
                xtr.next();
                if (xtr.isStartElement()
                        && CmmnXmlConstants.ELEMENT_FIELD_STRING.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setStringValue(xtr.getElementText().trim());

                } else if (xtr.isStartElement()
                        && CmmnXmlConstants.ELEMENT_FIELD_EXPRESSION.equalsIgnoreCase(xtr.getLocalName())) {
                    extension.setExpression(xtr.getElementText().trim());

                } else if (xtr.isEndElement() && getXMLElementName().equalsIgnoreCase(xtr.getLocalName())) {
                    readyWithFieldExtension = true;
                }
            }
        } catch (Exception e) {
            LOGGER.warn("Error parsing field extension child elements", e);
        }
    }

    serviceTask.getFieldExtensions().add(extension);

    return null;
}

From source file:org.flowable.dmn.converter.child.OutputValuesParser.java

@Override
public void parseChildElement(XMLStreamReader xtr, DmnElement parentElement, DecisionTable decisionTable)
        throws Exception {
    if (!(parentElement instanceof OutputClause))
        return;//from  w  ww.  j  a  v  a  2  s  . c om

    OutputClause clause = (OutputClause) parentElement;
    UnaryTests outputValues = new UnaryTests();

    boolean readyWithOutputValues = false;
    try {
        while (!readyWithOutputValues && xtr.hasNext()) {
            xtr.next();
            if (xtr.isStartElement() && ELEMENT_TEXT.equalsIgnoreCase(xtr.getLocalName())) {
                String outputValuesText = xtr.getElementText();

                if (StringUtils.isNotEmpty(outputValuesText)) {
                    String[] outputValuesSplit = outputValuesText.replaceAll("^\"", "")
                            .split("\"?(,|$)(?=(([^\"]*\"){2})*[^\"]*$) *\"?");
                    outputValues.setTextValues(new ArrayList<Object>(Arrays.asList(outputValuesSplit)));
                }

            } else if (xtr.isEndElement() && getElementName().equalsIgnoreCase(xtr.getLocalName())) {
                readyWithOutputValues = true;
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Error parsing output values", e);
    }

    clause.setOutputValues(outputValues);
}

From source file:org.gephi.statistics.StatisticsModelImpl.java

public void readXML(XMLStreamReader reader) throws XMLStreamException {
    Collection<? extends StatisticsUI> uis = Lookup.getDefault().lookupAll(StatisticsUI.class);
    Collection<? extends StatisticsBuilder> builders = Lookup.getDefault().lookupAll(StatisticsBuilder.class);

    boolean end = false;
    while (reader.hasNext() && !end) {
        int type = reader.next();

        switch (type) {
        case XMLStreamReader.START_ELEMENT:
            String name = reader.getLocalName();
            if ("report".equalsIgnoreCase(name)) {
                String classStr = reader.getAttributeValue(null, "class");
                Class reportClass = null;
                for (StatisticsBuilder builder : builders) {
                    if (builder.getStatisticsClass().getName().equals(classStr)) {
                        reportClass = builder.getStatisticsClass();
                    }//from   w  ww  .j a v a2s. co  m
                }
                if (reportClass != null) {
                    String report = reader.getAttributeValue(null, "value");
                    report = unembedImages(report);
                    reportMap.put(reportClass, report);
                }
            }
            break;
        case XMLStreamReader.END_ELEMENT:
            if ("statisticsmodel".equalsIgnoreCase(reader.getLocalName())) {
                end = true;
            }
            break;
        }
    }
}

From source file:org.graphipedia.wikipedia.parser.SimpleStaxParser.java

/**
 * Parses the elements in the XML file.//  ww w.j a va  2s  .  c o  m
 * @param reader The XML stream.
 * @throws XMLStreamException when something goes wrong while parsing the XML file.
 */
private void parseElements(XMLStreamReader reader) throws XMLStreamException {
    LinkedList<String> elementStack = new LinkedList<String>();
    StringBuilder textBuffer = new StringBuilder();
    List<String> attributeValues = new ArrayList<String>();

    while (reader.hasNext()) {
        switch (reader.next()) {
        case XMLEvent.START_ELEMENT:
            String startElement = reader.getName().getLocalPart();
            elementStack.push(startElement);
            attributeValues = new ArrayList<String>();
            if (isInterestingWithAttributes(startElement)) {
                int noAttributes = reader.getAttributeCount();
                for (int i = 0; i < noAttributes; i += 1)
                    attributeValues.add(reader.getAttributeValue(i));
            }
            textBuffer.setLength(0);
            break;
        case XMLEvent.END_ELEMENT:
            String element = elementStack.pop();
            if (isInterestingWithAttributes(element)) {
                if (!handleElement(element, textBuffer.toString().trim(), attributeValues))
                    return;
            } else if (isInteresting(element)) {
                if (!handleElement(element, textBuffer.toString().trim()))
                    return;
            }
            break;
        case XMLEvent.CHARACTERS:
            if (isInteresting(elementStack.peek())) {
                textBuffer.append(reader.getText());
            }
            break;
        }
    }
}

From source file:org.jabref.logic.importer.fileformat.MedlineImporter.java

@Override
public ParserResult importDatabase(BufferedReader reader) throws IOException {
    Objects.requireNonNull(reader);

    List<BibEntry> bibItems = new ArrayList<>();

    try {/*from w ww .j a  va2  s .c  o  m*/
        JAXBContext context = JAXBContext.newInstance("org.jabref.logic.importer.fileformat.medline");
        XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
        XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(reader);

        //go to the root element
        while (!xmlStreamReader.isStartElement()) {
            xmlStreamReader.next();
        }

        Unmarshaller unmarshaller = context.createUnmarshaller();
        Object unmarshalledObject = unmarshaller.unmarshal(xmlStreamReader);

        //check whether we have an article set, an article, a book article or a book article set
        if (unmarshalledObject instanceof PubmedArticleSet) {
            PubmedArticleSet articleSet = (PubmedArticleSet) unmarshalledObject;
            for (Object article : articleSet.getPubmedArticleOrPubmedBookArticle()) {
                if (article instanceof PubmedArticle) {
                    PubmedArticle currentArticle = (PubmedArticle) article;
                    parseArticle(currentArticle, bibItems);
                }
                if (article instanceof PubmedBookArticle) {
                    PubmedBookArticle currentArticle = (PubmedBookArticle) article;
                    parseBookArticle(currentArticle, bibItems);
                }
            }
        } else if (unmarshalledObject instanceof PubmedArticle) {
            PubmedArticle article = (PubmedArticle) unmarshalledObject;
            parseArticle(article, bibItems);
        } else if (unmarshalledObject instanceof PubmedBookArticle) {
            PubmedBookArticle currentArticle = (PubmedBookArticle) unmarshalledObject;
            parseBookArticle(currentArticle, bibItems);
        } else {
            PubmedBookArticleSet bookArticleSet = (PubmedBookArticleSet) unmarshalledObject;
            for (PubmedBookArticle bookArticle : bookArticleSet.getPubmedBookArticle()) {
                parseBookArticle(bookArticle, bibItems);
            }
        }
    } catch (JAXBException | XMLStreamException e) {
        LOGGER.debug("could not parse document", e);
        return ParserResult.fromError(e);
    }
    return new ParserResult(bibItems);
}