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  //
14  
15  package gov.nasa.pds.tools.label.validate;
16  
17  import java.util.Iterator;
18  import java.util.List;
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.ObjectStatement;
24  import gov.nasa.pds.tools.label.PointerStatement;
25  import gov.nasa.pds.tools.label.PointerType;
26  import gov.nasa.pds.tools.label.Statement;
27  import gov.nasa.pds.tools.logging.ToolsLogRecord;
28  
29  public class CatalogNameValidator implements LabelValidator, PointerType {
30      private static Logger log = Logger.getLogger(CatalogNameValidator.class.getName());
31  
32  	public boolean isValid(Label label) {
33  		return isValid(label, new DefaultValidationListener());
34  	}
35  
36  	public boolean isValid(Label label, ValidationListener listener) {
37  		boolean valid = true;
38  		
39  		if (!checkPointers(label.getPointers(), listener))
40  			valid = false;
41  		
42          if (!checkPointersInObjects(label.getObjects(), listener))
43          	valid = false;
44          
45          return valid;
46  	}
47  	
48  	private boolean checkPointersInObjects(List objects, ValidationListener listener) {
49  		boolean valid = true;
50  		
51  		for (Iterator i = objects.iterator(); i.hasNext();) {
52  			ObjectStatement object = (ObjectStatement) i.next();
53  			if (!checkPointers(object.getPointers(), listener))
54  				valid = false;
55  			if (!checkPointersInObjects(object.getObjects(), listener))
56  				valid = false;
57  		}
58  		
59  		return valid;
60  	}
61  	
62  	private boolean checkPointers(List pointers, ValidationListener listener) {
63  		boolean valid  = true;
64  		
65  		for (Iterator i = pointers.iterator(); i.hasNext();) {
66              Statement statement = (Statement) i.next();
67              
68              if (statement.getIdentifier().endsWith("_" + CATALOG)) {
69              	boolean found = false;
70              	for (int n = 0; !found && n < CATALOG_NAMES.length; n++) {
71              		if (statement.getIdentifier().equals(CATALOG_NAMES[n]))
72              			found = true;
73              	}
74              	if (!found) {
75              		listener.reportError(statement.getIdentifier() + " does not follow catalog pointer naming convention.");
76              		log.log(new ToolsLogRecord(Level.SEVERE, statement.getIdentifier() + " does not follow catalog pointer naming convention.",
77              				statement.getFilename(), statement.getContext(), statement.getLineNumber()));
78              		valid = false;
79              	}
80              }
81              
82          }
83          
84          return valid;
85  	}
86  
87  }