View Javadoc

1   /*
2    * Copyright 2010 Fraunhofer Gesellschaft, Munich, Germany,
3    * for its Fraunhofer Institute for Computer Architecture and Software
4    * Technology (FIRST), Berlin, Germany. All rights reserved.
5    * http://www.first.fraunhofer.de/
6    */
7   
8   package net.kwfgrid.gworkflowdl.structure;
9   
10  
11  import org.jdom.Element;
12  import org.jdom.Attribute;
13  
14  import java.util.List;
15  
16  /***
17   * Transformation class Java internal Token representation and JDom Element.
18   * Example Token XML:
19   * <pre>
20   * &lt;token&gt;
21   *    &lt;property name="resource.allocation.group.A"&gt;hardware:quadro.first.fhrg.fraunhofer.de&lt;/property&gt;
22   *    &lt;data/&gt;  -&gt; refer to DataJdom
23   * &lt;/token&gt;
24   * &lt;token&gt;
25   *    &lt;property name="resource.allocation.group.B"&gt;hardware:quadro.first.fhrg.fraunhofer.de&lt;/property&gt;
26   *    &lt;control&gt;true&lt;/control&gt;
27   * &lt;/token&gt;
28   * </pre>
29   */
30  public final class TokenJdom {
31      private TokenJdom() {
32      }
33  
34      /***
35       * Transformation Token to JDom element.
36       *
37       * @param token Token to be transformed
38       * @return JDom Token Element
39       */
40      public static Element java2element(final Token token) {
41          final Element te = new Element("token", JdomString.wfSpace);
42          // ID
43          te.setAttribute("ID", token.getID());
44          
45          // properties
46          final Element[] els = PropertiesJdom.java2elements(token.getProperties());
47          for (Element el : els) {
48              te.addContent(el);
49          }
50  
51          // control
52          if (token.getControl() != null) {
53              final Element ce = new Element("control", JdomString.wfSpace);
54              ce.setText(token.getControl().toString());
55              te.addContent(ce);
56          }
57  
58          // data
59          if (token.getData() != null) {
60              te.addContent(DataJdom.java2element(token.getData()));
61          }
62  
63          return te;
64      }
65  
66  
67      /***
68       * transformation JDom element to Token.
69       *
70       * @param tokenElement JDom Token Element
71       * @return Token
72       */
73      public static Token element2java(final Element tokenElement) {
74          Token token;
75          GenericProperties props = null;
76          String id = null;
77  
78          // ID
79          final Attribute ida = tokenElement.getAttribute("ID");
80          if (ida!=null) {
81              id = ida.getValue();
82          }
83  
84          // properties
85          final List els = tokenElement.getChildren("property", JdomString.wfSpace);
86          if (els != null && !els.isEmpty()) props = PropertiesJdom.elements2java(els);
87          if (props==null) props = Factory.newProperties();
88  
89          // control
90          final Element ce = tokenElement.getChild("control", JdomString.wfSpace);
91          if (ce != null) {
92              token = Factory.newToken(id,ce.getText().equals("true"),props);
93          }
94          else {
95              // data
96              final Element de = tokenElement.getChild("data", JdomString.wfSpace);
97              if (de != null) {
98                  token = Factory.newToken(id,DataJdom.element2java(de),props);
99              }
100             // null
101             else token = Factory.newToken(null);
102         }
103 
104         return token;
105     }
106 }