Java XPath Get getProcessIdFromBpmn(final String bpmn)

Here you can find the source of getProcessIdFromBpmn(final String bpmn)

Description

get Process Id From Bpmn

License

Open Source License

Declaration

public static String getProcessIdFromBpmn(final String bpmn) 

Method Source Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

public class Main {
    private static final String BPMN2_NAMESPACE_URI = "http://www.omg.org/spec/BPMN/20100524/MODEL";
    private static final String BPMN_PROCESS_ID_XPATH_EXPR = "//bpmn2:process[@id]";
    private static final String BPMN_PROCESS_ID_ATTR = "id";

    public static String getProcessIdFromBpmn(final String bpmn) {
        String processId = null;//  www  .  j av a2  s .co  m
        try {
            final DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            final Document doc = domFactory.newDocumentBuilder()
                    .parse(new ByteArrayInputStream(bpmn.getBytes("UTF-8")));
            final XPath xpath = XPathFactory.newInstance().newXPath();
            xpath.setNamespaceContext(new NamespaceContext() {
                @Override
                public Iterator<?> getPrefixes(final String namespaceURI) {
                    // Not used in this context.
                    return null;
                }

                @Override
                public String getPrefix(final String namespaceURI) {
                    // Not used in this context.
                    return null;
                }

                @Override
                public String getNamespaceURI(final String prefix) {
                    // Only require the URI for the bpmn2 NS.
                    return BPMN2_NAMESPACE_URI;
                }
            });
            final XPathExpression expr = xpath.compile(BPMN_PROCESS_ID_XPATH_EXPR);

            final Node node = (Node) expr.evaluate(doc, XPathConstants.NODE);
            processId = node.getAttributes().getNamedItem(BPMN_PROCESS_ID_ATTR).getNodeValue();
        } catch (final Exception e) {
            e.printStackTrace();
        }
        return processId;
    }
}

Related

  1. getNodesListXpathNode(final String xPath, final Node node)
  2. getNodeText(XPath xpath, String xp, Node n)
  3. getNodeTextByXPath(Document doc, String xpath)
  4. getNogeList(String path, Node node)
  5. getORSVersion()
  6. getPublicKeyFromKeyInfo(Node keyInfoNode)
  7. getResultXpathstring(String expr, InputSource inputSource)
  8. getScrProperties(String componentName)
  9. getSearchHandlerNode(final File solrconfig)