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: GroupValidator.java 2911 2007-09-27 13:14:33Z pramirez $ 
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          //Lookup group definition, can't do anything without it
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          //First check that required elements are captured in object
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          //Check to make sure all attributes are allowed within this definition
67          //If the definition contains the element PSDD then anything is allowed
68          //and this check can be skipped
69          //TODO: put magic string PSDD in an interface as a static final String
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          //Validate all attributes
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 }