1
2
3
4
5
6
7
8
9
10
11
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
49
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
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
133 }
134 return paddedKeyword;
135 }
136 }