KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > backend > rewriting > SimpleRewritingRule


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): ______________________.
22  */

23
24 package org.continuent.sequoia.controller.backend.rewriting;
25
26 /**
27  * This class defines a SimpleRewritingRule
28  *
29  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
30  * @version 1.0
31  */

32 public class SimpleRewritingRule extends AbstractRewritingRule
33 {
34
35   private int queryPatternLength;
36
37   /**
38    * Creates a new <code>SimpleRewritingRule.java</code> object
39    *
40    * @param queryPattern SQL pattern to match
41    * @param rewrite rewritten SQL query
42    * @param caseSensitive true if matching is case sensitive
43    * @param stopOnMatch true if rewriting must stop after this rule if it
44    * matches.
45    */

46   public SimpleRewritingRule(String JavaDoc queryPattern, String JavaDoc rewrite,
47       boolean caseSensitive, boolean stopOnMatch)
48   {
49     super(queryPattern, caseSensitive ? rewrite : rewrite.toLowerCase(),
50         caseSensitive, stopOnMatch);
51     queryPatternLength = queryPattern.length();
52   }
53
54   /**
55    * @see org.continuent.sequoia.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String)
56    */

57   public String JavaDoc rewrite(String JavaDoc sqlQuery)
58   {
59     // Check first if it is a match
60
int start;
61     if (isCaseSensitive)
62       start = sqlQuery.indexOf(queryPattern);
63     else
64       start = sqlQuery.toLowerCase().indexOf(queryPattern.toLowerCase());
65     if (start == -1)
66     { // No match
67
hasMatched = false;
68       return sqlQuery;
69     }
70     // Match, rewrite the query
71
hasMatched = true;
72     if (start == 0)
73     {
74       if (queryPatternLength < sqlQuery.length())
75         // Match at the beginning of the pattern
76
return rewrite + sqlQuery.substring(queryPatternLength);
77       else
78         // The query was exactly the pattern
79
return rewrite;
80     }
81     else
82     {
83       if (start + queryPatternLength < sqlQuery.length())
84         return sqlQuery.substring(0, start) + rewrite
85             + sqlQuery.substring(start + queryPatternLength);
86       else
87         // Match at the end of the pattern
88
return sqlQuery.substring(0, start) + rewrite;
89     }
90   }
91
92 }
Popular Tags