View Javadoc

1   /*
2   * Copyright (c) 2005, The K-Wf Grid Consortium
3   * Fraunhofer Institute for Computer Architecture and Software Technology
4   * See http://www.kwfgrid.eu and http://www.first.fraunhofer.de for more details.
5   */
6   package net.kwfgrid.gworkflowdl.protocol.xml;
7   
8   import net.kwfgrid.gworkflowdl.structure.*;
9   import org.apache.log4j.Logger;
10  import org.jdom.*;
11  import org.xmlpull.v1.XmlSerializer;
12  
13  import java.io.IOException;
14  import java.util.Iterator;
15  
16  /***
17   * Serializer for all GWorkflowDL subelements based on XPP3 XmlSeralizer.<br>
18   * See <a href="http://www.xmlpull.org">http://www.xmlpull.org</a> for information about XmlPull API.<br>
19   * See <a href="http://www.xmlpull.org">http://www.extreme.indiana.edu/xgws/xsoap/xpp/mxp1/index.html</a> for information about MXP1 implementation.<br>
20   *
21   * @author Tilman Linden and Andreas Hoheisel
22   */
23  public class GWDLMarshaller implements GWDLNamespace, OperationClassNamespace {
24  
25      static Logger logger = Logger.getLogger(GWDLMarshaller.class);
26  
27      /***
28       * This class is never instantiated.
29       */
30      private GWDLMarshaller() {
31          //
32      }
33  
34      public static void owlsgwdl(XmlSerializer s, Owls owls) throws IOException, IllegalStateException, IllegalArgumentException {
35          owlsgwdl(s, owls.getOwls());
36      }
37  
38      public static void owlsgwdl(XmlSerializer s, String[] owls) throws IOException, IllegalStateException, IllegalArgumentException {
39          for (int i = 0; i < owls.length; i++) {
40              owlgwdl(s, owls[i]);
41          }
42      }
43  
44      public static void owlgwdl(XmlSerializer s, String owl) throws IOException, IllegalStateException, IllegalArgumentException {
45          if (!isDefaultOwl(owl)) {
46              setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
47              s.startTag(GWDL_NS, "owl");
48              s.text(owl);
49              s.endTag(GWDL_NS, "owl");
50          }
51      }
52  
53      public static void type(XmlSerializer s, String type) throws IOException, IllegalStateException, IllegalArgumentException {
54          if (!isDefaultType(type)) {
55              s.attribute(GWDL_ATTRIBUTE_NS, "type", type);
56          }
57      }
58  
59      public static void description(XmlSerializer s, String description) throws IOException, IllegalStateException, IllegalArgumentException {
60          if (!isDefaultDescription(description)) {
61              setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
62              s.startTag(GWDL_NS, "description");
63              s.text(description);
64              s.endTag(GWDL_NS, "description");
65          }
66      }
67  
68      public static void transition(XmlSerializer s, Transition t) throws IOException, IllegalStateException, IllegalArgumentException {
69          setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
70          s.startTag(GWDL_NS, "transition");
71          ID(s, t.getID());
72          description(s, t.getDescription());
73          properties(s, t.getProperties());
74  
75          Edge[] e = t.getReadEdges();
76          if (e != null) {
77              for (int i = 0; i < e.length; i++) {
78                  readPlace(s, e[i]);
79              }
80          }
81          e = t.getInEdges();
82          if (e != null) {
83              for (int i = 0; i < e.length; i++) {
84                  inputPlace(s, e[i]);
85              }
86          }
87          e = t.getWriteEdges();
88          if (e != null) {
89              for (int i = 0; i < e.length; i++) {
90                  writePlace(s, e[i]);
91              }
92          }
93          e = t.getOutEdges();
94          if (e != null) {
95              for (int i = 0; i < e.length; i++) {
96                  outputPlace(s, e[i]);
97              }
98          }
99          String[] c = t.getConditions();
100         if (c != null) {
101             for (int i = 0; i < c.length; i++) {
102                 condition(s, c[i]);
103             }
104         }
105         operation(s, t.getOperation());
106         s.endTag(GWDL_NS, "transition");
107     }
108 
109     public static void ID(XmlSerializer s, String ID) throws IOException, IllegalStateException, IllegalArgumentException {
110         if (ID == null) ID = "null";
111         s.attribute(GWDL_ATTRIBUTE_NS, "ID", ID);
112     }
113 
114     public static void readPlace(XmlSerializer s, Edge e) throws IOException, IllegalStateException, IllegalArgumentException {
115         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
116         s.startTag(GWDL_NS, "readPlace");
117         placeID(s, e.getPlaceID());
118         edgeExpression(s, e.getExpression());
119         s.endTag(GWDL_NS, "readPlace");
120     }
121 
122     public static void inputPlace(XmlSerializer s, Edge e) throws IOException, IllegalStateException, IllegalArgumentException {
123         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
124         s.startTag(GWDL_NS, "inputPlace");
125         placeID(s, e.getPlaceID());
126         edgeExpression(s, e.getExpression());
127         s.endTag(GWDL_NS, "inputPlace");
128     }
129 
130     public static void writePlace(XmlSerializer s, Edge e) throws IOException, IllegalStateException, IllegalArgumentException {
131         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
132         s.startTag(GWDL_NS, "writePlace");
133         placeID(s, e.getPlaceID());
134         edgeExpression(s, e.getExpression());
135         s.endTag(GWDL_NS, "writePlace");
136     }
137 
138     public static void outputPlace(XmlSerializer s, Edge e) throws IOException, IllegalStateException, IllegalArgumentException {
139         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
140         s.startTag(GWDL_NS, "outputPlace");
141         placeID(s, e.getPlaceID());
142         edgeExpression(s, e.getExpression());
143         s.endTag(GWDL_NS, "outputPlace");
144     }
145 
146     public static void placeID(XmlSerializer s, String placeID) throws IOException, IllegalStateException, IllegalArgumentException {
147         s.attribute(GWDL_ATTRIBUTE_NS, "placeID", placeID);
148     }
149 
150     public static void edgeExpression(XmlSerializer s, String expression) throws IOException, IllegalStateException, IllegalArgumentException {
151         if (!isDefaultExpression(expression)) {
152             s.attribute(GWDL_ATTRIBUTE_NS, "edgeExpression", expression);
153         }
154     }
155 
156     public static void condition(XmlSerializer s, String condition) throws IOException, IllegalStateException, IllegalArgumentException {
157         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
158         s.startTag(GWDL_NS, "condition");
159         s.text(condition);
160         s.endTag(GWDL_NS, "condition");
161     }
162 
163     ///ToDo: support arbitrary Class Operations
164     public static void operation(XmlSerializer s, Operation op) throws IOException, IllegalStateException, IllegalArgumentException {
165         if (!isDefaultOperation(op)) {
166             setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
167             s.startTag(GWDL_NS, "operation");
168             if (op.get() instanceof OperationClass) {
169                 operationClass(s, (OperationClass) op.get());
170             } else {
171                 logger.warn("Arbitrary operations not yet supported.");
172             }
173             s.endTag(GWDL_NS, "operation");
174         }
175     }
176 
177     public static void operationClass(XmlSerializer s, OperationClass oc) throws IOException, IllegalStateException, IllegalArgumentException {
178         if (!isDefaultOperationClass(oc)) {
179             setPrefix(s, OPERATIONCLASS_NS_PREFIX, OPERATIONCLASS_NS);
180             s.startTag(OPERATIONCLASS_NS, "operationClass");
181             name(s, oc.getName());
182             owlsoc(s, oc.getOwls());
183             OperationCandidate[] ocs = oc.getOperationCandidates();
184             if (ocs != null) {
185                 for (int i = 0; i < ocs.length; i++) {
186                     operationCandidate(s, ocs[i]);
187                 }
188             }
189             s.endTag(OPERATIONCLASS_NS, "operationClass");
190         }
191     }
192 
193     public static void name(XmlSerializer s, String name) throws IOException, IllegalStateException, IllegalArgumentException {
194         if (!isDefaultName(name)) {
195             s.attribute(OPERATIONCLASS_ATTRIBUTE_NS, "name", name);
196         }
197     }
198 
199     public static void place(XmlSerializer s, Place p) throws IOException, IllegalStateException, IllegalArgumentException {
200         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
201         s.startTag(GWDL_NS, "place");
202         ID(s, p.getID());
203         capacity(s, p.getCapacity());
204         description(s, p.getDescription());
205         properties(s, p.getProperties());
206 
207         tokenClass(s, p, p.getTokenType());
208         Token[] toks = p.getTokens();
209         if (toks != null) {
210             for (int i = 0; i < toks.length; i++) {
211                 token(s, toks[i]);
212             }
213         }
214         s.endTag(GWDL_NS, "place");
215     }
216 
217     public static void capacity(XmlSerializer s, int capacity) throws IOException, IllegalStateException, IllegalArgumentException {
218         if (!isDefaultCapacity(capacity)) {
219             s.attribute(GWDL_ATTRIBUTE_NS, "capacity", "" + capacity);
220         }
221     }
222 
223     public static void tokenClass(XmlSerializer s, Owls owls, String type) throws IOException, IllegalStateException, IllegalArgumentException {
224         if (!isDefaultTokenClass(owls, type)) {
225             setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
226             s.startTag(GWDL_NS, "tokenClass");
227             type(s, type);
228             owlsgwdl(s, owls);
229             s.endTag(GWDL_NS, "tokenClass");
230         }
231     }
232 
233     public static void tokenClass(XmlSerializer s, String[] owls, String type) throws IOException, IllegalStateException, IllegalArgumentException {
234         if (owls.length > 0 || !isDefaultType(type)) {
235             setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
236             s.startTag(GWDL_NS, "tokenClass");
237             type(s, type);
238             owlsgwdl(s, owls);
239             s.endTag(GWDL_NS, "tokenClass");
240         }
241     }
242 
243     public static void token(XmlSerializer s, Token token) throws IOException, IllegalStateException, IllegalArgumentException {
244         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
245         s.startTag(GWDL_NS, "token");
246         properties(s, token.getProperties());
247         if (token.getControl() != null) {
248             s.startTag(GWDL_NS, "control");
249             s.text(token.getControl().toString());
250             s.endTag(GWDL_NS, "control");
251         } else if (token.getData() != null) {
252             data(s, token.getData());
253         }
254         s.endTag(GWDL_NS, "token");
255     }
256 
257     public static void data(XmlSerializer s, Data data) throws IOException, IllegalStateException, IllegalArgumentException {
258         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
259         Object content = data.get();
260         dataContent(s, content);
261     }
262 
263     public static void dataContent(XmlSerializer s, Object content) throws IOException, IllegalStateException, IllegalArgumentException {
264         if (content != null && content instanceof Element) jdomElement(s, (Element) content);
265     }
266 
267     /***
268      * Currently only serializes text content, CDATA, comments, attributes and child elements. TODO. FIXME.
269      */
270     public static void jdomElement(XmlSerializer s, Element elem) throws IOException, IllegalStateException, IllegalArgumentException {
271         Namespace ns = elem.getNamespace();
272         setPrefix(s, ns.getPrefix(), ns.getURI());
273         Iterator attributes = elem.getAttributes().iterator();
274         while (attributes.hasNext()) {
275             Attribute att = (Attribute) attributes.next();
276             Namespace attns = att.getNamespace();
277             if (!attns.equals(ns)) setPrefix(s, attns.getPrefix(), attns.getURI());
278         }
279         s.startTag(ns.getURI(), elem.getName());
280         attributes = elem.getAttributes().iterator();
281         while (attributes.hasNext()) {
282             Attribute att = (Attribute) attributes.next();
283             Namespace attns = att.getNamespace();
284             s.attribute(attns.getURI(), att.getName(), att.getValue());
285         }
286         Iterator children = elem.getContent().iterator();
287         while (children.hasNext()) {
288             Object child = children.next();
289             if (child instanceof Element)
290                 jdomElement(s, (Element) child);
291             else if (child instanceof CDATA)
292                 s.cdsect(((CDATA) child).getText());
293             else if (child instanceof Text)
294                 s.text(((Text) child).getText());
295             else if (child instanceof Comment)
296                 s.comment(((Comment) child).getText());
297         }
298         s.endTag(ns.getURI(), elem.getName());
299     }
300 
301     public static void property(XmlSerializer s, String key, String value) throws IOException, IllegalStateException, IllegalArgumentException {
302         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
303         s.startTag(GWDL_NS, "property");
304         s.attribute(GWDL_ATTRIBUTE_NS, "name", key);
305         s.text(value);
306         s.endTag(GWDL_NS, "property");
307     }
308 
309     public static void properties(XmlSerializer s, GenericProperties gp) throws IOException, IllegalStateException, IllegalArgumentException {
310         if (gp != null) {
311             Property[] props = gp.getProperties();
312             if (props != null) {
313                 for (int i = 0; i < props.length; i++) {
314                     property(s, props[i].getKey(), props[i].getValue());
315                 }
316             }
317         }
318     }
319 
320     public static void workflowID(XmlSerializer s, String id) throws IOException, IllegalStateException, IllegalArgumentException {
321         s.attribute(GWDL_ATTRIBUTE_NS, "ID", id);
322     }
323 
324     public static void workflow(XmlSerializer s, Workflow wf) throws IOException, IllegalStateException, IllegalArgumentException {
325         setPrefix(s, GWDL_NS_PREFIX, GWDL_NS);
326         s.startTag(GWDL_NS, "workflow");
327 
328         workflowContent(s, wf);
329 
330         s.endTag(GWDL_NS, "workflow");
331     }
332 
333     public static final void workflowContent(XmlSerializer s, Workflow wf) throws IOException, IllegalStateException, IllegalArgumentException {
334         workflowID(s, wf.getID());
335 
336         owlsgwdl(s, wf);
337         description(s, wf.getDescription());
338         properties(s, wf.getProperties());
339 
340         Place[] places = wf.getPlaces();
341         if (places != null) {
342             for (int i = 0; i < places.length; i++) {
343                 place(s, places[i]);
344             }
345         }
346 
347         Transition[] transitions = wf.getTransitions();
348         if (transitions != null) {
349             for (int i = 0; i < transitions.length; i++) {
350                 transition(s, transitions[i]);
351             }
352         }
353     }
354 
355     /// ----------------------------------------------------------------------------------------------------
356 
357     public static boolean isDefaultName(String name) {
358         return name == OperationClass.DEFAULT_OPERATIONCLASSNAME;
359     }
360     public static boolean isDefaultDescription(String desc) {
361         return desc == null;
362     }
363 
364     public static boolean isDefaultOwl(String owl) {
365         return owl == null;
366     }
367 
368     public static boolean isDefaultOwls(Owls owls) {
369         return owls == null || owls.owlsCount() == 0;
370     }
371 
372     public static boolean isDefaultType(String type) {
373         return
374                 (type == null && Place.DEFAULT_TOKENCLASS_TYPE == null) ||
375                         (type != null && type.equals(Place.DEFAULT_TOKENCLASS_TYPE));
376     }
377 
378     public static boolean isDefaultCapacity(int cap) {
379         return cap == Place.DEFAULT_CAPACITY;
380     }
381 
382     public static boolean isDefaultTokenClass(Owls owls, String type) {
383         return isDefaultOwls(owls) && isDefaultType(type);
384     }
385 
386     public static boolean isDefaultOperationClass(OperationClass op) {
387         return op == Operation.DEFAULT_OBJECT;
388     }
389 
390     public static boolean isDefaultOperation(Operation op) {
391         return
392                 (op == null && Transition.DEFAULT_OPERATION == null) ||
393                         (op != null && op.equals(Transition.DEFAULT_OPERATION));
394     }
395 
396     public static boolean isDefaultExpression(String exp) {
397         return
398                 (exp == null && Edge.DEFAULT_EXPRESSION == null) ||
399                         (exp != null && exp.equals(Edge.DEFAULT_EXPRESSION));
400     }
401 
402     protected static void setPrefix(XmlSerializer s, String prefix, String uri) throws IOException {
403         if (s.getPrefix(uri, false) == null) {
404             try {
405                 s.setPrefix(prefix, uri);
406             } catch (IllegalStateException e) {
407                 logger.warn("XML pull parser setPrefix: "+e);
408             }
409         }
410     }
411 
412     ///
413     /// OperationCandidate
414     /// ----------------------------------------------------------------------------------------------------
415 
416     public static void operationType(XmlSerializer s, String type) throws IOException, IllegalStateException, IllegalArgumentException {
417         if (!isDefaultOperationType(type)) {
418             s.attribute(OPERATIONCLASS_ATTRIBUTE_NS, "type", type);
419         }
420     }
421     
422     public static void operationName(XmlSerializer s, String name) throws IOException, IllegalStateException, IllegalArgumentException {
423         if (!isDefaultOperationName(name)) {
424             s.attribute(OPERATIONCLASS_ATTRIBUTE_NS, "operationName", name);
425         }
426     }
427 
428     public static void resourceName(XmlSerializer s, String name) throws IOException, IllegalStateException, IllegalArgumentException {
429         if (!isDefaultResourceName(name)) {
430             s.attribute(OPERATIONCLASS_ATTRIBUTE_NS, "resourceName", name);
431         }
432     }
433 
434     public static void quality(XmlSerializer s, float q) throws IOException, IllegalStateException, IllegalArgumentException {
435         if (!isDefaultQuality(q)) {
436             s.attribute(OPERATIONCLASS_ATTRIBUTE_NS, "quality", "" + q);
437         }
438     }
439 
440     public static void selected(XmlSerializer s, boolean selected) throws IOException, IllegalStateException, IllegalArgumentException {
441         if (!isDefaultSelected(selected)) {
442             s.attribute(OPERATIONCLASS_ATTRIBUTE_NS, "selected", "" + selected);
443         }
444     }
445 
446     public static boolean isDefaultOperationType(String type) {
447         return type == OperationCandidate.DEFAULT_TYPE;
448     }
449 
450     public static boolean isDefaultOperationName(String name) {
451         return name == OperationCandidate.DEFAULT_OPERATIONNAME;
452     }
453 
454     public static boolean isDefaultResourceName(String name) {
455         return name == OperationCandidate.DEFAULT_RESOURCENAME;
456     }
457 
458     public static boolean isDefaultQuality(float quality) {
459         return quality == OperationCandidate.DEFAULT_QUALITY;
460     }
461 
462     public static boolean isDefaultSelected(boolean selected) {
463         return selected == OperationCandidate.DEFAULT_SELECTED;
464     }
465 
466     ///
467     /// StdOperationCandidate
468     /// ----------------------------------------------------------------------------------------------------
469 
470     public static void operationCandidate(XmlSerializer s, OperationCandidate op) throws IOException, IllegalStateException, IllegalArgumentException {
471         setPrefix(s, OPERATIONCLASS_NS_PREFIX, OPERATIONCLASS_NS);
472         s.startTag(OPERATIONCLASS_NS, "operationCandidate");
473         operationType(s, op.getType());
474         operationName(s, op.getOperationName());
475         resourceName(s, op.getResourceName());
476         quality(s,op.getQuality());
477         selected(s,op.isSelected());
478         owlsoc(s,op.getOwls());
479         s.endTag(OPERATIONCLASS_NS, "operationCandidate");
480     }
481 
482     ///
483     /// OWL oc namespace
484     /// ----------------------------------------------------------------------------------------------------
485 
486     public static void owlsoc(XmlSerializer s, Owls owls) throws IOException, IllegalStateException, IllegalArgumentException {
487         owlsoc(s, owls.getOwls());
488     }
489 
490     public static void owlsoc(XmlSerializer s, String[] owls) throws IOException, IllegalStateException, IllegalArgumentException {
491         for (int i = 0; i < owls.length; i++) {
492             owloc(s, owls[i]);
493         }
494     }
495 
496     public static void owloc(XmlSerializer s, String owl) throws IOException, IllegalStateException, IllegalArgumentException {
497         if (!isDefaultOwl(owl)) {
498             setPrefix(s, OPERATIONCLASS_NS_PREFIX, OPERATIONCLASS_NS);
499             s.startTag(OPERATIONCLASS_NS, "owl");
500             s.text(owl);
501             s.endTag(OPERATIONCLASS_NS, "owl");
502         }
503     }
504 
505 }