KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > hql > classic > PreprocessingParser


1 //$Id: PreprocessingParser.java,v 1.2 2004/12/24 03:51:19 pgmjsd Exp $
2
package org.hibernate.hql.classic;
3
4 import org.hibernate.QueryException;
5 import org.hibernate.hql.CollectionProperties;
6 import org.hibernate.util.StringHelper;
7
8 import java.util.HashSet JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11
12 /**
13  *
14  */

15 public class PreprocessingParser implements Parser {
16
17     private static final Set JavaDoc HQL_OPERATORS;
18
19     static {
20         HQL_OPERATORS = new HashSet JavaDoc();
21         HQL_OPERATORS.add( "<=" );
22         HQL_OPERATORS.add( ">=" );
23         HQL_OPERATORS.add( "=>" );
24         HQL_OPERATORS.add( "=<" );
25         HQL_OPERATORS.add( "!=" );
26         HQL_OPERATORS.add( "<>" );
27         HQL_OPERATORS.add( "!#" );
28         HQL_OPERATORS.add( "!~" );
29         HQL_OPERATORS.add( "!<" );
30         HQL_OPERATORS.add( "!>" );
31         HQL_OPERATORS.add( "is not" );
32         HQL_OPERATORS.add( "not like" );
33         HQL_OPERATORS.add( "not in" );
34         HQL_OPERATORS.add( "not between" );
35         HQL_OPERATORS.add( "not exists" );
36     }
37
38     private Map JavaDoc replacements;
39     private boolean quoted;
40     private StringBuffer JavaDoc quotedString;
41     private ClauseParser parser = new ClauseParser();
42     private String JavaDoc lastToken;
43     private String JavaDoc currentCollectionProp;
44
45     public PreprocessingParser(Map JavaDoc replacements) {
46         this.replacements = replacements;
47     }
48
49     public void token(String JavaDoc token, QueryTranslatorImpl q) throws QueryException {
50
51         //handle quoted strings
52
if ( quoted ) {
53             quotedString.append( token );
54         }
55         if ( "'".equals( token ) ) {
56             if ( quoted ) {
57                 token = quotedString.toString();
58             }
59             else {
60                 quotedString = new StringBuffer JavaDoc( 20 ).append( token );
61             }
62             quoted = !quoted;
63         }
64         if ( quoted ) return;
65
66         //ignore whitespace
67
if ( ParserHelper.isWhitespace( token ) ) return;
68
69         //do replacements
70
String JavaDoc substoken = ( String JavaDoc ) replacements.get( token );
71         token = ( substoken == null ) ? token : substoken;
72
73         //handle HQL2 collection syntax
74
if ( currentCollectionProp != null ) {
75             if ( "(".equals( token ) ) {
76                 return;
77             }
78             else if ( ")".equals( token ) ) {
79                 currentCollectionProp = null;
80                 return;
81             }
82             else {
83                 token = StringHelper.qualify( token, currentCollectionProp );
84             }
85         }
86         else {
87             String JavaDoc prop = CollectionProperties.getNormalizedPropertyName( token.toLowerCase() );
88             if ( prop != null ) {
89                 currentCollectionProp = prop;
90                 return;
91             }
92         }
93
94
95         //handle <=, >=, !=, is not, not between, not in
96
if ( lastToken == null ) {
97             lastToken = token;
98         }
99         else {
100             String JavaDoc doubleToken = ( token.length() > 1 ) ?
101                     lastToken + ' ' + token :
102                     lastToken + token;
103             if ( HQL_OPERATORS.contains( doubleToken.toLowerCase() ) ) {
104                 parser.token( doubleToken, q );
105                 lastToken = null;
106             }
107             else {
108                 parser.token( lastToken, q );
109                 lastToken = token;
110             }
111         }
112
113     }
114
115     public void start(QueryTranslatorImpl q) throws QueryException {
116         quoted = false;
117         parser.start( q );
118     }
119
120     public void end(QueryTranslatorImpl q) throws QueryException {
121         if ( lastToken != null ) parser.token( lastToken, q );
122         parser.end( q );
123         lastToken = null;
124         currentCollectionProp = null;
125     }
126
127 }
128
129
130
131
132
133
134
Popular Tags