View Javadoc

1   package fr.in2p3.jsaga.adaptor.language.abstracts;
2   
3   import fr.in2p3.jsaga.adaptor.language.LanguageAdaptor;
4   import org.ogf.saga.error.BadParameterException;
5   import org.ogf.saga.error.NoSuccessException;
6   import org.w3c.dom.Document;
7   import org.w3c.dom.Element;
8   
9   import javax.xml.parsers.DocumentBuilderFactory;
10  import javax.xml.parsers.ParserConfigurationException;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.util.*;
14  
15  /* ***************************************************
16  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   AbstractLanguageAdaptorProperties
20  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
21  * Date:   2 nov. 2007
22  * ***************************************************
23  * Description:                                      */
24  /**
25   *
26   */
27  public abstract class AbstractLanguageAdaptorProperties implements LanguageAdaptor {
28      private static final String DEFAULT_SEPARATOR = ",";
29  
30      private Set m_requiredNames;
31      private Set m_propertyNames;
32      private Set m_vectorPropertyNames;
33      private String m_vectoryPropertySeparator;
34  
35      protected void _initParser(String[] requiredPropertyNames, String[] optionalPropertyNames,
36                       String[] requiredVectorPropertyNames, String[] optionalVectorPropertyNames, String vectorPropertySeparator)
37      {
38          m_requiredNames = new HashSet();
39          addAll(m_requiredNames, requiredPropertyNames);
40          addAll(m_requiredNames, requiredVectorPropertyNames);
41  
42          m_propertyNames = new HashSet();
43          addAll(m_propertyNames, requiredPropertyNames);
44          addAll(m_propertyNames, optionalPropertyNames);
45  
46          m_vectorPropertyNames = new HashSet();
47          addAll(m_vectorPropertyNames, requiredVectorPropertyNames);
48          addAll(m_vectorPropertyNames, optionalVectorPropertyNames);
49  
50          m_vectoryPropertySeparator = (vectorPropertySeparator!=null ? vectorPropertySeparator : DEFAULT_SEPARATOR);
51      }
52  
53      public Document parseJobDescription(InputStream jobDescStream) throws BadParameterException, NoSuccessException {
54          // load properties
55          Properties prop = new Properties();
56          try {
57              prop.load(jobDescStream);
58          } catch (IOException e) {
59              throw new NoSuccessException(e);
60          }
61  
62          // check if required elements are present
63          for (Iterator it=m_requiredNames.iterator(); it.hasNext(); ) {
64              String name = (String) it.next();
65              String value = prop.getProperty(name);
66              if (value == null) {
67                  throw new BadParameterException("Missing required attribute: "+name);
68              }
69          }
70  
71          // build DOM
72          Document jobDescDOM;
73          try {
74              jobDescDOM = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
75          } catch (ParserConfigurationException e) {
76              throw new NoSuccessException(e);
77          }
78          Element root = jobDescDOM.createElement("attributes");
79          jobDescDOM.appendChild(root);
80          for (Iterator it=prop.entrySet().iterator(); it.hasNext(); ) {
81              Map.Entry entry = (Map.Entry) it.next();
82              String name = (String) entry.getKey();
83              String value = (String) entry.getValue();
84              if (this.isProperty(name)) {
85                  Element attribute = jobDescDOM.createElement(name);
86                  attribute.setAttribute("value", value);
87                  root.appendChild(attribute);
88              } else if (this.isVectoryProperty(name)) {
89                  Element vectorAttribute = jobDescDOM.createElement(name);
90                  String[] values = value.split(m_vectoryPropertySeparator);
91                  for (int i=0; i<values.length; i++) {
92                      Element item = jobDescDOM.createElement("value");
93                      item.appendChild(jobDescDOM.createTextNode(values[i]));
94                      vectorAttribute.appendChild(item);
95                  }
96                  root.appendChild(vectorAttribute);
97              } else {
98                  throw new BadParameterException("Unexpected attribute name: "+name);
99              }
100         }
101 
102         // returns
103         return jobDescDOM;
104     }
105 
106     public boolean isProperty(String name) {
107         return m_propertyNames.contains(name);
108     }
109 
110     public boolean isVectoryProperty(String name) {
111         return m_vectorPropertyNames.contains(name);
112     }
113 
114     private static void addAll(Set set, String[] array) {
115         for (int i=0; array!=null && i<array.length; i++) {
116             set.add(array[i]);
117         }
118     }
119 }