001: /*
002: * Author: Chris Seguin
003: *
004: * This software has been developed under the copyleft
005: * rules of the GNU General Public License. Please
006: * consult the GNU General Public License for more
007: * details about use and distribution of this software.
008: */
009: package org.acm.seguin.pretty;
010:
011: import java.util.StringTokenizer;
012:
013: import net.sourceforge.jrefactory.ast.Node;
014: import net.sourceforge.jrefactory.parser.JavaParserConstants;
015: import net.sourceforge.jrefactory.parser.Token;
016:
017: /**
018: * Consume a multi line comment
019: *
020: *@author Chris Seguin
021: *@created October 14, 1999
022: *@date April 10, 1999
023: */
024: public class PrintSpecialMultiLineComment extends PrintSpecial {
025: /**
026: * Determines if this print special can handle the current object
027: *
028: *@param spec Description of Parameter
029: *@return true if this one should process the input
030: */
031: public boolean isAcceptable(SpecialTokenData spec) {
032: return (spec.getTokenType() == JavaParserConstants.MULTI_LINE_COMMENT);
033: }
034:
035: /**
036: * Processes the special token
037: *
038: *@param node the type of node this special is being processed for
039: *@param spec the special token data
040: *@return Description of the Returned Value
041: */
042: public boolean process(Node node, SpecialTokenData spec) {
043: // Get the print data
044: PrintData printData = spec.getPrintData();
045: String image = spec.getTokenImage();
046: int formatCode = printData.getCStyleFormatCode();
047:
048: if (formatCode == PrintData.CSC_LEAVE_UNTOUCHED) {
049: transcribe(printData, image);
050: } else {
051: cleanFormat(printData, image, formatCode, spec.isLast());
052: }
053:
054: // Changed something
055: return true;
056: }
057:
058: /**
059: * Cleanly format the code
060: *
061: *@param printData the print data
062: *@param image the comment from the original file
063: *@param formatCode the formatting style
064: *@param last Description of Parameter
065: */
066: private void cleanFormat(PrintData printData, String image,
067: int formatCode, boolean last) {
068: // Make sure we are indented
069: if ((formatCode != PrintData.CSC_MAINTAIN_STAR)
070: && !printData.isLineIndented()) {
071: printData.indent();
072: }
073:
074: if (formatCode == PrintData.CSC_MAINTAIN_STAR) {
075: if (!printData.isBufferEmpty()) {
076: printData.space();
077: } else if (!printData.isLineIndented()) {
078: printData.indent();
079: }
080: }
081:
082: // Start the comment
083: printData.appendComment("/*", PrintData.C_STYLE_COMMENT);
084: if (formatCode == PrintData.CSC_MAINTAIN_STAR) {
085: // Do nothing
086: } else {
087: startNewline(printData, true, formatCode);
088: }
089:
090: // Print the comment
091: JavadocTokenizer tok = new JavadocTokenizer(image);
092: tok.next();
093: boolean lastWasNewline = false;
094: boolean first = true;
095:
096: while (tok.hasNext()) {
097: Token token = tok.next();
098:
099: if (first
100: && ((formatCode == PrintData.CSC_ALIGN_STAR) || (formatCode == PrintData.CSC_ALIGN_BLANK))) {
101: while (token.kind != JavadocTokenizer.WORD) {
102: if (tok.hasNext()) {
103: token = tok.next();
104: } else {
105: break;
106: }
107: }
108:
109: first = false;
110: }
111:
112: // On a newline skip the space so that we realign things
113: if (lastWasNewline
114: && (token.kind == JavadocTokenizer.SPACE)
115: && (formatCode != PrintData.CSC_MAINTAIN_STAR)) {
116: token = tok.next();
117: }
118:
119: if (token.kind == JavadocTokenizer.NEWLINE) {
120: startNewline(printData, tok.hasNext(), formatCode);
121: lastWasNewline = true;
122: } else {
123: printData.appendComment(token.image,
124: PrintData.C_STYLE_COMMENT);
125: lastWasNewline = false;
126: }
127: }
128:
129: // Finish the comment
130: if (lastWasNewline) {
131: String rest = "/";
132: if (formatCode == PrintData.CSC_ALIGN_BLANK) {
133: rest = "*/";
134: }
135: printData.appendComment(rest, PrintData.C_STYLE_COMMENT);
136: } else {
137: if ((formatCode != PrintData.CSC_MAINTAIN_STAR)
138: && !printData.isLineIndented()) {
139: printData.indent();
140: }
141: if (!printData.isStarsAlignedWithSlash())
142: printData.space();
143: printData.appendComment("*/", PrintData.C_STYLE_COMMENT);
144: }
145:
146: // Newline
147: if (((formatCode == PrintData.CSC_ALIGN_STAR) || (formatCode == PrintData.CSC_ALIGN_BLANK))
148: && last) {
149: printData.newline();
150: printData.surpriseIndent();
151: }
152: }
153:
154: /**
155: * Starts a newline
156: *
157: *@param printData The print interface
158: *@param more Are there more tokens
159: *@param formatCode the formatting style
160: */
161: private void startNewline(PrintData printData, boolean more,
162: int formatCode) {
163: printData.indent();
164: if (formatCode == PrintData.CSC_ALIGN_BLANK) {
165: printData.appendComment(" ", PrintData.C_STYLE_COMMENT);
166: } else {
167: if (!printData.isStarsAlignedWithSlash())
168: printData.space();
169: printData.appendComment("*", PrintData.C_STYLE_COMMENT);
170: }
171:
172: if ((formatCode == PrintData.CSC_MAINTAIN_STAR) || !more) {
173: // Do nothing
174: } else {
175: for (int ndx = 0; ndx < printData.getCStyleIndent(); ndx++) {
176: printData.appendComment(" ", PrintData.C_STYLE_COMMENT);
177: }
178: }
179: }
180:
181: /**
182: * Simply copy the C style comment into the output file
183: *
184: *@param printData the print data
185: *@param image the comment
186: */
187: private void transcribe(PrintData printData, String image) {
188: StringTokenizer tok = new StringTokenizer(image, "\n\r");
189: if (!printData.isBufferEmpty()) {
190: printData.space();
191: } else if (!printData.isLineIndented()) {
192: printData.indent();
193: }
194:
195: while (tok.hasMoreTokens()) {
196: printData.appendComment(tok.nextToken(),
197: PrintData.C_STYLE_COMMENT);
198: if (tok.hasMoreTokens()) {
199: printData.newline();
200: }
201: }
202: }
203: }
|