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 net.sourceforge.jrefactory.parser.Token;
012: import net.sourceforge.jrefactory.parser.JavaParserConstants;
013: import org.acm.seguin.util.MissingSettingsException;
014: import org.acm.seguin.util.FileSettings;
015:
016: /**
017: * Store the data that understands how to output comments and newlines
018: *
019: *@author Chris Seguin
020: *@author Mike Atkinson
021: *@created April 30, 1999
022: */
023: public class SpecialTokenData {
024: // Instance Variables
025: private PrintData data;
026: private Token special;
027: private JavaDocable jdi;
028: private boolean lastReturnExpected;
029: private boolean acceptNewlines;
030: private boolean setAcceptingNewlines;
031: private boolean reformatComments;
032:
033: /**
034: * Creates a special token data object
035: *
036: *@param token the special token
037: *@param printData the print data
038: */
039: public SpecialTokenData(JavaDocable jdi, Token token,
040: PrintData printData) {
041: this (jdi, token, printData, true);
042: }
043:
044: /**
045: * Creates a special token data object
046: *
047: *@param token the special token
048: *@param printData the print data
049: */
050: public SpecialTokenData(Token token, PrintData printData) {
051: this (null, token, printData, true);
052: }
053:
054: /**
055: * Creates a special token data object
056: *
057: *@param token the special token
058: *@param printData the print data
059: *@param accept whether newlines should be accepted
060: */
061: public SpecialTokenData(Token token, PrintData printData,
062: boolean accept) {
063: this (null, token, printData, accept);
064: }
065:
066: /**
067: * Creates a special token data object
068: *
069: *@param token the special token
070: *@param printData the print data
071: *@param accept whether newlines should be accepted
072: */
073: public SpecialTokenData(JavaDocable jdi, Token token,
074: PrintData printData, boolean accept) {
075: this .jdi = jdi;
076: data = printData;
077: special = beginning(token);
078: lastReturnExpected = true;
079: acceptNewlines = accept;
080: setAcceptingNewlines = false;
081:
082: // Check if comments should be reformatted
083: reformatComments = printData.isReformatComments();
084: }
085:
086: /**
087: * Set that the last return was (or not) expected
088: *
089: *@param way the way it was expected
090: */
091: public void setReturnExpected(boolean way) {
092: if (!setAcceptingNewlines) {
093: lastReturnExpected = way;
094: setAcceptingNewlines = true;
095: }
096: }
097:
098: /**
099: * Returns true when it is the last
100: *
101: *@return true if we are at the last
102: */
103: public boolean isLast() {
104: return ((special == null) || (special.next == null) || (""
105: .equals(special.next)));
106: }
107:
108: /**
109: * Returns true when it is the last JavaDoc comment.
110: *
111: *@return true if we are the last
112: *@since JRefactory 2.7.00
113: */
114: public boolean isLastJavadocComment() {
115: boolean last = true;
116: Token s = special.next;
117: while (s != null) {
118: if (s.image != null && s.image.startsWith("/**")) {
119: last = false;
120: }
121: s = s.next;
122: }
123: return last;
124: }
125:
126: /**
127: * If the first special Token is a C_STYLE_COMMENT, then pretend it
128: * is a SINGLE_LINE_COMMENT.
129: *
130: * Fixes bug 761890 (at least partly).
131: *
132: *@since JRefactory 2.7.03
133: */
134: public void convertFirstCStyleCommentToSingleLine() {
135: if (special != null
136: && special.kind == JavaParserConstants.MULTI_LINE_COMMENT
137: && special.image.indexOf('\n') < 0) {
138: //System.out.println(" - converting");
139: special.kind = JavaParserConstants.SINGLE_LINE_COMMENT;
140: }
141: }
142:
143: /**
144: * Returns true when it is the first
145: *
146: *@return true if we are at the first
147: */
148: public boolean isFirst() {
149: return ((special == null) || (special.specialToken == null));
150: }
151:
152: /**
153: * Return the print data
154: *
155: *@return the print data object
156: */
157: public PrintData getPrintData() {
158: return data;
159: }
160:
161: /**
162: * Return the special token
163: *
164: *@return the special token
165: */
166: public Token getSpecialToken() {
167: return special;
168: }
169:
170: public JavaDocable getJDI() {
171: return jdi;
172: }
173:
174: /**
175: * Get the token type
176: *
177: *@return the token type
178: */
179: public int getTokenType() {
180: if (special == null) {
181: return -1;
182: }
183: return special.kind;
184: }
185:
186: /**
187: * Get the token image
188: *
189: *@return the token image
190: */
191: public String getTokenImage() {
192: if (special == null) {
193: return "";
194: }
195:
196: return special.image;
197: }
198:
199: /**
200: * Return true if the last return was expected
201: *
202: *@return true if last was expected
203: */
204: public boolean isReturnExpected() {
205: return lastReturnExpected;
206: }
207:
208: /**
209: * Return true if new lines should be accepted
210: *
211: *@return true if newlines should be accepted
212: */
213: public boolean isAcceptingReturns() {
214: return acceptNewlines;
215: }
216:
217: /**
218: * Returns true if comments are being reformatted
219: *
220: *@return true if comments should be reformatted
221: */
222: public boolean isReformattingComments() {
223: return reformatComments;
224: }
225:
226: /**
227: * Got to the beginning
228: *
229: *@param tok Description of Parameter
230: *@return Description of the Returned Value
231: */
232: public Token beginning(Token tok) {
233: if (tok == null) {
234: return null;
235: }
236:
237: // Find the first token
238: Token current = tok;
239: Token previous = tok.specialToken;
240: while (previous != null) {
241: current = previous;
242: previous = current.specialToken;
243: }
244:
245: // Return the first
246: return current;
247: }
248:
249: /**
250: * Get the next token
251: */
252: public void next() {
253: if (special != null) {
254: special = special.next;
255: }
256: }
257: }
|