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 2915 2007-09-27 13:16:28Z 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: 2915 $
27   * 
28   */
29  public class GroupStatement extends Statement {
30      private Map statements;
31      private List comments;
32      
33      /***
34       * @param lineNumber
35       * @param identifier
36       */
37      public GroupStatement(int lineNumber, String identifier) {
38          super(lineNumber, identifier);
39          statements = new HashMap();
40          comments = new ArrayList();
41      }
42      
43      /***
44       * Retrieves the named attribute.
45       * @param identifier
46       * @return The named AttributeStatement or null if not found.
47       */
48      public AttributeStatement getAttribute(String identifier) {
49          AttributeStatement attribute = null;       
50          if (statements.get(identifier) != null) {
51              for (Iterator i = ((List) statements.get(identifier)).iterator(); i.hasNext() && attribute == null;) {
52                  Statement statement = (Statement) i.next();
53                  if (statement instanceof AttributeStatement)
54                      attribute = (AttributeStatement) statement;
55              }
56          }
57          return attribute;
58      }
59      
60      /***
61       * Retrieves the attributes of this group.
62       * @return The list of AttributeStatment.
63       */
64      public List getAttributes() {
65          List attributes = new ArrayList(); 
66          for (Iterator i = statements.values().iterator(); i.hasNext();) {
67              for (Iterator s = ((List) i.next()).iterator(); s.hasNext();) {
68                  Statement statement = (Statement) s.next();
69                  if (statement instanceof AttributeStatement)
70                      attributes.add(statement);
71              }
72          }
73          return attributes;
74      }
75  
76      public void addStatement(Statement statement) {
77          List stmnts = (List) statements.get(statement.getIdentifier());
78          if (stmnts == null) {
79              stmnts = new ArrayList();
80              statements.put(statement.getIdentifier(), stmnts);
81          }
82          if (statement instanceof IncludePointer) {
83              stmnts.add(statement);
84              for (Iterator i = ((IncludePointer) statement).getStatements().iterator(); i.hasNext();)
85                  addStatement((Statement) i.next());
86          }
87          else if (statement instanceof PointerStatement || statement instanceof AttributeStatement)
88              stmnts.add(statement);
89          //else TODO Throw error
90      }
91  
92      public boolean hasAttribute(String identifier) {
93          if (getAttribute(identifier) == null)
94              return false;
95          return true;
96      }
97     
98      public void attachComment(CommentStatement comment) {
99          comments.add(comment);
100     }
101  
102     public List getStatements() {
103         List statementList = new ArrayList();
104         for (Iterator i = statements.values().iterator(); i.hasNext();) {
105             statementList.addAll((List) i.next());
106         }
107         return statementList;
108     }
109 }