Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

ReplaceAllRewritingRule.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2005 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Nicolas Modrzyk
00022  * Contributor(s): ______________________.
00023  */
00024 
00025 package org.objectweb.cjdbc.controller.backend.rewriting;
00026 
00027 /**
00028  * This class defines a ReplaceAllRewritingRule. Replace all instance of a
00029  * <code>String</code> token by another <code>String</code> token
00030  * 
00031  * @author <a href="mailto:Nicolas.Modrzyk@inria.fr">Nicolas Modrzyk </a>
00032  * @version 1.0
00033  */
00034 public class ReplaceAllRewritingRule extends AbstractRewritingRule
00035 {
00036 
00037   /**
00038    * @see org.objectweb.cjdbc.controller.backend.rewriting.AbstractRewritingRule#rewrite(java.lang.String)
00039    */
00040   public String rewrite(String sqlQuery)
00041   {
00042     // Check first if it is a match
00043     int start;
00044     if (isCaseSensitive)
00045       start = sqlQuery.indexOf(queryPattern);
00046     else
00047       start = sqlQuery.toLowerCase().indexOf(queryPattern.toLowerCase());
00048     if (start == -1)
00049     { // No match
00050       hasMatched = false;
00051       return sqlQuery;
00052     }
00053     // Match, rewrite the query
00054     hasMatched = true;
00055 
00056     return replace(sqlQuery, queryPattern, rewrite);
00057   }
00058 
00059   /**
00060    * Creates a new <code>ReplaceAllRewritingRule.java</code> object
00061    * 
00062    * @param queryPattern SQL pattern to match
00063    * @param rewrite rewritten SQL query
00064    * @param caseSensitive true if matching is case sensitive
00065    * @param stopOnMatch true if rewriting must stop after this rule if it
00066    *          matches.
00067    */
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 }

Generated on Mon Apr 11 22:01:34 2005 for C-JDBC by  doxygen 1.3.9.1