View Javadoc

1   // Copyright 2006-2007, by the California Institute of Technology.
2   // ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   // Any commercial use must be negotiated with the Office of Technology Transfer
4   // at the California Institute of Technology.
5   //
6   // This software is subject to U. S. export control laws and regulations
7   // (22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   // is subject to U.S. export control laws and regulations, the recipient has
9   // the responsibility to obtain export licenses or other export authority as
10  // may be required before exporting such information to foreign countries or
11  // providing access to foreign nationals.
12  //
13  // $Id: DefinitionFactory.java 2944 2007-09-27 20:24:01Z pramirez $ 
14  //
15  
16  package gov.nasa.pds.tools.dict.parser;
17  
18  import gov.nasa.pds.tools.label.antlr.ODLTokenTypes;
19  import gov.nasa.pds.tools.dict.Definition;
20  import gov.nasa.pds.tools.dict.DictionaryTokens;
21  import gov.nasa.pds.tools.dict.ElementDefinition;
22  import gov.nasa.pds.tools.dict.GroupDefinition;
23  import gov.nasa.pds.tools.dict.ObjectDefinition;
24  import gov.nasa.pds.tools.dict.type.InvalidTypeException;
25  import gov.nasa.pds.tools.dict.type.NumericTypeChecker;
26  import gov.nasa.pds.tools.dict.type.TypeChecker;
27  import gov.nasa.pds.tools.dict.type.TypeCheckerFactory;
28  import gov.nasa.pds.tools.dict.type.UnsupportedTypeException;
29  import gov.nasa.pds.tools.label.AttributeStatement;
30  import gov.nasa.pds.tools.label.ObjectStatement;
31  import gov.nasa.pds.tools.label.Set;
32  
33  import java.util.List;
34  import java.util.ArrayList;
35  import java.util.Iterator;
36  
37  
38  /***
39   * This class builds definitions from ObjectStatements. The format of the object 
40   * statement should be in compliance standard PDS dictionary. These definitions 
41   * can then be added to a {@link gov.nasa.pds.tools.dict.Dictionary}.
42   * 
43   * @author pramirez
44   * @version $Revision: 2944 $
45   * 
46   */
47  public class DefinitionFactory implements ODLTokenTypes, DictionaryTokens {
48  
49      /***
50       * This method will determine the type of definition and created it. If it can not 
51       * determine the type of definition that should be generated an error will be thrown.
52       * @param object from which the defintion will be created
53       * @return a {@link gov.nasa.pds.tools.dict.Definition} that represents an entry in a PDS data dictionary
54       * @throws UnknownDefinitionException thrown when the type of definition can not be recognized.
55       */
56      public static Definition createDefinition(ObjectStatement object) throws UnknownDefinitionException {
57          Definition definition = null;
58          
59          if (object.getIdentifier().endsWith(DEFINITION)) {
60              if (object.getIdentifier().equals(GENERIC_OBJECT) || object.getIdentifier().equals(SPECIFIC_OBJECT)) {
61                  if (OBJECT_TYPE_GENERIC_GROUP.equals(object.getAttribute(OBJECT_TYPE).getValue().toString()) ||
62                      OBJECT_TYPE_SPECIFIC_GROUP.equals(object.getAttribute(OBJECT_TYPE).getValue().toString()))
63                      definition = createGroupDefinition(object);
64                  else
65                      definition = createObjectDefinition(object);
66              } else if (object.getIdentifier().equals(ELEMENT_DEFINITION)) {
67                  definition = createElementDefinition(object);
68              } 
69          } 
70          
71          if (definition == null)
72              throw new UnknownDefinitionException("Can not resolve entry " + object.getIdentifier());
73  
74          return definition;
75      }
76      
77      /***
78       * This method creates an {@link ObjectDefinition} by gathering the attributes required 
79       * from the {@link ObjectStatement} as specified in the PDS Data Dictionary document.
80       * @param object that has the information to form an {@link ObjectDefinition}
81       * @return a {@link Definition} that represents an entry in a PDS data dictionary
82       * @throws UnknownDefinitionException thrown when the type of definition can not be recognized.
83       */
84      public static ObjectDefinition createObjectDefinition(ObjectStatement object) throws UnknownDefinitionException {
85          ObjectDefinition definition = null;
86          
87          if ((object.getIdentifier().equals(GENERIC_OBJECT) || object.getIdentifier().equals(SPECIFIC_OBJECT)) &&
88                  (OBJECT_TYPE_GENERIC.equals(object.getAttribute(OBJECT_TYPE).getValue().toString()) ||
89                   OBJECT_TYPE_SPECIFIC.equals(object.getAttribute(OBJECT_TYPE).getValue().toString()))) {
90              definition = new ObjectDefinition(object.getAttribute(NAME).getValue().toString());
91             
92              //Find and set the description
93              AttributeStatement attribute = object.getAttribute(DESCRIPTION);
94              if (attribute != null)
95                  definition.setDescription(attribute.getValue().toString());
96              
97              //Find and set the status type
98              attribute = object.getAttribute(STATUS_TYPE);
99              if (attribute != null)
100                 definition.setStatusType(attribute.getValue().toString());
101             
102             //Find and set the object type
103             attribute = object.getAttribute(OBJECT_TYPE);
104             if (attribute != null)
105                 definition.setObjectType(attribute.getValue().toString());
106             
107             //Find and set the names of the required objects
108             attribute = object.getAttribute(REQUIRED_OBJECTS);
109             if (attribute != null) {
110                 List requiredObjects = new ArrayList();
111                 Set values = (Set) attribute.getValue();
112                 for (Iterator i = values.iterator(); i.hasNext();) {
113                     requiredObjects.add(i.next().toString());
114                 }
115                 definition.setRequiredObjects(requiredObjects);
116             }
117 
118             //Find and set the names of the optional objects
119             attribute = object.getAttribute(OPTIONAL_OBJECTS);
120             if (attribute != null) {
121                 List optionalObjects = new ArrayList();
122                 Set values = (Set) attribute.getValue();
123                 for (Iterator i = values.iterator(); i.hasNext();) {
124                     optionalObjects.add(i.next().toString());
125                 }
126                 definition.setOptionalObjects(optionalObjects);
127             }
128             
129             //Find and set the names of the required elements
130             attribute = object.getAttribute(REQUIRED_ELEMENTS);
131             if (attribute != null) {
132                 List requiredElements = new ArrayList();
133                 Set values = (Set) attribute.getValue();
134                 for (Iterator i = values.iterator(); i.hasNext();) {
135                     requiredElements.add(i.next().toString());
136                 }
137                 definition.setRequiredElements(requiredElements);
138             }
139             
140             //Find and set the names of the required elements
141             attribute = object.getAttribute(OPTIONAL_ELEMENTS);
142             if (attribute != null) {
143                 List optionalElements = new ArrayList();
144                 Set values = (Set) attribute.getValue();
145                 for (Iterator i = values.iterator(); i.hasNext();) {
146                     optionalElements.add(i.next().toString());
147                 }
148                 definition.setOptionalElements(optionalElements);
149             }
150         } 
151         
152         if (definition == null)
153             throw new UnknownDefinitionException(object.getIdentifier() + " is not an object definition");
154         
155         return definition;
156     }
157     
158     /***
159      * This method creates an {@link GroupDefinition} by gathering the attributes required 
160      * from the {@link ObjectStatement} as specified in the PDS Data Dictionary document.
161      * @param object that has the information to form an {@link GroupDefinition}
162      * @return a {@link Definition} that represents an entry in a PDS data dictionary
163      * @throws UnknownDefinitionException thrown when the type of definition can not be recognized.
164      */
165     public static GroupDefinition createGroupDefinition(ObjectStatement object) throws UnknownDefinitionException {
166         GroupDefinition definition = null;
167         
168         if ((object.getIdentifier().equals(GENERIC_OBJECT) || object.getIdentifier().equals(SPECIFIC_OBJECT)) &&
169                 (OBJECT_TYPE_GENERIC_GROUP.equals(object.getAttribute(OBJECT_TYPE).getValue().toString()) ||
170                  OBJECT_TYPE_SPECIFIC_GROUP.equals(object.getAttribute(OBJECT_TYPE).getValue().toString()))) {
171             definition = new GroupDefinition(object.getAttribute(NAME).getValue().toString());
172             
173             //Find and set the description
174             AttributeStatement attribute = object.getAttribute(DESCRIPTION);
175             if (attribute != null)
176                 definition.setDescription(attribute.getValue().toString());
177             
178             //Find and set the status type
179             attribute = object.getAttribute(STATUS_TYPE);
180             if (attribute != null)
181                 definition.setStatusType(attribute.getValue().toString());
182             
183             //Find and set the object type
184             attribute = object.getAttribute(OBJECT_TYPE);
185             if (attribute != null)
186                 definition.setObjectType(attribute.getValue().toString());
187             
188             //Find and set the names of the required elements
189             attribute = object.getAttribute(REQUIRED_ELEMENTS);
190             if (attribute != null) {
191                 List requiredElements = new ArrayList();
192                 Set values = (Set) attribute.getValue();
193                 for (Iterator i = values.iterator(); i.hasNext();) {
194                     requiredElements.add(i.next().toString());
195                 }
196                 definition.setRequiredElements(requiredElements);
197             }
198             
199             //Find and set the names of the required elements
200             attribute = object.getAttribute(OPTIONAL_ELEMENTS);
201             if (attribute != null) {
202                 List optionalElements = new ArrayList();
203                 Set values = (Set) attribute.getValue();
204                 for (Iterator i = values.iterator(); i.hasNext();) {
205                     optionalElements.add(i.next().toString());
206                 }
207                 definition.setOptionalElements(optionalElements);
208             }
209         }
210         
211         if (definition == null)
212             throw new UnknownDefinitionException(object.getIdentifier() + " is not an group definition");
213         
214         return definition;
215     }
216     
217     /***
218      * This method creates an {@link ElementDefinition} by gathering the attributes required 
219      * from the {@link ObjectStatement} as specified in the PDS Data Dictionary document.
220      * @param object that has the information to form an {@link ElementDefinition}
221      * @return a {@link Definition} that represents an entry in a PDS data dictionary
222      * @throws UnknownDefinitionException thrown when the type of definition can not be recognized.
223      */
224     public static ElementDefinition createElementDefinition(ObjectStatement object) throws UnknownDefinitionException {
225         ElementDefinition definition = null;
226         
227         if (object.getIdentifier().equals(ELEMENT_DEFINITION)) {
228             definition = new ElementDefinition(object.getAttribute(NAME).getValue().toString());
229             
230             //Find and set status
231             AttributeStatement attribute = object.getAttribute(STATUS_TYPE);
232             if (attribute != null)
233                 definition.setStatusType(attribute.getValue().toString());
234             
235             //Find and set description
236             attribute = object.getAttribute(DESCRIPTION);
237             if (attribute != null) 
238                 definition.setDescription(attribute.getValue().toString());
239             
240             //Find and set data type
241             attribute = object.getAttribute(DATA_TYPE);
242             if (attribute != null)
243                 definition.setDataType(attribute.getValue().toString());
244             
245             //Find and set units
246             attribute = object.getAttribute(DictionaryTokens.UNITS);
247             if (attribute != null) 
248                 definition.setUnitId(attribute.getValue().toString());
249             
250             //Find and set min length
251             attribute = object.getAttribute(MIN_LENGTH);
252             if (attribute != null && !"NULL".equals(attribute.getValue().toString())) {
253                 try {
254                     definition.setMinLength(Integer.parseInt(attribute.getValue().toString()));
255                 } catch (NumberFormatException nfe) {}
256             }
257             
258             //Find and set max length
259             attribute = object.getAttribute(MAX_LENGTH);
260             if (attribute != null && !"NULL".equals(attribute.getValue().toString())) {
261                 try {
262                     definition.setMaxLength(Integer.parseInt(attribute.getValue().toString()));
263                 } catch (NumberFormatException nfe) {}
264             }
265             
266             //Find and set min value
267             attribute = object.getAttribute(MINIMUM);
268             if (attribute != null && !"NULL".equals(attribute.getValue().toString())) {
269                 try {
270                     TypeChecker checker = TypeCheckerFactory.getInstance().newInstance(definition.getDataType());
271                     if (checker instanceof NumericTypeChecker)
272                         definition.setMinimum((Number) checker.cast(attribute.getValue().toString()));
273                 } catch (InvalidTypeException e) {
274                     // TODO Auto-generated catch block
275                     //FIXME: throw an InvalidDefinitionException
276                 } catch (UnsupportedTypeException e) {
277                     // TODO Auto-generated catch block
278                     //FIXME: throw an InvalidDefinitionException
279                 }
280             }
281             
282             //Find and set max value
283             attribute = object.getAttribute(MAXIMUM);
284             if (attribute != null && !"NULL".equals(attribute.getValue().toString())) {
285                 try {
286                     TypeChecker checker = TypeCheckerFactory.getInstance().newInstance(definition.getDataType());
287                     if (checker instanceof NumericTypeChecker)
288                         definition.setMaximum((Number) checker.cast(attribute.getValue().toString()));
289                 } catch (InvalidTypeException e) {
290                     // TODO Auto-generated catch block
291                     //FIXME: throw an InvalidDefinitionException
292                 } catch (UnsupportedTypeException e) {
293                     // TODO Auto-generated catch block
294                     //FIXME: throw an InvalidDefinitionException
295                 }
296             }
297             
298             //Find and set value type
299             attribute = object.getAttribute(VALUE_TYPE);
300             if (attribute != null)
301                 definition.setValueType(attribute.getValue().toString());
302             
303             //Find and set value set
304             attribute = object.getAttribute(VALUES);
305             List values = new ArrayList();
306             if (attribute != null) {
307                 Set s = (Set) attribute.getValue();
308                 for (Iterator i = s.iterator(); i.hasNext();) {
309                     String value = i.next().toString();
310                     if (!"N/A".equals(value))
311                         values.add(value);
312                 }
313                 definition.setValues(values);
314             }
315             
316         }
317         
318         if (definition == null) 
319             throw new UnknownDefinitionException(object.getIdentifier() + " is not an element definition");
320         
321         return definition;
322     }
323 }