KickJava   Java API By Example, From Geeks To Geeks.

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


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): Nicolas Modrzyk
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.backend.rewriting;
26
27 /**
28  * This class defines a ReplaceAllRewritingRule. Replace all instance of a
29  * <code>String</code> token by another <code>String</code> token
30  *
31  * @author <a HREF="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
32  * @version 1.0
33  */

34 public class ReplaceAllRewritingRule extends AbstractRewritingRule
35 {
36
37   /**
38    * @see org.objectweb.cjdbc.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String)
39    */

40   public String JavaDoc rewrite(String JavaDoc sqlQuery)
41   {
42     // Check first if it is a match
43
int start;
44     if (isCaseSensitive)
45       start = sqlQuery.indexOf(queryPattern);
46     else
47       start = sqlQuery.toLowerCase().indexOf(queryPattern.toLowerCase());
48     if (start == -1)
49     { // No match
50
hasMatched = false;
51       return sqlQuery;
52     }
53     // Match, rewrite the query
54
hasMatched = true;
55
56     return replace(sqlQuery, queryPattern, rewrite);
57   }
58
59   /**
60    * Creates a new <code>ReplaceAllRewritingRule.java</code> object
61    *
62    * @param queryPattern SQL pattern to match
63    * @param rewrite rewritten SQL query
64    * @param caseSensitive true if matching is case sensitive
65    * @param stopOnMatch true if rewriting must stop after this rule if it
66    * matches.
67    */

68   public ReplaceAllRewritingRule(String JavaDoc queryPattern, String JavaDoc rewrite,
69       boolean caseSensitive, boolean stopOnMatch)
70   {
71     super(queryPattern, rewrite, caseSensitive, stopOnMatch);
72   }
73
74   private static String JavaDoc replace(String JavaDoc s, String JavaDoc oldText, String JavaDoc newText)
75   {
76     final int oldLength = oldText.length();
77     final int newLength = newText.length();
78
79     if (oldLength == 0)
80       throw new IllegalArgumentException JavaDoc("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 JavaDoc sb = new StringBuffer JavaDoc(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