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: ObjectStatement.java 2902 2007-09-27 13:05:20Z pramirez $ 
14  //
15  
16  package gov.nasa.pds.tools.label;
17  
18  import java.util.List;
19  import java.util.ArrayList;
20  import java.util.Map;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  
24  /***
25   * @author pramirez
26   * @version $Revision: 2902 $
27   * 
28   */
29  public class ObjectStatement extends Statement {
30      private Map statements;
31      private List comments;
32  
33      /***
34       * Constructs a new object statement with no attributes or nested objects
35       * @param lineNumber Line number of the statement.
36       * @param identifier Identifier for the statement.
37       */
38      public ObjectStatement(int lineNumber, String identifier) {
39          this(lineNumber, identifier, new HashMap());
40      }
41      
42      /***
43       * Constructs an ObjectStatement with only an identifier
44       * @param identifier Identifier of the statement
45       */
46      public ObjectStatement(String identifier) {
47          this(-1, identifier);
48      }
49      
50      /***
51       * Constructs an ObjectStatement
52       * @param lineNumber Line number of statement
53       * @param identifier Identifier of statement
54       * @param statements Map of {@link Statement} associated with this object
55       */
56      public ObjectStatement(int lineNumber, String identifier, Map statements) {
57          super(lineNumber, identifier);
58          this.statements = statements;
59          comments = new ArrayList();
60      }
61      
62      /***
63       * Retrieves the list of attributes associated with the ObjectStatement
64       * @return The list of AttributeStatement
65       */
66      public List getAttributes() {
67          List attributes = new ArrayList(); 
68          for (Iterator i = statements.values().iterator(); i.hasNext();) {
69              for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
70                  Statement statement = (Statement) s.next();
71                  if (statement instanceof AttributeStatement)
72                      attributes.add(statement);
73              }
74          }
75          return attributes;
76      }
77      
78      /***
79       * Retrieves pointers associated with this object
80       * @return list of {@link PointerStatement}
81       */
82      public List getPointers() {
83          List pointers = new ArrayList();
84          for (Iterator i = statements.values().iterator(); i.hasNext();) {
85              for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
86                  Statement statement = (Statement) s.next();
87                  if (statement instanceof PointerStatement)
88                      pointers.add(statement);
89              }
90          }
91          return pointers;
92      }
93      
94      /***
95       * Looks to see if this object contains a pointer with the given identifier
96       * @param identifier of pointer statement to look for
97       * @return flag indicating whether or not the pointer was found
98       */
99      public boolean hasPointer(String identifier) {
100         if (statements.get(identifier) != null) {
101             for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
102                 Statement statement = (Statement) i.next();
103                 if (statement instanceof PointerStatement)
104                     return true;
105             }
106         }
107         return false;
108     }
109     
110     /***
111      * Retrieves the named attribute
112      * @param identifier
113      * @return The named AttributeStatement or null if not found
114      */
115     public AttributeStatement getAttribute(String identifier) {
116         AttributeStatement attribute = null;       
117         if (statements.get(identifier) != null) {
118             for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext() && attribute == null;) {
119                 Statement statement = (Statement) i.next();
120                 if (statement instanceof AttributeStatement)
121                     attribute = (AttributeStatement) statement;
122             }
123         }
124         return attribute;
125     }
126     
127     /***
128      * Retrieves the list of objects associated with this object
129      * @return The list of ObjectStatement
130      */
131     public List getObjects() {
132         List objects = new ArrayList(); 
133         for (Iterator i = statements.values().iterator(); i.hasNext();) {
134             for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
135                 Statement statement = (Statement) s.next();
136                 if (statement instanceof ObjectStatement)
137                     objects.add(statement);
138             }
139         }
140         return objects;
141     }
142     
143     /***
144      * Retrieves the named object
145      * @param identifier
146      * @return The {@link List} of named objects
147      */
148     public List getObjects(String identifier) {
149         List objects = new ArrayList();
150         if (statements.get(identifier) != null) {
151             for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
152                 Statement statement = (Statement) i.next();
153                 if (statement instanceof ObjectStatement)
154                     objects.add(statement);
155             }
156         }
157         return objects;
158     }
159     
160     /***
161      * Associates a statement with this object
162      * @param statement to be added to object
163      */
164     public void addStatement(Statement statement) { 
165         List stmnts = (List) statements.get(statement.getIdentifier());
166         if (stmnts == null) {
167             stmnts = new ArrayList();
168             statements.put(statement.getIdentifier(), stmnts);
169         }
170         if (statement instanceof IncludePointer) {
171             stmnts.add(statement);
172             for (Iterator i = ((IncludePointer) statement).getStatements().iterator(); i.hasNext();)
173                 addStatement((Statement) i.next());
174         }
175         else 
176             stmnts.add(statement);
177     }
178     
179     public boolean hasAttribute(String identifier) {
180         if (getAttribute(identifier) == null)
181             return false;
182         return true;
183     }
184     
185     public boolean hasObject(String identifier) {
186         if (getObjects(identifier).size() == 0)
187             return false;
188         return true;
189     }
190 
191     public void attachComment(CommentStatement comment) {
192         comments.add(comment);
193     }
194     
195     public List getStatements() {
196         List statementList = new ArrayList();
197         for (Iterator i = statements.values().iterator(); i.hasNext();) {
198             statementList.addAll((List) i.next());
199         }
200         return statementList;
201     }
202 
203 }