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: PointerStatement.java 3836 2009-01-14 18:50:02Z pramirez $ 
14  //
15  
16  package gov.nasa.pds.tools.label;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Iterator;
21  
22  /***
23   * This class is the object representation of a pointer statement in a label.
24   * 
25   * @author pramirez
26   * @version $Revision: 3836 $
27   * 
28   */
29  public class PointerStatement extends Statement implements PointerType {
30      protected Value value;
31      private CommentStatement comment;
32      private int pointerType;
33      protected boolean externalReference;
34  
35      /***
36       * Constructs essentially a null pointer
37       * @param pointerType indicates whether it is data location, include, or description pointer
38       * @param lineNumber at which the statement occurs
39       * @param identifier of the statement
40       */
41      protected PointerStatement(int pointerType, int lineNumber, String identifier) {
42          this(pointerType, lineNumber, identifier, null);
43      }
44      
45      /***
46       * Constructs a pointer with a value on the right hand side
47       * @param pointerType indicates whether it is data location, include, or description pointer
48       * @param lineNumber at which the statement occurs
49       * @param identifier of the statement
50       * @param value of the assignment
51       */
52      protected PointerStatement(int pointerType, int lineNumber, String identifier, Value value) {
53          super(lineNumber, identifier);
54          this.value = value; 
55          this.pointerType = pointerType;
56          if (value instanceof TextString || value instanceof Sequence)
57  			this.externalReference = true;
58          comment = null;  
59      }
60      
61      /***
62       * Constructs a pointer with an unknown line number.
63       * @param identifier of the statement
64       * @param value of the assignment
65       */
66      protected PointerStatement(String identifier, Value value) {
67          this(UNDEFINED, -1, identifier, value);
68      }
69      
70      /***
71       * Returns the value portion (right hand side) of the statement.
72       * @return value
73       */
74      public Value getValue() { 
75          return value;
76      }
77  
78      /***
79       * Attaches a comment to this pointer
80       * @param comment that occurs on same line as this pointer statement
81       */
82      public void attachComment(CommentStatement comment) {
83          this.comment = comment;
84      }
85      
86      /***
87       * Returns the comment that occurs on the same line as this pointer assigment
88       * @return comment
89       */
90      public CommentStatement getComment() {
91          return comment;
92      }
93      
94      /***
95       * Indicates the type of pointer that this pointer statement represents. See {@link PointerType}
96       * @return type of pointer
97       */
98      public int getPointerType() {
99          return pointerType;
100     }
101     
102     /***
103      * Indicates whether or not the pointer makes reference to an external file.
104      * @return flag indicating whether an external reference is made
105      */
106     public boolean hasExternalReference() {
107     	return externalReference;
108     }
109     
110     public boolean hasMultipleReferences() {
111     	if (value instanceof Set) 
112     		return true;
113     	return false;
114     }
115 
116     /***
117      * Gets the name of the external file that this pointer references
118      * @return filename associated with this pointer. Returns null if this is not 
119      * an external reference or there is multiple values for external references
120      */
121     public String getExternalFileReference() {
122     	if (!externalReference || hasMultipleReferences())
123     		return null;
124     	
125         String filename = "";
126         
127         //The name of the file will be the first element in the sequence or the value if not 
128         //contained in a sequence
129         if (value instanceof Sequence)
130             filename = ((Sequence) value).get(0).toString();
131         else
132             filename = value.toString();
133         
134         return filename;
135     }
136     
137     public List getExternalFileReferences() {
138     	if (!externalReference)
139     		return null;
140     	
141     	List fileReferences = new ArrayList();
142     	if (!hasMultipleReferences()) {
143     		fileReferences.add(getExternalFileReference());
144     	} else {
145     	    for (Iterator i = ((Set) value).iterator(); i.hasNext();) {
146 	    		Value value = (Value) i.next();
147 	    		fileReferences.add(value.toString());
148 	        }
149     	}
150     	
151     	return fileReferences;
152     }
153 }