001: package org.acm.seguin.pretty;
002:
003: import java.io.PrintWriter;
004: import java.util.Vector;
005: import org.acm.seguin.util.FileSettings;
006: import org.acm.seguin.util.MissingSettingsException;
007:
008: /**
009: * Stores a queue of lines to be printed
010: *
011: *@author Chris Seguin
012: */
013: public class LineQueue {
014: /**
015: * The current line number
016: */
017: protected int lineNumber;
018: private Vector list;
019: private PrintWriter output;
020: private int absoluteSpace;
021: private int incrementalSpace;
022: private boolean ownline;
023: private boolean sharedIncremental;
024: private boolean ownlineCode;
025: private StringBuffer buffer;
026: private String endOfLine;
027:
028: /**
029: * Constructor for the LineQueue object
030: *
031: *@param init Description of Parameter
032: */
033: public LineQueue(PrintWriter init) {
034: output = init;
035: list = new Vector();
036: buffer = new StringBuffer();
037:
038: FileSettings bundle = FileSettings.getRefactoryPrettySettings();
039: try {
040: absoluteSpace = bundle
041: .getInteger("singleline.comment.absoluteindent");
042: } catch (MissingSettingsException mse) {
043: absoluteSpace = 0;
044: }
045:
046: try {
047: incrementalSpace = bundle
048: .getInteger("singleline.comment.incrementalindent");
049: } catch (MissingSettingsException mse) {
050: incrementalSpace = 0;
051: }
052:
053: try {
054: ownline = bundle.getBoolean("singleline.comment.ownline");
055: } catch (MissingSettingsException mse) {
056: ownline = true;
057: }
058:
059: try {
060: String temp = bundle
061: .getString("singleline.comment.indentstyle.shared");
062: sharedIncremental = temp.equalsIgnoreCase("incremental");
063: } catch (MissingSettingsException mse) {
064: sharedIncremental = true;
065: }
066:
067: try {
068: String temp = bundle
069: .getString("singleline.comment.indentstyle.ownline");
070: ownlineCode = temp.equalsIgnoreCase("code");
071: } catch (MissingSettingsException mse) {
072: ownlineCode = true;
073: }
074:
075: try {
076: String temp = bundle.getString("end.line");
077: if (temp.equalsIgnoreCase("CRNL")
078: || temp.equalsIgnoreCase("CRLF")
079: || temp.equalsIgnoreCase("DOS")) {
080: endOfLine = "\r\n";
081: } else if (temp.equalsIgnoreCase("CR")
082: || temp.equalsIgnoreCase("MAC")) {
083: endOfLine = "\r";
084: } else if (temp.equalsIgnoreCase("NL")
085: || temp.equalsIgnoreCase("LF")
086: || temp.equalsIgnoreCase("UNIX")) {
087: endOfLine = "\n";
088: } else {
089: endOfLine = "\n";
090: }
091: } catch (MissingSettingsException mse) {
092: endOfLine = "\n";
093: }
094:
095: lineNumber = 1;
096: }
097:
098: /**
099: * Sets the AbsoluteCommentSpacing attribute of the LineQueue object
100: *
101: *@param value The new AbsoluteCommentSpacing value
102: */
103: public void setAbsoluteCommentSpacing(int value) {
104: absoluteSpace = value;
105: }
106:
107: /**
108: * Sets the IncrementalCommentSpacing attribute of the LineQueue object
109: *
110: *@param value The new IncrementalCommentSpacing value
111: */
112: public void setIncrementalCommentSpacing(int value) {
113: incrementalSpace = value;
114: }
115:
116: /**
117: * Sets the Ownline attribute of the LineQueue object
118: *
119: *@param value The new Ownline value
120: */
121: public void setOwnline(boolean value) {
122: ownline = value;
123: }
124:
125: /**
126: * Sets the SharedIncremental attribute of the LineQueue object
127: *
128: *@param value The new SharedIncremental value
129: */
130: public void setSharedIncremental(boolean value) {
131: sharedIncremental = value;
132: }
133:
134: /**
135: * Sets the OwnlineCode attribute of the LineQueue object
136: *
137: *@param value The new OwnlineCode value
138: */
139: public void setOwnlineCode(boolean value) {
140: ownlineCode = value;
141: }
142:
143: /**
144: * Returns the current line
145: *
146: *@return the line number
147: */
148: public int getCurrentLine() {
149: return lineNumber;
150: }
151:
152: /**
153: * Description of the Method
154: *
155: *@param value Description of Parameter
156: */
157: public void println(String value) {
158: if (value.length() > 0) {
159: flush();
160: }
161: list.addElement(value);
162: }
163:
164: /**
165: * Appends a comment to the file
166: *
167: *@param comment the comment to append
168: *@param prefix the prefix to the line
169: */
170: public void appendSingleLineComment(String comment, String prefix) {
171: if (list.size() > 0) {
172: if (ownline) {
173: list.insertElementAt(makeLine(prefix, comment), 1);
174: flushFirstLine();
175: } else {
176: updateLine(comment, prefix);
177: }
178: } else {
179: println(makeLine(prefix, comment));
180: }
181: flushFirstLine();
182: }
183:
184: /**
185: * Appends a comment to the file
186: *
187: *@param comment the comment to append
188: *@param prefix the prefix to the line
189: */
190: public void appendCategoryComment(String comment, String prefix) {
191: if (list.size() > 0) {
192: String first = (String) list.elementAt(0);
193: if (first.length() > 0) {
194: list.insertElementAt(prefix + comment, 1);
195: flushFirstLine();
196: } else {
197: list.insertElementAt(prefix + comment, 0);
198: }
199: } else {
200: println(prefix + comment);
201: }
202: flushFirstLine();
203: }
204:
205: /**
206: * Flushes the first line in the cache
207: */
208: public void flushFirstLine() {
209: if (list.size() > 0) {
210: writeln((String) list.elementAt(0));
211: list.removeElementAt(0);
212: }
213: }
214:
215: /**
216: * Flushes all the lines in the buffer
217: */
218: public void flush() {
219: int last = list.size();
220: for (int ndx = 0; ndx < last; ndx++) {
221: writeln((String) list.elementAt(ndx));
222: }
223: list.removeAllElements();
224: output.flush();
225: }
226:
227: /**
228: * Gets the Output attribute of the LineQueue object
229: *
230: *@return The Output value
231: */
232: protected PrintWriter getOutput() {
233: return output;
234: }
235:
236: /**
237: * Writes a single line to the output stream
238: *
239: *@param value Description of Parameter
240: */
241: protected void writeln(String value) {
242: buffer.setLength(0);
243: buffer.append(value);
244: int length = value.length();
245: while ((length > 0)
246: && Character.isWhitespace(buffer.charAt(length - 1))) {
247: length--;
248: buffer.setLength(length);
249: }
250: output.print(buffer.toString());
251: output.print(endOfLine);
252:
253: // Next line
254: lineNumber++;
255: }
256:
257: /**
258: * Updates the line
259: *
260: *@param comment the comment to update
261: *@param prefix Description of Parameter
262: */
263: private void updateLine(String comment, String prefix) {
264: String first = (String) list.elementAt(0);
265:
266: if (first.length() == 0) {
267: list.setElementAt(makeLine(prefix, comment), 0);
268: return;
269: }
270:
271: if (!sharedIncremental) {
272: list.setElementAt(makeLine(first, comment), 0);
273: return;
274: }
275:
276: StringBuffer buffer = new StringBuffer(first);
277: for (int ndx = 0; ndx < incrementalSpace; ndx++) {
278: buffer.append(" ");
279: }
280: buffer.append(comment);
281:
282: list.setElementAt(buffer.toString(), 0);
283: }
284:
285: /**
286: * Creates a line
287: *
288: *@param prefix the prefix of the comment
289: *@param comment the comment
290: *@return a string with the correct length
291: */
292: private String makeLine(String prefix, String comment) {
293: //if (prefix.length() == 0) {
294: // return comment;
295: //}
296:
297: String trim = prefix.trim();
298: boolean hasNoCode = ((trim == null) || (trim.length() == 0));
299: boolean indent = (hasNoCode && !ownlineCode)
300: || (!hasNoCode && !sharedIncremental);
301:
302: if (indent) {
303: StringBuffer buffer;
304: if (!hasNoCode) {
305: buffer = new StringBuffer(prefix);
306: } else {
307: buffer = new StringBuffer();
308: }
309:
310: while (buffer.length() < absoluteSpace) {
311: buffer.append(" ");
312: }
313: buffer.append(comment);
314:
315: return buffer.toString();
316: } else {
317: return prefix + comment;
318: }
319: }
320: }
|