KickJava   Java API By Example, From Geeks To Geeks.

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


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): Nicolas Modrzyk
21  * Contributor(s): ______________________.
22  */

23
24 package org.continuent.sequoia.controller.backend.rewriting;
25
26 /**
27  * This class defines a ReplaceAllRewritingRule. Replace all instance of a
28  * <code>String</code> token by another <code>String</code> token
29  *
30  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
31  * @version 1.0
32  */

33 public class ReplaceAllRewritingRule extends AbstractRewritingRule
34 {
35
36   /**
37    * @see org.continuent.sequoia.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String)
38    */

39   public String JavaDoc rewrite(String JavaDoc sqlQuery)
40   {
41     // Check first if it is a match
42
int start;
43     if (isCaseSensitive)
44       start = sqlQuery.indexOf(queryPattern);
45     else
46       start = sqlQuery.toLowerCase().indexOf(queryPattern.toLowerCase());
47     if (start == -1)
48     { // No match
49
hasMatched = false;
50       return sqlQuery;
51     }
52     // Match, rewrite the query
53
hasMatched = true;
54
55     return replace(sqlQuery, queryPattern, rewrite);
56   }
57
58   /**
59    * Creates a new <code>ReplaceAllRewritingRule.java</code> object
60    *
61    * @param queryPattern SQL pattern to match
62    * @param rewrite rewritten SQL query
63    * @param caseSensitive true if matching is case sensitive
64    * @param stopOnMatch true if rewriting must stop after this rule if it
65    * matches.
66    */

67   public ReplaceAllRewritingRule(String JavaDoc queryPattern, String JavaDoc rewrite,
68       boolean caseSensitive, boolean stopOnMatch)
69   {
70     super(queryPattern, rewrite, caseSensitive, stopOnMatch);
71   }
72
73   private static String JavaDoc replace(String JavaDoc s, String JavaDoc oldText, String JavaDoc newText)
74   {
75     final int oldLength = oldText.length();
76     final int newLength = newText.length();
77
78     if (oldLength == 0)
79       throw new IllegalArgumentException JavaDoc("cannot replace the empty string");
80
81     if (oldText.equals(newText))
82       return s;
83
84     int i = 0;
85     int x = 0;
86
87     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s);
88
89     while ((i = sb.indexOf(oldText, x)) > -1)
90     {
91       sb.delete(i, i + oldLength);
92       sb.insert(i, newText);
93       x = i + newLength;
94     }
95
96     return sb.toString();
97   }
98 }
Popular Tags