1 24 25 package org.objectweb.cjdbc.controller.backend.rewriting; 26 27 34 public class ReplaceAllRewritingRule extends AbstractRewritingRule 35 { 36 37 40 public String rewrite(String sqlQuery) 41 { 42 int start; 44 if (isCaseSensitive) 45 start = sqlQuery.indexOf(queryPattern); 46 else 47 start = sqlQuery.toLowerCase().indexOf(queryPattern.toLowerCase()); 48 if (start == -1) 49 { hasMatched = false; 51 return sqlQuery; 52 } 53 hasMatched = true; 55 56 return replace(sqlQuery, queryPattern, rewrite); 57 } 58 59 68 public ReplaceAllRewritingRule(String queryPattern, String rewrite, 69 boolean caseSensitive, boolean stopOnMatch) 70 { 71 super(queryPattern, rewrite, caseSensitive, stopOnMatch); 72 } 73 74 private static String replace(String s, String oldText, String newText) 75 { 76 final int oldLength = oldText.length(); 77 final int newLength = newText.length(); 78 79 if (oldLength == 0) 80 throw new IllegalArgumentException ("cannot replace the empty string"); 81 82 if (oldText.equals(newText)) 83 return s; 84 85 int i = 0; 86 int x = 0; 87 88 StringBuffer sb = new StringBuffer (s); 89 90 while ((i = sb.indexOf(oldText, x)) > -1) 91 { 92 sb.delete(i, i + oldLength); 93 sb.insert(i, newText); 94 x = i + newLength; 95 } 96 97 return sb.toString(); 98 } 99 } | Popular Tags |