1
2
3
4
5
6
7
8 package net.kwfgrid.gworkflowdl.structure;
9
10 import org.jdom.Attribute;
11 import org.jdom.Element;
12
13 import java.util.Iterator;
14 import java.util.List;
15
16 /***
17 * Transformation class for Java intern Place representation and JDom Place elements.
18 * Example Place XML:
19 * <pre>
20 * <place ID="parameter" capacity="1">
21 * <description>parameter place</description>
22 * <property/> -> refer to PropertiesJdom
23 * <tokenClass owl="owlurl" type="file"/>
24 * <token/> -> refer to TokenJdom
25 * </place>
26 * </pre>
27 */
28 public final class PlaceJdom {
29 private PlaceJdom() {
30 }
31
32 /***
33 * Place to Jdom element.
34 *
35 * @param place to be transformed to JDom element
36 * @return Jdom (place) element
37 */
38 public static Element java2element(final Place place) {
39 final Element pe = new Element("place", JdomString.wfSpace);
40 pe.setAttribute("ID", place.getID());
41
42
43 final Integer c = place.getCapacity();
44 if (c != Place.DEFAULT_CAPACITY) {
45 pe.setAttribute("capacity", c.toString());
46 }
47
48
49 final String s = place.getDescription();
50 if (s != Place.DEFAULT_DESCRIPTION) {
51
52 final Element de = new Element("description", JdomString.wfSpace);
53 de.setText(s);
54 pe.addContent(de);
55 }
56
57
58 final Element[] els = PropertiesJdom.java2elements(place.getProperties());
59 for (Element el : els) {
60 pe.addContent(el);
61 }
62
63
64 final String[] owls = place.getOwls();
65 final String type = place.getTokenType();
66
67 if (owls.length > 0 || type != Place.DEFAULT_TOKENCLASS_TYPE) {
68 final Element tce = new Element("tokenClass", JdomString.wfSpace);
69
70 if (owls.length > 0) {
71 OwlsJdom.java2element(place, tce);
72 }
73
74 if (type != Place.DEFAULT_TOKENCLASS_TYPE) {
75 tce.setAttribute("type", type);
76 }
77 pe.addContent(tce);
78 }
79
80
81 final Token[] tokens = place.getTokens();
82 for (Token token : tokens) {
83 pe.addContent(TokenJdom.java2element(token));
84 }
85
86 return pe;
87 }
88
89 /***
90 * Jdom place element to Place.
91 *
92 * @param placeElement JDom Place representing element
93 * @return intern Place representation
94 * @throws CapacityException
95 */
96 public static Place element2java(final Element placeElement)
97 throws WorkflowFormatException, CapacityException {
98 final Place place = Factory.newPlace();
99
100
101 final Attribute ida = placeElement.getAttribute("ID");
102 if (ida==null) throw new WorkflowFormatException("Mandatory attribute \"ID\" of element <place> is missing!");
103 place.setID(ida.getValue());
104
105
106 final Attribute ca = placeElement.getAttribute("capacity");
107 if (ca != null) {
108 place.setCapacity(Integer.valueOf(ca.getValue()));
109 } else {
110 place.setCapacity(Place.DEFAULT_CAPACITY);
111 }
112
113
114
115 final Element de = placeElement.getChild("description", JdomString.wfSpace);
116 if (de != null) {
117 place.setDescription(de.getText());
118 }
119
120
121 final List els = placeElement.getChildren("property", JdomString.wfSpace);
122
123 GenericProperties props = PropertiesJdom.elements2java(els);
124 place.setProperties(props);
125
126
127
128 final Element tce = placeElement.getChild("tokenClass", JdomString.wfSpace);
129
130 if (tce != null) {
131 OwlsJdom.element2java(tce, place);
132
133
134
135
136
137
138 final Attribute tcaType = tce.getAttribute("type");
139 if (tcaType != null) {
140 place.setTokenType(tcaType.getValue());
141 }
142 }
143
144
145 final List list = placeElement.getChildren("token", JdomString.wfSpace);
146
147 for (Object aList : list) {
148 final Token t = TokenJdom.element2java((Element) aList);
149 place.addToken(t);
150 }
151
152 return place;
153 }
154 }