1
2
3
4
5
6
7
8 package net.kwfgrid.gworkflowdl.structure;
9
10
11 import org.apache.log4j.Logger;
12 import org.jdom.Element;
13
14 import java.util.List;
15
16
17 /***
18 * transformation of Java intern Operation representation and JDom Element.
19 */
20 public final class OperationJdom {
21
22 private static final Logger logger = Logger.getLogger(OperationJdom.class);
23
24 private OperationJdom() {
25 }
26
27 /***
28 * internal operation representation -> JDom operarion element.
29 *
30 * @param op operation
31 * @return operation JDom element
32 */
33 public static Element java2element(final Operation op) {
34
35 final Element oe = new Element("operation", JdomString.wfSpace);
36
37
38
39 Object object = op.get();
40 if (object instanceof OperationClass) {
41 oe.addContent(OperationClassJdom.java2element((OperationClass) object));
42 } else if (object instanceof Element) {
43 Element oel = (Element) object;
44 oel.detach();
45 oe.addContent(oel);
46 } else {
47 logger.error("Wrong type of object: "+object);
48 }
49
50 return oe;
51 }
52
53 /***
54 * JDom operation element -> internal operation representation.
55 *
56 * @param ope JDom operation element
57 * @return internal operation
58 */
59 public static Operation element2java(final Element ope) throws WorkflowFormatException {
60 final Operation op = Factory.newOperation();
61 Element el = ope.getChild("operationClass", JdomString.ocSpace);
62
63
64 if (el != null) {
65 op.set(OperationClassJdom.element2java(el));
66 }
67
68
69 else {
70 List elist = ope.getChildren();
71 if (elist.size() == 0) {
72
73 }
74 else if (elist.size() == 1) {
75 Element oel = (Element) elist.get(0);
76 op.set(oel);
77 } else {
78 throw new WorkflowFormatException("Element <operation> can only hold at least ONE child element but holds "+elist.size()+ "child elements.");
79 }
80 }
81 return op;
82
83 }
84
85 }