appli.diff
Class ASFormatter

java.lang.Object
  extended byappli.diff.ASResource
      extended byappli.diff.ASBeautifier
          extended byappli.diff.ASFormatter

public class ASFormatter
extends ASBeautifier


Field Summary
private  BracketMode bracketFormatMode
           
private  java.util.Stack bracketTypeStack
           
private  int charNum
           
private  char currentChar
           
private  java.lang.String currentHeader
           
private  java.lang.String currentLine
           
private  boolean doesLineStartComment
           
private  boolean endOfCodeReached
           
private  java.lang.String formattedLine
           
private  boolean foundClosingHeader
           
private  boolean foundPreCommandHeader
           
private  boolean foundPreDefinitionHeader
           
private  boolean foundQuestionMark
           
private  boolean isAppendPostBlockEmptyLineRequested
           
private  boolean isImmediatelyPostComment
           
private  boolean isImmediatelyPostEmptyBlock
           
private  boolean isImmediatelyPostHeader
           
private  boolean isImmediatelyPostLineComment
           
private  boolean isInClosingBracketLineBreak
           
private  boolean isInComment
           
private  boolean isInHeader
           
private  boolean isInLineBreak
           
private  boolean isInLineComment
           
private  boolean isInPotentialCalculation
           
private  boolean isInPreprocessor
           
private  boolean isInQuote
           
private  boolean isInTemplate
           
private  boolean isLineReady
           
private  boolean isNonParenHeader
           
private  boolean isPrependPostBlockEmptyLineRequested
           
private  boolean isPreviousBracketBlockRelated
           
private  boolean isSpecialChar
           
private  boolean isVirgin
           
private static java.util.Vector operators
           
private  java.util.Stack parenStack
           
private  boolean passedColon
           
private  boolean passedSemicolon
           
private  java.util.Stack preBracketHeaderStack
           
private static java.util.Vector preCommandHeaders
           
private static java.util.Vector preDefinitionHeaders
           
private  boolean prependEmptyLine
           
private static java.util.Vector preprocessorHeaders
           
private  char previousChar
           
private  char previousCommandChar
           
private  char previousNonWSChar
           
private  java.lang.String previousOperator
           
private  int previousReadyFormattedLineLength
           
private  char quoteChar
           
private  java.lang.String readyFormattedLine
           
private  boolean shouldBreakBlocks
           
private  boolean shouldBreakClosingHeaderBlocks
           
private  boolean shouldBreakClosingHeaderBrackets
           
private  boolean shouldBreakElseIfs
           
private  boolean shouldBreakLineAfterComments
           
private  boolean shouldBreakOneLineBlocks
           
private  boolean shouldBreakOneLineStatements
           
private  boolean shouldConvertTabs
           
private  boolean shouldPadOperators
           
private  boolean shouldPadParenthesies
           
private  boolean shouldReparseCurrentChar
           
private  ASSourceIterator sourceIterator
           
 
Fields inherited from class appli.diff.ASBeautifier
AS_ADD, AS_AND, AS_AND_ASSIGN, AS_ARROW, AS_ASM, AS_ASSIGN, AS_BAR_DEFINE, AS_BAR_EL, AS_BAR_ENDIF, AS_BAR_IF, AS_BAR_INCLUDE, AS_BIT_AND, AS_BIT_NOT, AS_BIT_OR, AS_BIT_XOR, AS_BLPAREN_BLPAREN, AS_CASE, AS_CATCH, AS_CLASS, AS_CLOSE_BRACKET, AS_CLOSE_COMMENT, AS_COLON, AS_COLON_COLON, AS_COMMA, AS_CONST, AS_DEFAULT, AS_DIV, AS_DIV_ASSIGN, AS_DO, AS_ELSE, AS_EQUAL, AS_EXTENDS, AS_EXTERN, AS_FINALLY, AS_FIXED, AS_FOR, AS_FOREACH, AS_GET, AS_GR, AS_GR_EQUAL, AS_GR_GR, AS_GR_GR_ASSIGN, AS_GR_GR_GR, AS_GR_GR_GR_ASSIGN, AS_IF, AS_INTERFACE, AS_LOCK, AS_LS, AS_LS_EQUAL, AS_LS_LS, AS_LS_LS_ASSIGN, AS_LS_LS_LS, AS_LS_LS_LS_ASSIGN, AS_MINUS, AS_MINUS_ASSIGN, AS_MINUS_MINUS, AS_MOD, AS_MOD_ASSIGN, AS_MULT, AS_MULT_ASSIGN, AS_NAMESPACE, AS_NOT, AS_NOT_EQUAL, AS_OPEN_BRACKET, AS_OPEN_COMMENT, AS_OPEN_LINE_COMMENT, AS_OPERATOR, AS_OR, AS_OR_ASSIGN, AS_PAREN_PAREN, AS_PLUS, AS_PLUS_ASSIGN, AS_PLUS_PLUS, AS_PRIVATE, AS_PROTECTED, AS_PUBLIC, AS_QUESTION, AS_REMOVE, AS_RETURN, AS_SEMICOLON, AS_SET, AS_STATIC, AS_STRUCT, AS_SWITCH, AS_SYNCHRONIZED, AS_TEMPLATE, AS_THROWS, AS_TRY, AS_UNION, AS_UNSAFE, AS_WHILE, AS_XOR_ASSIGN, assignmentOperators, headers, nonAssignmentOperators, nonParenHeaders, preBlockStatements
 
Constructor Summary
ASFormatter()
          Constructor of ASFormatter
 
Method Summary
private  void appendChar(char ch)
           
private  void appendChar(char ch, boolean canBreakLine)
          append a character to the current formatted line.
private  void appendCurrentChar()
           
private  void appendCurrentChar(boolean canBreakLine)
          append the CURRENT character (curentChar)to the current formatted line.
private  void appendSequence(java.lang.String sequence)
           
private  void appendSequence(java.lang.String sequence, boolean canBreakLine)
          append a String sequence to the current formatted line.
private  void appendSpacePad()
          append a space to the current formattedline, UNLESS the last character is already a white-space character.
private  void breakLine()
          register a line break for the formatted line.
private  boolean find(java.util.Vector v, java.lang.String s)
          Find the string in the vector
private  java.lang.String findHeader(java.util.Vector headers)
           
private  java.lang.String findHeader(java.util.Vector headers, boolean checkBoundry)
          check if one of a set of headers has been reached in the current position of the current line.
private  BracketType getBracketType()
          check if the currently reached open-bracket (i.e. '{') opens a: - a definition type block (such as a class or namespace), - a command block (such as a method block) - a static array this method takes for granted that the current character is an opening bracket.
private  boolean getNextChar()
          get the next character, increasing the current placement in the process.
private  void goForward(int i)
          jump over several characters.
 boolean hasMoreLines()
          check if there are any indented lines ready to be read by nextLine()
 void init(ASSourceIterator si)
          initialize the ASFormatter.
private  boolean isBeforeComment()
          check if current placement is before a comment or line-comment
private  boolean isFormattingEnabled()
          check if formatting options are enabled, in addition to indentation.
private  boolean isInExponent()
          check if the currently reached '-' or '+' character is part of an exponent, i.e. 0.2E-5.
private  boolean isOneLineBlockReached()
          check if a one-line bracket has been reached, i.e. if the currently reached '{' character is closed with a complimentry '}' elsewhere on the current line, .
private  boolean isPointerOrReference()
          check if the currently reached '*' or '&' character is a pointer-or-reference symbol, or another operator.
private  boolean isSequenceReached(java.lang.String sequence)
          check if a specific sequence exists in the current placement of the current line
private  boolean isUrinaryMinus()
          check if the currently reached '-' character is a urinary minus this method takes for granted that the current character is a '-'.
 java.lang.String nextLine()
          get the next formatted line.
private  char peekNextChar()
          peek at the next unread character.
 void setBracketFormatMode(int mode)
          set the bracket formatting mode.
 void setBreakBlocksMode(boolean state)
          set option to break unrelated blocks of code with empty lines.
 void setBreakClosingHeaderBlocksMode(boolean state)
          set option to break closing header blocks of code (such as 'else', 'catch', ...) with empty lines.
 void setBreakClosingHeaderBracketsMode(boolean state)
          set closing header bracket breaking mode options: true brackets just before closing headers (e.g.
 void setBreakElseIfsMode(boolean state)
          set 'else if()' breaking mode options: true 'else' headers will be broken from their succeeding 'if' headers.
 void setBreakOneLineBlocksMode(boolean state)
          set option to break/not break one-line blocks
 void setOperatorPaddingMode(boolean state)
          set operator padding mode.
 void setParenthesisPaddingMode(boolean state)
          set parentheies padding mode.
 void setSingleStatementsMode(boolean state)
          set option to break/not break lines consisting of multiple statements.
 void setTabSpaceConversionMode(boolean state)
          set option to convert tabs to spaces.
private  void trimNewLine()
          jump over the leading white space in the current line, IF the line does not begin a comment or is in a preprocessor definition.
 
Methods inherited from class appli.diff.ASBeautifier
beautify, COMPARE, findHeader, findHeader, getInputLineNumber, init, isLegalNameChar, registerInStatementIndent, setBlockIndent, setBracketIndent, setCaseIndent, setClassIndent, setCStyle, setEmptyLineFill, setJavaStyle, setLabelIndent, setMaxInStatementIndentLength, setMinConditionalIndentLength, setNamespaceIndent, setPreprocessorIndent, setSpaceIndentation, setSwitchIndent, setTabIndentation, setTabIndentation
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

preprocessorHeaders

private static java.util.Vector preprocessorHeaders

preDefinitionHeaders

private static java.util.Vector preDefinitionHeaders

preCommandHeaders

private static java.util.Vector preCommandHeaders

operators

private static java.util.Vector operators

sourceIterator

private ASSourceIterator sourceIterator

preBracketHeaderStack

private java.util.Stack preBracketHeaderStack

bracketTypeStack

private java.util.Stack bracketTypeStack

parenStack

private java.util.Stack parenStack

readyFormattedLine

private java.lang.String readyFormattedLine

currentLine

private java.lang.String currentLine

formattedLine

private java.lang.String formattedLine

currentHeader

private java.lang.String currentHeader

previousOperator

private java.lang.String previousOperator

currentChar

private char currentChar

previousChar

private char previousChar

previousNonWSChar

private char previousNonWSChar

previousCommandChar

private char previousCommandChar

quoteChar

private char quoteChar

charNum

private int charNum

bracketFormatMode

private BracketMode bracketFormatMode

isVirgin

private boolean isVirgin

shouldPadOperators

private boolean shouldPadOperators

shouldPadParenthesies

private boolean shouldPadParenthesies

shouldConvertTabs

private boolean shouldConvertTabs

isInLineComment

private boolean isInLineComment

isInComment

private boolean isInComment

isInPreprocessor

private boolean isInPreprocessor

isInTemplate

private boolean isInTemplate

doesLineStartComment

private boolean doesLineStartComment

isInQuote

private boolean isInQuote

isSpecialChar

private boolean isSpecialChar

isNonParenHeader

private boolean isNonParenHeader

foundQuestionMark

private boolean foundQuestionMark

foundPreDefinitionHeader

private boolean foundPreDefinitionHeader

foundPreCommandHeader

private boolean foundPreCommandHeader

isInLineBreak

private boolean isInLineBreak

isInClosingBracketLineBreak

private boolean isInClosingBracketLineBreak

endOfCodeReached

private boolean endOfCodeReached

isLineReady

private boolean isLineReady

isPreviousBracketBlockRelated

private boolean isPreviousBracketBlockRelated

isInPotentialCalculation

private boolean isInPotentialCalculation

shouldBreakOneLineBlocks

private boolean shouldBreakOneLineBlocks

shouldReparseCurrentChar

private boolean shouldReparseCurrentChar

shouldBreakOneLineStatements

private boolean shouldBreakOneLineStatements

shouldBreakLineAfterComments

private boolean shouldBreakLineAfterComments

shouldBreakClosingHeaderBrackets

private boolean shouldBreakClosingHeaderBrackets

shouldBreakElseIfs

private boolean shouldBreakElseIfs

passedSemicolon

private boolean passedSemicolon

passedColon

private boolean passedColon

isImmediatelyPostComment

private boolean isImmediatelyPostComment

isImmediatelyPostLineComment

private boolean isImmediatelyPostLineComment

isImmediatelyPostEmptyBlock

private boolean isImmediatelyPostEmptyBlock

shouldBreakBlocks

private boolean shouldBreakBlocks

shouldBreakClosingHeaderBlocks

private boolean shouldBreakClosingHeaderBlocks

isPrependPostBlockEmptyLineRequested

private boolean isPrependPostBlockEmptyLineRequested

isAppendPostBlockEmptyLineRequested

private boolean isAppendPostBlockEmptyLineRequested

prependEmptyLine

private boolean prependEmptyLine

foundClosingHeader

private boolean foundClosingHeader

previousReadyFormattedLineLength

private int previousReadyFormattedLineLength

isInHeader

private boolean isInHeader

isImmediatelyPostHeader

private boolean isImmediatelyPostHeader
Constructor Detail

ASFormatter

public ASFormatter()
Constructor of ASFormatter

Method Detail

init

public void init(ASSourceIterator si)
initialize the ASFormatter. init() should be called every time a ASFormatter object is to start formatting a NEW source file. init() recieves a pointer to a DYNAMICALLY CREATED ASSourceIterator object that will be used to iterate through the source code. This object will be deleted during the ASFormatter's destruction, and thus should not be deleted elsewhere.

Overrides:
init in class ASBeautifier
Parameters:
si - a pointer to the DYNAMICALLY CREATED ASSourceIterator object.

nextLine

public java.lang.String nextLine()
                          throws java.io.IOException
get the next formatted line.

Overrides:
nextLine in class ASBeautifier
Returns:
formatted line.
Throws:
java.io.IOException

hasMoreLines

public boolean hasMoreLines()
check if there are any indented lines ready to be read by nextLine()

Overrides:
hasMoreLines in class ASBeautifier
Returns:
are there any indented lines ready?

isFormattingEnabled

private boolean isFormattingEnabled()
check if formatting options are enabled, in addition to indentation.

Returns:
are formatting options enabled?

setBracketFormatMode

public void setBracketFormatMode(int mode)
set the bracket formatting mode. options: astyle::NONE_MODE no formatting of brackets. astyle::ATTACH_MODE Java, K&R style bracket placement. astyle::BREAK_MODE ANSI C/C++ style bracket placement.

Parameters:
mode - the bracket formatting mode.

setBreakClosingHeaderBracketsMode

public void setBreakClosingHeaderBracketsMode(boolean state)
set closing header bracket breaking mode options: true brackets just before closing headers (e.g. 'else', 'catch') will be broken, even if standard brackets are attached. false closing header brackets will be treated as standard brackets.


setBreakElseIfsMode

public void setBreakElseIfsMode(boolean state)
set 'else if()' breaking mode options: true 'else' headers will be broken from their succeeding 'if' headers. false 'else' headers will be attached to their succeeding 'if' headers.


setOperatorPaddingMode

public void setOperatorPaddingMode(boolean state)
set operator padding mode. options: true statement operators will be padded with spaces around them. false statement operators will not be padded.


setParenthesisPaddingMode

public void setParenthesisPaddingMode(boolean state)
set parentheies padding mode. options: true statement parenthesies will be padded with spaces around them. false statement parenthesies will not be padded.


setBreakOneLineBlocksMode

public void setBreakOneLineBlocksMode(boolean state)
set option to break/not break one-line blocks

Parameters:
state - true = break, false = don't break.

setSingleStatementsMode

public void setSingleStatementsMode(boolean state)
set option to break/not break lines consisting of multiple statements.

Parameters:
state - true = break, false = don't break.

setTabSpaceConversionMode

public void setTabSpaceConversionMode(boolean state)
set option to convert tabs to spaces.

Parameters:
state - true = convert, false = don't convert.

setBreakBlocksMode

public void setBreakBlocksMode(boolean state)
set option to break unrelated blocks of code with empty lines.

Parameters:
state - true = convert, false = don't convert.

setBreakClosingHeaderBlocksMode

public void setBreakClosingHeaderBlocksMode(boolean state)
set option to break closing header blocks of code (such as 'else', 'catch', ...) with empty lines.

Parameters:
state - true = convert, false = don't convert.

isSequenceReached

private boolean isSequenceReached(java.lang.String sequence)
check if a specific sequence exists in the current placement of the current line

Parameters:
sequence - the sequence to be checked
Returns:
whether sequence has been reached.

goForward

private void goForward(int i)
                throws java.io.IOException
jump over several characters.

Parameters:
i - the number of characters to jump over.
Throws:
java.io.IOException

peekNextChar

private char peekNextChar()
peek at the next unread character.

Returns:
the next unread character.

isBeforeComment

private boolean isBeforeComment()
check if current placement is before a comment or line-comment

Returns:
is before a comment or line-comment.

getNextChar

private boolean getNextChar()
                     throws java.io.IOException
get the next character, increasing the current placement in the process. the new character is inserted into the variable currentChar.

Returns:
whether succeded to recieve the new character.
Throws:
java.io.IOException

trimNewLine

private void trimNewLine()
jump over the leading white space in the current line, IF the line does not begin a comment or is in a preprocessor definition.


appendChar

private void appendChar(char ch,
                        boolean canBreakLine)
append a character to the current formatted line. Unless disabled (via canBreakLine == false), first check if a line-break has been registered, and if so break the formatted line, and only then append the character into the next formatted line.

Parameters:
ch - the character to append.
canBreakLine - if true, a registered line-break

appendChar

private void appendChar(char ch)

appendCurrentChar

private void appendCurrentChar(boolean canBreakLine)
append the CURRENT character (curentChar)to the current formatted line. Unless disabled (via canBreakLine == false), first check if a line-break has been registered, and if so break the formatted line, and only then append the character into the next formatted line.

Parameters:
canBreakLine - if true, a registered line-break

appendCurrentChar

private void appendCurrentChar()

appendSequence

private void appendSequence(java.lang.String sequence,
                            boolean canBreakLine)
append a String sequence to the current formatted line. Unless disabled (via canBreakLine == false), first check if a line-break has been registered, and if so break the formatted line, and only then append the sequence into the next formatted line.

Parameters:
sequence - the sequence to append.
canBreakLine - if true, a registered line-break

appendSequence

private void appendSequence(java.lang.String sequence)

appendSpacePad

private void appendSpacePad()
append a space to the current formattedline, UNLESS the last character is already a white-space character.


breakLine

private void breakLine()
register a line break for the formatted line.


getBracketType

private BracketType getBracketType()
check if the currently reached open-bracket (i.e. '{') opens a: - a definition type block (such as a class or namespace), - a command block (such as a method block) - a static array this method takes for granted that the current character is an opening bracket.

Returns:
the type of the opened block.

isPointerOrReference

private boolean isPointerOrReference()
check if the currently reached '*' or '&' character is a pointer-or-reference symbol, or another operator. this method takes for granted that the current character is either a '*' or '&'.

Returns:
whether current character is a reference-or-pointer

isUrinaryMinus

private boolean isUrinaryMinus()
check if the currently reached '-' character is a urinary minus this method takes for granted that the current character is a '-'.

Returns:
whether the current '-' is a urinary minus.

isInExponent

private boolean isInExponent()
check if the currently reached '-' or '+' character is part of an exponent, i.e. 0.2E-5. this method takes for granted that the current character is a '-' or '+'.

Returns:
whether the current '-' is in an exponent.

isOneLineBlockReached

private boolean isOneLineBlockReached()
check if a one-line bracket has been reached, i.e. if the currently reached '{' character is closed with a complimentry '}' elsewhere on the current line, .

Returns:
has a one-line bracket been reached?

findHeader

private java.lang.String findHeader(java.util.Vector headers,
                                    boolean checkBoundry)
check if one of a set of headers has been reached in the current position of the current line.

Parameters:
headers - a vector of headers
checkBoundry -
Returns:
a pointer to the found header. Or a null if no header has been reached.

findHeader

private java.lang.String findHeader(java.util.Vector headers)

find

private boolean find(java.util.Vector v,
                     java.lang.String s)
Find the string in the vector

Parameters:
v - Vector
s - String
Returns:
true if found