1
2
3
4
5
6
7
8 package net.kwfgrid.gworkflowdl.structure;
9
10
11
12
13
14 import org.jdom.Attribute;
15 import org.jdom.Element;
16
17 import java.util.Iterator;
18 import java.util.List;
19
20 /***
21 * Transformation Java intern Workflow representation and JDom Element.
22 * Example Workflow XML:
23 * <pre>
24 * <workflow xmlns="http://www.gridworkflow.org/gworkflowdl"
25 * xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
26 * xmlns:xsd="http://www.w3.org/2001/XMLSchema"
27 * xsi:schemaLocation="http://www.gridworkflow.org/gworkflowdl http://www.gridworkflow.org/kwfgrid/src/xsd/gworkflowdl_2_1.xsd" ID="No_ID">
28 * <owl/> -> refer to OwlsJdom
29 * <description>Test workflow</description>
30 * <property/> -> refer to PropertiesJdom
31 * <place/> -> refer to PlaceJdom
32 * <transition/> -> refer to TransitionJdom
33 * </workflow>
34 * </pre>
35 */
36 public final class WorkflowJdom {
37
38 private WorkflowJdom() {
39 }
40
41 /***
42 * Workflow to JDom Element.
43 *
44 * @param wf Workflow
45 * @return JDom Workflow Element
46 */
47 public static Element java2element(Workflow wf) {
48 final Element wfe = new Element("workflow", JdomString.wfSpace);
49 wfe.addNamespaceDeclaration(JdomString.ocSpace);
50 wfe.addNamespaceDeclaration(JdomString.NAMESPACE_XSI);
51 wfe.addNamespaceDeclaration(JdomString.NAMESPACE_XSD);
52 wfe.setAttribute("schemaLocation", JdomString.SCHEMA_LOCATION, JdomString.NAMESPACE_XSI);
53 OwlsJdom.java2element(wf, wfe);
54
55
56 wfe.setAttribute("ID", wf.getID());
57
58
59 if (wf.getDescription() != null) {
60 final Element de = new Element("description", JdomString.wfSpace);
61 de.setText(wf.getDescription());
62 wfe.addContent(de);
63 }
64
65
66 final Element[] els = PropertiesJdom.java2elements(wf.getProperties());
67 for (Element el : els) {
68 wfe.addContent(el);
69 }
70
71
72 final Place[] pls = wf.getPlaces();
73 for (Place pl : pls) {
74 wfe.addContent(PlaceJdom.java2element(pl));
75 }
76
77
78 final Transition[] trs = wf.getTransitions();
79 for (Transition tr : trs) {
80 wfe.addContent(TransitionJdom.java2element(tr));
81 }
82
83 return wfe;
84 }
85
86 /***
87 * JDom Workflow Element to intern Java Workflow representation.
88 * Places must be added before Transitions
89 *
90 * @param wfe Workflow Element
91 * @return Workflow
92 * @throws CapacityException
93 */
94 public static Workflow element2java(Element wfe) throws CapacityException, WorkflowFormatException {
95 Attribute ida = wfe.getAttribute("ID");
96 if (ida==null) throw new WorkflowFormatException("Mandatory attribute \"ID\" of element <workflow> is missing!");
97 final Workflow wf = Factory.newWorkflow(ida.getValue());
98
99 element2java(wfe, wf);
100
101 return wf;
102 }
103
104 /***
105 * Copy GWorfklowDL representatin in JDOM Element into the specified Workflow.
106 * All previous contents of <code>target</code> will be overwritten.
107 *
108 * @param wfe The JDOM element holding the workflow representation.
109 * @param target The java object to copy the workflow information to.
110 * @throws CapacityException
111 */
112 public static void element2java(Element wfe, Workflow target) throws CapacityException, WorkflowFormatException {
113
114
115
116 target.setTransitions(new Transition[0]);
117 target.setPlaces(new Place[0]);
118 target.setDescription(Workflow.DEFAULT_DESCRIPTION);
119 target.setOwls(new String[0]);
120 target.getProperties().setProperties(new Property[0]);
121
122
123
124
125
126
127
128
129
130
131 OwlsJdom.element2java(wfe, target);
132
133
134 final Element de = wfe.getChild("description", JdomString.wfSpace);
135 if (de != null) {
136 target.setDescription(de.getText());
137 }
138
139 final List els = wfe.getChildren("property", JdomString.wfSpace);
140 final GenericProperties props = PropertiesJdom.elements2java(els);
141 target.setProperties(props);
142
143
144
145 List list = wfe.getChildren("place", JdomString.wfSpace);
146 Iterator it = list.iterator();
147 while (it.hasNext()) {
148 final Element pe = (Element) it.next();
149 target.addPlace(PlaceJdom.element2java(pe));
150 }
151
152
153 list = wfe.getChildren("transition", JdomString.wfSpace);
154 it = list.iterator();
155 while (it.hasNext()) {
156 final Element te = (Element) it.next();
157 target.addTransition(TransitionJdom.element2java(target, te));
158 }
159 }
160
161 }
162
163