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: GroupStatement.java 3355 2008-07-16 17:35:41Z pramirez $ 
14  //
15  
16  package gov.nasa.pds.tools.label;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  import java.util.ArrayList;
21  import java.util.Map;
22  import java.util.HashMap;
23  
24  /***
25   * @author pramirez
26   * @version $Revision: 3355 $
27   * 
28   */
29  public class GroupStatement extends Statement {
30      private Map statements;
31      private List comments;
32      
33      /***
34       * Constructs an empty group statement
35       * @param lineNumber at which this statement occurs
36       * @param identifier of the statement
37       */
38      public GroupStatement(int lineNumber, String identifier) {
39      	this(lineNumber, identifier, new HashMap());
40      }
41      
42      /***
43       * Constructs a group statement that contains the given statements
44       * @param lineNumber at which this statement occurs
45       * @param identifier of the statement
46       * @param statements contained within this group statement
47       */
48      public GroupStatement(int lineNumber, String identifier, Map statements) {
49      	super(lineNumber, identifier);
50      	this.statements = statements;
51      	comments = new ArrayList();
52      }
53      
54      /***
55       * Retrieves the named attribute.
56       * @param identifier
57       * @return The named AttributeStatement or null if not found.
58       */
59      public AttributeStatement getAttribute(String identifier) {
60          AttributeStatement attribute = null;       
61          if (statements.get(identifier) != null) {
62              for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext() && attribute == null;) {
63                  Statement statement = (Statement) i.next();
64                  if (statement instanceof AttributeStatement)
65                      attribute = (AttributeStatement) statement;
66              }
67          }
68          return attribute;
69      }
70      
71      /***
72       * Retrieves the attributes of this group.
73       * @return The list of AttributeStatment.
74       */
75      public List getAttributes() {
76          List attributes = new ArrayList(); 
77          for (Iterator i = statements.values().iterator(); i.hasNext();) {
78              for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
79                  Statement statement = (Statement) s.next();
80                  if (statement instanceof AttributeStatement)
81                      attributes.add(statement);
82              }
83          }
84          return attributes;
85      }
86  
87      public void addStatement(Statement statement) {
88          List stmnts = (List) statements.get(statement.getIdentifier());
89          if (stmnts == null) {
90              stmnts = new ArrayList();
91              statements.put(statement.getIdentifier(), stmnts);
92          }
93          if (statement instanceof IncludePointer) {
94          	stmnts.add(statement);
95              IncludePointer ip = (IncludePointer) statement;
96              for (Iterator i = ip.getStatements().iterator(); i.hasNext();) {
97              	Statement stmnt = (Statement) i.next();
98              	List subStmnts = (List) statements.get(stmnt.getIdentifier());
99                  if (subStmnts == null) {
100                     subStmnts = new ArrayList();
101                     statements.put(stmnt.getIdentifier(), subStmnts);
102                 }
103                 subStmnts.add(stmnt);
104             }
105         }
106         else if (statement instanceof PointerStatement || statement instanceof AttributeStatement)
107             stmnts.add(statement);
108         //else TODO Throw error
109     }
110 
111     public boolean hasAttribute(String identifier) {
112         if (getAttribute(identifier) == null)
113             return false;
114         return true;
115     }
116    
117     public void attachComment(CommentStatement comment) {
118         comments.add(comment);
119     }
120  
121     public List getStatements() {
122         List statementList = new ArrayList();
123         for (Iterator i = statements.values().iterator(); i.hasNext();) {
124             statementList.addAll((List) i.next());
125         }
126         return statementList;
127     }
128 }