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 3347 2008-07-15 20:20:43Z 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: 3347 $
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          return isValid(dictionary, group, new DefaultValidationListener());
41      }
42      
43      public static boolean isValid(Dictionary dictionary, GroupStatement group, ValidationListener listener) {
44          boolean valid = true;
45          
46          //Lookup group definition, can't do anything without it
47          GroupDefinition definition = dictionary.findGroupClassDefinition(group.getIdentifier());
48          if (definition == null) {
49          	listener.reportError("Undefined Group: " + group.getIdentifier());
50          	log.log(new ToolsLogRecord(Level.SEVERE, "Undefined Group: " + group.getIdentifier(), group.getFilename(), group.getContext(), group.getLineNumber()));
51          	valid = false;
52          } else {
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  	        
87          //Validate all attributes
88          List attributes = group.getAttributes();
89          Collections.sort(attributes);
90          for (Iterator i = group.getAttributes().iterator(); i.hasNext();) {
91              AttributeStatement attribute = (AttributeStatement) i.next();
92              if (!ElementValidator.isValid(dictionary, attribute, listener))
93                  valid = false;
94          }
95          
96          return valid;
97      }
98  }