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: Label.java 2608 2007-04-19 15:23:06Z pramirez $ 
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: 2608 $
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      
38      /***
39       * Constructs an object representation of a PDS label.
40       *
41       */
42      public Label() {
43          statements = new HashMap();
44          labelType = UNDEFINED;
45          filename = null;
46          status = Status.UNKNOWN;
47      }
48  
49      /***
50       * Retrieves a statement with the identifier
51       * @param identifier Identifies the statement to retrieve
52       * @return The named statement or null if not found
53       */
54      public List getStatement(String identifier) {
55          return (List) statements.get(identifier);
56      }
57      
58      /***
59       * Retrieves the attribute with the identifier or null if not found
60       * @param identifier of attribute to find
61       * @return attribute or null
62       */
63      public AttributeStatement getAttribute(String identifier) {
64          AttributeStatement attribute = null;
65          
66          if (statements.get(identifier) != null) {
67              for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext() && attribute == null;) {
68                  Statement statement = (Statement) i.next();
69                  if (statement instanceof AttributeStatement)
70                      attribute = (AttributeStatement) statement;
71              }
72          }
73          
74          return attribute;
75      }
76      
77      /***
78       * Retrieves the groups with the identifier or null if not found
79       * @param identifier of group to find
80       * @return {@link List} of {@link GroupStatement}
81       */
82      public List getGroups(String identifier) {
83          List groups = new ArrayList();
84          
85          if (statements.get(identifier) != null) {
86              for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
87                  Statement statement = (Statement) i.next();
88                  if (statement instanceof GroupStatement)
89                      groups.add(statement);
90              }
91          }
92          
93          return groups;
94      }
95    
96      /***
97       * Retrieves the object with the identifier or null if not found
98       * @param identifier of object to find
99       * @return {@link List} of {@link ObjectStatement}
100      */
101     public List getObjects(String identifier) {
102         List objects = new ArrayList();
103         
104         if (statements.get(identifier) != null) {
105             for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
106                 Statement statement = (Statement) i.next();
107                 if (statement instanceof ObjectStatement)
108                     objects.add(statement);
109             }
110         }
111         
112         return objects;
113     }
114  
115     /***
116      * Retrieves the statements associated with this label
117      * @return {@link List} of {@link Statement}
118      */
119     public List getStatements() { 
120         List results = new ArrayList();
121         for (Iterator i = statements.values().iterator(); i.hasNext();)
122             results.addAll((List) i.next());
123         return results;
124     }
125  
126     /***
127      * Retrieves objects associated with this label
128      * @return List of {@link ObjectStatement}
129      */
130     public List getObjects() {
131         List objects = new ArrayList(); 
132         for (Iterator i = statements.values().iterator(); i.hasNext();) {
133             for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
134                 Statement statement = (Statement) s.next();
135                 if (statement instanceof ObjectStatement)
136                     objects.add(statement);
137             }
138         }
139         return objects;
140     }
141     
142     /***
143      * Retrieves groups associated with this label
144      * @return list of {@link GroupStatement}
145      */
146     public List getGroups() {
147         List groups = new ArrayList(); 
148         for (Iterator i = statements.values().iterator(); i.hasNext();) {
149             for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
150                 Statement statement = (Statement) s.next();
151                 if (statement instanceof GroupStatement)
152                     groups.add(statement);
153             }
154         }
155         return groups;
156     }
157     
158     /***
159      * Retrieves attributes associated with this label
160      * @return list of {@link AttributeStatement}
161      */
162     public List getAttributes() {
163         List attributes = new ArrayList(); 
164         for (Iterator i = statements.values().iterator(); i.hasNext();) {
165             for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
166                 Statement statement = (Statement) s.next();
167                 if (statement instanceof AttributeStatement)
168                     attributes.add(statement);
169             }
170         }
171         return attributes;
172     }
173     
174     /***
175      * Retrieves pointers associated with this label
176      * @return list of {@link PointerStatement}
177      */
178     public List getPointers() {
179         List pointers = new ArrayList();
180         for (Iterator i = statements.values().iterator(); i.hasNext();) {
181             for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
182                 Statement statement = (Statement) s.next();
183                 if (statement instanceof PointerStatement)
184                     pointers.add(statement);
185             }
186         }
187         return pointers;
188     }
189  
190     /***
191      * Associates a statement with this label
192      * @param statement to be added to label
193      */
194     public synchronized void addStatement(Statement statement) { 
195         labelType = UNDEFINED;
196         List stmnts = (List) statements.get(statement.getIdentifier());
197         if (stmnts == null) {
198             stmnts = new ArrayList();
199             statements.put(statement.getIdentifier(), stmnts);
200         }
201         if (statement instanceof IncludePointer) {
202             stmnts.add(statement);
203             IncludePointer ip = (IncludePointer) statement;
204             setStatus(ip.getLoadStatus());
205             for (Iterator i = ip.getStatements().iterator(); i.hasNext();)
206                 addStatement((Statement) i.next());
207         }
208         else 
209             stmnts.add(statement);
210     }
211 
212     /***
213      * Returns the type of label, see {@link LabelType} for the types of label.
214      * @return type of label
215      */
216     public synchronized int getLabelType() {
217         //Check to see if flag has been set if not figure it out and set it
218         if (labelType == UNDEFINED) {
219             List pointers = getPointers();
220             for (Iterator i = pointers.iterator(); i.hasNext() && labelType == UNDEFINED;) {
221                 PointerStatement pointer = (PointerStatement) i.next();
222                 if (pointer.getPointerType() == PointerType.DATA_LOCATION && pointer.getValue() instanceof Numeric)
223                     labelType = ATTACHED;
224                 else
225                     labelType = DETACHED;
226             }
227             //If label type is still undefined we need to check if its a combined detached label
228             if (labelType == UNDEFINED && getObjects("FILE").size() != 0)
229                 labelType = COMBINED_DETACHED;
230             //If all fails default to attached label
231             if (labelType == UNDEFINED)
232                 labelType = ATTACHED;
233         }
234         
235         return labelType;
236     }
237     
238     /***
239      * Sets the type of label
240      * @param labelType of this label
241      */
242     public void setLabelType(int labelType) {
243         this.labelType = labelType;
244     }
245     
246     public void setFilename(String filename) {
247         this.filename = filename;
248     }
249     
250     public String getFilename() {
251         return filename;
252     }
253     
254     public String getStatus() {
255         return status;
256     }
257     
258     public void setStatus(String status) {
259         //Make sure we aren't trying to set status to unknown
260         if (!UNKNOWN.equals(status)) {
261            //Set to pass if unknown 
262            //Set to fail if that is the status being passed in
263            //Drop everything else
264            if (PASS.equals(status) && UNKNOWN.equals(this.status))
265               this.status = PASS;
266            else if (FAIL.equals(status))
267               this.status = FAIL;
268         }
269     }
270 }