1
2
3
4
5
6
7
8
9
10
11
12
13 package gov.nasa.pds.ltdt.label.statement;
14
15 import gov.nasa.pds.ltdt.gui.util.Utility;
16 import gov.nasa.pds.ltdt.label.reformat.KeywordTooBigException;
17 import gov.nasa.pds.ltdt.label.reformat.NoIndexFoundException;
18 import gov.nasa.pds.tools.label.AttributeStatement;
19
20 /***
21 * Class to provide a "pretty print" string representation of
22 * an attribute statement in a PDS label.
23 *
24 * @author mcayanan
25 *
26 */
27 public class PrettyAttributeStatement extends AttributeStatement implements PrettyStatement {
28
29 private final static int MAX_LINE = 78;
30 private static String NEWLINE = Character.toString((char)0x0d) + Character.toString((char)0x0a);
31
32 /***
33 * Constructs this object with no value.
34 *
35 * @param identifier The data element name.
36 */
37 public PrettyAttributeStatement(String identifier) {
38 this(identifier, null);
39 }
40
41 /***
42 * Constructs this object with an identifier and value.
43 *
44 * @param identifier The data element name.
45 * @param value The data element value.
46 */
47 public PrettyAttributeStatement(String identifier, Value value) {
48 super(identifier, value);
49 }
50
51 /***
52 * Returns a string representation of the attribute with no indentation.
53 * The equals position will be located 2 spaces after the identifier.
54 *
55 * @return A string representation of the attribute.
56 */
57 public String toString() {
58 return toString((short)0,0);
59 }
60
61 /***
62 * Returns a string representation of the attribute with a given
63 * indentation.
64 *
65 * @param indentLength The number of spaces to indent the statement.
66 *
67 * @return A string representation of the attribute.
68 */
69 public String toString(short indentLength) {
70 return toString(indentLength, 0);
71 }
72
73 /***
74 * Returns a string representation of the attribute.
75 *
76 * @param indentLength The number of spaces to indent the statement.
77 * @param equalsPosition Where to place the equals sign.
78 *
79 * @return A string representation of the attribute.
80 */
81 public String toString(short indentLength, int equalsPosition) {
82 String result = null;
83
84 String paddedKeyword = padKeyword(getIdentifier(), indentLength, equalsPosition);
85
86
87 try {
88 if(getValue() == null) {
89 throw new NoValueException("No value found");
90 }
91 String value = getValue().toString();
92 result = paddedKeyword + " = " + value;
93 result = wordWrap(result, indentLength);
94 } catch(NoValueException v) {
95 result = paddedKeyword;
96 }
97 return result;
98 }
99
100 /***
101 * Performs word wrapping
102 *
103 * @param text The text to word wrap.
104 *
105 * @return The string with word wrapping.
106 */
107 private String wordWrap(String text, int indentLength) {
108 char delimiters[] = {' ', ','};
109 int end = 0;
110
111 if(text.length() <= MAX_LINE)
112 return text;
113
114 StringBuffer textCopy = new StringBuffer(text);
115 for(int start=0; start < textCopy.length(); start = start + end + 2) {
116 String temp = null;
117 try {
118
119 temp = textCopy.substring(start, start + MAX_LINE);
120 try {
121 end = lastIndexOfAny(temp, delimiters);
122 temp = textCopy.substring(start, start + end);
123 } catch (NoIndexFoundException e) {
124 end = temp.length();
125 }
126
127 while(textCopy.toString().startsWith(" ", start+end))
128 textCopy = textCopy.deleteCharAt(start+end);
129
130 String newLine = NEWLINE + Utility.addLeadingWhiteSpace("", indentLength + 2);
131 textCopy.insert((start + end), newLine);
132 } catch(IndexOutOfBoundsException i) {
133 temp = textCopy.substring(start);
134 }
135 }
136 return textCopy.toString();
137 }
138
139 /***
140 * Find the last index of any of the given delimiters.
141 *
142 * @param text The string to search.
143 * @param delimiters The delimiters to look for.
144 * @return The index where the last given delimiter is found.
145 * @throws NoIndexFoundException
146 */
147 private int lastIndexOfAny(String text, char []delimiters) throws NoIndexFoundException {
148 int maxIndex = 0;
149
150 for(int i=0; i < delimiters.length; i++) {
151 maxIndex = Math.max(maxIndex, text.lastIndexOf(delimiters[i]));
152 }
153 if(maxIndex == 0)
154 throw new NoIndexFoundException("No index value found");
155
156 return maxIndex + 1;
157 }
158
159 /***
160 * Returns the number of spaces to add between the keyword and equals sign
161 * @param indentLength
162 * @param equalsPosition
163 * @return The padded keyword.
164 */
165 private String padKeyword(String keyword, int indentLength, int equalsPosition) {
166 String paddedKeyword="";
167 try {
168 int spacesToAdd = equalsPosition - (indentLength + keyword.length() + 2);
169 paddedKeyword = Utility.addLeadingWhiteSpace(keyword, indentLength);
170 if(spacesToAdd <= 0) {
171 throw new KeywordTooBigException("Keyword too big for equals position");
172 }
173 paddedKeyword = Utility.addTrailingWhiteSpace(paddedKeyword, spacesToAdd);
174 } catch(KeywordTooBigException k) {
175
176 }
177 return paddedKeyword;
178 }
179 }