1 package org.jbpm.jpdl.xml; 2 3 import java.io.*; 4 import java.util.*; 5 6 import org.dom4j.*; 7 import org.dom4j.io.*; 8 import org.jbpm.graph.action.ActionTypes; 9 import org.jbpm.graph.def.*; 10 import org.jbpm.graph.node.*; 11 import org.jbpm.jpdl.*; 12 13 public class JpdlXmlWriter { 14 15 private static final String JPDL_NAMESPACE = "http://jbpm.org/3/jpdl"; 16 private static final Namespace jbpmNamespace = new Namespace(null, JPDL_NAMESPACE); 17 18 private Writer writer = null; 19 private List problems = new ArrayList(); 20 private boolean useNamespace = false; 21 22 public JpdlXmlWriter( Writer writer ) { 23 if (writer==null) throw new NullPointerException ("writer is null"); 24 this.writer = writer; 25 } 26 27 public void addProblem(String msg) { 28 problems.add(msg); 29 } 30 public static String toString(ProcessDefinition processDefinition) { 31 StringWriter stringWriter = new StringWriter(); 32 JpdlXmlWriter jpdlWriter = new JpdlXmlWriter(stringWriter); 33 jpdlWriter.write(processDefinition); 34 return stringWriter.toString(); 35 } 36 37 public void setUseNamespace(boolean useNamespace) 38 { 39 this.useNamespace = useNamespace; 40 } 41 42 44 public void write(ProcessDefinition processDefinition) { 45 problems = new ArrayList(); 46 if (processDefinition==null) throw new NullPointerException ("processDefinition is null"); 47 try { 48 Document document = createDomTree(processDefinition); 53 54 OutputFormat outputFormat = new OutputFormat( " ", true ); 56 XMLWriter xmlWriter = new XMLWriter( writer, outputFormat ); 58 xmlWriter.write( document ); 59 xmlWriter.flush(); 60 writer.flush(); 61 } catch (IOException e) { 62 e.printStackTrace(); 63 addProblem("couldn't write process definition xml: "+e.getMessage()); 64 } 65 66 if (problems.size()>0) { 67 throw new JpdlException(problems); 68 } 69 } 70 71 private Document createDomTree(ProcessDefinition processDefinition) { 72 Document document = DocumentHelper.createDocument(); 73 Element root = null; 74 75 if (useNamespace) 76 root = document.addElement( "process-definition", jbpmNamespace.getURI() ); 77 else 78 root = document.addElement( "process-definition" 79 ); 80 addAttribute( root, "name", processDefinition.getName() ); 81 82 if ( processDefinition.getStartState()!=null ) { 84 writeComment(root, "START-STATE"); 85 writeStartNode( root, processDefinition.getStartState() ); 86 } 87 if ( ( processDefinition.getNodes()!=null ) 89 && ( processDefinition.getNodes().size() > 0 ) ) { 90 writeComment(root, "NODES"); 91 writeNodes( root, processDefinition.getNodes() ); 92 } 93 if ( processDefinition.hasEvents() ) { 95 writeComment(root, "PROCESS-EVENTS"); 96 writeEvents( root, processDefinition ); 97 } 98 if( processDefinition.hasActions() ) { 99 writeComment(root, "ACTIONS"); 100 List namedProcessActions = getNamedProcessActions(processDefinition.getActions()); 101 writeActions(root, namedProcessActions); 102 } 103 104 root.addText( System.getProperty("line.separator") ); 105 106 return document; 107 } 108 109 private List getNamedProcessActions(Map actions) { 110 List namedProcessActions = new ArrayList(); 111 Iterator iter = actions.values().iterator(); 112 while (iter.hasNext()) { 113 Action action = (Action) iter.next(); 114 if ( (action.getEvent()==null) 115 && (action.getName()!=null) ) { 116 namedProcessActions.add(action); 117 } 118 } 119 return namedProcessActions; 120 } 121 122 private void writeStartNode(Element element, StartState startState ) { 123 if (startState!=null) { 124 writeNode( addElement( element, getTypeName(startState) ), startState ); 125 } 126 } 127 128 private void writeNodes(Element parentElement, Collection nodes) { 129 Iterator iter = nodes.iterator(); 130 while (iter.hasNext()) { 131 org.jbpm.graph.def.Node node = (org.jbpm.graph.def.Node) iter.next(); 132 if ( ! (node instanceof StartState) ) { 133 Element nodeElement = addElement( parentElement, ProcessFactory.getTypeName(node) ); 134 node.write(nodeElement); 135 writeNode( nodeElement, node ); 136 } 137 } 138 } 139 140 private void writeNode(Element element, org.jbpm.graph.def.Node node ) { 141 addAttribute( element, "name", node.getName() ); 142 writeTransitions(element, node); 143 writeEvents(element, node); 144 } 145 146 private void writeTransitions(Element element, org.jbpm.graph.def.Node node) { 147 if ( node.getLeavingTransitionsMap()!=null ) { 148 Iterator iter = node.getLeavingTransitionsList().iterator(); 149 while (iter.hasNext()) { 150 Transition transition = (Transition) iter.next(); 151 writeTransition( element.addElement("transition"), transition ); 152 } 153 } 154 } 155 156 private void writeTransition(Element transitionElement, Transition transition) { 157 if (transition.getTo()!=null) { 158 transitionElement.addAttribute( "to", transition.getTo().getName() ); 159 } 160 if ( transition.getName()!=null ) { 161 transitionElement.addAttribute( "name", transition.getName() ); 162 } 163 Event transitionEvent = transition.getEvent(Event.EVENTTYPE_TRANSITION); 164 if ( (transitionEvent!=null) 165 && (transitionEvent.hasActions()) ){ 166 writeActions(transitionElement, transitionEvent.getActions()); 167 } 168 } 169 170 private void writeEvents(Element element, GraphElement graphElement) { 171 if (graphElement.hasEvents()) { 172 Iterator iter = graphElement.getEvents().values().iterator(); 173 while (iter.hasNext()) { 174 Event event = (Event) iter.next(); 175 writeEvent( element.addElement("event"), event ); 176 } 177 } 178 } 179 180 private void writeEvent(Element eventElement, Event event) { 181 eventElement.addAttribute("type", event.getEventType()); 182 if (event.hasActions()) { 183 Iterator actionIter = event.getActions().iterator(); 184 while (actionIter.hasNext()) { 185 Action action = (Action) actionIter.next(); 186 writeAction( eventElement, action ); 187 } 188 } 189 } 190 191 private void writeActions(Element parentElement, Collection actions) { 192 Iterator actionIter = actions.iterator(); 193 while (actionIter.hasNext()) { 194 Action action = (Action) actionIter.next(); 195 writeAction( parentElement, action ); 196 } 197 } 198 199 private void writeAction(Element parentElement, Action action ) { 200 String actionName = ActionTypes.getActionName(action.getClass()); 201 Element actionElement = parentElement.addElement(actionName); 202 203 if (action.getName()!=null) { 204 actionElement.addAttribute("name", action.getName()); 205 } 206 207 if (!action.acceptsPropagatedEvents()) { 208 actionElement.addAttribute("accept-propagated-events", "false"); 209 } 210 211 action.write(actionElement); 212 } 213 214 private void writeComment(Element element, String comment ) { 215 element.addText( System.getProperty("line.separator") ); 216 element.addComment( " " + comment + " " ); 217 } 218 219 private Element addElement( Element element, String elementName ) { 220 Element newElement = element.addElement( elementName ); 221 return newElement; 222 } 223 224 private void addAttribute( Element e, String attributeName, String value ) { 225 if ( value != null ) { 226 e.addAttribute( attributeName, value ); 227 } 228 } 229 230 private String getTypeName( Object o ) { 231 return ProcessFactory.getTypeName( (org.jbpm.graph.def.Node) o ); 232 } 233 234 } 236 | Popular Tags |