001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2005, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jbpm.jpdl.xml;
023:
024: import java.io.*;
025: import java.util.*;
026:
027: import org.dom4j.*;
028: import org.dom4j.io.*;
029: import org.jbpm.JbpmException;
030: import org.jbpm.graph.action.ActionTypes;
031: import org.jbpm.graph.def.*;
032: import org.jbpm.graph.node.*;
033: import org.jbpm.jpdl.*;
034:
035: /**
036: * @deprecated xml generation was never finished and will be removed in the future.
037: */
038: public class JpdlXmlWriter {
039:
040: static final String JPDL_NAMESPACE = "http://jbpm.org/3/jpdl";
041: static final Namespace jbpmNamespace = new Namespace(null,
042: JPDL_NAMESPACE);
043:
044: Writer writer = null;
045: List problems = new ArrayList();
046: boolean useNamespace = false;
047:
048: public JpdlXmlWriter(Writer writer) {
049: if (writer == null)
050: throw new JbpmException("writer is null");
051: this .writer = writer;
052: }
053:
054: public void addProblem(String msg) {
055: problems.add(msg);
056: }
057:
058: public static String toString(ProcessDefinition processDefinition) {
059: StringWriter stringWriter = new StringWriter();
060: JpdlXmlWriter jpdlWriter = new JpdlXmlWriter(stringWriter);
061: jpdlWriter.write(processDefinition);
062: return stringWriter.toString();
063: }
064:
065: public void setUseNamespace(boolean useNamespace) {
066: this .useNamespace = useNamespace;
067: }
068:
069: //newElement.add( jbpmNamespace );
070:
071: public void write(ProcessDefinition processDefinition) {
072: problems = new ArrayList();
073: if (processDefinition == null)
074: throw new JbpmException("processDefinition is null");
075: try {
076: // collect the actions of the process definition
077: // we will remove each named event action and the remaining ones will be written
078: // on the process definition.
079: // create a dom4j dom-tree for the process definition
080: Document document = createDomTree(processDefinition);
081:
082: // write the dom-tree to the given writer
083: OutputFormat outputFormat = new OutputFormat(" ", true);
084: // OutputFormat outputFormat = OutputFormat.createPrettyPrint();
085: XMLWriter xmlWriter = new XMLWriter(writer, outputFormat);
086: xmlWriter.write(document);
087: xmlWriter.flush();
088: writer.flush();
089: } catch (IOException e) {
090: e.printStackTrace();
091: addProblem("couldn't write process definition xml: "
092: + e.getMessage());
093: }
094:
095: if (problems.size() > 0) {
096: throw new JpdlException(problems);
097: }
098: }
099:
100: private Document createDomTree(ProcessDefinition processDefinition) {
101: Document document = DocumentHelper.createDocument();
102: Element root = null;
103:
104: if (useNamespace)
105: root = document.addElement("process-definition",
106: jbpmNamespace.getURI());
107: else
108: root = document.addElement("process-definition");
109: addAttribute(root, "name", processDefinition.getName());
110:
111: // write the start-state
112: if (processDefinition.getStartState() != null) {
113: writeComment(root, "START-STATE");
114: writeStartNode(root, (StartState) processDefinition
115: .getStartState());
116: }
117: // write the nodeMap
118: if ((processDefinition.getNodes() != null)
119: && (processDefinition.getNodes().size() > 0)) {
120: writeComment(root, "NODES");
121: writeNodes(root, processDefinition.getNodes());
122: }
123: // write the process level actions
124: if (processDefinition.hasEvents()) {
125: writeComment(root, "PROCESS-EVENTS");
126: writeEvents(root, processDefinition);
127: }
128: if (processDefinition.hasActions()) {
129: writeComment(root, "ACTIONS");
130: List namedProcessActions = getNamedProcessActions(processDefinition
131: .getActions());
132: writeActions(root, namedProcessActions);
133: }
134:
135: root.addText(System.getProperty("line.separator"));
136:
137: return document;
138: }
139:
140: private List getNamedProcessActions(Map actions) {
141: List namedProcessActions = new ArrayList();
142: Iterator iter = actions.values().iterator();
143: while (iter.hasNext()) {
144: Action action = (Action) iter.next();
145: if ((action.getEvent() == null)
146: && (action.getName() != null)) {
147: namedProcessActions.add(action);
148: }
149: }
150: return namedProcessActions;
151: }
152:
153: private void writeStartNode(Element element, StartState startState) {
154: if (startState != null) {
155: writeNode(addElement(element, getTypeName(startState)),
156: startState);
157: }
158: }
159:
160: private void writeNodes(Element parentElement, Collection nodes) {
161: Iterator iter = nodes.iterator();
162: while (iter.hasNext()) {
163: org.jbpm.graph.def.Node node = (org.jbpm.graph.def.Node) iter
164: .next();
165: if (!(node instanceof StartState)) {
166: Element nodeElement = addElement(parentElement,
167: ProcessFactory.getTypeName(node));
168: node.write(nodeElement);
169: writeNode(nodeElement, node);
170: }
171: }
172: }
173:
174: private void writeNode(Element element, org.jbpm.graph.def.Node node) {
175: addAttribute(element, "name", node.getName());
176: writeTransitions(element, node);
177: writeEvents(element, node);
178: }
179:
180: private void writeTransitions(Element element,
181: org.jbpm.graph.def.Node node) {
182: if (node.getLeavingTransitionsMap() != null) {
183: Iterator iter = node.getLeavingTransitionsList().iterator();
184: while (iter.hasNext()) {
185: Transition transition = (Transition) iter.next();
186: writeTransition(element.addElement("transition"),
187: transition);
188: }
189: }
190: }
191:
192: private void writeTransition(Element transitionElement,
193: Transition transition) {
194: if (transition.getTo() != null) {
195: transitionElement.addAttribute("to", transition.getTo()
196: .getName());
197: }
198: if (transition.getName() != null) {
199: transitionElement
200: .addAttribute("name", transition.getName());
201: }
202: Event transitionEvent = transition
203: .getEvent(Event.EVENTTYPE_TRANSITION);
204: if ((transitionEvent != null) && (transitionEvent.hasActions())) {
205: writeActions(transitionElement, transitionEvent
206: .getActions());
207: }
208: }
209:
210: private void writeEvents(Element element, GraphElement graphElement) {
211: if (graphElement.hasEvents()) {
212: Iterator iter = graphElement.getEvents().values()
213: .iterator();
214: while (iter.hasNext()) {
215: Event event = (Event) iter.next();
216: writeEvent(element.addElement("event"), event);
217: }
218: }
219: }
220:
221: private void writeEvent(Element eventElement, Event event) {
222: eventElement.addAttribute("type", event.getEventType());
223: if (event.hasActions()) {
224: Iterator actionIter = event.getActions().iterator();
225: while (actionIter.hasNext()) {
226: Action action = (Action) actionIter.next();
227: writeAction(eventElement, action);
228: }
229: }
230: }
231:
232: private void writeActions(Element parentElement, Collection actions) {
233: Iterator actionIter = actions.iterator();
234: while (actionIter.hasNext()) {
235: Action action = (Action) actionIter.next();
236: writeAction(parentElement, action);
237: }
238: }
239:
240: private void writeAction(Element parentElement, Action action) {
241: String actionName = ActionTypes
242: .getActionName(action.getClass());
243: Element actionElement = parentElement.addElement(actionName);
244:
245: if (action.getName() != null) {
246: actionElement.addAttribute("name", action.getName());
247: }
248:
249: if (!action.acceptsPropagatedEvents()) {
250: actionElement.addAttribute("accept-propagated-events",
251: "false");
252: }
253:
254: action.write(actionElement);
255: }
256:
257: private void writeComment(Element element, String comment) {
258: element.addText(System.getProperty("line.separator"));
259: element.addComment(" " + comment + " ");
260: }
261:
262: private Element addElement(Element element, String elementName) {
263: Element newElement = element.addElement(elementName);
264: return newElement;
265: }
266:
267: private void addAttribute(Element e, String attributeName,
268: String value) {
269: if (value != null) {
270: e.addAttribute(attributeName, value);
271: }
272: }
273:
274: private String getTypeName(Object o) {
275: return ProcessFactory.getTypeName((org.jbpm.graph.def.Node) o);
276: }
277:
278: // private static final Log log = LogFactory.getLog(JpdlXmlWriter.class);
279: }
|