java - Parsing BPMN file -


my question simple. how can parse bpmn file ? there package in java ? found example code googling, there no main , there no method take bpmn file parse it.

import java.net.url; import java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.enumeration; import java.util.list;  import javax.xml.namespace.qname;  import org.w3c.dom.element;  import org.jbpm.api.jbpmexception; import org.jbpm.api.activity.activitybehaviour; import org.jbpm.bpmn.common.resource; import org.jbpm.bpmn.common.resourceparameter; import org.jbpm.bpmn.flownodes.bpmnactivity; import org.jbpm.bpmn.model.bpmnprocessdefinition; import org.jbpm.bpmn.model.sequenceflowcondition; import org.jbpm.bpmn.parser.bindingsparser; import org.jbpm.internal.log.log; import org.jbpm.pvm.internal.cal.cronexpression; import org.jbpm.pvm.internal.cal.duration; import org.jbpm.pvm.internal.env.environmentimpl; import org.jbpm.pvm.internal.model.activityimpl; import org.jbpm.pvm.internal.model.compositeelementimpl; import org.jbpm.pvm.internal.model.processdefinitionimpl; import org.jbpm.pvm.internal.model.scopeelementimpl; import org.jbpm.pvm.internal.model.timerdefinitionimpl; import org.jbpm.pvm.internal.model.transitionimpl; import org.jbpm.pvm.internal.model.variabledefinitionimpl; import org.jbpm.pvm.internal.task.taskdefinitionimpl; import org.jbpm.pvm.internal.util.collectionutil; import org.jbpm.pvm.internal.util.tagbinding; import org.jbpm.pvm.internal.util.xmlutil; import org.jbpm.pvm.internal.xml.bindings; import org.jbpm.pvm.internal.xml.parse; import org.jbpm.pvm.internal.xml.parser;  public class bpmnparser extends parser {    private static final log log = log.getlog(bpmnparser.class.getname());    private static final string[] default_activities_resources = { "jbpm.bpmn.flownodes.xml" };   private static final string category_activity = "activity";   private static final string[] schema_resources = { "bpmn20.xsd", "diagramdefinition.xsd",     "diagraminterchange.xsd", "bpmndi.xsd" };    static bindingsparser bindingsparser = new bindingsparser();    public bpmnparser() {     // initialises bindings     parsebindings();     // setting bpmn2 xsd schema     setschemaresources(schema_resources);   }    public object parsedocumentelement(element documentelement, parse parse) {     list<processdefinitionimpl> processdefinitions = new arraylist<processdefinitionimpl>();      parsedefinition(documentelement, parse);      (element processelement : xmlutil.elements(documentelement, "process")) {       processdefinitionimpl processdefinition = parseprocess(processelement, parse);       processdefinitions.add(processdefinition);     }      return processdefinitions;   }    public processdefinitionimpl parseprocess(element processelement, parse parse) {     bpmnprocessdefinition processdefinition = new bpmnprocessdefinition();      parse.contextstackpush(processdefinition);     try {        string id = xmlutil.attribute(processelement, "id", parse);       string name = xmlutil.attribute(processelement, "name");        if (id != null && !"".equals(id)) {         processdefinition.setname(id);       } else {         parse.addproblem("process has no or empty id");       }        if (name != null) {         processdefinition.setkey(name);       }        element descriptionelement = xmlutil.element(processelement, "documentation");       if (descriptionelement != null) {         string description = xmlutil.getcontenttext(descriptionelement);         processdefinition.setdescription(description);       }        parseresources((element)processelement.getparentnode(), parse, processdefinition);        parseinterfaces((element)processelement.getparentnode(), parse, processdefinition);        parseitemdefinitions((element)processelement.getparentnode(), parse, processdefinition);        parsemessages((element)processelement.getparentnode(), parse, processdefinition);        parsedataobjects(processelement, parse, processdefinition);        // activities       parseactivities(processelement, parse, processdefinition);        // bind activities destinations       parsesequenceflow(processelement, parse, processdefinition);      } {       parse.contextstackpop();     }      return processdefinition;   }    // /////////////////////////////////////////////////////////////////////////////////////////    protected void parsebindings() {     bindings bindings = new bindings();     setbindings(bindings);      (string activityresource : default_activities_resources) {       enumeration<url> resourceurls = getresources(activityresource);       if (resourceurls.hasmoreelements()) {         while (resourceurls.hasmoreelements()) {           url resourceurl = resourceurls.nextelement();           log.trace("loading bpmn activities resource: " + resourceurl);           list<?> activitybindings = (list<?>) bindingsparser.createparse()             .seturl(resourceurl)             .execute()             .checkerrors("bpmn activities " + resourceurl.tostring())             .getdocumentobject();            (tagbinding binding: collectionutil.checklist(activitybindings, tagbinding.class)) {             binding.setcategory(category_activity);             bindings.addbinding(binding);           }         }       } else {         log.trace("skipping unavailable activities resource: " + activityresource);       }     }   }    protected enumeration<url> getresources(string resourcename) {     classloader classloader = thread.currentthread().getcontextclassloader();     enumeration<url> resourceurls;     try {       resourceurls = classloader.getresources(resourcename);        if (!resourceurls.hasmoreelements()) {         resourceurls = bpmnparser.class.getclassloader().getresources(resourcename);       }     } catch (exception e) {       throw new jbpmexception("couldn't resource urls "+resourcename, e);     }     return resourceurls;   }    private void parsedataobjects(element element, parse parse, bpmnprocessdefinition processdefinition) {     list<variabledefinitionimpl> variabledefinitions = new arraylist<variabledefinitionimpl>();      (element dataelement : xmlutil.elements(element, "dataobject")) {       variabledefinitionimpl variabledefinition = new variabledefinitionimpl();        string name = xmlutil.attribute(dataelement, "id", parse);       variabledefinition.setname(name);        string typeref = xmlutil.attribute(dataelement, "itemsubjectref");       variabledefinition.settypename(processdefinition.gettype(typeref));        variabledefinitions.add(variabledefinition);     }      processdefinition.setvariabledefinition(variabledefinitions);   }    public void parseactivities(element element, parse parse, compositeelementimpl compositeelement) {     list<element> elements = xmlutil.elements(element);     (element nestedelement : elements) {       string tagname = nestedelement.getlocalname();       string name = xmlutil.attribute(nestedelement, "name");       string id = xmlutil.attribute(nestedelement, "id", parse);        tagbinding activitybinding = (tagbinding) getbinding(nestedelement, category_activity);       if (activitybinding == null) {         if (!"sequenceflow".equals(tagname)) {           log.debug("unrecognized activity: " + tagname);         }         continue;       }        activityimpl activity = compositeelement.createactivity();       parse.contextstackpush(activity);       try {         activity.settype(activitybinding.gettagname());         activity.setname(id);         activity.setdescription(name);          if (log.isdebugenabled()) {           log.debug("parsing activity: " + name + "(id=" + id + ")");         }          activitybehaviour activitybehaviour = (activitybehaviour) activitybinding.parse(nestedelement, parse, this);         activity.setactivitybehaviour(activitybehaviour);       } {         parse.contextstackpop();       }     }   }    public void parsesequenceflow(element element, parse parse, bpmnprocessdefinition processdefinition) {     list<element> transitionelements = xmlutil.elements(element, "sequenceflow");     (element transitionelement : transitionelements) {        // parse attributes       string transitionname = xmlutil.attribute(transitionelement, "name");       string transitionid = xmlutil.attribute(transitionelement, "id", parse);       string sourceref = xmlutil.attribute(transitionelement, "sourceref", parse);       string targetref = xmlutil.attribute(transitionelement, "targetref", parse);        if (log.isdebugenabled()) {         log.debug(transitionid + ": " + sourceref + " -> " + targetref);       }        // create new outgoing transition on sourceactivity       activityimpl sourceactivity = processdefinition.findactivity(sourceref);       transitionimpl transition = null;       if (sourceactivity != null) {         transition = sourceactivity.createoutgoingtransition();         transition.setname(transitionid);         transition.setdescription(transitionname);       } else {         parse.addproblem("sourceref " + sourceref + " cannot found");       }        // create incoming transition on targetactivity       activityimpl destinationactivity = processdefinition.findactivity(targetref);       if (destinationactivity != null) {         destinationactivity.addincomingtransition(transition);       } else {         parse.addproblem("targetref '" + targetref + "' cannot found");       }        // set default sequence flow if applicable       try {         // if went wrong parsing activity, there no behaviour , exception thrown in .getbehaviour()         activitybehaviour behaviour = sourceactivity.getactivitybehaviour();         if (behaviour instanceof bpmnactivity) {           bpmnactivity bpmnactivity = (bpmnactivity) behaviour;           string defaultseqflow = bpmnactivity.getdefault();           if (bpmnactivity.isdefaultenabled() && defaultseqflow != null) {             if (transitionid.equals(defaultseqflow)) {               processdefinition.findactivity(sourceref).setdefaultoutgoingtransition(transition);             }           } else {             processdefinition.findactivity(sourceref).setdefaultoutgoingtransition(null);           }         } else {           // other flownodes not have default sequenceflows, set null           processdefinition.findactivity(sourceref).setdefaultoutgoingtransition(null);         }        } catch (jbpmexception je) {         // catch , re-throw if not specific exception.         if (!je.getmessage().contains("no behaviour on")) {           throw je;         }       }        parseconditiononsequenceflow(parse, transitionelement, transitionid, transition);        processdefinition.addsequenceflow(transitionid, transition);     }   }    public void parseconditiononsequenceflow(parse parse, element transitionelement, string transitionid, transitionimpl transition) {     element conditionelement = xmlutil.element(transitionelement, "conditionexpression");     if (conditionelement != null) {       string type = conditionelement.getattributens("http://www.w3.org/2001/xmlschema-instance", "type");       if ("bpmn:tformalexpression".equals(type) || "tformalexpression".equals(type)) {          string expr = conditionelement.gettextcontent();         string lang = xmlutil.attribute(conditionelement, "language");         // todo: add looking default language in document under definitions if lang  null.          if (expr != null) {           expr = expr.trim();         }          sequenceflowcondition condition = new sequenceflowcondition();         condition.setexpression(expr);         condition.setlanguage(lang);         transition.setcondition(condition);        } else {         parse.addproblem("type of conditionexpression on sequenceflow id=" +                 transitionid + " of onsupported type 'bpmn:texpression'", transitionelement);       }     }   }    public void parsedefinition(element documentelement, parse parse) {     parseimports(documentelement, parse);   }    public taskdefinitionimpl parsetaskdefinition(element element, parse parse, scopeelementimpl scopeelement) {     taskdefinitionimpl taskdefinition = new taskdefinitionimpl();      string taskname = xmlutil.attribute(element, "name");     taskdefinition.setname(taskname);      bpmnprocessdefinition processdefinition = parse.contextstackfind(bpmnprocessdefinition.class);     if (processdefinition.gettaskdefinition(taskname) != null) {       parse.addproblem("duplicate task name " + taskname, element);     } else {       processdefinition.addtaskdefinitionimpl(taskdefinition);     }      return taskdefinition;   }    /**    * parses <timereventdefinition> element:    *    * sets duedate if 'timedate' used    *    * sets duedatedescription if duration expression used    *    * set cronexpression if cron expression used    *    * @param timereventdefinitionelement xml element defines timer definition    * @param activity activity on timer definition must created    * @param eventid id of event on timer defined    */   public timerdefinitionimpl parsetimereventdefinition(element timereventdefinitionelement, parse parse, string eventid) {      element timedate = xmlutil.element(timereventdefinitionelement, "timedate");     element timecycle = xmlutil.element(timereventdefinitionelement, "timecycle");      if ( (timedate != null && timecycle != null)             || (timedate == null && timecycle == null) ) {       parse.addproblem("timereventdefinition event '" + eventid +               "' requires either timedate or timecycle definition (but not both)");       return null;     }      timerdefinitionimpl timerdefinition = new timerdefinitionimpl();      if (timedate != null) {       parsetimedate(eventid, parse, timedate, timerdefinition);     }      if (timecycle != null) {       parsetimecycle(eventid, parse, timecycle, timerdefinition);     }      return timerdefinition;   }    protected void parsetimedate(string catcheventid, parse parse, element timedate, timerdefinitionimpl timerdefinition) {     string duedatetime = timedate.gettextcontent();     string duedatetimeformattext = (string) environmentimpl.getfromcurrent("jbpm.duedatetime.format", false);     if (duedatetimeformattext==null) {       duedatetimeformattext = "dd/mm/yyyy hh:mm:ss";     }     simpledateformat dateformat = new simpledateformat(duedatetimeformattext);     try {       date duedatetimedate = dateformat.parse(duedatetime);       timerdefinition.setduedate(duedatetimedate);     } catch (parseexception e) {       parse.addproblem("couldn't parse timedate '"+ duedatetime               + "' on intermediate catch timer event " + catcheventid, e);     }   }    protected void parsetimecycle(string catcheventid, parse parse, element timecycle, timerdefinitionimpl timerdefinition) {     string cycleexpression = timecycle.gettextcontent();     if (duration.isvalidexpression(cycleexpression)) {       timerdefinition.setduedatedescription(cycleexpression);     } else if (cronexpression.isvalidexpression(cycleexpression)) {       timerdefinition.setcronexpression(cycleexpression);     } else {       parse.addproblem("couldn't parse timedate duration '"+ cycleexpression               + "' on intermediate catch timer event " + catcheventid);     }   }    public void parseresources(element documentelement, parse parse, bpmnprocessdefinition processdefinition) {      (element resourceelement : xmlutil.elements(documentelement, "resource")) {        resource resource = new resource();        resource.setid(xmlutil.attribute(resourceelement, "id"));       resource.setname(xmlutil.attribute(resourceelement, "name"));        (element resourceparameterelement : xmlutil.elements(documentelement, "resourceparameter")) {          resourceparameter resourceparameter = new resourceparameter();         resourceparameter.setid(xmlutil.attribute(resourceparameterelement, "id"));         resourceparameter.setname(xmlutil.attribute(resourceparameterelement, "name"));         resourceparameter.settype(qname.valueof(xmlutil.attribute(resourceparameterelement, "name")));          resource.getparameters().put(xmlutil.attribute(resourceparameterelement, "name"), resourceparameter);       }        processdefinition.getresources().put(resource.getname(), resource);     }    }    public void parseinterfaces(element documentelement, parse parse, bpmnprocessdefinition processdefinition) {      (element interfaceelement : xmlutil.elements(documentelement, "interface")) {       (element operationelement : xmlutil.elements(interfaceelement, "operation")) {         processdefinition.getoperations().put(xmlutil.attribute(operationelement, "id"), operationelement);       }       processdefinition.getinterfaces().put(xmlutil.attribute(interfaceelement, "id"), interfaceelement);     }   }    public void parsemessages(element documentelement, parse parse, bpmnprocessdefinition processdefinition) {      (element messageelement : xmlutil.elements(documentelement, "message")) {       processdefinition.getmessages().put(xmlutil.attribute(messageelement, "id"), messageelement);     }   }    public void parseitemdefinitions(element documentelement, parse parse, bpmnprocessdefinition processdefinition) {      (element itemdefinitionelement : xmlutil.elements(documentelement, "itemdefinition")) {       processdefinition.getitemdefinitions().put(xmlutil.attribute(itemdefinitionelement, "id"), itemdefinitionelement);     }    }    public void parseimports(element documentelement, parse parse) {    }  } 

as bpmn use xml files interoperability can use standard java xml parser (you can refer jaxp tutorial).

you can xml schema bpmn specification page.


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -