KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > backend > rewriting > SimpleRewritingRule


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.backend.rewriting;
26
27 /**
28  * This class defines a SimpleRewritingRule
29  *
30  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
31  * @version 1.0
32  */

33 public class SimpleRewritingRule extends AbstractRewritingRule
34 {
35
36   private int queryPatternLength;
37
38   /**
39    * Creates a new <code>SimpleRewritingRule.java</code> object
40    *
41    * @param queryPattern SQL pattern to match
42    * @param rewrite rewritten SQL query
43    * @param caseSensitive true if matching is case sensitive
44    * @param stopOnMatch true if rewriting must stop after this rule if it
45    * matches.
46    */

47   public SimpleRewritingRule(String JavaDoc queryPattern, String JavaDoc rewrite,
48       boolean caseSensitive, boolean stopOnMatch)
49   {
50     super(queryPattern, caseSensitive ? rewrite : rewrite.toLowerCase(),
51         caseSensitive, stopOnMatch);
52     queryPatternLength = queryPattern.length();
53   }
54
55   /**
56    * @see org.objectweb.cjdbc.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String)
57    */

58   public String JavaDoc rewrite(String JavaDoc sqlQuery)
59   {
60     // Check first if it is a match
61
int start;
62     if (isCaseSensitive)
63       start = sqlQuery.indexOf(queryPattern);
64     else
65       start = sqlQuery.toLowerCase().indexOf(queryPattern.toLowerCase());
66     if (start == -1)
67     { // No match
68
hasMatched = false;
69       return sqlQuery;
70     }
71     // Match, rewrite the query
72
hasMatched = true;
73     if (start == 0)
74     {
75       if (queryPatternLength < sqlQuery.length())
76         // Match at the beginning of the pattern
77
return rewrite + sqlQuery.substring(queryPatternLength);
78       else
79         // The query was exactly the pattern
80
return rewrite;
81     }
82     else
83     {
84       if (start + queryPatternLength < sqlQuery.length())
85         return sqlQuery.substring(0, start) + rewrite
86             + sqlQuery.substring(start + queryPatternLength);
87       else
88         // Match at the end of the pattern
89
return sqlQuery.substring(0, start) + rewrite;
90     }
91   }
92
93 }
Popular Tags