FileDocCategorySizeDatePackage
PreprocessingParser.javaAPI DocHibernate 3.2.53015Thu Dec 23 20:51:20 GMT 2004org.hibernate.hql.classic

PreprocessingParser

public class PreprocessingParser extends Object implements Parser

Fields Summary
private static final Set
HQL_OPERATORS
private Map
replacements
private boolean
quoted
private StringBuffer
quotedString
private ClauseParser
parser
private String
lastToken
private String
currentCollectionProp
Constructors Summary
public PreprocessingParser(Map replacements)


	   
		this.replacements = replacements;
	
Methods Summary
public voidend(QueryTranslatorImpl q)

		if ( lastToken != null ) parser.token( lastToken, q );
		parser.end( q );
		lastToken = null;
		currentCollectionProp = null;
	
public voidstart(QueryTranslatorImpl q)

		quoted = false;
		parser.start( q );
	
public voidtoken(java.lang.String token, QueryTranslatorImpl q)


		//handle quoted strings
		if ( quoted ) {
			quotedString.append( token );
		}
		if ( "'".equals( token ) ) {
			if ( quoted ) {
				token = quotedString.toString();
			}
			else {
				quotedString = new StringBuffer( 20 ).append( token );
			}
			quoted = !quoted;
		}
		if ( quoted ) return;

		//ignore whitespace
		if ( ParserHelper.isWhitespace( token ) ) return;

		//do replacements
		String substoken = ( String ) replacements.get( token );
		token = ( substoken == null ) ? token : substoken;

		//handle HQL2 collection syntax
		if ( currentCollectionProp != null ) {
			if ( "(".equals( token ) ) {
				return;
			}
			else if ( ")".equals( token ) ) {
				currentCollectionProp = null;
				return;
			}
			else {
				token = StringHelper.qualify( token, currentCollectionProp );
			}
		}
		else {
			String prop = CollectionProperties.getNormalizedPropertyName( token.toLowerCase() );
			if ( prop != null ) {
				currentCollectionProp = prop;
				return;
			}
		}


		//handle <=, >=, !=, is not, not between, not in
		if ( lastToken == null ) {
			lastToken = token;
		}
		else {
			String doubleToken = ( token.length() > 1 ) ?
					lastToken + ' " + token :
					lastToken + token;
			if ( HQL_OPERATORS.contains( doubleToken.toLowerCase() ) ) {
				parser.token( doubleToken, q );
				lastToken = null;
			}
			else {
				parser.token( lastToken, q );
				lastToken = token;
			}
		}