A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / formatter / CodeFormatter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v0.5
5  *
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/cpl-v05.html
8  *
9  * Contributors:
10  *     IBM Corporation - initial API and implementation
11  ******************************************************************************/
12 package net.sourceforge.phpdt.internal.formatter;
13
14 import java.io.BufferedReader;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.util.Hashtable;
18 import java.util.Locale;
19 import java.util.Map;
20
21 import net.sourceforge.phpdt.core.ICodeFormatter;
22 import net.sourceforge.phpdt.core.compiler.CharOperation;
23 import net.sourceforge.phpdt.core.compiler.ITerminalSymbols;
24 import net.sourceforge.phpdt.core.compiler.InvalidInputException;
25 import net.sourceforge.phpdt.internal.compiler.ConfigurableOption;
26 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
27 import net.sourceforge.phpdt.internal.formatter.impl.FormatterOptions;
28 import net.sourceforge.phpdt.internal.formatter.impl.SplitLine;
29
30 /**
31  * <h2>How to format a piece of code ?</h2>
32  * <ul>
33  * <li>Create an instance of <code>CodeFormatter</code>
34  * <li>Use the method <code>void format(aString)</code> on this instance to
35  * format <code>aString</code>. It will return the formatted string.
36  * </ul>
37  */
38 public class CodeFormatter implements ITerminalSymbols, ICodeFormatter {
39         // IContentFormatterExtension {
40         public FormatterOptions options;
41
42         /**
43          * Represents a block in the <code>constructions</code> stack.
44          */
45         public static final int BLOCK = ITerminalSymbols.TokenNameLBRACE;
46
47         /**
48          * Represents a block following a control statement in the
49          * <code>constructions</code> stack.
50          */
51         public static final int NONINDENT_BLOCK = -100;
52
53         /**
54          * Contains the formatted output.
55          */
56         StringBuffer formattedSource;
57
58         /**
59          * Contains the current line. <br>
60          * Will be dumped at the next "newline"
61          */
62         StringBuffer currentLineBuffer;
63
64         /**
65          * Used during the formatting to get each token.
66          */
67         Scanner scanner;
68
69         /**
70          * Contains the tokens responsible for the current indentation level and the
71          * blocks not closed yet.
72          */
73         private int[] constructions;
74
75         /**
76          * Index in the <code>constructions</code> array.
77          */
78         private int constructionsCount;
79
80         /**
81          * Level of indentation of the current token (number of tab char put in
82          * front of it).
83          */
84         private int indentationLevel;
85
86         /**
87          * Regular level of indentation of all the lines
88          */
89         private int initialIndentationLevel;
90
91         /**
92          * Used to split a line.
93          */
94         Scanner splitScanner;
95
96         /**
97          * To remember the offset between the beginning of the line and the
98          * beginning of the comment.
99          */
100         int currentCommentOffset;
101
102         int currentLineIndentationLevel;
103
104         int maxLineSize = 30;
105
106         private boolean containsOpenCloseBraces;
107
108         private int indentationLevelForOpenCloseBraces;
109
110         /**
111          * Collections of positions to map
112          */
113         private int[] positionsToMap;
114
115         /**
116          * Collections of mapped positions
117          */
118         private int[] mappedPositions;
119
120         private int indexToMap;
121
122         private int indexInMap;
123
124         private int globalDelta;
125
126         private int lineDelta;
127
128         private int splitDelta;
129
130         private int beginningOfLineIndex;
131
132         private int multipleLineCommentCounter;
133
134         /**
135          * Creates a new instance of Code Formatter using the given settings.
136          * 
137          * @deprecated backport 1.0 internal functionality
138          */
139         public CodeFormatter(ConfigurableOption[] settings) {
140                 this(convertConfigurableOptions(settings));
141         }
142
143         /**
144          * Creates a new instance of Code Formatter using the FormattingOptions
145          * object given as argument
146          * 
147          * @deprecated Use CodeFormatter(ConfigurableOption[]) instead
148          */
149         public CodeFormatter() {
150                 this((Map) null);
151         }
152
153         /**
154          * Creates a new instance of Code Formatter using the given settings.
155          */
156         public CodeFormatter(Map settings) {
157                 // initialize internal state
158                 constructionsCount = 0;
159                 constructions = new int[10];
160                 currentLineIndentationLevel = indentationLevel = initialIndentationLevel;
161                 currentCommentOffset = -1;
162                 // initialize primary and secondary scanners
163                 scanner = new Scanner(true /* comment */
164                 , true /* whitespace */
165                 , false /* nls */
166                 , false /* assert */
167                 , true, /* tokenizeStrings */
168                 null, null, true /* taskCaseSensitive */); // regular scanner for
169                                                                                                         // forming lines
170                 scanner.recordLineSeparator = true;
171                 scanner.ignorePHPOneLiner = true;
172                 // to remind of the position of the beginning of the line.
173                 splitScanner = new Scanner(true /* comment */
174                 , true /* whitespace */
175                 , false /* nls */
176                 , false /* assert */
177                 , true, /* tokenizeStrings */
178                 null, null, true /* taskCaseSensitive */);
179                 splitScanner.ignorePHPOneLiner = true;
180                 // secondary scanner to split long lines formed by primary scanning
181                 // initialize current line buffer
182                 currentLineBuffer = new StringBuffer();
183                 this.options = new FormatterOptions(settings);
184         }
185
186         /**
187          * Returns true if a lineSeparator has to be inserted before
188          * <code>operator</code> false otherwise.
189          */
190         private static boolean breakLineBeforeOperator(int operator) {
191                 switch (operator) {
192                 case TokenNameCOMMA:
193                 case TokenNameSEMICOLON:
194                 case TokenNameEQUAL:
195                         return false;
196                 default:
197                         return true;
198                 }
199         }
200
201         /**
202          * @deprecated backport 1.0 internal functionality
203          */
204         private static Map convertConfigurableOptions(ConfigurableOption[] settings) {
205                 Hashtable options = new Hashtable(10);
206                 for (int i = 0; i < settings.length; i++) {
207                         if (settings[i].getComponentName().equals(
208                                         CodeFormatter.class.getName())) {
209                                 String optionName = settings[i].getOptionName();
210                                 int valueIndex = settings[i].getCurrentValueIndex();
211                                 if (optionName.equals("newline.openingBrace")) { //$NON-NLS-1$
212                                         options
213                                                         .put(
214                                                                         "net.sourceforge.phpdt.core.formatter.newline.openingBrace", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
215                                 } else if (optionName.equals("newline.controlStatement")) { //$NON-NLS-1$
216                                         options
217                                                         .put(
218                                                                         "net.sourceforge.phpdt.core.formatter.newline.controlStatement", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
219                                 } else if (optionName.equals("newline.clearAll")) { //$NON-NLS-1$
220                                         options
221                                                         .put(
222                                                                         "net.sourceforge.phpdt.core.formatter.newline.clearAll", valueIndex == 0 ? "clear all" : "preserve one"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
223                                 } else if (optionName.equals("newline.elseIf")) { //$NON-NLS-1$
224                                         options
225                                                         .put(
226                                                                         "net.sourceforge.phpdt.core.formatter.newline.elseIf", valueIndex == 0 ? "do not insert" : "insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
227                                 } else if (optionName.equals("newline.emptyBlock")) { //$NON-NLS-1$
228                                         options
229                                                         .put(
230                                                                         "net.sourceforge.phpdt.core.formatter.newline.emptyBlock", valueIndex == 0 ? "insert" : "do not insert"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
231                                 } else if (optionName.equals("lineSplit")) { //$NON-NLS-1$
232                                         options
233                                                         .put(
234                                                                         "net.sourceforge.phpdt.core.formatter.lineSplit", String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$
235                                 } else if (optionName.equals("style.assignment")) { //$NON-NLS-1$
236                                         options
237                                                         .put(
238                                                                         "net.sourceforge.phpdt.core.formatter.style.assignment", valueIndex == 0 ? "compact" : "normal"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
239                                 } else if (optionName.equals("tabulation.char")) { //$NON-NLS-1$
240                                         options
241                                                         .put(
242                                                                         "net.sourceforge.phpdt.core.formatter.tabulation.char", valueIndex == 0 ? "tab" : "space"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
243                                 } else if (optionName.equals("tabulation.size")) { //$NON-NLS-1$
244                                         options
245                                                         .put(
246                                                                         "net.sourceforge.phpdt.core.formatter.tabulation.size", String.valueOf(valueIndex)); //$NON-NLS-1$ //$NON-NLS-2$
247                                 }
248                         }
249                 }
250                 return options;
251         }
252
253         /**
254          * Returns the end of the source code.
255          */
256         private final String copyRemainingSource() {
257                 char str[] = scanner.source;
258                 int startPosition = scanner.startPosition;
259                 int length = str.length - startPosition;
260                 StringBuffer bufr = new StringBuffer(length);
261                 if (startPosition < str.length) {
262                         bufr.append(str, startPosition, length);
263                 }
264                 return (bufr.toString());
265         }
266
267         /**
268          * Inserts <code>tabCount</code> tab character or their equivalent number
269          * of spaces.
270          */
271         private void dumpTab(int tabCount) {
272                 if (options.indentWithTab) {
273                         for (int j = 0; j < tabCount; j++) {
274                                 formattedSource.append('\t');
275                                 increaseSplitDelta(1);
276                         }
277                 } else {
278                         for (int i = 0, max = options.tabSize * tabCount; i < max; i++) {
279                                 formattedSource.append(' ');
280                                 increaseSplitDelta(1);
281                         }
282                 }
283         }
284
285         /**
286          * Dumps <code>currentLineBuffer</code> into the formatted string.
287          */
288         private void flushBuffer() {
289                 String currentString = currentLineBuffer.toString();
290                 splitDelta = 0;
291                 beginningOfLineIndex = formattedSource.length();
292                 if (containsOpenCloseBraces) {
293                         containsOpenCloseBraces = false;
294                         outputLine(currentString, false,
295                                         indentationLevelForOpenCloseBraces, 0, -1, null, 0);
296                         indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
297                 } else {
298                         outputLine(currentString, false, currentLineIndentationLevel, 0,
299                                         -1, null, 0);
300                 }
301                 int scannerSourceLength = scanner.source.length;
302                 if ((scannerSourceLength > 2)
303                                 && (scanner.startPosition < scannerSourceLength)) {
304                         if (scanner.source[scannerSourceLength - 1] == '\n'
305                                         && scanner.source[scannerSourceLength - 2] == '\r') {
306                                 formattedSource.append(options.lineSeparatorSequence);
307                                 increaseGlobalDelta(options.lineSeparatorSequence.length - 2);
308                         } else if (scanner.source[scannerSourceLength - 1] == '\n') {
309                                 formattedSource.append(options.lineSeparatorSequence);
310                                 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
311                         } else if (scanner.source[scannerSourceLength - 1] == '\r') {
312                                 formattedSource.append(options.lineSeparatorSequence);
313                                 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
314                         }
315                 }
316                 updateMappedPositions(scanner.startPosition);
317         }
318
319         /**
320          * Formats the input string.
321          */
322         private void format() {
323                 int token = 0;
324                 int previousToken = 0;
325                 int previousCompilableToken = 0;
326                 int indentationOffset = 0;
327                 int newLinesInWhitespace = 0;
328                 // number of new lines in the previous whitespace token
329                 // (used to leave blank lines before comments)
330                 int pendingNewLines = 0;
331                 boolean expectingOpenBrace = false;
332                 boolean clearNonBlockIndents = false;
333                 // true if all indentations till the 1st { (usefull after } or ;)
334                 boolean pendingSpace = true;
335                 boolean pendingNewlineAfterParen = false;
336                 // true when a cr is to be put after a ) (in conditional statements)
337                 boolean inAssignment = false;
338                 boolean inArrayAssignment = false;
339                 boolean inThrowsClause = false;
340                 boolean inClassOrInterfaceHeader = false;
341                 int dollarBraceCount = 0;
342                 // openBracketCount is used to count the number of open brackets not
343                 // closed
344                 // yet.
345                 int openBracketCount = 0;
346                 int unarySignModifier = 0;
347                 // openParenthesis[0] is used to count the parenthesis not belonging to
348                 // a
349                 // condition
350                 // (eg foo();). parenthesis in for (...) are count elsewhere in the
351                 // array.
352                 int openParenthesisCount = 1;
353                 int[] openParenthesis = new int[10];
354                 // tokenBeforeColon is used to know what token goes along with the
355                 // current
356                 // :
357                 // it can be case or ?
358                 int tokenBeforeColonCount = 0;
359                 int[] tokenBeforeColon = new int[10];
360                 constructionsCount = 0; // initializes the constructions count.
361                 // contains DO if in a DO..WHILE statement, UNITIALIZED otherwise.
362                 int nlicsToken = 0;
363                 // fix for 1FF17XY: LFCOM:ALL - Format problem on not matching } and
364                 // else
365                 boolean specialElse = false;
366                 // OPTION (IndentationLevel): initial indentation level may be non-zero.
367                 currentLineIndentationLevel += constructionsCount;
368                 // An InvalidInputException exception might cause the termination of
369                 // this
370                 // loop.
371                 int arrayDeclarationCount = 0;
372                 int[] arrayDeclarationParenthesis = new int[10];
373                 try {
374                         while (true) {
375                                 // Get the next token. Catch invalid input and output it
376                                 // with minimal formatting, also catch end of input and
377                                 // exit the loop.
378                                 try {
379                                         token = scanner.getNextToken();
380                                         if (Scanner.DEBUG) {
381                                                 int currentEndPosition = scanner
382                                                                 .getCurrentTokenEndPosition();
383                                                 int currentStartPosition = scanner
384                                                                 .getCurrentTokenStartPosition();
385                                                 System.out.print(currentStartPosition + ","
386                                                                 + currentEndPosition + ": ");
387                                                 System.out.println(scanner.toStringAction(token));
388                                         }
389                                         // Patch for line comment
390                                         // See PR http://dev.eclipse.org/bugs/show_bug.cgi?id=23096
391                                         if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
392                                                 int length = scanner.currentPosition;
393                                                 loop: for (int index = length - 1; index >= 0; index--) {
394                                                         switch (scanner.source[index]) {
395                                                         case '\r':
396                                                         case '\n':
397                                                                 scanner.currentPosition--;
398                                                                 break;
399                                                         default:
400                                                                 break loop;
401                                                         }
402                                                 }
403                                         }
404                                 } catch (InvalidInputException e) {
405                                         if (!handleInvalidToken(e)) {
406                                                 throw e;
407                                         }
408                                         token = 0;
409                                 }
410                                 if (token == Scanner.TokenNameEOF) {
411                                         break;
412                                 } else if (token == Scanner.TokenNameHEREDOC) {
413                                         // no indentation for heredocs and HTML !
414                                         outputCurrentTokenWithoutIndent(Scanner.TokenNameHEREDOC, 0);
415                                         continue;
416                                 } else if (token == Scanner.TokenNameINLINE_HTML) {
417                                         // no indentation for heredocs and HTML !
418                                         int newLineCount = 1;
419                                         if (scanner.startPosition == 0) {
420                                                 newLineCount = 0;
421                                         }
422                                         outputCurrentTokenWithoutIndent(
423                                                         Scanner.TokenNameINLINE_HTML, newLineCount);
424                                         int srcLen = scanner.source.length;
425                                         if (scanner.currentPosition < srcLen - 1) {
426                                                 newLine(1);
427                                         }
428                                         continue;
429                                 }
430                                 /*
431                                  * ## MODIFYING the indentation level before generating new
432                                  * lines and indentation in the output string
433                                  */
434                                 // Removes all the indentations made by statements not followed
435                                 // by a
436                                 // block
437                                 // except if the current token is ELSE, CATCH or if we are in a
438                                 // switch/case
439                                 if (clearNonBlockIndents
440                                                 && (token != Scanner.TokenNameWHITESPACE)) {
441                                         switch (token) {
442                                         case TokenNameelse:
443                                                 if (constructionsCount > 0
444                                                                 && constructions[constructionsCount - 1] == TokenNameelse) {
445                                                         pendingNewLines = 1;
446                                                         specialElse = true;
447                                                 }
448                                                 indentationLevel += popInclusiveUntil(TokenNameif);
449                                                 break;
450                                         // case TokenNamecatch :
451                                         // indentationLevel += popInclusiveUntil(TokenNamecatch);
452                                         // break;
453                                         // case TokenNamefinally :
454                                         // indentationLevel += popInclusiveUntil(TokenNamecatch);
455                                         // break;
456                                         case TokenNamewhile:
457                                                 if (nlicsToken == TokenNamedo) {
458                                                         indentationLevel += pop(TokenNamedo);
459                                                         break;
460                                                 }
461                                         default:
462                                                 indentationLevel += popExclusiveUntilBlockOrCase();
463                                                 // clear until a CASE, DEFAULT or BLOCK is encountered.
464                                                 // Thus, the indentationLevel is correctly cleared
465                                                 // either
466                                                 // in a switch/case statement or in any other situation.
467                                         }
468                                         clearNonBlockIndents = false;
469                                 }
470                                 // returns to the indentation level created by the SWITCH
471                                 // keyword
472                                 // if the current token is a CASE or a DEFAULT
473                                 if (token == TokenNamecase || token == TokenNamedefault) {
474                                         indentationLevel += pop(TokenNamecase);
475                                 }
476                                 // if (token == Scanner.TokenNamethrows) {
477                                 // inThrowsClause = true;
478                                 // }
479                                 if ((token == Scanner.TokenNameclass || token == Scanner.TokenNameinterface)
480                                                 && previousToken != Scanner.TokenNameDOT) {
481                                         inClassOrInterfaceHeader = true;
482                                 }
483                                 /*
484                                  * ## APPEND newlines and indentations to the output string
485                                  */
486                                 // Do not add a new line between ELSE and IF, if the option
487                                 // elseIfOnSameLine is true.
488                                 // Fix for 1ETLWPZ: IVJCOM:ALL - incorrect "else if" formatting
489                                 // if (pendingNewlineAfterParen
490                                 // && previousCompilableToken == TokenNameelse
491                                 // && token == TokenNameif
492                                 // && options.compactElseIfMode) {
493                                 // pendingNewlineAfterParen = false;
494                                 // pendingNewLines = 0;
495                                 // indentationLevel += pop(TokenNameelse);
496                                 // // because else if is now one single statement,
497                                 // // the indentation level after it is increased by one and not
498                                 // by 2
499                                 // // (else = 1 indent, if = 1 indent, but else if = 1 indent,
500                                 // not 2).
501                                 // }
502                                 // Add a newline & indent to the formatted source string if
503                                 // a for/if-else/while statement was scanned and there is no
504                                 // block
505                                 // following it.
506                                 pendingNewlineAfterParen = pendingNewlineAfterParen
507                                                 || (previousCompilableToken == TokenNameRPAREN && token == TokenNameLBRACE);
508                                 if (pendingNewlineAfterParen
509                                                 && token != Scanner.TokenNameWHITESPACE) {
510                                         pendingNewlineAfterParen = false;
511                                         // Do to add a newline & indent sequence if the current
512                                         // token is an
513                                         // open brace or a period or if the current token is a
514                                         // semi-colon and
515                                         // the
516                                         // previous token is a close paren.
517                                         // add a new line if a parenthesis belonging to a for()
518                                         // statement
519                                         // has been closed and the current token is not an opening
520                                         // brace
521                                         if (token != TokenNameLBRACE && !isComment(token)
522                                                         // to avoid adding new line between else and a
523                                                         // comment
524                                                         && token != TokenNameDOT
525                                                         && !(previousCompilableToken == TokenNameRPAREN && token == TokenNameSEMICOLON)) {
526                                                 newLine(1);
527                                                 currentLineIndentationLevel = indentationLevel;
528                                                 pendingNewLines = 0;
529                                                 pendingSpace = false;
530                                         } else {
531                                                 if (token == TokenNameLBRACE
532                                                                 && options.newLineBeforeOpeningBraceMode) {
533                                                         newLine(1);
534                                                         if (constructionsCount > 0
535                                                                         && constructions[constructionsCount - 1] != BLOCK
536                                                                         && constructions[constructionsCount - 1] != NONINDENT_BLOCK) {
537                                                                 currentLineIndentationLevel = indentationLevel - 1;
538                                                         } else {
539                                                                 currentLineIndentationLevel = indentationLevel;
540                                                         }
541                                                         pendingNewLines = 0;
542                                                         pendingSpace = false;
543                                                 }
544                                         }
545                                 }
546                                 if (token == TokenNameLBRACE
547                                                 && options.newLineBeforeOpeningBraceMode
548                                                 && constructionsCount > 0
549                                                 && constructions[constructionsCount - 1] == TokenNamedo) {
550                                         newLine(1);
551                                         currentLineIndentationLevel = indentationLevel - 1;
552                                         pendingNewLines = 0;
553                                         pendingSpace = false;
554                                 }
555                                 // see PR 1G5G8EC
556                                 if (token == TokenNameLBRACE && inThrowsClause) {
557                                         inThrowsClause = false;
558                                         if (options.newLineBeforeOpeningBraceMode) {
559                                                 newLine(1);
560                                                 currentLineIndentationLevel = indentationLevel;
561                                                 pendingNewLines = 0;
562                                                 pendingSpace = false;
563                                         }
564                                 }
565                                 // see PR 1G5G82G
566                                 if (token == TokenNameLBRACE && inClassOrInterfaceHeader) {
567                                         inClassOrInterfaceHeader = false;
568                                         if (options.newLineBeforeOpeningBraceMode) {
569                                                 newLine(1);
570                                                 currentLineIndentationLevel = indentationLevel;
571                                                 pendingNewLines = 0;
572                                                 pendingSpace = false;
573                                         }
574                                 }
575                                 // don't linebreak empty array declarations
576                                 if (token == TokenNameRPAREN && arrayDeclarationCount > 0) {
577                                         if (previousCompilableToken == TokenNameLPAREN) {
578                                                 pendingNewLines = 0;
579                                         }
580                                 }
581                                 // Add pending new lines to the formatted source string.
582                                 // Note: pending new lines are not added if the current token
583                                 // is a single line comment or whitespace.
584                                 // if the comment is between parenthesis, there is no blank line
585                                 // preservation
586                                 // (if it's a one-line comment, a blank line is added after it).
587                                 if (((pendingNewLines > 0 && (!isComment(token)))
588                                                 || (newLinesInWhitespace > 0 && (openParenthesisCount <= 1 && isComment(token)))
589                                                 || (previousCompilableToken == TokenNameLBRACE && token == TokenNameRBRACE) || (newLinesInWhitespace > 0 && previousCompilableToken == TokenNameDOT))
590                                                 && token != Scanner.TokenNameWHITESPACE) {
591                                         // Do not add newline & indent between an adjoining close
592                                         // brace and
593                                         // close paren. Anonymous inner classes may use this form.
594                                         boolean closeBraceAndCloseParen = previousToken == TokenNameRBRACE
595                                                         && token == TokenNameRPAREN;
596                                         // OPTION (NewLineInCompoundStatement): do not add newline &
597                                         // indent
598                                         // between close brace and else, (do) while, catch, and
599                                         // finally if
600                                         // newlineInCompoundStatement is true.
601                                         boolean nlicsOption = previousToken == TokenNameRBRACE
602                                                         && !options.newlineInControlStatementMode
603                                                         && (token == TokenNameelse
604                                                                         || (token == TokenNamewhile && nlicsToken == TokenNamedo)
605                                                                         || token == TokenNamecatch || token == TokenNamefinally);
606                                         // Do not add a newline & indent between a close brace and
607                                         // semi-colon.
608                                         boolean semiColonAndCloseBrace = previousToken == TokenNameRBRACE
609                                                         && token == TokenNameSEMICOLON;
610                                         // Do not add a new line & indent between a multiline
611                                         // comment and a
612                                         // opening brace
613                                         boolean commentAndOpenBrace = previousToken == Scanner.TokenNameCOMMENT_BLOCK
614                                                         && token == TokenNameLBRACE;
615                                         // Do not add a newline & indent between a close brace and a
616                                         // colon
617                                         // (in array assignments, for example).
618                                         boolean commaAndCloseBrace = previousToken == TokenNameRBRACE
619                                                         && token == TokenNameCOMMA;
620                                         // Add a newline and indent, if appropriate.
621                                         if (specialElse
622                                                         || (!commentAndOpenBrace
623                                                                         && !closeBraceAndCloseParen && !nlicsOption
624                                                                         && !semiColonAndCloseBrace && !commaAndCloseBrace)) {
625                                                 // if clearAllBlankLinesMode=false, leaves the blank
626                                                 // lines
627                                                 // inserted by the user
628                                                 // if clearAllBlankLinesMode=true, removes all of then
629                                                 // and insert only blank lines required by the
630                                                 // formatting.
631                                                 if (!options.clearAllBlankLinesMode) {
632                                                         // (isComment(token))
633                                                         pendingNewLines = (pendingNewLines < newLinesInWhitespace) ? newLinesInWhitespace
634                                                                         : pendingNewLines;
635                                                         pendingNewLines = (pendingNewLines > 2) ? 2
636                                                                         : pendingNewLines;
637                                                 }
638                                                 if (previousCompilableToken == TokenNameLBRACE
639                                                                 && token == TokenNameRBRACE) {
640                                                         containsOpenCloseBraces = true;
641                                                         indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
642                                                         if (isComment(previousToken)) {
643                                                                 newLine(pendingNewLines);
644                                                         } else {
645                                                                 /*
646                                                                  * if (!(constructionsCount > 1 &&
647                                                                  * constructions[constructionsCount-1] ==
648                                                                  * NONINDENT_BLOCK &&
649                                                                  * (constructions[constructionsCount-2] ==
650                                                                  * TokenNamefor
651                                                                  */
652                                                                 if (options.newLineInEmptyBlockMode) {
653                                                                         if (inArrayAssignment) {
654                                                                                 newLine(1); // array assigment with an
655                                                                                                         // empty block
656                                                                         } else {
657                                                                                 newLine(pendingNewLines);
658                                                                         }
659                                                                 }
660                                                                 // }
661                                                         }
662                                                 } else {
663                                                         // see PR 1FKKC3U: LFCOM:WINNT - Format problem with
664                                                         // a comment
665                                                         // before the ';'
666                                                         if (!((previousToken == Scanner.TokenNameCOMMENT_BLOCK || previousToken == Scanner.TokenNameCOMMENT_PHPDOC) && token == TokenNameSEMICOLON)) {
667                                                                 newLine(pendingNewLines);
668                                                         }
669                                                 }
670                                                 if (((previousCompilableToken == TokenNameSEMICOLON)
671                                                                 || (previousCompilableToken == TokenNameLBRACE)
672                                                                 || (previousCompilableToken == TokenNameRBRACE) || (isComment(previousToken)))
673                                                                 && (token == TokenNameRBRACE)) {
674                                                         indentationOffset = -1;
675                                                         indentationLevel += popExclusiveUntilBlock();
676                                                 }
677                                                 if (previousToken == Scanner.TokenNameCOMMENT_LINE
678                                                                 && inAssignment) {
679                                                         // PR 1FI5IPO
680                                                         currentLineIndentationLevel++;
681                                                 } else {
682                                                         currentLineIndentationLevel = indentationLevel
683                                                                         + indentationOffset;
684                                                 }
685                                                 pendingSpace = false;
686                                                 indentationOffset = 0;
687                                         }
688                                         pendingNewLines = 0;
689                                         newLinesInWhitespace = 0;
690                                         specialElse = false;
691                                         if (nlicsToken == TokenNamedo && token == TokenNamewhile) {
692                                                 nlicsToken = 0;
693                                         }
694                                 }
695                                 boolean phpTagAndWhitespace = previousToken == TokenNameINLINE_HTML
696                                                 && token == TokenNameWHITESPACE;
697                                 switch (token) {
698                                 // case TokenNameDOLLAR :
699                                 // dollarBraceCount++;
700                                 // break;
701                                 case TokenNameelse:
702                                         // case TokenNamefinally :
703                                         expectingOpenBrace = true;
704                                         pendingNewlineAfterParen = true;
705                                         indentationLevel += pushControlStatement(token);
706                                         break;
707                                 case TokenNamecase:
708                                 case TokenNamedefault:
709                                         if (tokenBeforeColonCount == tokenBeforeColon.length) {
710                                                 System
711                                                                 .arraycopy(
712                                                                                 tokenBeforeColon,
713                                                                                 0,
714                                                                                 (tokenBeforeColon = new int[tokenBeforeColonCount * 2]),
715                                                                                 0, tokenBeforeColonCount);
716                                         }
717                                         tokenBeforeColon[tokenBeforeColonCount++] = TokenNamecase;
718                                         indentationLevel += pushControlStatement(TokenNamecase);
719                                         break;
720                                 case TokenNameQUESTION:
721                                         if (tokenBeforeColonCount == tokenBeforeColon.length) {
722                                                 System
723                                                                 .arraycopy(
724                                                                                 tokenBeforeColon,
725                                                                                 0,
726                                                                                 (tokenBeforeColon = new int[tokenBeforeColonCount * 2]),
727                                                                                 0, tokenBeforeColonCount);
728                                         }
729                                         tokenBeforeColon[tokenBeforeColonCount++] = token;
730                                         break;
731                                 case TokenNameswitch:
732                                 case TokenNamefor:
733                                 case TokenNameforeach:
734                                 case TokenNameif:
735                                 case TokenNamewhile:
736                                         if (openParenthesisCount == openParenthesis.length) {
737                                                 System
738                                                                 .arraycopy(
739                                                                                 openParenthesis,
740                                                                                 0,
741                                                                                 (openParenthesis = new int[openParenthesisCount * 2]),
742                                                                                 0, openParenthesisCount);
743                                         }
744                                         openParenthesis[openParenthesisCount++] = 0;
745                                         expectingOpenBrace = true;
746                                         indentationLevel += pushControlStatement(token);
747                                         break;
748                                 case TokenNametry:
749                                         pendingNewlineAfterParen = true;
750                                 case TokenNamecatch:
751                                         // several CATCH statements can be contiguous.
752                                         // a CATCH is encountered pop until first CATCH (if a CATCH
753                                         // follows a TRY it works the same way,
754                                         // as CATCH and TRY are the same token in the stack).
755                                         expectingOpenBrace = true;
756                                         indentationLevel += pushControlStatement(TokenNamecatch);
757                                         break;
758                                 case TokenNamedo:
759                                         expectingOpenBrace = true;
760                                         indentationLevel += pushControlStatement(token);
761                                         nlicsToken = token;
762                                         break;
763                                 case TokenNamenew:
764                                         break;
765                                 case TokenNameLPAREN:
766                                         // if (previousToken == TokenNamesynchronized) {
767                                         // indentationLevel += pushControlStatement(previousToken);
768                                         // } else {
769                                         // Put a space between the previous and current token if the
770                                         // previous token was not a keyword, open paren, logical
771                                         // compliment (eg: !), semi-colon, open brace, close brace,
772                                         // super, or this.
773                                         if (previousCompilableToken != TokenNameLBRACKET
774                                                         && previousToken != TokenNameIdentifier
775                                                         && previousToken != 0
776                                                         && previousToken != TokenNameNOT
777                                                         && previousToken != TokenNameLPAREN
778                                                         && previousToken != TokenNameTWIDDLE
779                                                         && previousToken != TokenNameSEMICOLON
780                                                         && previousToken != TokenNameLBRACE
781                                                         && previousToken != TokenNameRBRACE
782                                                         && previousToken != TokenNamesuper) {
783                                                 // && previousToken != TokenNamethis) {
784                                                 space();
785                                         }
786                                         // If in a for/if/while statement, increase the parenthesis
787                                         // count
788                                         // for the current openParenthesisCount
789                                         // else increase the count for stand alone parenthesis.
790                                         if (openParenthesisCount > 0)
791                                                 openParenthesis[openParenthesisCount - 1]++;
792                                         else
793                                                 openParenthesis[0]++;
794                                         pendingSpace = false;
795                                         // recognize array declaration for nice output
796                                         if (previousCompilableToken == TokenNamearray) {
797                                                 arrayDeclarationCount++;
798                                                 arrayDeclarationParenthesis[arrayDeclarationCount] = openParenthesis[openParenthesisCount];
799                                                 indentationLevel++;
800                                                 pendingNewLines = 1;
801                                         }
802                                         // S }
803                                         break;
804                                 case TokenNameRPAREN:
805                                         // check for closing array declaration
806                                         if (arrayDeclarationCount > 0) {
807                                                 if (arrayDeclarationParenthesis[arrayDeclarationCount] == openParenthesis[openParenthesisCount]) {
808                                                         if (previousCompilableToken != TokenNameLPAREN) {
809                                                                 newLine(1);
810                                                         }
811                                                         indentationLevel--;
812                                                         currentLineIndentationLevel = indentationLevel;
813                                                         pendingNewLines = 0;
814                                                         arrayDeclarationCount--;
815                                                 }
816                                         }
817                                         // Decrease the parenthesis count
818                                         // if there is no more unclosed parenthesis,
819                                         // a new line and indent may be append (depending on the
820                                         // next
821                                         // token).
822                                         if ((openParenthesisCount > 1)
823                                                         && (openParenthesis[openParenthesisCount - 1] > 0)) {
824                                                 openParenthesis[openParenthesisCount - 1]--;
825                                                 if (openParenthesis[openParenthesisCount - 1] <= 0) {
826                                                         pendingNewlineAfterParen = true;
827                                                         inAssignment = false;
828                                                         openParenthesisCount--;
829                                                 }
830                                         } else {
831                                                 openParenthesis[0]--;
832                                         }
833                                         pendingSpace = false;
834                                         break;
835                                 case TokenNameLBRACE:
836                                         if (previousCompilableToken == TokenNameDOLLAR) {
837                                                 dollarBraceCount++;
838                                         } else {
839                                                 if ((previousCompilableToken == TokenNameRBRACKET)
840                                                                 || (previousCompilableToken == TokenNameEQUAL)) {
841                                                         // if (previousCompilableToken == TokenNameRBRACKET)
842                                                         // {
843                                                         inArrayAssignment = true;
844                                                         inAssignment = false;
845                                                 }
846                                                 if (inArrayAssignment) {
847                                                         indentationLevel += pushBlock();
848                                                 } else {
849                                                         // Add new line and increase indentation level after
850                                                         // open brace.
851                                                         pendingNewLines = 1;
852                                                         indentationLevel += pushBlock();
853                                                         inAssignment = false;
854                                                 }
855                                         }
856                                         break;
857                                 case TokenNameRBRACE:
858                                         if (dollarBraceCount > 0) {
859                                                 dollarBraceCount--;
860                                                 break;
861                                         }
862                                         if (previousCompilableToken == TokenNameRPAREN) {
863                                                 pendingSpace = false;
864                                         }
865                                         if (inArrayAssignment) {
866                                                 inArrayAssignment = false;
867                                                 pendingNewLines = 1;
868                                                 indentationLevel += popInclusiveUntilBlock();
869                                         } else {
870                                                 pendingNewLines = 1;
871                                                 indentationLevel += popInclusiveUntilBlock();
872                                                 if (previousCompilableToken == TokenNameRPAREN) {
873                                                         // fix for 1FGDDV6: LFCOM:WIN98 - Weird splitting on
874                                                         // message
875                                                         // expression
876                                                         currentLineBuffer
877                                                                         .append(options.lineSeparatorSequence);
878                                                         increaseLineDelta(options.lineSeparatorSequence.length);
879                                                 }
880                                                 if (constructionsCount > 0) {
881                                                         switch (constructions[constructionsCount - 1]) {
882                                                         case TokenNamefor:
883                                                         case TokenNameforeach:
884                                                                 // indentationLevel += popExclusiveUntilBlock();
885                                                                 // break;
886                                                         case TokenNameswitch:
887                                                         case TokenNameif:
888                                                         case TokenNameelse:
889                                                         case TokenNametry:
890                                                         case TokenNamecatch:
891                                                         case TokenNamefinally:
892                                                         case TokenNamewhile:
893                                                         case TokenNamedo:
894                                                                 // case TokenNamesynchronized :
895                                                                 clearNonBlockIndents = true;
896                                                         default:
897                                                                 break;
898                                                         }
899                                                 }
900                                         }
901                                         break;
902                                 case TokenNameLBRACKET:
903                                         openBracketCount++;
904                                         pendingSpace = false;
905                                         break;
906                                 case TokenNameRBRACKET:
907                                         openBracketCount -= (openBracketCount > 0) ? 1 : 0;
908                                         // if there is no left bracket to close, the right bracket
909                                         // is
910                                         // ignored.
911                                         pendingSpace = false;
912                                         break;
913                                 case TokenNameCOMMA:
914                                         pendingSpace = false;
915                                         if (arrayDeclarationCount > 0) {
916                                                 pendingNewLines = 1;
917                                         }
918                                         break;
919                                 case TokenNameDOT:
920                                         space();
921                                         pendingSpace = false;
922                                         break;
923                                 case TokenNameSEMICOLON:
924                                         // Do not generate line terminators in the definition of
925                                         // the for statement.
926                                         // if not in this case, jump a line and reduce indentation
927                                         // after
928                                         // the brace
929                                         // if the block it closes belongs to a conditional statement
930                                         // (if,
931                                         // while, do...).
932                                         if (openParenthesisCount <= 1) {
933                                                 pendingNewLines = 1;
934                                                 if (expectingOpenBrace) {
935                                                         clearNonBlockIndents = true;
936                                                         expectingOpenBrace = false;
937                                                 }
938                                         }
939                                         inAssignment = false;
940                                         pendingSpace = false;
941                                         break;
942                                 case TokenNamePLUS_PLUS:
943                                 case TokenNameMINUS_MINUS:
944                                         // Do not put a space between a post-increment/decrement
945                                         // and the identifier being modified.
946                                         if (previousToken == TokenNameIdentifier
947                                                         || previousToken == TokenNameRBRACKET
948                                                         || previousToken == TokenNameVariable) {
949                                                 pendingSpace = false;
950                                         }
951                                         break;
952                                 case TokenNamePLUS:
953                                         // previously ADDITION
954                                 case TokenNameMINUS:
955                                         // Handle the unary operators plus and minus via a flag
956                                         if (!isLiteralToken(previousToken)
957                                                         && previousToken != TokenNameIdentifier
958                                                         && previousToken != TokenNameRPAREN
959                                                         && previousToken != TokenNameRBRACKET) {
960                                                 unarySignModifier = 1;
961                                         }
962                                         break;
963                                 case TokenNameCOLON:
964                                         // In a switch/case statement, add a newline & indent
965                                         // when a colon is encountered.
966                                         if (tokenBeforeColonCount > 0) {
967                                                 if (tokenBeforeColon[tokenBeforeColonCount - 1] == TokenNamecase) {
968                                                         pendingNewLines = 1;
969                                                 }
970                                                 tokenBeforeColonCount--;
971                                         }
972                                         break;
973                                 case TokenNameEQUAL:
974                                         inAssignment = true;
975                                         break;
976                                 case Scanner.TokenNameCOMMENT_LINE:
977                                         pendingNewLines = 1;
978                                         if (inAssignment) {
979                                                 currentLineIndentationLevel++;
980                                         }
981                                         break; // a line is always inserted after a one-line
982                                                         // comment
983                                 case Scanner.TokenNameCOMMENT_PHPDOC:
984                                 case Scanner.TokenNameCOMMENT_BLOCK:
985                                         currentCommentOffset = getCurrentCommentOffset();
986                                         pendingNewLines = 1;
987                                         break;
988                                 case Scanner.TokenNameWHITESPACE:
989                                         if (!phpTagAndWhitespace) {
990                                                 // Count the number of line terminators in the
991                                                 // whitespace so
992                                                 // line spacing can be preserved near comments.
993                                                 char[] source = scanner.source;
994                                                 newLinesInWhitespace = 0;
995                                                 for (int i = scanner.startPosition, max = scanner.currentPosition; i < max; i++) {
996                                                         if (source[i] == '\r') {
997                                                                 if (i < max - 1) {
998                                                                         if (source[++i] == '\n') {
999                                                                                 newLinesInWhitespace++;
1000                                                                         } else {
1001                                                                                 newLinesInWhitespace++;
1002                                                                         }
1003                                                                 } else {
1004                                                                         newLinesInWhitespace++;
1005                                                                 }
1006                                                         } else if (source[i] == '\n') {
1007                                                                 newLinesInWhitespace++;
1008                                                         }
1009                                                 }
1010                                                 increaseLineDelta(scanner.startPosition
1011                                                                 - scanner.currentPosition);
1012                                                 break;
1013                                         }
1014                                         // case TokenNameHTML :
1015                                         // // Add the next token to the formatted source string.
1016                                         // // outputCurrentToken(token);
1017                                         // int startPosition = scanner.startPosition;
1018                                         // flushBuffer();
1019                                         // for (int i = startPosition, max =
1020                                         // scanner.currentPosition; i <
1021                                         // max; i++) {
1022                                         // char currentCharacter = scanner.source[i];
1023                                         // updateMappedPositions(i);
1024                                         // currentLineBuffer.append(currentCharacter);
1025                                         // }
1026                                         // break;
1027                                 default:
1028                                         if ((token == TokenNameIdentifier) || isLiteralToken(token)
1029                                                         || token == TokenNamesuper) {
1030                                                 // || token == TokenNamethis) {
1031                                                 // Do not put a space between a unary operator
1032                                                 // (eg: ++, --, +, -) and the identifier being modified.
1033                                                 if (previousToken == TokenNamePLUS_PLUS
1034                                                                 || previousToken == TokenNameMINUS_MINUS
1035                                                                 || (previousToken == TokenNameMINUS_GREATER && options.compactDereferencingMode) // ->
1036                                                                 || (previousToken == TokenNamePLUS && unarySignModifier > 0)
1037                                                                 || (previousToken == TokenNameMINUS && unarySignModifier > 0)) {
1038                                                         pendingSpace = false;
1039                                                 }
1040                                                 unarySignModifier = 0;
1041                                         }
1042                                         break;
1043                                 }
1044                                 // Do not output whitespace tokens.
1045                                 if (token != Scanner.TokenNameWHITESPACE || phpTagAndWhitespace) {
1046                                         /*
1047                                          * Add pending space to the formatted source string. Do not
1048                                          * output a space under the following circumstances: 1) this
1049                                          * is the first pass 2) previous token is an open paren 3)
1050                                          * previous token is a period 4) previous token is the
1051                                          * logical compliment (eg: !) 5) previous token is the
1052                                          * bitwise compliment (eg: ~) 6) previous token is the open
1053                                          * bracket (eg: [) 7) in an assignment statement, if the
1054                                          * previous token is an open brace or the current token is a
1055                                          * close brace 8) previous token is a single line comment 9)
1056                                          * current token is a '->'
1057                                          */
1058                                         if (token == TokenNameMINUS_GREATER
1059                                                         && options.compactDereferencingMode)
1060                                                 pendingSpace = false;
1061
1062                                         boolean openAndCloseBrace = previousCompilableToken == TokenNameLBRACE
1063                                                         && token == TokenNameRBRACE;
1064                                         if (pendingSpace
1065                                                         && insertSpaceAfter(previousToken)
1066                                                         && !(inAssignment && (previousToken == TokenNameLBRACE || token == TokenNameRBRACE))
1067                                                         && previousToken != Scanner.TokenNameCOMMENT_LINE) {
1068                                                 if ((!(options.compactAssignmentMode && token == TokenNameEQUAL))
1069                                                                 && !openAndCloseBrace)
1070                                                         space();
1071                                         }
1072                                         // Add the next token to the formatted source string.
1073                                         outputCurrentToken(token);
1074                                         if (token == Scanner.TokenNameCOMMENT_LINE
1075                                                         && openParenthesisCount > 1) {
1076                                                 pendingNewLines = 0;
1077                                                 currentLineBuffer.append(options.lineSeparatorSequence);
1078                                                 increaseLineDelta(options.lineSeparatorSequence.length);
1079                                         }
1080                                         pendingSpace = true;
1081                                 }
1082                                 // Whitespace tokens do not need to be remembered.
1083                                 if (token != Scanner.TokenNameWHITESPACE || phpTagAndWhitespace) {
1084                                         previousToken = token;
1085                                         if (token != Scanner.TokenNameCOMMENT_BLOCK
1086                                                         && token != Scanner.TokenNameCOMMENT_LINE
1087                                                         && token != Scanner.TokenNameCOMMENT_PHPDOC) {
1088                                                 previousCompilableToken = token;
1089                                         }
1090                                 }
1091                         }
1092                         output(copyRemainingSource());
1093                         flushBuffer();
1094                         // dump the last token of the source in the formatted output.
1095                 } catch (InvalidInputException e) {
1096                         output(copyRemainingSource());
1097                         flushBuffer();
1098                         // dump the last token of the source in the formatted output.
1099                 }
1100         }
1101
1102         /**
1103          * Formats the char array <code>sourceString</code>, and returns a string
1104          * containing the formatted version.
1105          * 
1106          * @return the formatted ouput.
1107          */
1108         public String formatSourceString(String sourceString) {
1109                 char[] sourceChars = sourceString.toCharArray();
1110                 formattedSource = new StringBuffer(sourceChars.length);
1111                 scanner.setSource(sourceChars);
1112                 format();
1113                 return formattedSource.toString();
1114         }
1115
1116         /**
1117          * Formats the char array <code>sourceString</code>, and returns a string
1118          * containing the formatted version.
1119          * 
1120          * @param string
1121          *            the string to format
1122          * @param indentationLevel
1123          *            the initial indentation level
1124          * @return the formatted ouput.
1125          */
1126         public String format(String string, int indentationLevel) {
1127                 return format(string, indentationLevel, (int[]) null);
1128         }
1129
1130         /**
1131          * Formats the char array <code>sourceString</code>, and returns a string
1132          * containing the formatted version. The positions array is modified to
1133          * contain the mapped positions.
1134          * 
1135          * @param string
1136          *            the string to format
1137          * @param indentationLevel
1138          *            the initial indentation level
1139          * @param positions
1140          *            the array of positions to map
1141          * @return the formatted ouput.
1142          */
1143         public String format(String string, int indentationLevel, int[] positions) {
1144                 return this.format(string, indentationLevel, positions, null);
1145         }
1146
1147         public String format(String string, int indentationLevel, int[] positions,
1148                         String lineSeparator) {
1149                 if (lineSeparator != null) {
1150                         this.options.setLineSeparator(lineSeparator);
1151                 }
1152                 if (positions != null) {
1153                         this.setPositionsToMap(positions);
1154                         this.setInitialIndentationLevel(indentationLevel);
1155                         String formattedString = this.formatSourceString(string);
1156                         int[] mappedPositions = this.getMappedPositions();
1157                         System
1158                                         .arraycopy(mappedPositions, 0, positions, 0,
1159                                                         positions.length);
1160                         return formattedString;
1161                 } else {
1162                         this.setInitialIndentationLevel(indentationLevel);
1163                         return this.formatSourceString(string);
1164                 }
1165         }
1166
1167         /**
1168          * Formats the char array <code>sourceString</code>, and returns a string
1169          * containing the formatted version. The initial indentation level is 0.
1170          * 
1171          * @param string
1172          *            the string to format
1173          * @return the formatted ouput.
1174          */
1175         public String format(String string) {
1176                 return this.format(string, 0, (int[]) null);
1177         }
1178
1179         /**
1180          * Formats a given source string, starting indenting it at a particular
1181          * depth and using the given options
1182          * 
1183          * @deprecated backport 1.0 internal functionality
1184          */
1185         public static String format(String sourceString,
1186                         int initialIndentationLevel, ConfigurableOption[] options) {
1187                 CodeFormatter formatter = new CodeFormatter(options);
1188                 formatter.setInitialIndentationLevel(initialIndentationLevel);
1189                 return formatter.formatSourceString(sourceString);
1190         }
1191
1192         /**
1193          * Returns the number of characters and tab char between the beginning of
1194          * the line and the beginning of the comment.
1195          */
1196         private int getCurrentCommentOffset() {
1197                 int linePtr = scanner.linePtr;
1198                 // if there is no beginning of line, return 0.
1199                 if (linePtr < 0)
1200                         return 0;
1201                 int offset = 0;
1202                 int beginningOfLine = scanner.lineEnds[linePtr];
1203                 int currentStartPosition = scanner.startPosition;
1204                 char[] source = scanner.source;
1205                 // find the position of the beginning of the line containing the comment
1206                 while (beginningOfLine > currentStartPosition) {
1207                         if (linePtr > 0) {
1208                                 beginningOfLine = scanner.lineEnds[--linePtr];
1209                         } else {
1210                                 beginningOfLine = 0;
1211                                 break;
1212                         }
1213                 }
1214                 for (int i = currentStartPosition - 1; i >= beginningOfLine; i--) {
1215                         char currentCharacter = source[i];
1216                         switch (currentCharacter) {
1217                         case '\t':
1218                                 offset += options.tabSize;
1219                                 break;
1220                         case ' ':
1221                                 offset++;
1222                                 break;
1223                         case '\r':
1224                         case '\n':
1225                                 break;
1226                         default:
1227                                 return offset;
1228                         }
1229                 }
1230                 return offset;
1231         }
1232
1233         /**
1234          * Returns an array of descriptions for the configurable options. The
1235          * descriptions may be changed and passed back to a different compiler.
1236          * 
1237          * @deprecated backport 1.0 internal functionality
1238          */
1239         public static ConfigurableOption[] getDefaultOptions(Locale locale) {
1240                 String componentName = CodeFormatter.class.getName();
1241                 FormatterOptions options = new FormatterOptions();
1242                 return new ConfigurableOption[] {
1243                                 new ConfigurableOption(componentName, "newline.openingBrace",
1244                                                 locale, options.newLineBeforeOpeningBraceMode ? 0 : 1),
1245                                 //$NON-NLS-1$
1246                                 new ConfigurableOption(componentName,
1247                                                 "newline.controlStatement", locale,
1248                                                 options.newlineInControlStatementMode ? 0 : 1),
1249                                 //$NON-NLS-1$
1250                                 new ConfigurableOption(componentName, "newline.clearAll",
1251                                                 locale, options.clearAllBlankLinesMode ? 0 : 1),
1252                                 //$NON-NLS-1$
1253                                 // new ConfigurableOption(componentName, "newline.elseIf",
1254                                 // locale,
1255                                 // options.compactElseIfMode ? 0 : 1), //$NON-NLS-1$
1256                                 new ConfigurableOption(componentName, "newline.emptyBlock",
1257                                                 locale, options.newLineInEmptyBlockMode ? 0 : 1),
1258                                 //$NON-NLS-1$
1259                                 new ConfigurableOption(componentName, "line.split", locale,
1260                                                 options.maxLineLength),
1261                                 //$NON-NLS-1$
1262                                 new ConfigurableOption(componentName,
1263                                                 "style.compactAssignment", locale,
1264                                                 options.compactAssignmentMode ? 0 : 1),
1265                                 //$NON-NLS-1$
1266                                 new ConfigurableOption(componentName, "tabulation.char",
1267                                                 locale, options.indentWithTab ? 0 : 1),
1268                                 //$NON-NLS-1$
1269                                 new ConfigurableOption(componentName,
1270                                                 "tabulation.size", locale, options.tabSize) //$NON-NLS-1$
1271                 };
1272         }
1273
1274         /**
1275          * Returns the array of mapped positions. Returns null is no positions have
1276          * been set.
1277          * 
1278          * @return int[]
1279          * @deprecated There is no need to retrieve the mapped positions anymore.
1280          */
1281         public int[] getMappedPositions() {
1282                 if (null != mappedPositions) {
1283                         for (int i = 0; i < mappedPositions.length; i++) {
1284                                 if (mappedPositions[i] >= formattedSource.length()) {
1285                                         mappedPositions[i] = formattedSource.length() - 1;
1286                                 }
1287                         }
1288                 }
1289                 return mappedPositions;
1290         }
1291
1292         /**
1293          * Returns the priority of the token given as argument <br>
1294          * The most prioritary the token is, the smallest the return value is.
1295          * 
1296          * @return the priority of <code>token</code>
1297          * @param token
1298          *            the token of which the priority is requested
1299          */
1300         private static int getTokenPriority(int token) {
1301                 switch (token) {
1302                 case TokenNameextends:
1303                         // case TokenNameimplements :
1304                         // case TokenNamethrows :
1305                         return 10;
1306                 case TokenNameSEMICOLON:
1307                         // ;
1308                         return 20;
1309                 case TokenNameCOMMA:
1310                         // ,
1311                         return 25;
1312                 case TokenNameEQUAL:
1313                         // =
1314                         return 30;
1315                 case TokenNameAND_AND:
1316                         // &&
1317                 case TokenNameOR_OR:
1318                         // ||
1319                         return 40;
1320                 case TokenNameQUESTION:
1321                         // ?
1322                 case TokenNameCOLON:
1323                         // :
1324                         return 50; // it's better cutting on ?: than on ;
1325                 case TokenNameEQUAL_EQUAL:
1326                         // ==
1327                 case TokenNameEQUAL_EQUAL_EQUAL:
1328                         // ===
1329                 case TokenNameNOT_EQUAL:
1330                         // !=
1331                 case TokenNameNOT_EQUAL_EQUAL:
1332                         // !=
1333                         return 60;
1334                 case TokenNameLESS:
1335                         // <
1336                 case TokenNameLESS_EQUAL:
1337                         // <=
1338                 case TokenNameGREATER:
1339                         // >
1340                 case TokenNameGREATER_EQUAL:
1341                         // >=
1342                         // case TokenNameinstanceof : // instanceof
1343                         return 70;
1344                 case TokenNamePLUS:
1345                         // +
1346                 case TokenNameMINUS:
1347                         // -
1348                         return 80;
1349                 case TokenNameMULTIPLY:
1350                         // *
1351                 case TokenNameDIVIDE:
1352                         // /
1353                 case TokenNameREMAINDER:
1354                         // %
1355                         return 90;
1356                 case TokenNameLEFT_SHIFT:
1357                         // <<
1358                 case TokenNameRIGHT_SHIFT:
1359                         // >>
1360                         // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>>
1361                         return 100;
1362                 case TokenNameAND:
1363                         // &
1364                 case TokenNameOR:
1365                         // |
1366                 case TokenNameXOR:
1367                         // ^
1368                         return 110;
1369                 case TokenNameMULTIPLY_EQUAL:
1370                         // *=
1371                 case TokenNameDIVIDE_EQUAL:
1372                         // /=
1373                 case TokenNameREMAINDER_EQUAL:
1374                         // %=
1375                 case TokenNamePLUS_EQUAL:
1376                         // +=
1377                 case TokenNameMINUS_EQUAL:
1378                         // -=
1379                 case TokenNameLEFT_SHIFT_EQUAL:
1380                         // <<=
1381                 case TokenNameRIGHT_SHIFT_EQUAL:
1382                         // >>=
1383                         // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>=
1384                 case TokenNameAND_EQUAL:
1385                         // &=
1386                 case TokenNameXOR_EQUAL:
1387                         // ^=
1388                 case TokenNameOR_EQUAL:
1389                         // .=
1390                 case TokenNameDOT_EQUAL:
1391                         // |=
1392                         return 120;
1393                 case TokenNameDOT:
1394                         // .
1395                         return 130;
1396                 default:
1397                         return Integer.MAX_VALUE;
1398                 }
1399         }
1400
1401         /**
1402          * Handles the exception raised when an invalid token is encountered.
1403          * Returns true if the exception has been handled, false otherwise.
1404          */
1405         private boolean handleInvalidToken(Exception e) {
1406                 if (e.getMessage().equals(Scanner.INVALID_CHARACTER_CONSTANT)
1407                                 || e.getMessage().equals(Scanner.INVALID_CHAR_IN_STRING)
1408                                 || e.getMessage().equals(Scanner.INVALID_ESCAPE)) {
1409                         return true;
1410                 }
1411                 return false;
1412         }
1413
1414         private final void increaseGlobalDelta(int offset) {
1415                 globalDelta += offset;
1416         }
1417
1418         private final void increaseLineDelta(int offset) {
1419                 lineDelta += offset;
1420         }
1421
1422         private final void increaseSplitDelta(int offset) {
1423                 splitDelta += offset;
1424         }
1425
1426         /**
1427          * Returns true if a space has to be inserted after <code>operator</code>
1428          * false otherwise.
1429          */
1430         private boolean insertSpaceAfter(int token) {
1431                 switch (token) {
1432                 case TokenNameLPAREN:
1433                 case TokenNameNOT:
1434                 case TokenNameTWIDDLE:
1435                 case 0:
1436                         // no token
1437                 case TokenNameWHITESPACE:
1438                 case TokenNameLBRACKET:
1439                 case TokenNameDOLLAR:
1440                 case Scanner.TokenNameCOMMENT_LINE:
1441                         return false;
1442                 default:
1443                         return true;
1444                 }
1445         }
1446
1447         /**
1448          * Returns true if a space has to be inserted before <code>operator</code>
1449          * false otherwise. <br>
1450          * Cannot be static as it uses the code formatter options (to know if the
1451          * compact assignment mode is on).
1452          */
1453         private boolean insertSpaceBefore(int token) {
1454                 switch (token) {
1455                 case TokenNameEQUAL:
1456                         return (!options.compactAssignmentMode);
1457                 default:
1458                         return false;
1459                 }
1460         }
1461
1462         private static boolean isComment(int token) {
1463                 boolean result = token == Scanner.TokenNameCOMMENT_BLOCK
1464                                 || token == Scanner.TokenNameCOMMENT_LINE
1465                                 || token == Scanner.TokenNameCOMMENT_PHPDOC;
1466                 return result;
1467         }
1468
1469         private static boolean isLiteralToken(int token) {
1470                 boolean result = token == TokenNameIntegerLiteral
1471                 // || token == TokenNameLongLiteral
1472                                 // || token == TokenNameFloatingPointLiteral
1473                                 || token == TokenNameDoubleLiteral
1474                                 // || token == TokenNameCharacterLiteral
1475                                 || token == TokenNameStringDoubleQuote;
1476                 return result;
1477         }
1478
1479         /**
1480          * If the length of <code>oneLineBuffer</code> exceeds
1481          * <code>maxLineLength</code>, it is split and the result is dumped in
1482          * <code>formattedSource</code>
1483          * 
1484          * @param newLineCount
1485          *            the number of new lines to append
1486          */
1487         private void newLine(int newLineCount) {
1488                 // format current line
1489                 splitDelta = 0;
1490                 beginningOfLineIndex = formattedSource.length();
1491                 String currentLine = currentLineBuffer.toString();
1492                 if (containsOpenCloseBraces) {
1493                         containsOpenCloseBraces = false;
1494                         outputLine(currentLine, false, indentationLevelForOpenCloseBraces,
1495                                         0, -1, null, 0);
1496                         indentationLevelForOpenCloseBraces = currentLineIndentationLevel;
1497                 } else {
1498                         outputLine(currentLine, false, currentLineIndentationLevel, 0, -1,
1499                                         null, 0);
1500                 }
1501                 // dump line break(s)
1502                 for (int i = 0; i < newLineCount; i++) {
1503                         formattedSource.append(options.lineSeparatorSequence);
1504                         increaseSplitDelta(options.lineSeparatorSequence.length);
1505                 }
1506                 // reset formatter for next line
1507                 int currentLength = currentLine.length();
1508                 currentLineBuffer = new StringBuffer(
1509                                 currentLength > maxLineSize ? maxLineSize = currentLength
1510                                                 : maxLineSize);
1511                 increaseGlobalDelta(splitDelta);
1512                 increaseGlobalDelta(lineDelta);
1513                 lineDelta = 0;
1514                 currentLineIndentationLevel = initialIndentationLevel;
1515         }
1516
1517         private String operatorString(int operator) {
1518                 switch (operator) {
1519                 case TokenNameextends:
1520                         return "extends"; //$NON-NLS-1$
1521                         // case TokenNameimplements :
1522                         // return "implements"; //$NON-NLS-1$
1523                         //
1524                         // case TokenNamethrows :
1525                         // return "throws"; //$NON-NLS-1$
1526                 case TokenNameSEMICOLON:
1527                         // ;
1528                         return ";"; //$NON-NLS-1$
1529                 case TokenNameCOMMA:
1530                         // ,
1531                         return ","; //$NON-NLS-1$
1532                 case TokenNameEQUAL:
1533                         // =
1534                         return "="; //$NON-NLS-1$
1535                 case TokenNameAND_AND:
1536                         // && (15.22)
1537                         return "&&"; //$NON-NLS-1$
1538                 case TokenNameOR_OR:
1539                         // || (15.23)
1540                         return "||"; //$NON-NLS-1$
1541                 case TokenNameQUESTION:
1542                         // ? (15.24)
1543                         return "?"; //$NON-NLS-1$
1544                 case TokenNameCOLON:
1545                         // : (15.24)
1546                         return ":"; //$NON-NLS-1$
1547                 case TokenNamePAAMAYIM_NEKUDOTAYIM:
1548                         // : (15.24)
1549                         return "::"; //$NON-NLS-1$
1550                 case TokenNameEQUAL_EQUAL:
1551                         // == (15.20, 15.20.1, 15.20.2, 15.20.3)
1552                         return "=="; //$NON-NLS-1$
1553                 case TokenNameEQUAL_EQUAL_EQUAL:
1554                         // == (15.20, 15.20.1, 15.20.2, 15.20.3)
1555                         return "==="; //$NON-NLS-1$
1556                 case TokenNameEQUAL_GREATER:
1557                         // -= (15.25.2)
1558                         return "=>"; //$NON-NLS-1$
1559                 case TokenNameNOT_EQUAL:
1560                         // != (15.20, 15.20.1, 15.20.2, 15.20.3)
1561                         return "!="; //$NON-NLS-1$
1562                 case TokenNameNOT_EQUAL_EQUAL:
1563                         // != (15.20, 15.20.1, 15.20.2, 15.20.3)
1564                         return "!=="; //$NON-NLS-1$
1565                 case TokenNameLESS:
1566                         // < (15.19.1)
1567                         return "<"; //$NON-NLS-1$
1568                 case TokenNameLESS_EQUAL:
1569                         // <= (15.19.1)
1570                         return "<="; //$NON-NLS-1$
1571                 case TokenNameGREATER:
1572                         // > (15.19.1)
1573                         return ">"; //$NON-NLS-1$
1574                 case TokenNameGREATER_EQUAL:
1575                         // >= (15.19.1)
1576                         return ">="; //$NON-NLS-1$
1577                         // case TokenNameinstanceof : // instanceof
1578                         // return "instanceof"; //$NON-NLS-1$
1579                 case TokenNamePLUS:
1580                         // + (15.17, 15.17.2)
1581                         return "+"; //$NON-NLS-1$
1582                 case TokenNameMINUS:
1583                         // - (15.17.2)
1584                         return "-"; //$NON-NLS-1$
1585                 case TokenNameMULTIPLY:
1586                         // * (15.16.1)
1587                         return "*"; //$NON-NLS-1$
1588                 case TokenNameDIVIDE:
1589                         // / (15.16.2)
1590                         return "/"; //$NON-NLS-1$
1591                 case TokenNameREMAINDER:
1592                         // % (15.16.3)
1593                         return "%"; //$NON-NLS-1$
1594                 case TokenNameLEFT_SHIFT:
1595                         // << (15.18)
1596                         return "<<"; //$NON-NLS-1$
1597                 case TokenNameRIGHT_SHIFT:
1598                         // >> (15.18)
1599                         return ">>"; //$NON-NLS-1$
1600                         // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18)
1601                         // return ">>>"; //$NON-NLS-1$
1602                 case TokenNameAND:
1603                         // & (15.21, 15.21.1, 15.21.2)
1604                         return "&"; //$NON-NLS-1$
1605                 case TokenNameOR:
1606                         // | (15.21, 15.21.1, 15.21.2)
1607                         return "|"; //$NON-NLS-1$
1608                 case TokenNameXOR:
1609                         // ^ (15.21, 15.21.1, 15.21.2)
1610                         return "^"; //$NON-NLS-1$
1611                 case TokenNameMULTIPLY_EQUAL:
1612                         // *= (15.25.2)
1613                         return "*="; //$NON-NLS-1$
1614                 case TokenNameDIVIDE_EQUAL:
1615                         // /= (15.25.2)
1616                         return "/="; //$NON-NLS-1$
1617                 case TokenNameREMAINDER_EQUAL:
1618                         // %= (15.25.2)
1619                         return "%="; //$NON-NLS-1$
1620                 case TokenNamePLUS_EQUAL:
1621                         // += (15.25.2)
1622                         return "+="; //$NON-NLS-1$
1623                 case TokenNameMINUS_EQUAL:
1624                         // -= (15.25.2)
1625                         return "-="; //$NON-NLS-1$
1626                 case TokenNameMINUS_GREATER:
1627                         // -= (15.25.2)
1628                         return "->"; //$NON-NLS-1$
1629                 case TokenNameLEFT_SHIFT_EQUAL:
1630                         // <<= (15.25.2)
1631                         return "<<="; //$NON-NLS-1$
1632                 case TokenNameRIGHT_SHIFT_EQUAL:
1633                         // >>= (15.25.2)
1634                         return ">>="; //$NON-NLS-1$
1635                         // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2)
1636                         // return ">>>="; //$NON-NLS-1$
1637                 case TokenNameAND_EQUAL:
1638                         // &= (15.25.2)
1639                         return "&="; //$NON-NLS-1$
1640                 case TokenNameXOR_EQUAL:
1641                         // ^= (15.25.2)
1642                         return "^="; //$NON-NLS-1$
1643                 case TokenNameOR_EQUAL:
1644                         // |= (15.25.2)
1645                         return "|="; //$NON-NLS-1$
1646                 case TokenNameDOT_EQUAL:
1647                         // .=
1648                         return ".="; //$NON-NLS-1$
1649                 case TokenNameDOT:
1650                         // .
1651                         return "."; //$NON-NLS-1$
1652                 default:
1653                         return ""; //$NON-NLS-1$
1654                 }
1655         }
1656
1657         /**
1658          * Appends <code>stringToOutput</code> to the formatted output. <br>
1659          * If it contains \n, append a LINE_SEPARATOR and indent after it.
1660          */
1661         private void output(String stringToOutput) {
1662                 char currentCharacter;
1663                 for (int i = 0, max = stringToOutput.length(); i < max; i++) {
1664                         currentCharacter = stringToOutput.charAt(i);
1665                         if (currentCharacter != '\t') {
1666                                 currentLineBuffer.append(currentCharacter);
1667                         }
1668                 }
1669         }
1670
1671         private void outputCurrentTokenWithoutIndent(int token, int newLineCount) {
1672                 newLine(newLineCount);
1673                 formattedSource.append(scanner.source, scanner.startPosition,
1674                                 scanner.currentPosition - scanner.startPosition);
1675         }
1676
1677         /**
1678          * Appends <code>token</code> to the formatted output. <br>
1679          * If it contains <code>\n</code>, append a LINE_SEPARATOR and indent
1680          * after it.
1681          */
1682         private void outputCurrentToken(int token) {
1683                 char[] source = scanner.source;
1684                 int startPosition = scanner.startPosition;
1685                 switch (token) {
1686                 case Scanner.TokenNameCOMMENT_PHPDOC:
1687                 case Scanner.TokenNameCOMMENT_BLOCK:
1688                 case Scanner.TokenNameCOMMENT_LINE:
1689                         boolean endOfLine = false;
1690                         int currentCommentOffset = getCurrentCommentOffset();
1691                         int beginningOfLineSpaces = 0;
1692                         endOfLine = false;
1693                         currentCommentOffset = getCurrentCommentOffset();
1694                         beginningOfLineSpaces = 0;
1695                         boolean pendingCarriageReturn = false;
1696                         for (int i = startPosition, max = scanner.currentPosition; i < max; i++) {
1697                                 char currentCharacter = source[i];
1698                                 updateMappedPositions(i);
1699                                 switch (currentCharacter) {
1700                                 case '\r':
1701                                         pendingCarriageReturn = true;
1702                                         endOfLine = true;
1703                                         break;
1704                                 case '\n':
1705                                         if (pendingCarriageReturn) {
1706                                                 increaseGlobalDelta(options.lineSeparatorSequence.length - 2);
1707                                         } else {
1708                                                 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1709                                         }
1710                                         pendingCarriageReturn = false;
1711                                         currentLineBuffer.append(options.lineSeparatorSequence);
1712                                         beginningOfLineSpaces = 0;
1713                                         endOfLine = true;
1714                                         break;
1715                                 case '\t':
1716                                         if (pendingCarriageReturn) {
1717                                                 pendingCarriageReturn = false;
1718                                                 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1719                                                 currentLineBuffer.append(options.lineSeparatorSequence);
1720                                                 beginningOfLineSpaces = 0;
1721                                                 endOfLine = true;
1722                                         }
1723                                         if (endOfLine) {
1724                                                 // we remove a maximum of currentCommentOffset
1725                                                 // characters (tabs
1726                                                 // are converted to space numbers).
1727                                                 beginningOfLineSpaces += options.tabSize;
1728                                                 if (beginningOfLineSpaces > currentCommentOffset) {
1729                                                         currentLineBuffer.append(currentCharacter);
1730                                                 } else {
1731                                                         increaseGlobalDelta(-1);
1732                                                 }
1733                                         } else {
1734                                                 currentLineBuffer.append(currentCharacter);
1735                                         }
1736                                         break;
1737                                 case ' ':
1738                                         if (pendingCarriageReturn) {
1739                                                 pendingCarriageReturn = false;
1740                                                 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1741                                                 currentLineBuffer.append(options.lineSeparatorSequence);
1742                                                 beginningOfLineSpaces = 0;
1743                                                 endOfLine = true;
1744                                         }
1745                                         if (endOfLine) {
1746                                                 // we remove a maximum of currentCommentOffset
1747                                                 // characters (tabs
1748                                                 // are converted to space numbers).
1749                                                 beginningOfLineSpaces++;
1750                                                 if (beginningOfLineSpaces > currentCommentOffset) {
1751                                                         currentLineBuffer.append(currentCharacter);
1752                                                 } else {
1753                                                         increaseGlobalDelta(-1);
1754                                                 }
1755                                         } else {
1756                                                 currentLineBuffer.append(currentCharacter);
1757                                         }
1758                                         break;
1759                                 default:
1760                                         if (pendingCarriageReturn) {
1761                                                 pendingCarriageReturn = false;
1762                                                 increaseGlobalDelta(options.lineSeparatorSequence.length - 1);
1763                                                 currentLineBuffer.append(options.lineSeparatorSequence);
1764                                                 beginningOfLineSpaces = 0;
1765                                                 endOfLine = true;
1766                                         } else {
1767                                                 beginningOfLineSpaces = 0;
1768                                                 currentLineBuffer.append(currentCharacter);
1769                                                 endOfLine = false;
1770                                         }
1771                                 }
1772                         }
1773                         updateMappedPositions(scanner.currentPosition - 1);
1774                         multipleLineCommentCounter++;
1775                         break;
1776                 default:
1777                         for (int i = startPosition, max = scanner.currentPosition; i < max; i++) {
1778                                 char currentCharacter = source[i];
1779                                 updateMappedPositions(i);
1780                                 currentLineBuffer.append(currentCharacter);
1781                         }
1782                 }
1783         }
1784
1785         /**
1786          * Outputs <code>currentString</code>:<br>
1787          * <ul>
1788          * <li>If its length is < maxLineLength, output
1789          * <li>Otherwise it is split.
1790          * </ul>
1791          * 
1792          * @param currentString
1793          *            string to output
1794          * @param preIndented
1795          *            whether the string to output was pre-indented
1796          * @param depth
1797          *            number of indentation to put in front of
1798          *            <code>currentString</code>
1799          * @param operator
1800          *            value of the operator belonging to <code>currentString</code>.
1801          */
1802         private void outputLine(String currentString, boolean preIndented,
1803                         int depth, int operator, int substringIndex,
1804                         int[] startSubstringIndexes, int offsetInGlobalLine) {
1805                 boolean emptyFirstSubString = false;
1806                 String operatorString = operatorString(operator);
1807                 boolean placeOperatorBehind = !breakLineBeforeOperator(operator);
1808                 boolean placeOperatorAhead = !placeOperatorBehind;
1809                 // dump prefix operator?
1810                 if (placeOperatorAhead) {
1811                         if (!preIndented) {
1812                                 dumpTab(depth);
1813                                 preIndented = true;
1814                         }
1815                         if (operator != 0) {
1816                                 if (insertSpaceBefore(operator)) {
1817                                         formattedSource.append(' ');
1818                                         increaseSplitDelta(1);
1819                                 }
1820                                 formattedSource.append(operatorString);
1821                                 increaseSplitDelta(operatorString.length());
1822                                 if (insertSpaceAfter(operator)
1823                                                 && operator != TokenNameimplements
1824                                                 && operator != TokenNameextends) {
1825                                         // && operator != TokenNamethrows) {
1826                                         formattedSource.append(' ');
1827                                         increaseSplitDelta(1);
1828                                 }
1829                         }
1830                 }
1831                 SplitLine splitLine = null;
1832                 if (options.maxLineLength == 0
1833                                 || getLength(currentString, depth) < options.maxLineLength
1834                                 || (splitLine = split(currentString, offsetInGlobalLine)) == null) {
1835                         // depending on the type of operator, outputs new line before of
1836                         // after
1837                         // dumping it
1838                         // indent before postfix operator
1839                         // indent also when the line cannot be split
1840                         if (operator == TokenNameextends || operator == TokenNameimplements) {
1841                                 // || operator == TokenNamethrows) {
1842                                 formattedSource.append(' ');
1843                                 increaseSplitDelta(1);
1844                         }
1845                         if (placeOperatorBehind) {
1846                                 if (!preIndented) {
1847                                         dumpTab(depth);
1848                                 }
1849                         }
1850                         int max = currentString.length();
1851                         if (multipleLineCommentCounter != 0) {
1852                                 try {
1853                                         BufferedReader reader = new BufferedReader(
1854                                                         new StringReader(currentString));
1855                                         String line = reader.readLine();
1856                                         while (line != null) {
1857                                                 updateMappedPositionsWhileSplitting(
1858                                                                 beginningOfLineIndex, beginningOfLineIndex
1859                                                                                 + line.length()
1860                                                                                 + options.lineSeparatorSequence.length);
1861                                                 formattedSource.append(line);
1862                                                 beginningOfLineIndex = beginningOfLineIndex
1863                                                                 + line.length();
1864                                                 if ((line = reader.readLine()) != null) {
1865                                                         formattedSource
1866                                                                         .append(options.lineSeparatorSequence);
1867                                                         beginningOfLineIndex += options.lineSeparatorSequence.length;
1868                                                         dumpTab(currentLineIndentationLevel);
1869                                                 }
1870                                         }
1871                                         reader.close();
1872                                 } catch (IOException e) {
1873                                         e.printStackTrace();
1874                                 }
1875                         } else {
1876                                 updateMappedPositionsWhileSplitting(beginningOfLineIndex,
1877                                                 beginningOfLineIndex + max);
1878                                 for (int i = 0; i < max; i++) {
1879                                         char currentChar = currentString.charAt(i);
1880                                         switch (currentChar) {
1881                                         case '\r':
1882                                                 break;
1883                                         case '\n':
1884                                                 if (i != max - 1) {
1885                                                         // fix for 1FFYL5C: LFCOM:ALL - Incorrect
1886                                                         // indentation when
1887                                                         // split with a comment inside a condition
1888                                                         // a substring cannot end with a
1889                                                         // lineSeparatorSequence,
1890                                                         // except if it has been added by format() after a
1891                                                         // one-line
1892                                                         // comment
1893                                                         formattedSource
1894                                                                         .append(options.lineSeparatorSequence);
1895                                                         // 1FGDDV6: LFCOM:WIN98 - Weird splitting on message
1896                                                         // expression
1897                                                         dumpTab(depth - 1);
1898                                                 }
1899                                                 break;
1900                                         default:
1901                                                 formattedSource.append(currentChar);
1902                                         }
1903                                 }
1904                         }
1905                         // update positions inside the mappedPositions table
1906                         if (substringIndex != -1) {
1907                                 if (multipleLineCommentCounter == 0) {
1908                                         int startPosition = beginningOfLineIndex
1909                                                         + startSubstringIndexes[substringIndex];
1910                                         updateMappedPositionsWhileSplitting(startPosition,
1911                                                         startPosition + max);
1912                                 }
1913                                 // compute the splitDelta resulting with the operator and blank
1914                                 // removal
1915                                 if (substringIndex + 1 != startSubstringIndexes.length) {
1916                                         increaseSplitDelta(startSubstringIndexes[substringIndex]
1917                                                         + max - startSubstringIndexes[substringIndex + 1]);
1918                                 }
1919                         }
1920                         // dump postfix operator?
1921                         if (placeOperatorBehind) {
1922                                 if (insertSpaceBefore(operator)) {
1923                                         formattedSource.append(' ');
1924                                         if (operator != 0) {
1925                                                 increaseSplitDelta(1);
1926                                         }
1927                                 }
1928                                 formattedSource.append(operatorString);
1929                                 if (operator != 0) {
1930                                         increaseSplitDelta(operatorString.length());
1931                                 }
1932                         }
1933                         return;
1934                 }
1935                 // fix for 1FG0BA3: LFCOM:WIN98 - Weird splitting on interfaces
1936                 // extends has to stand alone on a line when currentString has been
1937                 // split.
1938                 if (options.maxLineLength != 0 && splitLine != null
1939                                 && (operator == TokenNameextends)) {
1940                         // || operator == TokenNameimplements
1941                         // || operator == TokenNamethrows)) {
1942                         formattedSource.append(options.lineSeparatorSequence);
1943                         increaseSplitDelta(options.lineSeparatorSequence.length);
1944                         dumpTab(depth + 1);
1945                 } else {
1946                         if (operator == TokenNameextends) {
1947                                 // || operator == TokenNameimplements
1948                                 // || operator == TokenNamethrows) {
1949                                 formattedSource.append(' ');
1950                                 increaseSplitDelta(1);
1951                         }
1952                 }
1953                 // perform actual splitting
1954                 String result[] = splitLine.substrings;
1955                 int[] splitOperators = splitLine.operators;
1956                 if (result[0].length() == 0) {
1957                         // when the substring 0 is null, the substring 1 is correctly
1958                         // indented.
1959                         depth--;
1960                         emptyFirstSubString = true;
1961                 }
1962                 // the operator going in front of the result[0] string is the operator
1963                 // parameter
1964                 for (int i = 0, max = result.length; i < max; i++) {
1965                         // the new depth is the current one if this is the first substring,
1966                         // the current one + 1 otherwise.
1967                         // if the substring is a comment, use the current indentation Level
1968                         // instead of the depth
1969                         // (-1 because the ouputline increases depth).
1970                         // (fix for 1FFC72R: LFCOM:ALL - Incorrect line split in presence of
1971                         // line
1972                         // comments)
1973                         String currentResult = result[i];
1974                         if (currentResult.length() != 0 || splitOperators[i] != 0) {
1975                                 int newDepth = (currentResult.startsWith("/*") //$NON-NLS-1$
1976                                 || currentResult.startsWith("//")) //$NON-NLS-1$
1977                                 ? indentationLevel - 1
1978                                                 : depth;
1979                                 outputLine(currentResult, i == 0
1980                                                 || (i == 1 && emptyFirstSubString) ? preIndented
1981                                                 : false, i == 0 ? newDepth : newDepth + 1,
1982                                                 splitOperators[i], i, splitLine.startSubstringsIndexes,
1983                                                 currentString.indexOf(currentResult));
1984                                 if (i != max - 1) {
1985                                         formattedSource.append(options.lineSeparatorSequence);
1986                                         increaseSplitDelta(options.lineSeparatorSequence.length);
1987                                 }
1988                         }
1989                 }
1990                 if (result.length == splitOperators.length - 1) {
1991                         int lastOperator = splitOperators[result.length];
1992                         String lastOperatorString = operatorString(lastOperator);
1993                         formattedSource.append(options.lineSeparatorSequence);
1994                         increaseSplitDelta(options.lineSeparatorSequence.length);
1995                         if (breakLineBeforeOperator(lastOperator)) {
1996                                 dumpTab(depth + 1);
1997                                 if (lastOperator != 0) {
1998                                         if (insertSpaceBefore(lastOperator)) {
1999                                                 formattedSource.append(' ');
2000                                                 increaseSplitDelta(1);
2001                                         }
2002                                         formattedSource.append(lastOperatorString);
2003                                         increaseSplitDelta(lastOperatorString.length());
2004                                         if (insertSpaceAfter(lastOperator)
2005                                                         && lastOperator != TokenNameimplements
2006                                                         && lastOperator != TokenNameextends) {
2007                                                 // && lastOperator != TokenNamethrows) {
2008                                                 formattedSource.append(' ');
2009                                                 increaseSplitDelta(1);
2010                                         }
2011                                 }
2012                         }
2013                 }
2014                 if (placeOperatorBehind) {
2015                         if (insertSpaceBefore(operator)) {
2016                                 formattedSource.append(' ');
2017                                 increaseSplitDelta(1);
2018                         }
2019                         formattedSource.append(operatorString);
2020                         // increaseSplitDelta(operatorString.length());
2021                 }
2022         }
2023
2024         /**
2025          * Pops the top statement of the stack if it is <code>token</code>
2026          */
2027         private int pop(int token) {
2028                 int delta = 0;
2029                 if ((constructionsCount > 0)
2030                                 && (constructions[constructionsCount - 1] == token)) {
2031                         delta--;
2032                         constructionsCount--;
2033                 }
2034                 return delta;
2035         }
2036
2037         /**
2038          * Pops the top statement of the stack if it is a <code>BLOCK</code> or a
2039          * <code>NONINDENT_BLOCK</code>.
2040          */
2041         private int popBlock() {
2042                 int delta = 0;
2043                 if ((constructionsCount > 0)
2044                                 && ((constructions[constructionsCount - 1] == BLOCK) || (constructions[constructionsCount - 1] == NONINDENT_BLOCK))) {
2045                         if (constructions[constructionsCount - 1] == BLOCK)
2046                                 delta--;
2047                         constructionsCount--;
2048                 }
2049                 return delta;
2050         }
2051
2052         /**
2053          * Pops elements until the stack is empty or the top element is
2054          * <code>token</code>.<br>
2055          * Does not remove <code>token</code> from the stack.
2056          * 
2057          * @param token
2058          *            the token to be left as the top of the stack
2059          */
2060         private int popExclusiveUntil(int token) {
2061                 int delta = 0;
2062                 int startCount = constructionsCount;
2063                 for (int i = startCount - 1; i >= 0 && constructions[i] != token; i--) {
2064                         if (constructions[i] != NONINDENT_BLOCK)
2065                                 delta--;
2066                         constructionsCount--;
2067                 }
2068                 return delta;
2069         }
2070
2071         /**
2072          * Pops elements until the stack is empty or the top element is a
2073          * <code>BLOCK</code> or a <code>NONINDENT_BLOCK</code>.<br>
2074          * Does not remove it from the stack.
2075          */
2076         private int popExclusiveUntilBlock() {
2077                 int startCount = constructionsCount;
2078                 int delta = 0;
2079                 for (int i = startCount - 1; i >= 0 && constructions[i] != BLOCK
2080                                 && constructions[i] != NONINDENT_BLOCK; i--) {
2081                         constructionsCount--;
2082                         delta--;
2083                 }
2084                 return delta;
2085         }
2086
2087         /**
2088          * Pops elements until the stack is empty or the top element is a
2089          * <code>BLOCK</code>, a <code>NONINDENT_BLOCK</code> or a
2090          * <code>CASE</code>.<br>
2091          * Does not remove it from the stack.
2092          */
2093         private int popExclusiveUntilBlockOrCase() {
2094                 int startCount = constructionsCount;
2095                 int delta = 0;
2096                 for (int i = startCount - 1; i >= 0 && constructions[i] != BLOCK
2097                                 && constructions[i] != NONINDENT_BLOCK
2098                                 && constructions[i] != TokenNamecase; i--) {
2099                         constructionsCount--;
2100                         delta--;
2101                 }
2102                 return delta;
2103         }
2104
2105         /**
2106          * Pops elements until the stack is empty or the top element is
2107          * <code>token</code>.<br>
2108          * Removes <code>token</code> from the stack too.
2109          * 
2110          * @param token
2111          *            the token to remove from the stack
2112          */
2113         private int popInclusiveUntil(int token) {
2114                 int startCount = constructionsCount;
2115                 int delta = 0;
2116                 for (int i = startCount - 1; i >= 0 && constructions[i] != token; i--) {
2117                         if (constructions[i] != NONINDENT_BLOCK)
2118                                 delta--;
2119                         constructionsCount--;
2120                 }
2121                 if (constructionsCount > 0) {
2122                         if (constructions[constructionsCount - 1] != NONINDENT_BLOCK)
2123                                 delta--;
2124                         constructionsCount--;
2125                 }
2126                 return delta;
2127         }
2128
2129         /**
2130          * Pops elements until the stack is empty or the top element is a
2131          * <code>BLOCK</code> or a <code>NONINDENT_BLOCK</code>.<br>
2132          * Does not remove it from the stack.
2133          */
2134         private int popInclusiveUntilBlock() {
2135                 int startCount = constructionsCount;
2136                 int delta = 0;
2137                 for (int i = startCount - 1; i >= 0
2138                                 && (constructions[i] != BLOCK && constructions[i] != NONINDENT_BLOCK); i--) {
2139                         delta--;
2140                         constructionsCount--;
2141                 }
2142                 if (constructionsCount > 0) {
2143                         if (constructions[constructionsCount - 1] == BLOCK)
2144                                 delta--;
2145                         constructionsCount--;
2146                 }
2147                 return delta;
2148         }
2149
2150         /**
2151          * Pushes a block in the stack. <br>
2152          * Pushes a <code>BLOCK</code> if the stack is empty or if the top element
2153          * is a <code>BLOCK</code>, pushes <code>NONINDENT_BLOCK</code>
2154          * otherwise. Creates a new bigger array if the current one is full.
2155          */
2156         private int pushBlock() {
2157                 int delta = 0;
2158                 if (constructionsCount == constructions.length)
2159                         System.arraycopy(constructions, 0,
2160                                         (constructions = new int[constructionsCount * 2]), 0,
2161                                         constructionsCount);
2162                 if ((constructionsCount == 0)
2163                                 || (constructions[constructionsCount - 1] == BLOCK)
2164                                 || (constructions[constructionsCount - 1] == NONINDENT_BLOCK)
2165                                 || (constructions[constructionsCount - 1] == TokenNamecase)) {
2166                         delta++;
2167                         constructions[constructionsCount++] = BLOCK;
2168                 } else {
2169                         constructions[constructionsCount++] = NONINDENT_BLOCK;
2170                 }
2171                 return delta;
2172         }
2173
2174         /**
2175          * Pushes <code>token</code>.<br>
2176          * Creates a new bigger array if the current one is full.
2177          */
2178         private int pushControlStatement(int token) {
2179                 if (constructionsCount == constructions.length)
2180                         System.arraycopy(constructions, 0,
2181                                         (constructions = new int[constructionsCount * 2]), 0,
2182                                         constructionsCount);
2183                 constructions[constructionsCount++] = token;
2184                 return 1;
2185         }
2186
2187         private static boolean separateFirstArgumentOn(int currentToken) {
2188                 // return (currentToken == TokenNameCOMMA || currentToken ==
2189                 // TokenNameSEMICOLON);
2190                 return currentToken != TokenNameif && currentToken != TokenNameLPAREN
2191                                 && currentToken != TokenNameNOT
2192                                 && currentToken != TokenNamewhile
2193                                 && currentToken != TokenNamefor
2194                                 && currentToken != TokenNameforeach
2195                                 && currentToken != TokenNameswitch;
2196         }
2197
2198         /**
2199          * Set the positions to map. The mapped positions should be retrieved using
2200          * the getMappedPositions() method.
2201          * 
2202          * @param positions
2203          *            int[]
2204          * @deprecated Set the positions to map using the format(String, int, int[])
2205          *             method.
2206          * 
2207          * @see #getMappedPositions()
2208          */
2209         public void setPositionsToMap(int[] positions) {
2210                 positionsToMap = positions;
2211                 lineDelta = 0;
2212                 globalDelta = 0;
2213                 mappedPositions = new int[positions.length];
2214         }
2215
2216         /**
2217          * Appends a space character to the current line buffer.
2218          */
2219         private void space() {
2220                 currentLineBuffer.append(' ');
2221                 increaseLineDelta(1);
2222         }
2223
2224         /**
2225          * Splits <code>stringToSplit</code> on the top level token <br>
2226          * If there are several identical token at the same level, the string is cut
2227          * into many pieces.
2228          * 
2229          * @return an object containing the operator and all the substrings or null
2230          *         if the string cannot be split
2231          */
2232         public SplitLine split(String stringToSplit) {
2233                 return split(stringToSplit, 0);
2234         }
2235
2236         /**
2237          * Splits <code>stringToSplit</code> on the top level token <br>
2238          * If there are several identical token at the same level, the string is cut
2239          * into many pieces.
2240          * 
2241          * @return an object containing the operator and all the substrings or null
2242          *         if the string cannot be split
2243          */
2244         public SplitLine split(String stringToSplit, int offsetInGlobalLine) {
2245                 /*
2246                  * See http://dev.eclipse.org/bugs/show_bug.cgi?id=12540 and
2247                  * http://dev.eclipse.org/bugs/show_bug.cgi?id=14387
2248                  */
2249                 if (stringToSplit.indexOf("//$NON-NLS") != -1) { //$NON-NLS-1$
2250                         return null;
2251                 }
2252                 // split doesn't work correct for PHP
2253                 return null;
2254                 // local variables
2255                 // int currentToken = 0;
2256                 // int splitTokenType = 0;
2257                 // int splitTokenDepth = Integer.MAX_VALUE;
2258                 // int splitTokenPriority = Integer.MAX_VALUE;
2259                 // int[] substringsStartPositions = new int[10];
2260                 // // contains the start position of substrings
2261                 // int[] substringsEndPositions = new int[10];
2262                 // // contains the start position of substrings
2263                 // int substringsCount = 1; // index in the substringsStartPosition
2264                 // array
2265                 // int[] splitOperators = new int[10];
2266                 // // contains the start position of substrings
2267                 // int splitOperatorsCount = 0; // index in the substringsStartPosition
2268                 // array
2269                 // int[] openParenthesisPosition = new int[10];
2270                 // int openParenthesisPositionCount = 0;
2271                 // int position = 0;
2272                 // int lastOpenParenthesisPosition = -1;
2273                 // // used to remember the position of the 1st open parenthesis
2274                 // // needed for a pattern like: A.B(C); we want formatted like A.B(
2275                 // split C);
2276                 // // setup the scanner with a new source
2277                 // int lastCommentStartPosition = -1;
2278                 // // to remember the start position of the last comment
2279                 // int firstTokenOnLine = -1;
2280                 // // to remember the first token of the line
2281                 // int previousToken = -1;
2282                 // // to remember the previous token.
2283                 // splitScanner.setSource(stringToSplit.toCharArray());
2284                 // try {
2285                 // // start the loop
2286                 // while (true) {
2287                 // // takes the next token
2288                 // try {
2289                 // if (currentToken != Scanner.TokenNameWHITESPACE)
2290                 // previousToken = currentToken;
2291                 // currentToken = splitScanner.getNextToken();
2292                 // if (Scanner.DEBUG) {
2293                 // int currentEndPosition = splitScanner.getCurrentTokenEndPosition();
2294                 // int currentStartPosition = splitScanner
2295                 // .getCurrentTokenStartPosition();
2296                 // System.out.print(currentStartPosition + "," + currentEndPosition
2297                 // + ": ");
2298                 // System.out.println(scanner.toStringAction(currentToken));
2299                 // }
2300                 // } catch (InvalidInputException e) {
2301                 // if (!handleInvalidToken(e))
2302                 // throw e;
2303                 // currentToken = 0;
2304                 // // this value is not modify when an exception is raised.
2305                 // }
2306                 // if (currentToken == TokenNameEOF)
2307                 // break;
2308                 // if (firstTokenOnLine == -1) {
2309                 // firstTokenOnLine = currentToken;
2310                 // }
2311                 // switch (currentToken) {
2312                 // case TokenNameRBRACE :
2313                 // case TokenNameRPAREN :
2314                 // if (openParenthesisPositionCount > 0) {
2315                 // if (openParenthesisPositionCount == 1
2316                 // && lastOpenParenthesisPosition < openParenthesisPosition[0]) {
2317                 // lastOpenParenthesisPosition = openParenthesisPosition[0];
2318                 // } else if ((splitTokenDepth == Integer.MAX_VALUE)
2319                 // || (splitTokenDepth > openParenthesisPositionCount &&
2320                 // openParenthesisPositionCount == 1)) {
2321                 // splitTokenType = 0;
2322                 // splitTokenDepth = openParenthesisPositionCount;
2323                 // splitTokenPriority = Integer.MAX_VALUE;
2324                 // substringsStartPositions[0] = 0;
2325                 // // better token means the whole line until now is the first
2326                 // // substring
2327                 // substringsCount = 1; // resets the count of substrings
2328                 // substringsEndPositions[0] = openParenthesisPosition[0];
2329                 // // substring ends on operator start
2330                 // position = openParenthesisPosition[0];
2331                 // // the string mustn't be cut before the closing parenthesis but
2332                 // // after the opening one.
2333                 // splitOperatorsCount = 1; // resets the count of split operators
2334                 // splitOperators[0] = 0;
2335                 // }
2336                 // openParenthesisPositionCount--;
2337                 // }
2338                 // break;
2339                 // case TokenNameLBRACE :
2340                 // case TokenNameLPAREN :
2341                 // if (openParenthesisPositionCount == openParenthesisPosition.length) {
2342                 // System
2343                 // .arraycopy(
2344                 // openParenthesisPosition,
2345                 // 0,
2346                 // (openParenthesisPosition = new int[openParenthesisPositionCount *
2347                 // 2]),
2348                 // 0, openParenthesisPositionCount);
2349                 // }
2350                 // openParenthesisPosition[openParenthesisPositionCount++] =
2351                 // splitScanner.currentPosition;
2352                 // if (currentToken == TokenNameLPAREN
2353                 // && previousToken == TokenNameRPAREN) {
2354                 // openParenthesisPosition[openParenthesisPositionCount - 1] =
2355                 // splitScanner.startPosition;
2356                 // }
2357                 // break;
2358                 // case TokenNameSEMICOLON :
2359                 // // ;
2360                 // case TokenNameCOMMA :
2361                 // // ,
2362                 // case TokenNameEQUAL :
2363                 // // =
2364                 // if (openParenthesisPositionCount < splitTokenDepth
2365                 // || (openParenthesisPositionCount == splitTokenDepth &&
2366                 // splitTokenPriority > getTokenPriority(currentToken))) {
2367                 // // the current token is better than the one we currently have
2368                 // // (in level or in priority if same level)
2369                 // // reset the substringsCount
2370                 // splitTokenDepth = openParenthesisPositionCount;
2371                 // splitTokenType = currentToken;
2372                 // splitTokenPriority = getTokenPriority(currentToken);
2373                 // substringsStartPositions[0] = 0;
2374                 // // better token means the whole line until now is the first
2375                 // // substring
2376                 // if (separateFirstArgumentOn(firstTokenOnLine)
2377                 // && openParenthesisPositionCount > 0) {
2378                 // substringsCount = 2; // resets the count of substrings
2379                 // substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth -
2380                 // 1];
2381                 // substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth
2382                 // - 1];
2383                 // substringsEndPositions[1] = splitScanner.startPosition;
2384                 // splitOperatorsCount = 2; // resets the count of split operators
2385                 // splitOperators[0] = 0;
2386                 // splitOperators[1] = currentToken;
2387                 // position = splitScanner.currentPosition;
2388                 // // next substring will start from operator end
2389                 // } else {
2390                 // substringsCount = 1; // resets the count of substrings
2391                 // substringsEndPositions[0] = splitScanner.startPosition;
2392                 // // substring ends on operator start
2393                 // position = splitScanner.currentPosition;
2394                 // // next substring will start from operator end
2395                 // splitOperatorsCount = 1; // resets the count of split operators
2396                 // splitOperators[0] = currentToken;
2397                 // }
2398                 // } else {
2399                 // if ((openParenthesisPositionCount == splitTokenDepth &&
2400                 // splitTokenPriority == getTokenPriority(currentToken))
2401                 // && splitTokenType != TokenNameEQUAL
2402                 // && currentToken != TokenNameEQUAL) {
2403                 // // fix for 1FG0BCN: LFCOM:WIN98 - Missing one indentation after
2404                 // // split
2405                 // // take only the 1st = into account.
2406                 // // if another token with the same priority is found,
2407                 // // push the start position of the substring and
2408                 // // push the token into the stack.
2409                 // // create a new array object if the current one is full.
2410                 // if (substringsCount == substringsStartPositions.length) {
2411                 // System
2412                 // .arraycopy(
2413                 // substringsStartPositions,
2414                 // 0,
2415                 // (substringsStartPositions = new int[substringsCount * 2]),
2416                 // 0, substringsCount);
2417                 // System.arraycopy(substringsEndPositions, 0,
2418                 // (substringsEndPositions = new int[substringsCount * 2]),
2419                 // 0, substringsCount);
2420                 // }
2421                 // if (splitOperatorsCount == splitOperators.length) {
2422                 // System.arraycopy(splitOperators, 0,
2423                 // (splitOperators = new int[splitOperatorsCount * 2]), 0,
2424                 // splitOperatorsCount);
2425                 // }
2426                 // substringsStartPositions[substringsCount] = position;
2427                 // substringsEndPositions[substringsCount++] =
2428                 // splitScanner.startPosition;
2429                 // // substring ends on operator start
2430                 // position = splitScanner.currentPosition;
2431                 // // next substring will start from operator end
2432                 // splitOperators[splitOperatorsCount++] = currentToken;
2433                 // }
2434                 // }
2435                 // break;
2436                 // case TokenNameCOLON :
2437                 // // : (15.24)
2438                 // // see 1FK7C5R, we only split on a colon, when it is associated
2439                 // // with a question-mark.
2440                 // // indeed it might appear also behind a case statement, and we do
2441                 // // not to break at this point.
2442                 // if ((splitOperatorsCount == 0)
2443                 // || splitOperators[splitOperatorsCount - 1] != TokenNameQUESTION) {
2444                 // break;
2445                 // }
2446                 // case TokenNameextends :
2447                 // case TokenNameimplements :
2448                 // //case TokenNamethrows :
2449                 // case TokenNameDOT :
2450                 // // .
2451                 // case TokenNameMULTIPLY :
2452                 // // * (15.16.1)
2453                 // case TokenNameDIVIDE :
2454                 // // / (15.16.2)
2455                 // case TokenNameREMAINDER :
2456                 // // % (15.16.3)
2457                 // case TokenNamePLUS :
2458                 // // + (15.17, 15.17.2)
2459                 // case TokenNameMINUS :
2460                 // // - (15.17.2)
2461                 // case TokenNameLEFT_SHIFT :
2462                 // // << (15.18)
2463                 // case TokenNameRIGHT_SHIFT :
2464                 // // >> (15.18)
2465                 // // case TokenNameUNSIGNED_RIGHT_SHIFT : // >>> (15.18)
2466                 // case TokenNameLESS :
2467                 // // < (15.19.1)
2468                 // case TokenNameLESS_EQUAL :
2469                 // // <= (15.19.1)
2470                 // case TokenNameGREATER :
2471                 // // > (15.19.1)
2472                 // case TokenNameGREATER_EQUAL :
2473                 // // >= (15.19.1)
2474                 // // case TokenNameinstanceof : // instanceof
2475                 // case TokenNameEQUAL_EQUAL :
2476                 // // == (15.20, 15.20.1, 15.20.2, 15.20.3)
2477                 // case TokenNameEQUAL_EQUAL_EQUAL :
2478                 // // == (15.20, 15.20.1, 15.20.2, 15.20.3)
2479                 // case TokenNameNOT_EQUAL :
2480                 // // != (15.20, 15.20.1, 15.20.2, 15.20.3)
2481                 // case TokenNameNOT_EQUAL_EQUAL :
2482                 // // != (15.20, 15.20.1, 15.20.2, 15.20.3)
2483                 // case TokenNameAND :
2484                 // // & (15.21, 15.21.1, 15.21.2)
2485                 // case TokenNameOR :
2486                 // // | (15.21, 15.21.1, 15.21.2)
2487                 // case TokenNameXOR :
2488                 // // ^ (15.21, 15.21.1, 15.21.2)
2489                 // case TokenNameAND_AND :
2490                 // // && (15.22)
2491                 // case TokenNameOR_OR :
2492                 // // || (15.23)
2493                 // case TokenNameQUESTION :
2494                 // // ? (15.24)
2495                 // case TokenNameMULTIPLY_EQUAL :
2496                 // // *= (15.25.2)
2497                 // case TokenNameDIVIDE_EQUAL :
2498                 // // /= (15.25.2)
2499                 // case TokenNameREMAINDER_EQUAL :
2500                 // // %= (15.25.2)
2501                 // case TokenNamePLUS_EQUAL :
2502                 // // += (15.25.2)
2503                 // case TokenNameMINUS_EQUAL :
2504                 // // -= (15.25.2)
2505                 // case TokenNameLEFT_SHIFT_EQUAL :
2506                 // // <<= (15.25.2)
2507                 // case TokenNameRIGHT_SHIFT_EQUAL :
2508                 // // >>= (15.25.2)
2509                 // // case TokenNameUNSIGNED_RIGHT_SHIFT_EQUAL : // >>>= (15.25.2)
2510                 // case TokenNameAND_EQUAL :
2511                 // // &= (15.25.2)
2512                 // case TokenNameXOR_EQUAL :
2513                 // // ^= (15.25.2)
2514                 // case TokenNameOR_EQUAL :
2515                 // // |= (15.25.2)
2516                 // if ((openParenthesisPositionCount < splitTokenDepth ||
2517                 // (openParenthesisPositionCount == splitTokenDepth &&
2518                 // splitTokenPriority
2519                 // > getTokenPriority(currentToken)))
2520                 // && !((currentToken == TokenNamePLUS || currentToken ==
2521                 // TokenNameMINUS) && (previousToken == TokenNameLBRACE
2522                 // || previousToken == TokenNameLBRACKET || splitScanner.startPosition
2523                 // == 0))) {
2524                 // // the current token is better than the one we currently have
2525                 // // (in level or in priority if same level)
2526                 // // reset the substringsCount
2527                 // splitTokenDepth = openParenthesisPositionCount;
2528                 // splitTokenType = currentToken;
2529                 // splitTokenPriority = getTokenPriority(currentToken);
2530                 // substringsStartPositions[0] = 0;
2531                 // // better token means the whole line until now is the first
2532                 // // substring
2533                 // if (separateFirstArgumentOn(firstTokenOnLine)
2534                 // && openParenthesisPositionCount > 0) {
2535                 // substringsCount = 2; // resets the count of substrings
2536                 // substringsEndPositions[0] = openParenthesisPosition[splitTokenDepth -
2537                 // 1];
2538                 // substringsStartPositions[1] = openParenthesisPosition[splitTokenDepth
2539                 // - 1];
2540                 // substringsEndPositions[1] = splitScanner.startPosition;
2541                 // splitOperatorsCount = 3; // resets the count of split operators
2542                 // splitOperators[0] = 0;
2543                 // splitOperators[1] = 0;
2544                 // splitOperators[2] = currentToken;
2545                 // position = splitScanner.currentPosition;
2546                 // // next substring will start from operator end
2547                 // } else {
2548                 // substringsCount = 1; // resets the count of substrings
2549                 // substringsEndPositions[0] = splitScanner.startPosition;
2550                 // // substring ends on operator start
2551                 // position = splitScanner.currentPosition;
2552                 // // next substring will start from operator end
2553                 // splitOperatorsCount = 2; // resets the count of split operators
2554                 // splitOperators[0] = 0;
2555                 // // nothing for first operand since operator will be inserted in
2556                 // // front of the second operand
2557                 // splitOperators[1] = currentToken;
2558                 // }
2559                 // } else {
2560                 // if (openParenthesisPositionCount == splitTokenDepth
2561                 // && splitTokenPriority == getTokenPriority(currentToken)) {
2562                 // // if another token with the same priority is found,
2563                 // // push the start position of the substring and
2564                 // // push the token into the stack.
2565                 // // create a new array object if the current one is full.
2566                 // if (substringsCount == substringsStartPositions.length) {
2567                 // System
2568                 // .arraycopy(
2569                 // substringsStartPositions,
2570                 // 0,
2571                 // (substringsStartPositions = new int[substringsCount * 2]),
2572                 // 0, substringsCount);
2573                 // System.arraycopy(substringsEndPositions, 0,
2574                 // (substringsEndPositions = new int[substringsCount * 2]),
2575                 // 0, substringsCount);
2576                 // }
2577                 // if (splitOperatorsCount == splitOperators.length) {
2578                 // System.arraycopy(splitOperators, 0,
2579                 // (splitOperators = new int[splitOperatorsCount * 2]), 0,
2580                 // splitOperatorsCount);
2581                 // }
2582                 // substringsStartPositions[substringsCount] = position;
2583                 // substringsEndPositions[substringsCount++] =
2584                 // splitScanner.startPosition;
2585                 // // substring ends on operator start
2586                 // position = splitScanner.currentPosition;
2587                 // // next substring will start from operator end
2588                 // splitOperators[splitOperatorsCount++] = currentToken;
2589                 // }
2590                 // }
2591                 // default :
2592                 // break;
2593                 // }
2594                 // if (isComment(currentToken)) {
2595                 // lastCommentStartPosition = splitScanner.startPosition;
2596                 // } else {
2597                 // lastCommentStartPosition = -1;
2598                 // }
2599                 // }
2600                 // } catch (InvalidInputException e) {
2601                 // return null;
2602                 // }
2603                 // // if the string cannot be split, return null.
2604                 // if (splitOperatorsCount == 0)
2605                 // return null;
2606                 // // ## SPECIAL CASES BEGIN
2607                 // if (((splitOperatorsCount == 2 && splitOperators[1] == TokenNameDOT
2608                 // && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1)
2609                 // || (splitOperatorsCount > 2 && splitOperators[1] == TokenNameDOT
2610                 // && splitTokenDepth == 0 && lastOpenParenthesisPosition > -1 &&
2611                 // lastOpenParenthesisPosition <= options.maxLineLength) ||
2612                 // (separateFirstArgumentOn(firstTokenOnLine)
2613                 // && splitTokenDepth > 0 && lastOpenParenthesisPosition > -1))
2614                 // && (lastOpenParenthesisPosition < splitScanner.source.length &&
2615                 // splitScanner.source[lastOpenParenthesisPosition] != ')')) {
2616                 // // fix for 1FH4J2H: LFCOM:WINNT - Formatter - Empty parenthesis
2617                 // should
2618                 // // not be broken on two lines
2619                 // // only one split on a top level .
2620                 // // or more than one split on . and substring before open parenthesis
2621                 // fits
2622                 // // one line.
2623                 // // or split inside parenthesis and first token is not a for/while/if
2624                 // SplitLine sl = split(
2625                 // stringToSplit.substring(lastOpenParenthesisPosition),
2626                 // lastOpenParenthesisPosition);
2627                 // if (sl == null || sl.operators[0] != TokenNameCOMMA) {
2628                 // // trim() is used to remove the extra blanks at the end of the
2629                 // // substring. See PR 1FGYPI1
2630                 // return new SplitLine(new int[]{0, 0}, new String[]{
2631                 // stringToSplit.substring(0, lastOpenParenthesisPosition).trim(),
2632                 // stringToSplit.substring(lastOpenParenthesisPosition)}, new int[]{
2633                 // offsetInGlobalLine,
2634                 // lastOpenParenthesisPosition + offsetInGlobalLine});
2635                 // } else {
2636                 // // right substring can be split and is split on comma
2637                 // // copy substrings and operators
2638                 // // except if the 1st string is empty.
2639                 // int startIndex = (sl.substrings[0].length() == 0) ? 1 : 0;
2640                 // int subStringsLength = sl.substrings.length + 1 - startIndex;
2641                 // String[] result = new String[subStringsLength];
2642                 // int[] startIndexes = new int[subStringsLength];
2643                 // int operatorsLength = sl.operators.length + 1 - startIndex;
2644                 // int[] operators = new int[operatorsLength];
2645                 // result[0] = stringToSplit.substring(0, lastOpenParenthesisPosition);
2646                 // operators[0] = 0;
2647                 // System.arraycopy(sl.startSubstringsIndexes, startIndex, startIndexes,
2648                 // 1, subStringsLength - 1);
2649                 // for (int i = subStringsLength - 1; i >= 0; i--) {
2650                 // startIndexes[i] += offsetInGlobalLine;
2651                 // }
2652                 // System.arraycopy(sl.substrings, startIndex, result, 1,
2653                 // subStringsLength - 1);
2654                 // System.arraycopy(sl.operators, startIndex, operators, 1,
2655                 // operatorsLength - 1);
2656                 // return new SplitLine(operators, result, startIndexes);
2657                 // }
2658                 // }
2659                 // // if the last token is a comment and the substring before the
2660                 // comment fits
2661                 // // on a line,
2662                 // // split before the comment and return the result.
2663                 // if (lastCommentStartPosition > -1
2664                 // && lastCommentStartPosition < options.maxLineLength
2665                 // && splitTokenPriority > 50) {
2666                 // int end = lastCommentStartPosition;
2667                 // int start = lastCommentStartPosition;
2668                 // if (stringToSplit.charAt(end - 1) == ' ') {
2669                 // end--;
2670                 // }
2671                 // if (start != end && stringToSplit.charAt(start) == ' ') {
2672                 // start++;
2673                 // }
2674                 // return new SplitLine(new int[]{0, 0}, new String[]{
2675                 // stringToSplit.substring(0, end), stringToSplit.substring(start)},
2676                 // new int[]{0, start});
2677                 // }
2678                 // if (position != stringToSplit.length()) {
2679                 // if (substringsCount == substringsStartPositions.length) {
2680                 // System.arraycopy(substringsStartPositions, 0,
2681                 // (substringsStartPositions = new int[substringsCount * 2]), 0,
2682                 // substringsCount);
2683                 // System.arraycopy(substringsEndPositions, 0,
2684                 // (substringsEndPositions = new int[substringsCount * 2]), 0,
2685                 // substringsCount);
2686                 // }
2687                 // // avoid empty extra substring, e.g. line terminated with a
2688                 // semi-colon
2689                 // substringsStartPositions[substringsCount] = position;
2690                 // substringsEndPositions[substringsCount++] = stringToSplit.length();
2691                 // }
2692                 // if (splitOperatorsCount == splitOperators.length) {
2693                 // System.arraycopy(splitOperators, 0,
2694                 // (splitOperators = new int[splitOperatorsCount * 2]), 0,
2695                 // splitOperatorsCount);
2696                 // }
2697                 // splitOperators[splitOperatorsCount] = 0;
2698                 // // the last element of the stack is the position of the end of
2699                 // // StringToSPlit
2700                 // // +1 because the substring method excludes the last character
2701                 // String[] result = new String[substringsCount];
2702                 // for (int i = 0; i < substringsCount; i++) {
2703                 // int start = substringsStartPositions[i];
2704                 // int end = substringsEndPositions[i];
2705                 // if (stringToSplit.charAt(start) == ' ') {
2706                 // start++;
2707                 // substringsStartPositions[i]++;
2708                 // }
2709                 // if (end != start && stringToSplit.charAt(end - 1) == ' ') {
2710                 // end--;
2711                 // }
2712                 // result[i] = stringToSplit.substring(start, end);
2713                 // substringsStartPositions[i] += offsetInGlobalLine;
2714                 // }
2715                 // if (splitOperatorsCount > substringsCount) {
2716                 // System.arraycopy(substringsStartPositions, 0,
2717                 // (substringsStartPositions = new int[splitOperatorsCount]), 0,
2718                 // substringsCount);
2719                 // System.arraycopy(substringsEndPositions, 0,
2720                 // (substringsEndPositions = new int[splitOperatorsCount]), 0,
2721                 // substringsCount);
2722                 // for (int i = substringsCount; i < splitOperatorsCount; i++) {
2723                 // substringsStartPositions[i] = position;
2724                 // substringsEndPositions[i] = position;
2725                 // }
2726                 // System.arraycopy(splitOperators, 0,
2727                 // (splitOperators = new int[splitOperatorsCount]), 0,
2728                 // splitOperatorsCount);
2729                 // } else {
2730                 // System.arraycopy(substringsStartPositions, 0,
2731                 // (substringsStartPositions = new int[substringsCount]), 0,
2732                 // substringsCount);
2733                 // System.arraycopy(substringsEndPositions, 0,
2734                 // (substringsEndPositions = new int[substringsCount]), 0,
2735                 // substringsCount);
2736                 // System.arraycopy(splitOperators, 0,
2737                 // (splitOperators = new int[substringsCount]), 0, substringsCount);
2738                 // }
2739                 // SplitLine splitLine = new SplitLine(splitOperators, result,
2740                 // substringsStartPositions);
2741                 // return splitLine;
2742         }
2743
2744         private void updateMappedPositions(int startPosition) {
2745                 if (positionsToMap == null) {
2746                         return;
2747                 }
2748                 char[] source = scanner.source;
2749                 int sourceLength = source.length;
2750                 while (indexToMap < positionsToMap.length
2751                                 && positionsToMap[indexToMap] <= startPosition) {
2752                         int posToMap = positionsToMap[indexToMap];
2753                         if (posToMap < 0 || posToMap >= sourceLength) {
2754                                 // protection against out of bounds position
2755                                 if (posToMap == sourceLength) {
2756                                         mappedPositions[indexToMap] = formattedSource.length();
2757                                 }
2758                                 indexToMap = positionsToMap.length; // no more mapping
2759                                 return;
2760                         }
2761                         if (CharOperation.isWhitespace(source[posToMap])) {
2762                                 mappedPositions[indexToMap] = startPosition + globalDelta
2763                                                 + lineDelta;
2764                         } else {
2765                                 if (posToMap == sourceLength - 1) {
2766                                         mappedPositions[indexToMap] = startPosition + globalDelta
2767                                                         + lineDelta;
2768                                 } else {
2769                                         mappedPositions[indexToMap] = posToMap + globalDelta
2770                                                         + lineDelta;
2771                                 }
2772                         }
2773                         indexToMap++;
2774                 }
2775         }
2776
2777         private void updateMappedPositionsWhileSplitting(int startPosition,
2778                         int endPosition) {
2779                 if (mappedPositions == null || mappedPositions.length == indexInMap)
2780                         return;
2781                 while (indexInMap < mappedPositions.length
2782                                 && startPosition <= mappedPositions[indexInMap]
2783                                 && mappedPositions[indexInMap] < endPosition
2784                                 && indexInMap < indexToMap) {
2785                         mappedPositions[indexInMap] += splitDelta;
2786                         indexInMap++;
2787                 }
2788         }
2789
2790         private int getLength(String s, int tabDepth) {
2791                 int length = 0;
2792                 for (int i = 0; i < tabDepth; i++) {
2793                         length += options.tabSize;
2794                 }
2795                 for (int i = 0, max = s.length(); i < max; i++) {
2796                         char currentChar = s.charAt(i);
2797                         switch (currentChar) {
2798                         case '\t':
2799                                 length += options.tabSize;
2800                                 break;
2801                         default:
2802                                 length++;
2803                         }
2804                 }
2805                 return length;
2806         }
2807
2808         /**
2809          * Sets the initial indentation level
2810          * 
2811          * @param indentationLevel
2812          *            new indentation level
2813          * 
2814          * @deprecated
2815          */
2816         public void setInitialIndentationLevel(int newIndentationLevel) {
2817                 this.initialIndentationLevel = currentLineIndentationLevel = indentationLevel = newIndentationLevel;
2818         }
2819
2820 }