KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > backend > rewriting > PatternRewritingRule


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.backend.rewriting;
26
27 import java.util.Hashtable JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29
30 /**
31  * This class defines a PatternRewritingRule
32  *
33  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
34  * @version 1.0
35  */

36 public class PatternRewritingRule extends AbstractRewritingRule
37 {
38   private char tokenDelimiter = '?';
39   private String JavaDoc[] patternArray;
40   private String JavaDoc[] rewriteArray;
41
42   /**
43    * Creates a new <code>PatternRewritingRule.java</code> object
44    *
45    * @param queryPattern SQL pattern to match
46    * @param rewrite rewritten SQL query
47    * @param caseSensitive true if matching is case sensitive
48    * @param stopOnMatch true if rewriting must stop after this rule if it
49    * matches.
50    */

51   public PatternRewritingRule(String JavaDoc queryPattern, String JavaDoc rewrite,
52       boolean caseSensitive, boolean stopOnMatch)
53   {
54     super(queryPattern, rewrite, caseSensitive, stopOnMatch);
55
56     // Parse queryPattern and rewrite to extract the parameters ?1 ?2 ...
57
StringTokenizer JavaDoc patternTokenizer = new StringTokenizer JavaDoc(queryPattern, String
58         .valueOf(tokenDelimiter), true);
59     patternArray = new String JavaDoc[patternTokenizer.countTokens()];
60     int i = 0;
61     try
62     {
63       do
64       {
65         patternArray[i] = patternTokenizer.nextToken();
66         if (patternArray[i].charAt(0) == tokenDelimiter)
67         { // We found a delimiter (?)
68
String JavaDoc nextToken = patternTokenizer.nextToken();
69           // Add the parameter number (only works with 1 digit parameters)
70
//For example ?124 will be recognized as ?1
71
patternArray[i] += nextToken.charAt(0);
72           i++;
73           if (nextToken.length() > 1)
74             // This is the next token
75
patternArray[i] = nextToken.substring(1);
76         }
77         i++;
78       }
79       while (patternTokenizer.hasMoreTokens());
80     }
81     catch (RuntimeException JavaDoc e)
82     {
83       throw new RuntimeException JavaDoc("Malformed query pattern: " + queryPattern);
84     }
85     StringTokenizer JavaDoc rewriteTokenizer = new StringTokenizer JavaDoc(rewrite, String
86         .valueOf(tokenDelimiter), true);
87     rewriteArray = new String JavaDoc[rewriteTokenizer.countTokens()];
88     i = 0;
89     try
90     {
91       do
92       {
93         rewriteArray[i] = rewriteTokenizer.nextToken();
94         if (rewriteArray[i].charAt(0) == tokenDelimiter)
95         { // We found a delimiter (?)
96
String JavaDoc nextToken = rewriteTokenizer.nextToken();
97           // Add the parameter number (only works with 1 digit parameters)
98
//For example ?124 will be recognized as ?1
99
rewriteArray[i] += nextToken.charAt(0);
100           i++;
101           if (nextToken.length() > 1)
102             // This is the next token
103
rewriteArray[i] = nextToken.substring(1);
104         }
105         i++;
106       }
107       while (rewriteTokenizer.hasMoreTokens());
108     }
109     catch (RuntimeException JavaDoc e1)
110     {
111       throw new RuntimeException JavaDoc("Malformed rewrite element: " + rewrite);
112     }
113   }
114
115   /**
116    * @see org.objectweb.cjdbc.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String)
117    */

118   public String JavaDoc rewrite(String JavaDoc sqlQuery)
119   {
120     Hashtable JavaDoc tokens = null; // Parameters value in the query
121
String JavaDoc lastParameter = null;
122     String JavaDoc currentToken;
123     int oldIndex = 0;
124     int newIndex = 0;
125
126     // Check for match and collect parameters into tokens
127
for (int i = 0; i < patternArray.length; i++)
128     {
129       currentToken = patternArray[i];
130       if (currentToken == null)
131         break; // Last token was a parameter
132
if (currentToken.charAt(0) == tokenDelimiter)
133       { // A new parameter is expected
134
lastParameter = currentToken;
135         continue;
136       }
137       // Here is the value of the parameter
138
newIndex = sqlQuery.indexOf(currentToken, oldIndex);
139       if (newIndex == -1)
140       { // No match
141
hasMatched = false;
142         return sqlQuery;
143       }
144
145       if (lastParameter != null)
146       { // Add the parameter value
147
if (tokens == null)
148           tokens = new Hashtable JavaDoc();
149         tokens.put(lastParameter, sqlQuery.substring(oldIndex, newIndex));
150       }
151       oldIndex = newIndex + currentToken.length();
152     }
153     // Last parameter
154
if (newIndex < sqlQuery.length())
155     {
156       if (tokens != null)
157       {
158         if (tokens.containsKey(lastParameter))
159         { // No match on the end of the pattern
160
hasMatched = false;
161           return sqlQuery;
162         }
163         else
164           tokens.put(lastParameter, sqlQuery.substring(oldIndex));
165       }
166       // Here, we probably had a match without parameters. What's the point?
167
}
168
169     hasMatched = true;
170
171     StringBuffer JavaDoc rewrittenQuery = new StringBuffer JavaDoc();
172     for (int i = 0; i < rewriteArray.length; i++)
173     {
174       currentToken = rewriteArray[i];
175       if (currentToken == null)
176         break; // Last token was a parameter
177
if (currentToken.charAt(0) != tokenDelimiter)
178         rewrittenQuery.append(currentToken);
179       else
180         rewrittenQuery.append(tokens.get(currentToken));
181     }
182     return rewrittenQuery.toString();
183   }
184 }
Popular Tags