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 2606 2007-04-18 19:35:09Z 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: 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          //Lookup group definition, can't do anything without it
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          //First check that required elements are captured in object
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          //Check to make sure all attributes are allowed within this definition
60          //If the definition contains the element PSDD then anything is allowed
61          //and this check can be skipped
62          //TODO: put magic string PSDD in an interface as a static final String
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          //Validate all attributes
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  }