1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package gov.nasa.pds.tools.label;
17
18 import gov.nasa.pds.tools.label.validate.Status;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.HashMap;
24 import java.util.Iterator;
25
26 /***
27 * This class represents a PDS label.
28 * @author pramirez
29 * @version $Revision: 3353 $
30 *
31 */
32 public class Label implements LabelType, Status {
33 private volatile int labelType;
34 private Map statements;
35 private String filename;
36 private String status;
37 private int numErrors;
38 private int numWarnings;
39
40 /***
41 * Constructs an object representation of a PDS label.
42 *
43 */
44 public Label() {
45 statements = new HashMap();
46 labelType = UNDEFINED;
47 filename = null;
48 status = Status.UNKNOWN;
49 numErrors = 0;
50 numWarnings = 0;
51 }
52
53 /***
54 * Retrieves a statement with the identifier
55 * @param identifier Identifies the statement to retrieve
56 * @return The named statement or null if not found
57 */
58 public List getStatement(String identifier) {
59 return (List) statements.get(identifier);
60 }
61
62 /***
63 * Retrieves the attribute with the identifier or null if not found
64 * @param identifier of attribute to find
65 * @return attribute or null
66 */
67 public AttributeStatement getAttribute(String identifier) {
68 AttributeStatement attribute = null;
69
70 if (statements.get(identifier) != null) {
71 for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext() && attribute == null;) {
72 Statement statement = (Statement) i.next();
73 if (statement instanceof AttributeStatement)
74 attribute = (AttributeStatement) statement;
75 }
76 }
77
78 return attribute;
79 }
80
81 /***
82 * Retrieves the groups with the identifier or null if not found
83 * @param identifier of group to find
84 * @return {@link List} of {@link GroupStatement}
85 */
86 public List getGroups(String identifier) {
87 List groups = new ArrayList();
88
89 if (statements.get(identifier) != null) {
90 for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
91 Statement statement = (Statement) i.next();
92 if (statement instanceof GroupStatement)
93 groups.add(statement);
94 }
95 }
96
97 return groups;
98 }
99
100 /***
101 * Retrieves the object with the identifier or null if not found
102 * @param identifier of object to find
103 * @return {@link List} of {@link ObjectStatement}
104 */
105 public List getObjects(String identifier) {
106 List objects = new ArrayList();
107
108 if (statements.get(identifier) != null) {
109 for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
110 Statement statement = (Statement) i.next();
111 if (statement instanceof ObjectStatement)
112 objects.add(statement);
113 }
114 }
115
116 return objects;
117 }
118
119 /***
120 * Retrieves the statements associated with this label
121 * @return {@link List} of {@link Statement}
122 */
123 public List getStatements() {
124 List results = new ArrayList();
125 for (Iterator i = statements.values().iterator(); i.hasNext();)
126 results.addAll((List) i.next());
127 return results;
128 }
129
130 /***
131 * Retrieves objects associated with this label
132 * @return List of {@link ObjectStatement}
133 */
134 public List getObjects() {
135 List objects = new ArrayList();
136 for (Iterator i = statements.values().iterator(); i.hasNext();) {
137 for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
138 Statement statement = (Statement) s.next();
139 if (statement instanceof ObjectStatement)
140 objects.add(statement);
141 }
142 }
143 return objects;
144 }
145
146 /***
147 * Retrieves groups associated with this label
148 * @return list of {@link GroupStatement}
149 */
150 public List getGroups() {
151 List groups = new ArrayList();
152 for (Iterator i = statements.values().iterator(); i.hasNext();) {
153 for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
154 Statement statement = (Statement) s.next();
155 if (statement instanceof GroupStatement)
156 groups.add(statement);
157 }
158 }
159 return groups;
160 }
161
162 /***
163 * Retrieves attributes associated with this label
164 * @return list of {@link AttributeStatement}
165 */
166 public List getAttributes() {
167 List attributes = new ArrayList();
168 for (Iterator i = statements.values().iterator(); i.hasNext();) {
169 for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
170 Statement statement = (Statement) s.next();
171 if (statement instanceof AttributeStatement)
172 attributes.add(statement);
173 }
174 }
175 return attributes;
176 }
177
178 /***
179 * Retrieves pointers associated with this label
180 * @return list of {@link PointerStatement}
181 */
182 public List getPointers() {
183 List pointers = new ArrayList();
184 for (Iterator i = statements.values().iterator(); i.hasNext();) {
185 for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
186 Statement statement = (Statement) s.next();
187 if (statement instanceof PointerStatement)
188 pointers.add(statement);
189 }
190 }
191 return pointers;
192 }
193
194 /***
195 * Associates a statement with this label
196 * @param statement to be added to label
197 */
198 public synchronized void addStatement(Statement statement) {
199 labelType = UNDEFINED;
200 List stmnts = (List) statements.get(statement.getIdentifier());
201 if (stmnts == null) {
202 stmnts = new ArrayList();
203 statements.put(statement.getIdentifier(), stmnts);
204 }
205 if (statement instanceof IncludePointer) {
206 stmnts.add(statement);
207 IncludePointer ip = (IncludePointer) statement;
208 setStatus(ip.getLoadStatus());
209 for (Iterator i = ip.getStatements().iterator(); i.hasNext();) {
210 Statement stmnt = (Statement) i.next();
211 List subStmnts = (List) statements.get(stmnt.getIdentifier());
212 if (subStmnts == null) {
213 subStmnts = new ArrayList();
214 statements.put(stmnt.getIdentifier(), subStmnts);
215 }
216 subStmnts.add(stmnt);
217 }
218 }
219 else
220 stmnts.add(statement);
221 }
222
223 /***
224 * Returns the type of label, see {@link LabelType} for the types of label.
225 * @return type of label
226 */
227 public synchronized int getLabelType() {
228
229 if (labelType == UNDEFINED) {
230 List pointers = getPointers();
231 for (Iterator i = pointers.iterator(); i.hasNext() && labelType == UNDEFINED;) {
232 PointerStatement pointer = (PointerStatement) i.next();
233 if (pointer.getPointerType() == PointerType.DATA_LOCATION && pointer.getValue() instanceof Numeric)
234 labelType = ATTACHED;
235 else
236 labelType = DETACHED;
237 }
238
239 if (labelType == UNDEFINED && getObjects("FILE").size() != 0)
240 labelType = COMBINED_DETACHED;
241
242 if (labelType == UNDEFINED)
243 labelType = ATTACHED;
244 }
245
246 return labelType;
247 }
248
249 /***
250 * Sets the type of label
251 * @param labelType of this label
252 */
253 public void setLabelType(int labelType) {
254 this.labelType = labelType;
255 }
256
257 public void setFilename(String filename) {
258 this.filename = filename;
259 }
260
261 public String getFilename() {
262 return filename;
263 }
264
265 public String getStatus() {
266 return status;
267 }
268
269 public void setStatus(String status) {
270
271 if (!UNKNOWN.equals(status)) {
272
273
274
275 if (PASS.equals(status) && UNKNOWN.equals(this.status))
276 this.status = PASS;
277 else if (SKIP.equals(status) && UNKNOWN.equals(this.status))
278 this.status = SKIP;
279 else if (FAIL.equals(status))
280 this.status = FAIL;
281 }
282 }
283
284 public void incrementErrors() {
285 numErrors++;
286 }
287
288 public void incrementWarnings() {
289 numWarnings++;
290 }
291
292 public void incrementErrors(int numErrors) {
293 this.numErrors += numErrors;
294 }
295
296 public void incrementWarnings(int numWarnings) {
297 this.numWarnings += numWarnings;
298 }
299
300 public int getNumErrors() {return numErrors;}
301 public int getNumWarnings() {return numWarnings;}
302 }