View Javadoc

1   //Copyright 2007-2008, 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  package gov.nasa.pds.ltdt.label.statement;
14  
15  import java.util.ArrayList;
16  import java.util.Iterator;
17  import java.util.LinkedHashMap;
18  import java.util.List;
19  
20  import gov.nasa.pds.ltdt.gui.util.Utility;
21  import gov.nasa.pds.ltdt.label.reformat.CommentStatementFoundException;
22  import gov.nasa.pds.ltdt.label.reformat.KeywordTooBigException;
23  import gov.nasa.pds.tools.label.GroupStatement;
24  import gov.nasa.pds.tools.label.IncludePointer;
25  import gov.nasa.pds.tools.label.Statement;
26  
27  /***
28   * Class used to provide a string representation of a GROUP in a PDS label.
29   * 
30   * @author mcayanan
31   *
32   */
33  public class PrettyGroupStatement extends GroupStatement implements PrettyStatement {
34  	
35  	private LinkedHashMap statements;
36  	private static String NEWLINE = Character.toString((char)0x0d) + Character.toString((char)0x0a);
37  	
38  	/***
39  	 * Constructs the PrettyGroupStatement.
40  	 * 
41  	 * @param identifier The name that associates this object.
42  	 */
43  	public PrettyGroupStatement(String identifier) {
44  		this(-1, identifier, new LinkedHashMap());
45  	}
46  	
47  	/***
48  	 * Constructs the PrettyGroupStatement.
49  	 * 
50  	 * @param lineNumber The line number of the location of the group object
51  	 * in the label.
52  	 * @param identifier The name that identifies this object.
53  	 * @param statements A linked hash map of statements associated with this
54  	 * object.
55  	 */
56  	public PrettyGroupStatement(int lineNumber, String identifier, LinkedHashMap statements) {
57  		super(-1, identifier, statements);
58  		this.statements = statements;
59  	}
60  	
61  	/***
62  	 * Returns the identifier.
63  	 * 
64  	 */
65  	public String getIdentifier() {
66  		StringBuffer buffer = new StringBuffer(identifier);
67  		//TODO: Remove the index portion. Can be omitted once we can find a
68  		//clean way to store statements with the same name within a group.
69  		try {
70  			return buffer.delete(identifier.lastIndexOf("_Line_"), identifier.length()).toString();
71  		}catch(StringIndexOutOfBoundsException i) {
72  			return identifier;
73  		}
74  	}
75  	
76      /***
77       * Associates a statement with this object. This overwrites the parent
78       * method in that it allows groups and objects to be stored within a
79       * group object.
80       * 
81       * @param statement to be added to object
82       */
83      public void addStatement(Statement statement) { 
84          List stmnts = (List) statements.get(statement.getIdentifier());
85          if (stmnts == null) {
86              stmnts = new ArrayList();
87              statements.put(statement.getIdentifier(), stmnts);
88          }
89          if (statement instanceof IncludePointer) {
90              stmnts.add(statement);
91              for (Iterator i = ((IncludePointer) statement).getStatements().iterator(); i.hasNext();)
92                  addStatement((Statement) i.next());
93          }
94          else 
95              stmnts.add(statement);
96      }
97  	
98  	/***
99  	 * Return a string representation of the GROUP. Nested statements
100 	 * will be indented appropriately.
101 	 * 
102 	 * @param indentLength The number of spaces to indent the GROUP.
103 	 * 
104 	 * @return A string representation of the GROUP.
105 	 * 
106 	 */
107 	public String toString(short indentLength) {
108 		return toString(indentLength, 0);	
109 	}
110 
111 	/***
112 	 * Return a string representation of the GROUP. Nested statements
113 	 * will be indented appropriately.
114 	 * 
115 	 * @param indentLength The number of spaces to indent the GROUP.
116 	 * @param equalsPosition Where the equals sign will be located.
117 	 * 
118 	 * @return A string representation of the GROUP.
119 	 * 
120 	 */
121 	public String toString(short indentLength, int equalsPosition) {
122 		String result = "";
123 		String paddedKeyword = padKeyword("GROUP", indentLength, equalsPosition);
124 		result = paddedKeyword + " = " + getIdentifier() + NEWLINE;
125 		for(Iterator i=getStatements().listIterator(); i.hasNext();) {
126 			PrettyStatement s = (PrettyStatement) i.next();
127 			try {
128 				if(s instanceof PrettyCommentStatement)
129 					throw new CommentStatementFoundException("Comment statement found.");
130 				//Add whitespace in between a nested GROUP or OBJECT
131 				else if(s instanceof PrettyObjectStatement ||
132 						s instanceof PrettyGroupStatement) {
133 					result += NEWLINE;
134 				}
135 				result += s.toString((short)(indentLength+2), equalsPosition) + NEWLINE;
136 				
137 			}catch(CommentStatementFoundException c) {
138 				result += s.toString((short)(indentLength+2)) + NEWLINE;
139 			}
140 		}
141 		String end = padKeyword("END_GROUP", indentLength, equalsPosition);
142 		result += end + " = " + getIdentifier();
143 		return result;
144 	}
145 	
146 	/***
147 	 * Returns the number of spaces to add between the keyword and equals sign
148 	 * 
149 	 * @param keyword The keyword to pad with spaces.
150 	 * @param indentLength The number of spaces to indent the keyword.
151 	 * @param equalsPosition Where the equals sign will be located.
152 	 * 
153 	 * @return The padded keyword. If the equals position is too small, then
154 	 * the keyword will be returned.
155 	 */
156 	private String padKeyword(String keyword, int indentLength, int equalsPosition) {
157 		String paddedKeyword="";
158 		try {
159 			int spacesToAdd = equalsPosition - (indentLength + keyword.length() + 2);
160 			paddedKeyword = Utility.addLeadingWhiteSpace(keyword, indentLength);
161 			if(spacesToAdd <= 0) {
162 				throw new KeywordTooBigException("Keyword too big for equals position");
163 			}
164 			paddedKeyword = Utility.addTrailingWhiteSpace(paddedKeyword, spacesToAdd);
165 		} catch(KeywordTooBigException k) {
166 			//For now, we don't do anything
167 		}
168 		return paddedKeyword;
169 	}
170 }