01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2002-2004 French National Institute For Research In Computer
04: * Science And Control (INRIA).
05: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
06: * Contact: sequoia@continuent.org
07: *
08: * Licensed under the Apache License, Version 2.0 (the "License");
09: * 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: */package org.continuent.sequoia.controller.backend.rewriting;
23:
24: /**
25: * This class defines a ReplaceAllRewritingRule. Replace all instance of a
26: * <code>String</code> token by another <code>String</code> token
27: *
28: * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
29: * @version 1.0
30: */
31: public class ReplaceAllRewritingRule extends AbstractRewritingRule {
32:
33: /**
34: * @see org.continuent.sequoia.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String)
35: */
36: public String rewrite(String sqlQuery) {
37: // Check first if it is a match
38: int start;
39: if (isCaseSensitive)
40: start = sqlQuery.indexOf(queryPattern);
41: else
42: start = sqlQuery.toLowerCase().indexOf(
43: queryPattern.toLowerCase());
44: if (start == -1) { // No match
45: hasMatched = false;
46: return sqlQuery;
47: }
48: // Match, rewrite the query
49: hasMatched = true;
50:
51: return replace(sqlQuery, queryPattern, rewrite);
52: }
53:
54: /**
55: * Creates a new <code>ReplaceAllRewritingRule.java</code> object
56: *
57: * @param queryPattern SQL pattern to match
58: * @param rewrite rewritten SQL query
59: * @param caseSensitive true if matching is case sensitive
60: * @param stopOnMatch true if rewriting must stop after this rule if it
61: * matches.
62: */
63: public ReplaceAllRewritingRule(String queryPattern, String rewrite,
64: boolean caseSensitive, boolean stopOnMatch) {
65: super (queryPattern, rewrite, caseSensitive, stopOnMatch);
66: }
67:
68: private static String replace(String s, String oldText,
69: String newText) {
70: final int oldLength = oldText.length();
71: final int newLength = newText.length();
72:
73: if (oldLength == 0)
74: throw new IllegalArgumentException(
75: "cannot replace the empty string");
76:
77: if (oldText.equals(newText))
78: return s;
79:
80: int i = 0;
81: int x = 0;
82:
83: StringBuffer sb = new StringBuffer(s);
84:
85: while ((i = sb.indexOf(oldText, x)) > -1) {
86: sb.delete(i, i + oldLength);
87: sb.insert(i, newText);
88: x = i + newLength;
89: }
90:
91: return sb.toString();
92: }
93: }
|