1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package gov.nasa.pds.tools.label.validate;
17
18 import java.util.Collections;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 import gov.nasa.pds.tools.dict.Dictionary;
25 import gov.nasa.pds.tools.dict.GroupDefinition;
26 import gov.nasa.pds.tools.dict.type.UnsupportedTypeException;
27 import gov.nasa.pds.tools.label.AttributeStatement;
28 import gov.nasa.pds.tools.label.GroupStatement;
29 import gov.nasa.pds.tools.logging.ToolsLogRecord;
30
31 /***
32 * @author pramirez
33 * @version $Revision: 2606 $
34 *
35 */
36 public class GroupValidator {
37 private static Logger log = Logger.getLogger(GroupValidator.class.getName());
38
39 public static boolean isValid(Dictionary dictionary, GroupStatement group)
40 throws DefinitionNotFoundException {
41 boolean valid = true;
42
43
44 GroupDefinition definition = dictionary.findGroupClassDefinition(group.getIdentifier());
45 if (definition == null)
46 throw new DefinitionNotFoundException("Could not find group definition for " + group.getIdentifier());
47
48
49 for (Iterator i = definition.getRequiredElements().iterator(); i.hasNext();) {
50 String required = (String) i.next();
51 if (!group.hasAttribute(required)) {
52 valid = false;
53 log.log(new ToolsLogRecord(Level.SEVERE, "Group " + group.getIdentifier() +
54 " does not contain required element " + required,
55 group.getFilename(), group.getContext(), group.getLineNumber()));
56 }
57 }
58
59
60
61
62
63 if (!definition.hasElement("PSDD")) {
64 for (Iterator i = group.getAttributes().iterator(); i.hasNext();) {
65 AttributeStatement attribute = (AttributeStatement) i.next();
66 if (!definition.canHaveElement(attribute.getIdentifier())) {
67 valid = false;
68 log.log(new ToolsLogRecord(Level.SEVERE, "Group " + group.getIdentifier() +
69 " contains the element " + attribute.getIdentifier() +
70 " which is neither required nor optional.",
71 attribute.getFilename(), attribute.getContext(), attribute.getLineNumber()));
72 }
73 }
74 }
75
76
77 List attributes = group.getAttributes();
78 Collections.sort(attributes);
79 for (Iterator i = group.getAttributes().iterator(); i.hasNext();) {
80 AttributeStatement attribute = (AttributeStatement) i.next();
81 boolean elementValid = false;
82 try {
83 elementValid = ElementValidator.isValid(dictionary, attribute);
84 } catch (UnsupportedTypeException ute) {
85 log.log(new ToolsLogRecord(Level.SEVERE, ute.getMessage(), attribute.getFilename(),
86 attribute.getContext(), attribute.getLineNumber()));
87 } catch (DefinitionNotFoundException dnfe) {
88 log.log(new ToolsLogRecord(Level.SEVERE, dnfe.getMessage(), attribute.getFilename(),
89 attribute.getContext(), attribute.getLineNumber()));
90 }
91 if (!elementValid)
92 valid = false;
93 }
94
95 return valid;
96 }
97 }