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.Iterator;
16  import java.util.LinkedHashMap;
17  
18  import gov.nasa.pds.ltdt.gui.util.Utility;
19  import gov.nasa.pds.ltdt.label.reformat.CommentStatementFoundException;
20  import gov.nasa.pds.ltdt.label.reformat.KeywordTooBigException;
21  import gov.nasa.pds.tools.label.ObjectStatement;
22  
23  /***
24   * Class used to provide a string representation of an OBJECT in a PDS label.
25   * 
26   * @author mcayanan
27   *
28   */
29  public class PrettyObjectStatement extends ObjectStatement implements PrettyStatement {
30  
31  	private static String NEWLINE = Character.toString((char)0x0d) + Character.toString((char)0x0a);
32  	
33  	/***
34  	 * Constructs the object.
35  	 * 
36  	 * @param identifier The name that identifies this object.
37  	 */
38  	public PrettyObjectStatement(String identifier) {
39  		super(-1, identifier, new LinkedHashMap());
40  	}
41  	
42  	/***
43  	 * Returns the identifier.
44  	 * 
45  	 */
46  	public String getIdentifier() {
47  		StringBuffer buffer = new StringBuffer(identifier);
48  		//TODO: Remove the index portion. Can be omitted once we can find a
49  		//clean way to store statements with the same name within an object.
50  		try {
51  			return buffer.delete(identifier.lastIndexOf("_Line_"), identifier.length()).toString();
52  		} catch(StringIndexOutOfBoundsException i) {
53  			return identifier;
54  		}
55  		
56  	}
57  	
58  	/***
59  	 * Return a string representation of the OBJECT. Nested statements
60  	 * will be indented appropriately.
61  	 * 
62  	 * @return A string representation of the OBJECT.
63  	 * 
64  	 */
65  	public String toString() {
66  		return toString((short) 0, 0);
67  	}
68  	
69  	/***
70  	 * Return a string representation of the OBJECT. Nested statements
71  	 * will be indented appropriately.
72  	 * 
73  	 * @param indentLength The number of spaces to indent the OBJECT.
74  	 * 
75  	 * @return A string representation of the OBJECT.
76  	 * 
77  	 */
78  	public String toString(short indentLength) {
79  		return toString(indentLength, 0);
80  	}
81  	
82  	/***
83  	 * Return a string representation of the OBJECT. Nested statements
84  	 * will be indented appropriately.
85  	 * 
86  	 * @param indentLength The number of spaces to indent the OBJECT.
87  	 * @param equalsPosition Where the equals sign will be located.
88  	 * 
89  	 * @return A string representation of the OBJECT.
90  	 * 
91  	 */
92  	public String toString(short indentLength, int equalsPosition) {
93  		String result = "";
94  		String paddedKeyword = padKeyword("OBJECT", indentLength, equalsPosition);
95  		result = paddedKeyword + " = " + getIdentifier() + NEWLINE;
96  		for(Iterator i=getStatements().listIterator(); i.hasNext();) {
97  			PrettyStatement s = (PrettyStatement) i.next();
98  			try {
99  				if(s instanceof PrettyCommentStatement)
100 					throw new CommentStatementFoundException("Comment statement found.");
101 				//Add whitespace in between a nested GROUP or OBJECT
102 				else if(s instanceof PrettyObjectStatement ||
103 						s instanceof PrettyGroupStatement) {
104 					result += NEWLINE;
105 				}
106 				result += s.toString((short)(indentLength+2), equalsPosition) + NEWLINE;
107 			} catch(CommentStatementFoundException c) {
108 				result += s.toString((short)(indentLength+2)) + NEWLINE;
109 			}
110 		}
111 		String end = padKeyword("END_OBJECT", indentLength, equalsPosition);
112 		result += end + " = " + getIdentifier();
113 		return result;
114 	}
115 	
116 	/***
117 	 * Returns the number of spaces to add between the keyword and equals sign
118 	 * @param indentLength
119 	 * @param equalsPosition
120 	 * @return
121 	 */
122 	private String padKeyword(String keyword, int indentLength, int equalsPosition) {
123 		String paddedKeyword="";
124 		try {
125 			int spacesToAdd = equalsPosition - (indentLength + keyword.length() + 2);
126 			paddedKeyword = Utility.addLeadingWhiteSpace(keyword, indentLength);
127 			if(spacesToAdd <= 0) {
128 				throw new KeywordTooBigException("Keyword too big for equals position");
129 			}
130 			paddedKeyword = Utility.addTrailingWhiteSpace(paddedKeyword, spacesToAdd);
131 		} catch(KeywordTooBigException k) {
132 			//For now, we don't do anything
133 		}
134 		return paddedKeyword;
135 	}
136 }