1
2
3
4
5
6
7
8
9
10
11
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 }