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: 2911 $
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 return isValid(dictionary, group, new DefaultValidationListener());
42 }
43
44 public static boolean isValid(Dictionary dictionary, GroupStatement group, ValidationListener listener)
45 throws DefinitionNotFoundException {
46 boolean valid = true;
47
48
49 GroupDefinition definition = dictionary.findGroupClassDefinition(group.getIdentifier());
50 if (definition == null)
51 throw new DefinitionNotFoundException("Could not find group definition for " + group.getIdentifier());
52
53
54 for (Iterator i = definition.getRequiredElements().iterator(); i.hasNext();) {
55 String required = (String) i.next();
56 if (!group.hasAttribute(required)) {
57 valid = false;
58 listener.reportError("Group " + group.getIdentifier() +
59 " does not contain required element " + required);
60 log.log(new ToolsLogRecord(Level.SEVERE, "Group " + group.getIdentifier() +
61 " does not contain required element " + required,
62 group.getFilename(), group.getContext(), group.getLineNumber()));
63 }
64 }
65
66
67
68
69
70 if (!definition.hasElement("PSDD")) {
71 for (Iterator i = group.getAttributes().iterator(); i.hasNext();) {
72 AttributeStatement attribute = (AttributeStatement) i.next();
73 if (!definition.canHaveElement(attribute.getIdentifier())) {
74 valid = false;
75 listener.reportError("Group " + group.getIdentifier() +
76 " contains the element " + attribute.getIdentifier() +
77 " which is neither required nor optional.");
78 log.log(new ToolsLogRecord(Level.SEVERE, "Group " + group.getIdentifier() +
79 " contains the element " + attribute.getIdentifier() +
80 " which is neither required nor optional.",
81 attribute.getFilename(), attribute.getContext(), attribute.getLineNumber()));
82 }
83 }
84 }
85
86
87 List attributes = group.getAttributes();
88 Collections.sort(attributes);
89 for (Iterator i = group.getAttributes().iterator(); i.hasNext();) {
90 AttributeStatement attribute = (AttributeStatement) i.next();
91 boolean elementValid = false;
92 try {
93 elementValid = ElementValidator.isValid(dictionary, attribute, listener);
94 } catch (UnsupportedTypeException ute) {
95 listener.reportError(ute.getMessage());
96 log.log(new ToolsLogRecord(Level.SEVERE, ute.getMessage(), attribute.getFilename(),
97 attribute.getContext(), attribute.getLineNumber()));
98 } catch (DefinitionNotFoundException dnfe) {
99 listener.reportError(dnfe.getMessage());
100 log.log(new ToolsLogRecord(Level.SEVERE, dnfe.getMessage(), attribute.getFilename(),
101 attribute.getContext(), attribute.getLineNumber()));
102 }
103 if (!elementValid)
104 valid = false;
105 }
106
107 return valid;
108 }
109 }