1 24 25 package org.objectweb.cjdbc.controller.backend.rewriting; 26 27 import java.util.Hashtable ; 28 import java.util.StringTokenizer ; 29 30 36 public class PatternRewritingRule extends AbstractRewritingRule 37 { 38 private char tokenDelimiter = '?'; 39 private String [] patternArray; 40 private String [] rewriteArray; 41 42 51 public PatternRewritingRule(String queryPattern, String rewrite, 52 boolean caseSensitive, boolean stopOnMatch) 53 { 54 super(queryPattern, rewrite, caseSensitive, stopOnMatch); 55 56 StringTokenizer patternTokenizer = new StringTokenizer (queryPattern, String 58 .valueOf(tokenDelimiter), true); 59 patternArray = new String [patternTokenizer.countTokens()]; 60 int i = 0; 61 try 62 { 63 do 64 { 65 patternArray[i] = patternTokenizer.nextToken(); 66 if (patternArray[i].charAt(0) == tokenDelimiter) 67 { String nextToken = patternTokenizer.nextToken(); 69 patternArray[i] += nextToken.charAt(0); 72 i++; 73 if (nextToken.length() > 1) 74 patternArray[i] = nextToken.substring(1); 76 } 77 i++; 78 } 79 while (patternTokenizer.hasMoreTokens()); 80 } 81 catch (RuntimeException e) 82 { 83 throw new RuntimeException ("Malformed query pattern: " + queryPattern); 84 } 85 StringTokenizer rewriteTokenizer = new StringTokenizer (rewrite, String 86 .valueOf(tokenDelimiter), true); 87 rewriteArray = new String [rewriteTokenizer.countTokens()]; 88 i = 0; 89 try 90 { 91 do 92 { 93 rewriteArray[i] = rewriteTokenizer.nextToken(); 94 if (rewriteArray[i].charAt(0) == tokenDelimiter) 95 { String nextToken = rewriteTokenizer.nextToken(); 97 rewriteArray[i] += nextToken.charAt(0); 100 i++; 101 if (nextToken.length() > 1) 102 rewriteArray[i] = nextToken.substring(1); 104 } 105 i++; 106 } 107 while (rewriteTokenizer.hasMoreTokens()); 108 } 109 catch (RuntimeException e1) 110 { 111 throw new RuntimeException ("Malformed rewrite element: " + rewrite); 112 } 113 } 114 115 118 public String rewrite(String sqlQuery) 119 { 120 Hashtable tokens = null; String lastParameter = null; 122 String currentToken; 123 int oldIndex = 0; 124 int newIndex = 0; 125 126 for (int i = 0; i < patternArray.length; i++) 128 { 129 currentToken = patternArray[i]; 130 if (currentToken == null) 131 break; if (currentToken.charAt(0) == tokenDelimiter) 133 { lastParameter = currentToken; 135 continue; 136 } 137 newIndex = sqlQuery.indexOf(currentToken, oldIndex); 139 if (newIndex == -1) 140 { hasMatched = false; 142 return sqlQuery; 143 } 144 145 if (lastParameter != null) 146 { if (tokens == null) 148 tokens = new Hashtable (); 149 tokens.put(lastParameter, sqlQuery.substring(oldIndex, newIndex)); 150 } 151 oldIndex = newIndex + currentToken.length(); 152 } 153 if (newIndex < sqlQuery.length()) 155 { 156 if (tokens != null) 157 { 158 if (tokens.containsKey(lastParameter)) 159 { hasMatched = false; 161 return sqlQuery; 162 } 163 else 164 tokens.put(lastParameter, sqlQuery.substring(oldIndex)); 165 } 166 } 168 169 hasMatched = true; 170 171 StringBuffer rewrittenQuery = new StringBuffer (); 172 for (int i = 0; i < rewriteArray.length; i++) 173 { 174 currentToken = rewriteArray[i]; 175 if (currentToken == null) 176 break; if (currentToken.charAt(0) != tokenDelimiter) 178 rewrittenQuery.append(currentToken); 179 else 180 rewrittenQuery.append(tokens.get(currentToken)); 181 } 182 return rewrittenQuery.toString(); 183 } 184 } | Popular Tags |