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$ 
14  //
15  
16  package gov.nasa.pds.tools.label.validate;
17  
18  import java.util.Iterator;
19  import java.util.logging.Level;
20  import java.util.logging.Logger;
21  
22  import gov.nasa.pds.tools.label.Label;
23  import gov.nasa.pds.tools.label.LabelType;
24  import gov.nasa.pds.tools.label.ObjectStatement;
25  import gov.nasa.pds.tools.logging.ToolsLogRecord;
26  
27  /***
28   * This class validates that all file characteristics are found in a label;
29   * @author pramirez
30   * @version $Revision$
31   * 
32   */
33  public class FileCharacteristicValidator implements LabelValidator, LabelType, RecordType {
34      private static Logger log = Logger.getLogger(FileCharacteristicValidator.class.getName());
35      //This is the set of predefined required file characteristic elements
36      private final static String RECORD_TYPE = "RECORD_TYPE";
37      private final static String RECORD_BYTES = "RECORD_BYTES";
38      private final static String FILE_RECORDS = "FILE_RECORDS";
39      private final static String LABEL_RECORDS = "LABEL_RECORDS";
40  
41      /*** (non-Javadoc)
42       * @see gov.nasa.pds.tools.label.validate.LabelValidator#isValid(gov.nasa.pds.tools.label.Label)
43       */
44      public boolean isValid(Label label) {
45          if (label.getLabelType() == LabelType.UNDEFINED) {
46              log.log(new ToolsLogRecord(Level.WARNING, "File characteristics will not be checked as the type of label is UNDEFINED.", label.getFilename()));
47              return true;
48          }
49          else if (label.getLabelType() == COMBINED_DETACHED)
50              return checkCombinedDetached(label);
51          else if (label.getLabelType() == ATTACHED || label.getLabelType() == DETACHED){
52              if (label.getAttribute(RECORD_TYPE) == null) {
53                  log.log(new ToolsLogRecord(Level.SEVERE, "Label does not contain the required RECORD_TYPE element.", label.getFilename()));
54                  return false;
55              }
56              
57              String recordType = label.getAttribute(RECORD_TYPE).getValue().toString();
58              if (!FIXED_LENGTH.equals(recordType) && !VARIABLE_LENGTH.equals(recordType) && 
59                      !STREAM.equals(recordType) && !RecordType.UNDEFINED.equals(recordType)) {
60                  log.log(new ToolsLogRecord(Level.WARNING, "Could not determine RECORD_TYPE file characteristics will not be checked", label.getFilename()));
61                  return false;
62              }
63              
64              if (RecordType.UNDEFINED.equals(recordType)) {
65                  return true;
66              }
67              
68              if (label.getLabelType() == ATTACHED)
69                  return checkAttached(label);
70              else if (label.getLabelType() == DETACHED)
71                  return checkDetached(label);
72          } else
73              log.log(new ToolsLogRecord(Level.WARNING, "Could not check file characteristics as the type of label could not be determined.", label.getFilename()));
74          
75          return false;
76      }
77  
78      private boolean checkAttached(Label label) {
79          boolean pass = true;
80          String recordType = label.getAttribute(RECORD_TYPE).getValue().toString();
81          if (FIXED_LENGTH.equals(recordType) || VARIABLE_LENGTH.equals(recordType)) {
82              if (label.getAttribute(RECORD_BYTES) == null) {
83                  log.log(new ToolsLogRecord(Level.SEVERE, "Label does not contain required element " + RECORD_BYTES, label.getFilename()));
84                  pass = false;
85              }
86              if (label.getAttribute(FILE_RECORDS) == null) {
87                  log.log(new ToolsLogRecord(Level.SEVERE, "Label does not contain required element " + FILE_RECORDS, label.getFilename()));
88                  pass = false;
89              }
90              if (label.getAttribute(LABEL_RECORDS) == null) {
91                  log.log(new ToolsLogRecord(Level.SEVERE, "Label does not contain required element " + LABEL_RECORDS, label.getFilename()));
92                  pass = false;
93              }
94          } 
95          
96          return pass;
97      }
98      
99      private boolean checkDetached(Label label) {
100         boolean pass = true;
101         String recordType = label.getAttribute(RECORD_TYPE).getValue().toString();
102         if (FIXED_LENGTH.equals(recordType) || VARIABLE_LENGTH.equals(recordType)) {
103             if (label.getAttribute(RECORD_BYTES) == null) {
104                 log.log(new ToolsLogRecord(Level.SEVERE, "Label does not contain required element " + RECORD_BYTES, label.getFilename()));
105                 pass = false;
106             }
107             if (label.getAttribute(FILE_RECORDS) == null) {
108                 log.log(new ToolsLogRecord(Level.SEVERE, "Label does not contain required element " + FILE_RECORDS, label.getFilename()));
109                 pass = false;
110             }
111         } 
112         return pass;
113     }
114     
115     private boolean checkCombinedDetached(Label label) {
116         boolean pass = true;
117         
118         for (Iterator i = label.getObjects("FILE").iterator(); i.hasNext();) {
119             ObjectStatement fileObject = (ObjectStatement) i.next();
120             pass = checkFileObject(fileObject);
121         }
122         
123         return pass;
124     }
125     
126     private boolean checkFileObject(ObjectStatement object) {
127         boolean pass = true;
128         
129         if (object.getAttribute(RECORD_TYPE) == null) {
130             log.log(new ToolsLogRecord(Level.SEVERE, "File object does not contain the required RECORD_TYPE element.", 
131                     object.getFilename(), object.getContext(), object.getLineNumber()));
132             return false;
133         }
134         
135         String recordType = object.getAttribute(RECORD_TYPE).getValue().toString();
136         if (!FIXED_LENGTH.equals(recordType) && !VARIABLE_LENGTH.equals(recordType) && 
137                 !STREAM.equals(recordType) && !RecordType.UNDEFINED.equals(recordType)) {
138             log.log(new ToolsLogRecord(Level.WARNING, "Could not determine RECORD_TYPE file characteristics will not be checked", 
139                     object.getFilename(), object.getContext(), object.getLineNumber()));
140             return false;
141         }
142         
143         if (FIXED_LENGTH.equals(recordType) || VARIABLE_LENGTH.equals(recordType)) {
144             if (object.getAttribute(RECORD_BYTES) == null) {
145                 log.log(new ToolsLogRecord(Level.SEVERE, "File object does not contain required element " + RECORD_BYTES, 
146                         object.getFilename(), object.getContext(), object.getLineNumber()));
147                 pass = false;
148             }
149             if (object.getAttribute(FILE_RECORDS) == null) {
150                 log.log(new ToolsLogRecord(Level.SEVERE, "File object does not contain required element " + FILE_RECORDS, 
151                         object.getFilename(), object.getContext(), object.getLineNumber()));
152                 pass = false;
153             }
154         } 
155         return pass;
156     }
157 }