00001
00025
package org.objectweb.cjdbc.controller.backend.rewriting;
00026
00034 public class ReplaceAllRewritingRule extends AbstractRewritingRule
00035 {
00036
00040 public String
rewrite(String sqlQuery)
00041 {
00042
00043
int start;
00044
if (isCaseSensitive)
00045 start = sqlQuery.indexOf(queryPattern);
00046
else
00047 start = sqlQuery.toLowerCase().indexOf(queryPattern);
00048
if (start == -1)
00049 {
00050 hasMatched =
false;
00051
return sqlQuery;
00052 }
00053
00054 hasMatched =
true;
00055
00056
return replace(sqlQuery, queryPattern,
rewrite);
00057 }
00058
00068 public ReplaceAllRewritingRule(String queryPattern, String rewrite,
00069
boolean caseSensitive,
boolean stopOnMatch)
00070 {
00071 super(queryPattern, rewrite, caseSensitive, stopOnMatch);
00072 }
00073
00074 private static String
replace(String s, String oldText, String newText)
00075 {
00076
final int oldLength = oldText.length();
00077
final int newLength = newText.length();
00078
00079
if (oldLength == 0)
00080
throw new IllegalArgumentException(
"cannot replace the empty string");
00081
00082
if (oldText.equals(newText))
00083
return s;
00084
00085
int i = 0;
00086
int x = 0;
00087
00088 StringBuffer sb =
new StringBuffer(s);
00089
00090
while ((i = sb.indexOf(oldText, x)) > -1)
00091 {
00092 sb.delete(i, i + oldLength);
00093 sb.insert(i, newText);
00094 x = i + newLength;
00095 }
00096
00097
return sb.toString();
00098 }
00099 }