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   * User: hans
18   * Date: 05.10.2005
19   * Time: 13:54:49
20   */
21  public final class PropertiesJdom {
22      private PropertiesJdom() {}
23  
24      public static Element[] java2elements(final GenericProperties props) {
25          if (props == null) return new Element[0];
26          final Property[] ps = props.getProperties();
27          final Element[] els = new Element[ps.length];
28          for (int i = 0; i < ps.length; i++) {
29              els[i] = new Element("property", JdomString.wfSpace);
30              //els[i] = new Element("property");
31              els[i].setAttribute("name", ps[i].getKey());
32              els[i].setText(ps[i].getValue());
33  
34          }
35          return els;
36      }
37  
38      public static GenericProperties elements2java(final Element[] els) throws WorkflowFormatException {
39          final GenericProperties props = Factory.newProperties();
40          final Property[] ps = new Property[els.length];
41          for (int i = 0; i < ps.length; i++) {
42              Attribute na = els[i].getAttribute("name");
43              if (na==null) throw new WorkflowFormatException("Mandatory attribute \"name\" of element <property> is missing!");
44              final String name = els[i].getAttribute("name").getValue();
45              final String value = els[i].getText();
46              //props.put(name, value);
47  
48              ps[i] = Factory.newProperty(name, value);
49  
50          }
51          props.setProperties(ps);
52          return props;
53      }
54  
55      public static GenericProperties elements2java(final List els) {
56          final GenericProperties props = Factory.newProperties();
57          for (Object el1 : els) {
58              final Element el = (Element) el1;
59              final String name = el.getAttribute("name").getValue();
60              final String value = el.getText();
61              props.put(name, value);
62          }
63          return props;
64      }
65  }