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 3380 2008-07-22 17:40:03Z 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: 3380 $
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       * Retrieves groups associated with this object
96       * @return list of {@link GroupStatement}
97       */
98      public List getGroups() {
99          List groups = new ArrayList();
100         for (Iterator i = statements.values().iterator(); i.hasNext();) {
101             for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
102                 Statement statement = (Statement) s.next();
103                 if (statement instanceof GroupStatement)
104                     groups.add(statement);
105             }
106         }
107         return groups;
108     }
109     
110     /***
111      * Looks to see if this object contains a pointer with the given identifier
112      * @param identifier of pointer statement to look for
113      * @return flag indicating whether or not the pointer was found
114      */
115     public boolean hasPointer(String identifier) {
116         if (statements.get(identifier) != null) {
117             for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
118                 Statement statement = (Statement) i.next();
119                 if (statement instanceof PointerStatement)
120                     return true;
121             }
122         }
123         return false;
124     }
125     
126     /***
127      * Retrieves the named attribute
128      * @param identifier
129      * @return The named AttributeStatement or null if not found
130      */
131     public AttributeStatement getAttribute(String identifier) {
132         AttributeStatement attribute = null;       
133         if (statements.get(identifier) != null) {
134             for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext() && attribute == null;) {
135                 Statement statement = (Statement) i.next();
136                 if (statement instanceof AttributeStatement)
137                     attribute = (AttributeStatement) statement;
138             }
139         }
140         return attribute;
141     }
142     
143     /***
144      * Retrieves the list of objects associated with this object
145      * @return The list of ObjectStatement
146      */
147     public List getObjects() {
148         List objects = new ArrayList(); 
149         for (Iterator i = statements.values().iterator(); i.hasNext();) {
150             for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
151                 Statement statement = (Statement) s.next();
152                 if (statement instanceof ObjectStatement)
153                     objects.add(statement);
154             }
155         }
156         return objects;
157     }
158     
159     /***
160      * Retrieves the named object
161      * @param identifier
162      * @return The {@link List} of named objects
163      */
164     public List getObjects(String identifier) {
165         List objects = new ArrayList();
166         if (statements.get(identifier) != null) {
167             for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
168                 Statement statement = (Statement) i.next();
169                 if (statement instanceof ObjectStatement)
170                     objects.add(statement);
171             }
172         }
173         return objects;
174     }
175     
176     /***
177      * Retrieves the named group
178      * @param identifier of the group
179      * @return the {@link List} of named groups
180      */
181     public List getGroups(String identifier) {
182     	List groups = new ArrayList();
183     	if (statements.get(identifier) != null) {
184     		for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext();) {
185     			Statement statement = (Statement) i.next();
186     			if (statement instanceof GroupStatement)
187     				groups.add(statement);
188     		}
189     	}
190     	return groups;
191     }
192     
193     /***
194      * Associates a statement with this object
195      * @param statement to be added to object
196      */
197     public void addStatement(Statement statement) { 
198         List stmnts = (List) statements.get(statement.getIdentifier());
199         if (stmnts == null) {
200             stmnts = new ArrayList();
201             statements.put(statement.getIdentifier(), stmnts);
202         }
203         if (statement instanceof IncludePointer) {
204         	stmnts.add(statement);
205             IncludePointer ip = (IncludePointer) statement;
206             for (Iterator i = ip.getStatements().iterator(); i.hasNext();) {
207             	Statement stmnt = (Statement) i.next();
208             	List subStmnts = (List) statements.get(stmnt.getIdentifier());
209                 if (subStmnts == null) {
210                     subStmnts = new ArrayList();
211                     statements.put(stmnt.getIdentifier(), subStmnts);
212                 }
213                 subStmnts.add(stmnt);
214             }
215         }
216         else 
217             stmnts.add(statement);
218     }
219     
220     public boolean hasAttribute(String identifier) {
221         return (getAttribute(identifier) == null) ? false : true;
222     }
223     
224     public boolean hasObject(String identifier) {
225         return (getObjects(identifier).size() == 0) ? false : true;
226     }
227     
228     public boolean hasGroup(String identifier) {
229     	return (getGroups(identifier).size() == 0) ? false : true;
230     }
231 
232     public void attachComment(CommentStatement comment) {
233         comments.add(comment);
234     }
235     
236     public List getStatements() {
237         List statementList = new ArrayList();
238         for (Iterator i = statements.values().iterator(); i.hasNext();) {
239             statementList.addAll((List) i.next());
240         }
241         return statementList;
242     }
243 
244 }