FileDocCategorySizeDatePackage
Formatter.javaAPI DocHibernate 3.2.57373Tue May 30 08:17:46 BST 2006org.hibernate.pretty

Formatter

public class Formatter extends Object

Fields Summary
private static final Set
BEGIN_CLAUSES
private static final Set
END_CLAUSES
private static final Set
LOGICAL
private static final Set
QUANTIFIERS
private static final Set
DML
private static final Set
MISC
String
indentString
String
initial
boolean
beginLine
boolean
afterBeginBeforeEnd
boolean
afterByOrSetOrFromOrSelect
boolean
afterValues
boolean
afterOn
boolean
afterBetween
boolean
afterInsert
int
inFunction
int
parensSinceSelect
private LinkedList
parenCounts
private LinkedList
afterByOrFromOrSelects
int
indent
StringBuffer
result
StringTokenizer
tokens
String
lastToken
String
token
String
lcToken
Constructors Summary
public Formatter(String sql)

	
	   
		tokens = new StringTokenizer(
				sql, 
				"()+*/-=<>'`\"[]," + StringHelper.WHITESPACE, 
				true
			);
	
Methods Summary
private voidbeginNewClause()

		if (!afterBeginBeforeEnd) {
			if (afterOn) {
				indent--;
				afterOn=false;
			}
			indent--;
			newline();
		}
		out();
		beginLine = false;
		afterBeginBeforeEnd = true;
	
private voidcloseParen()

		parensSinceSelect--;
		if (parensSinceSelect<0) {
			indent--;
			parensSinceSelect = ( (Integer) parenCounts.removeLast() ).intValue();
			afterByOrSetOrFromOrSelect = ( (Boolean) afterByOrFromOrSelects.removeLast() ).booleanValue();
		}
		if ( inFunction>0 ) {
			inFunction--;
			out();
		}
		else {
			if (!afterByOrSetOrFromOrSelect) {
				indent--;
				newline();
			}
			out();
		}
		beginLine = false;
	
private voidcommaAfterByOrFromOrSelect()

		out();
		newline();
	
private voidcommaAfterOn()

		out();
		indent--;
		newline();
		afterOn = false;
		afterByOrSetOrFromOrSelect = true;
	
private voidendNewClause()

		if (!afterBeginBeforeEnd) {
			indent--;
			if (afterOn) {
				indent--;
				afterOn=false;
			}
			newline();
		}
		out();
		if ( !"union".equals(lcToken) ) indent++;
		newline();
		afterBeginBeforeEnd = false;
		afterByOrSetOrFromOrSelect = "by".equals(lcToken) 
				|| "set".equals(lcToken)
				|| "from".equals(lcToken);
	
public java.lang.Stringformat()

		
		result.append(initial);
		
		while ( tokens.hasMoreTokens() ) {
			token = tokens.nextToken();
			lcToken = token.toLowerCase();
			
			if ( "'".equals(token) ) {
				String t;
				do {
					t = tokens.nextToken();
					token += t;
				} 
				while ( !"'".equals(t) && tokens.hasMoreTokens() ); // cannot handle single quotes
			}		
			else if ( "\"".equals(token) ) {
				String t;
				do {
					t = tokens.nextToken();
					token += t;
				} 
				while ( !"\"".equals(t) );
			}
			
			if ( afterByOrSetOrFromOrSelect && ",".equals(token) ) {
				commaAfterByOrFromOrSelect();
			}
			else if ( afterOn && ",".equals(token) ) {
				commaAfterOn();
			}
			
			else if ( "(".equals(token) ) {
				openParen();
			}
			else if ( ")".equals(token) ) {
				closeParen();
			}

			else if ( BEGIN_CLAUSES.contains(lcToken) ) {
				beginNewClause();
			}

			else if ( END_CLAUSES.contains(lcToken) ) {
				endNewClause(); 
			}
			
			else if ( "select".equals(lcToken) ) {
				select();
			}
			
			else if ( DML.contains(lcToken) ) {
				updateOrInsertOrDelete();
			}
			
			else if ( "values".equals(lcToken) ) {
				values();
			}
			
			else if ( "on".equals(lcToken) ) {
				on();
			}
			
			else if ( afterBetween && lcToken.equals("and") ) {
				misc();
				afterBetween = false;
			}
			
			else if ( LOGICAL.contains(lcToken) ) {
				logical();
			}
			
			else if ( isWhitespace(token) ) {
				white();
			}
			
			else {
				misc();
			}
			
			if ( !isWhitespace( token ) ) lastToken = lcToken;
			
		}
		return result.toString();
	
private static booleanisFunctionName(java.lang.String tok)

		final char begin = tok.charAt(0);
		final boolean isIdentifier = Character.isJavaIdentifierStart( begin ) || '""==begin;
		return isIdentifier && 
				!LOGICAL.contains(tok) && 
				!END_CLAUSES.contains(tok) &&
				!QUANTIFIERS.contains(tok) &&
				!DML.contains(tok) &&
				!MISC.contains(tok);
	
private static booleanisWhitespace(java.lang.String token)

		return StringHelper.WHITESPACE.indexOf(token)>=0;
	
private voidlogical()

		if ( "end".equals(lcToken) ) indent--;
		newline();
		out();
		beginLine = false;
	
public static voidmain(java.lang.String[] args)

		if ( args.length>0 ) System.out.println( 
			new Formatter( StringHelper.join(" ", args) ).format()
		);
	
private voidmisc()

		out();
		if ( "between".equals(lcToken) ) {
			afterBetween = true;
		}
		if (afterInsert) {
			newline();
			afterInsert = false;
		}
		else {
			beginLine = false;
			if ( "case".equals(lcToken) ) {
				indent++;
			}
		}
	
private voidnewline()

		result.append("\n");
		for ( int i=0; i<indent; i++ ) {
			result.append(indentString);
		}
		beginLine = true;
	
private voidon()

		indent++;
		afterOn = true;
		newline();
		out();
		beginLine = false;
	
private voidopenParen()

		if ( isFunctionName( lastToken ) || inFunction>0 ) {
			inFunction++;
		}
		beginLine = false;
		if ( inFunction>0 ) {
			out();
		}
		else {
			out();
			if (!afterByOrSetOrFromOrSelect) {
				indent++;
				newline();
				beginLine = true;
			}
		}
		parensSinceSelect++;
	
private voidout()

		result.append(token);
	
private voidselect()

		out();
		indent++;
		newline();
		parenCounts.addLast( new Integer(parensSinceSelect) );
		afterByOrFromOrSelects.addLast( new Boolean(afterByOrSetOrFromOrSelect) );
		parensSinceSelect = 0;
		afterByOrSetOrFromOrSelect = true;
	
public org.hibernate.pretty.FormattersetIndentString(java.lang.String indent)

		this.indentString = indent;
		return this;
	
public org.hibernate.pretty.FormattersetInitialString(java.lang.String initial)

		this.initial = initial;
		return this;
	
private voidupdateOrInsertOrDelete()

		out();
		indent++;
		beginLine = false;
		if ( "update".equals(lcToken) ) newline();
		if ( "insert".equals(lcToken) ) afterInsert = true;
	
private voidvalues()

		indent--;
		newline();
		out();
		indent++;
		newline();
		afterValues = true;
	
private voidwhite()

		if ( !beginLine ) {
			result.append(" ");
		}