Example usage for javax.xml.stream XMLStreamReader getName

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

Introduction

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

Prototype

public QName getName();

Source Link

Document

Returns a QName for the current START_ELEMENT or END_ELEMENT event

Usage

From source file:org.socraticgrid.workbench.ontomodel.service.SyntacticalOntoModelServiceImpl.java

/**
 * Extracts all the Fact Types used in a process definition.
 * Each Fact Type is declared as://from   w  w w. j ava 2  s  .  c o  m
 * 
 *   <bpmn2:textAnnotation id="_92807518-95EC-447B-A292-D46C9D5958E9">
 *       <bpmn2:text>KMRCustom--Diagnosis--code--49320</bpmn2:text>
 *   </bpmn2:textAnnotation>
 * 
 * In the previous example, Diagnosis is the Fact Type
 * 
 * @param processXml
 * @return
 * @throws XMLStreamException 
 */
private Set<String> getProcessFactTypesFromXml(String processXml) throws XMLStreamException {

    Set<String> facts = new HashSet<String>();

    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(processXml.getBytes()));

    boolean parsingTextAnnotation = false;

    //<bpmn2:text>KMRCustom--Diagnosis--code--49320</bpmn2:text>
    while (reader.hasNext()) {
        switch (reader.next()) {
        case XMLStreamReader.START_ELEMENT:
            if ("bpmn2".equals(reader.getName().getPrefix())
                    && "textAnnotation".equals(reader.getName().getLocalPart())) {
                parsingTextAnnotation = true;
            }
            if (parsingTextAnnotation && "bpmn2".equals(reader.getName().getPrefix())
                    && "text".equals(reader.getName().getLocalPart())) {
                String text = reader.getElementText();
                if (text.startsWith("KMRCustom--")) {
                    String[] parts = text.split("--");
                    facts.add(parts[1]);
                }
            }
            break;
        case XMLStreamReader.END_ELEMENT:
            if ("bpmn2".equals(reader.getName().getPrefix())
                    && "textAnnotation".equals(reader.getName().getLocalPart())) {
                parsingTextAnnotation = false;
            }
        }
    }

    return facts;
}