1
2
3
4
5
6
7
8
9
10
11
12
13
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: 2648 $
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) ||
61 object.getIdentifier().equals(SPECIFIC_OBJECT)) {
62 if (!OBJECT_TYPE_GENERIC_GROUP.equals(object.getAttribute(OBJECT_TYPE).getValue().toString()))
63 definition = createObjectDefinition(object);
64 else
65 definition = createGroupDefinition(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) ||
88 object.getIdentifier().equals(SPECIFIC_OBJECT) &&
89 !OBJECT_TYPE_GENERIC_GROUP.equals(object.getAttribute(OBJECT_TYPE).getValue().toString())) {
90 definition = new ObjectDefinition(object.getAttribute(NAME).getValue().toString());
91
92
93 AttributeStatement attribute = object.getAttribute(DESCRIPTION);
94 if (attribute != null)
95 definition.setDescription(attribute.getValue().toString());
96
97
98 attribute = object.getAttribute(STATUS_TYPE);
99 if (attribute != null)
100 definition.setStatusType(attribute.getValue().toString());
101
102
103 attribute = object.getAttribute(REQUIRED_OBJECTS);
104 if (attribute != null) {
105 List requiredObjects = new ArrayList();
106 Set values = (Set) attribute.getValue();
107 for (Iterator i = values.iterator(); i.hasNext();) {
108 requiredObjects.add(i.next().toString());
109 }
110 definition.setRequiredObjects(requiredObjects);
111 }
112
113
114 attribute = object.getAttribute(OPTIONAL_OBJECTS);
115 if (attribute != null) {
116 List optionalObjects = new ArrayList();
117 Set values = (Set) attribute.getValue();
118 for (Iterator i = values.iterator(); i.hasNext();) {
119 optionalObjects.add(i.next().toString());
120 }
121 definition.setOptionalObjects(optionalObjects);
122 }
123
124
125 attribute = object.getAttribute(REQUIRED_ELEMENTS);
126 if (attribute != null) {
127 List requiredElements = new ArrayList();
128 Set values = (Set) attribute.getValue();
129 for (Iterator i = values.iterator(); i.hasNext();) {
130 requiredElements.add(i.next().toString());
131 }
132 definition.setRequiredElements(requiredElements);
133 }
134
135
136 attribute = object.getAttribute(OPTIONAL_ELEMENTS);
137 if (attribute != null) {
138 List optionalElements = new ArrayList();
139 Set values = (Set) attribute.getValue();
140 for (Iterator i = values.iterator(); i.hasNext();) {
141 optionalElements.add(i.next().toString());
142 }
143 definition.setOptionalElements(optionalElements);
144 }
145 }
146
147 if (definition == null)
148 throw new UnknownDefinitionException(object.getIdentifier() + " is not an object definition");
149
150 return definition;
151 }
152
153 /***
154 * This method creates an {@link GroupDefinition} by gathering the attributes required
155 * from the {@link ObjectStatement} as specified in the PDS Data Dictionary document.
156 * @param object that has the information to form an {@link GroupDefinition}
157 * @return a {@link Definition} that represents an entry in a PDS data dictionary
158 * @throws UnknownDefinitionException thrown when the type of definition can not be recognized.
159 */
160 public static GroupDefinition createGroupDefinition(ObjectStatement object) throws UnknownDefinitionException {
161 GroupDefinition definition = null;
162
163 if (object.getIdentifier().equals(GENERIC_OBJECT) ||
164 object.getIdentifier().equals(SPECIFIC_OBJECT) &&
165 OBJECT_TYPE_GENERIC_GROUP.equals(object.getAttribute(OBJECT_TYPE).getValue().toString())) {
166 definition = new GroupDefinition(object.getAttribute(NAME).getValue().toString());
167
168
169 AttributeStatement attribute = object.getAttribute(DESCRIPTION);
170 if (attribute != null)
171 definition.setDescription(attribute.getValue().toString());
172
173
174 attribute = object.getAttribute(STATUS_TYPE);
175 if (attribute != null)
176 definition.setStatusType(attribute.getValue().toString());
177
178 attribute = object.getAttribute(REQUIRED_ELEMENTS);
179 if (attribute != null) {
180 List requiredElements = new ArrayList();
181 Set values = (Set) attribute.getValue();
182 for (Iterator i = values.iterator(); i.hasNext();) {
183 requiredElements.add(i.next().toString());
184 }
185 definition.setRequiredElements(requiredElements);
186 }
187
188
189 attribute = object.getAttribute(OPTIONAL_ELEMENTS);
190 if (attribute != null) {
191 List optionalElements = new ArrayList();
192 Set values = (Set) attribute.getValue();
193 for (Iterator i = values.iterator(); i.hasNext();) {
194 optionalElements.add(i.next().toString());
195 }
196 definition.setOptionalElements(optionalElements);
197 }
198 }
199
200 if (definition == null)
201 throw new UnknownDefinitionException(object.getIdentifier() + " is not an group definition");
202
203 return definition;
204 }
205
206 /***
207 * This method creates an {@link ElementDefinition} by gathering the attributes required
208 * from the {@link ObjectStatement} as specified in the PDS Data Dictionary document.
209 * @param object that has the information to form an {@link ElementDefinition}
210 * @return a {@link Definition} that represents an entry in a PDS data dictionary
211 * @throws UnknownDefinitionException thrown when the type of definition can not be recognized.
212 */
213 public static ElementDefinition createElementDefinition(ObjectStatement object) throws UnknownDefinitionException {
214 ElementDefinition definition = null;
215
216 if (object.getIdentifier().equals(ELEMENT_DEFINITION)) {
217 definition = new ElementDefinition(object.getAttribute(NAME).getValue().toString());
218
219
220 AttributeStatement attribute = object.getAttribute(STATUS_TYPE);
221 if (attribute != null)
222 definition.setStatusType(attribute.getValue().toString());
223
224
225 attribute = object.getAttribute(DESCRIPTION);
226 if (attribute != null)
227 definition.setDescription(attribute.getValue().toString());
228
229
230 attribute = object.getAttribute(DATA_TYPE);
231 if (attribute != null)
232 definition.setDataType(attribute.getValue().toString());
233
234
235 attribute = object.getAttribute(DictionaryTokens.UNITS);
236 if (attribute != null)
237 definition.setUnitId(attribute.getValue().toString());
238
239
240 attribute = object.getAttribute(MIN_LENGTH);
241 if (attribute != null && !"NULL".equals(attribute.getValue().toString())) {
242 try {
243 definition.setMinLength(Integer.parseInt(attribute.getValue().toString()));
244 } catch (NumberFormatException nfe) {}
245 }
246
247
248 attribute = object.getAttribute(MAX_LENGTH);
249 if (attribute != null && !"NULL".equals(attribute.getValue().toString())) {
250 try {
251 definition.setMaxLength(Integer.parseInt(attribute.getValue().toString()));
252 } catch (NumberFormatException nfe) {}
253 }
254
255
256 attribute = object.getAttribute(MINIMUM);
257 if (attribute != null && !"NULL".equals(attribute.getValue().toString())) {
258 try {
259 TypeChecker checker = TypeCheckerFactory.getInstance().newInstance(definition.getDataType());
260 if (checker instanceof NumericTypeChecker)
261 definition.setMinimum((Number) checker.cast(attribute.getValue().toString()));
262 } catch (InvalidTypeException e) {
263
264
265 } catch (UnsupportedTypeException e) {
266
267
268 }
269 }
270
271
272 attribute = object.getAttribute(MAXIMUM);
273 if (attribute != null && !"NULL".equals(attribute.getValue().toString())) {
274 try {
275 TypeChecker checker = TypeCheckerFactory.getInstance().newInstance(definition.getDataType());
276 if (checker instanceof NumericTypeChecker)
277 definition.setMaximum((Number) checker.cast(attribute.getValue().toString()));
278 } catch (InvalidTypeException e) {
279
280
281 } catch (UnsupportedTypeException e) {
282
283
284 }
285 }
286
287
288 attribute = object.getAttribute(VALUE_TYPE);
289 if (attribute != null)
290 definition.setValueType(attribute.getValue().toString());
291
292
293 attribute = object.getAttribute(VALUES);
294 List values = new ArrayList();
295 if (attribute != null) {
296 Set s = (Set) attribute.getValue();
297 for (Iterator i = s.iterator(); i.hasNext();) {
298 String value = i.next().toString();
299 if (!"N/A".equals(value))
300 values.add(value);
301 }
302 definition.setValues(values);
303 }
304
305 }
306
307 if (definition == null)
308 throw new UnknownDefinitionException(object.getIdentifier() + " is not an element definition");
309
310 return definition;
311 }
312 }