cancel
Showing results for 
Search instead for 
Did you mean: 

using JAXB to create xml

vishnukumarnekk
Champ in-the-making
Champ in-the-making
I have compiles the BPMN20.xsd, BPMNDI.xsd, DI.xsd, DC.xsd, Semantics.xsd togather with xjc for generating classes for jaxb,
I am able to use these classes to create most of the tags in xml, but I am not able to find how to assign form data or declare variables in user task.

here is the code:


package trial;

import javax.xml.bind.*;
import jaxbRefClasses.*;
import java.io.*;

public class conv {
    private ObjectFactory ofact;
    private TDefinitions def;
    private TProcess proc;
   
    public conv(){
           ofact = new ObjectFactory();
           def = ofact.createTDefinitions();
           proc = ofact.createTProcess();
       }  

       public void makeDefinition(){
           def.setId("initial");
           def.setName("new def");
           def.setTargetNamespace("http://activiti.org/bpmn20");
       }  

       public void makeProcess(){
          proc.setId("processes1");
           proc.setName("my processes");
           JAXBElement<TProcess> thepro = ofact.createProcess(proc);
           def.getRootElement().add(thepro);
       }
      
       public TFlowElement makeStartEvent(String id, String name){
          TStartEvent st = ofact.createTStartEvent();
          st.setId(id);
          st.setName(name);
          JAXBElement<TStartEvent> start = ofact.createStartEvent(st);
          proc.getFlowElement().add(start);
          return st;
       }
      
       public TFlowElement makeTask(String id, String name, TDocumentation doc,String asignee){
          TUserTask t = ofact.createTUserTask();
          JAXBElement<TUserTask> usertask = ofact.createUserTask(t);
           proc.getFlowElement().add(usertask);
           t.setId(id);
           t.setName(name);
           t.getDocumentation().add(doc);
           //TExtensionElements ext = ofact.createTExtensionElements();
           if(asignee != ""){
           TPotentialOwner owner = ofact.createTPotentialOwner();
           TResourceAssignmentExpression rae = ofact.createTResourceAssignmentExpression();
           TFormalExpression fe = ofact.createTFormalExpression();
           fe.getContent().add(asignee);
           rae.setExpression(ofact.createFormalExpression(fe));
           owner.setResourceAssignmentExpression(rae);
           t.getResourceRole().add(ofact.createPotentialOwner(owner));
           }
           return t;
           }
      
       public void makeSeqFlow(String id, TFlowElement tskfrm, TFlowElement tskto , String condition){
          TSequenceFlow sflow = ofact.createTSequenceFlow();
          JAXBElement<TSequenceFlow> seqflow = ofact.createSequenceFlow(sflow);
          proc.getFlowElement().add(seqflow);
          sflow.setId(id);
          sflow.setSourceRef(tskfrm);
          sflow.setTargetRef(tskto);
          if(condition != ""){
          TFormalExpression fe = ofact.createTFormalExpression();
          fe.getContent().add("${accepted == " + condition + "}");
          sflow.setConditionExpression(fe);
          }
       }
      
       public TFlowElement makeExclusiveGateway(String id){
          TExclusiveGateway eg = ofact.createTExclusiveGateway();
          JAXBElement<TExclusiveGateway> gway = ofact.createExclusiveGateway(eg);
          proc.getFlowElement().add(gway);
          eg.setId(id);
          return eg;
       }
      
       public TDocumentation makeDocumentation(String inp){
          TDocumentation doc = ofact.createTDocumentation();
          doc.getContent().add(inp);
          return doc;
       }
      
       public TFlowElement makeEndEvent(String id, String name){
          TEndEvent ed = ofact.createTEndEvent();
          ed.setId(id);
          ed.setName(name);
          JAXBElement<TEndEvent> end = ofact.createEndEvent(ed);
          proc.getFlowElement().add(end);
          return ed;
       }
   
    public void marshal() throws IOException {
           try {
               JAXBElement<TDefinitions> mydef =
                       ofact.createDefinitions( def );
               JAXBContext jc = JAXBContext.newInstance( "jaxbRefClasses" );
               Marshaller m = jc.createMarshaller();
               m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
               m.marshal( mydef, System.out );
              FileWriter writer = new FileWriter(new File("a.bpmn20.xml"));
              m.marshal(mydef, writer);
              writer.close();
           }
           catch( JAXBException jbe ){
                   System.out.println("fail");
           }
    }
   
   public static void main(String[] args) throws IOException {
    conv k = new conv();
   k.makeDefinition();
   k.makeProcess();
    TFlowElement tsk = k.makeTask("task1", "initiate",k.makeDocumentation("this is first documentation"), "user(fozzie)");
    TFlowElement endtask = k.makeEndEvent("end", "the end");
    TFlowElement gate;
   // k.makeSeqFlow("start", "the start", k.makeStartEvent("start", "the start"), tsk);
    k.makeSeqFlow("startflow", k.makeStartEvent("start", "the start"), gate = k.makeExclusiveGateway("first"), "");
    //while tasks exist, loop
   k.makeSeqFlow("flow1", gate, tsk = k.makeTask("task2", "ender",k.makeDocumentation("2nd"), "user(kermit)"), "'true'");
   k.makeSeqFlow("flow2", gate, endtask, "'false'");
   k.makeSeqFlow("flow3", tsk, gate = k.makeExclusiveGateway("second"), "");
   //end loop
   k.makeSeqFlow("flow4", gate, endtask, "");
   k.makeSeqFlow("flow5", gate, endtask, "");
   k.marshal();
   }

}



The output is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:ns2="http://www.omg.org/spec/DD/20100524/DI" xmlns:ns3="http://www.omg.org/spec/DD/20100524/DC" xmlns:ns4="http://www.omg.org/spec/BPMN/20100524/DI" id="initial" name="new def" targetNamespace="http://activiti.org/bpmn20">
    <process name="my processes" id="processes1">
        <userTask name="initiate" id="task1">
            <documentation>this is first documentation</documentation>
            <potentialOwner>
                <resourceAssignmentExpression>
                    <formalExpression>user(fozzie)</formalExpression>
                </resourceAssignmentExpression>
            </potentialOwner>
        </userTask>
        <endEvent name="the end" id="end"/>
        <startEvent name="the start" id="start"/>
        <exclusiveGateway id="first"/>
        <sequenceFlow sourceRef="start" targetRef="first" id="startflow"/>
        <userTask name="ender" id="task2">
            <documentation>2nd</documentation>
            <potentialOwner>
                <resourceAssignmentExpression>
                    <formalExpression>user(kermit)</formalExpression>
                </resourceAssignmentExpression>
            </potentialOwner>
        </userTask>
        <sequenceFlow sourceRef="first" targetRef="task2" id="flow1">
            <conditionExpression xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="tFormalExpression">${accepted == 'true'}</conditionExpression>
        </sequenceFlow>
        <sequenceFlow sourceRef="first" targetRef="end" id="flow2">
            <conditionExpression xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="tFormalExpression">${accepted == 'false'}</conditionExpression>
        </sequenceFlow>
        <exclusiveGateway id="second"/>
        <sequenceFlow sourceRef="task2" targetRef="second" id="flow3"/>
        <sequenceFlow sourceRef="second" targetRef="end" id="flow4"/>
        <sequenceFlow sourceRef="second" targetRef="end" id="flow5"/>
    </process>
</definitions>

1 REPLY 1

jbarrez
Star Contributor
Star Contributor
Use the Activiti classes to do this: we have a BpmnModel class that exactly does that: give a pojo way of creating a process and exporting it to XML. And it contains all Activiti specific stuff.