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  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   * &lt;place ID="parameter" capacity="1"&gt;
21   *   &lt;description&gt;parameter place&lt;/description&gt;
22   *   &lt;property/&gt;   -&gt; refer to PropertiesJdom 
23   *   &lt;tokenClass owl="owlurl" type="file"/&gt; 
24   *   &lt;token/&gt;      -&gt; refer to TokenJdom
25   * &lt;/place&gt;
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          // capacity
43          final Integer c = place.getCapacity();
44          if (c != Place.DEFAULT_CAPACITY) {
45              pe.setAttribute("capacity", c.toString());
46          }
47  
48          // description
49          final String s = place.getDescription();
50          if (s != Place.DEFAULT_DESCRIPTION) {
51              //final Element de = new Element("description");
52              final Element de = new Element("description", JdomString.wfSpace);
53              de.setText(s);
54              pe.addContent(de);
55          }
56  
57          // properties
58          final Element[] els = PropertiesJdom.java2elements(place.getProperties());
59          for (Element el : els) {
60              pe.addContent(el);
61          }
62  
63          // token class (owl & type)
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              //final Element tce = new Element("tokenClass");
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          // tokens
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         // ID
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         // capacity
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         // description
114         //final Element de = placeElement.getChild("description");
115         final Element de = placeElement.getChild("description", JdomString.wfSpace);
116         if (de != null) {
117             place.setDescription(de.getText());
118         }
119 
120         // properties
121         final List els = placeElement.getChildren("property", JdomString.wfSpace);
122         //final List els = placeElement.getChildren("property");
123         GenericProperties props = PropertiesJdom.elements2java(els);
124         place.setProperties(props);
125 
126 
127         // token class
128         final Element tce = placeElement.getChild("tokenClass", JdomString.wfSpace);
129         //final Element tce = placeElement.getChild("tokenClass");
130         if (tce != null) {
131             OwlsJdom.element2java(tce, place);
132             /*
133             final Attribute tcaOwl = tce.getAttribute("owl");
134             if (tcaOwl != null) {
135                 place.setOwl(tcaOwl.getValue());
136             }
137             */
138             final Attribute tcaType = tce.getAttribute("type");
139             if (tcaType != null) {
140                 place.setTokenType(tcaType.getValue());
141             }
142         }
143 
144         // tokens
145         final List list = placeElement.getChildren("token", JdomString.wfSpace);
146         //final List list = placeElement.getChildren("token");
147         for (Object aList : list) {
148             final Token t = TokenJdom.element2java((Element) aList);
149             place.addToken(t);
150         }
151 
152         return place;
153     }
154 }