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: 2903 $
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 addStatement((Statement) i.next());
211 }
212 else
213 stmnts.add(statement);
214 }
215
216 /***
217 * Returns the type of label, see {@link LabelType} for the types of label.
218 * @return type of label
219 */
220 public synchronized int getLabelType() {
221
222 if (labelType == UNDEFINED) {
223 List pointers = getPointers();
224 for (Iterator i = pointers.iterator(); i.hasNext() && labelType == UNDEFINED;) {
225 PointerStatement pointer = (PointerStatement) i.next();
226 if (pointer.getPointerType() == PointerType.DATA_LOCATION && pointer.getValue() instanceof Numeric)
227 labelType = ATTACHED;
228 else
229 labelType = DETACHED;
230 }
231
232 if (labelType == UNDEFINED && getObjects("FILE").size() != 0)
233 labelType = COMBINED_DETACHED;
234
235 if (labelType == UNDEFINED)
236 labelType = ATTACHED;
237 }
238
239 return labelType;
240 }
241
242 /***
243 * Sets the type of label
244 * @param labelType of this label
245 */
246 public void setLabelType(int labelType) {
247 this.labelType = labelType;
248 }
249
250 public void setFilename(String filename) {
251 this.filename = filename;
252 }
253
254 public String getFilename() {
255 return filename;
256 }
257
258 public String getStatus() {
259 return status;
260 }
261
262 public void setStatus(String status) {
263
264 if (!UNKNOWN.equals(status)) {
265
266
267
268 if (PASS.equals(status) && UNKNOWN.equals(this.status))
269 this.status = PASS;
270 else if (FAIL.equals(status))
271 this.status = FAIL;
272 }
273 }
274
275 public void incrementErrors() {
276 numErrors++;
277 }
278
279 public void incrementWarnings() {
280 numWarnings++;
281 }
282
283 public void incrementErrors(int numErrors) {
284 this.numErrors += numErrors;
285 }
286
287 public void incrementWarnings(int numWarnings) {
288 this.numWarnings += numWarnings;
289 }
290
291 public int getNumErrors() {return numErrors;}
292 public int getNumWarnings() {return numWarnings;}
293 }