[gatein-commits] gatein SVN: r2065 - in components/common/trunk/common/src: main/java/org/gatein/common/util and 1 other directories.

do-not-reply at jboss.org do-not-reply at jboss.org
Tue Mar 9 19:30:49 EST 2010


Author: chris.laprun at jboss.com
Date: 2010-03-09 19:30:47 -0500 (Tue, 09 Mar 2010)
New Revision: 2065

Modified:
   components/common/trunk/common/src/main/java/org/gatein/common/text/TextTools.java
   components/common/trunk/common/src/main/java/org/gatein/common/util/Tools.java
   components/common/trunk/common/src/test/java/org/gatein/common/StringTestCase.java
Log:
- Moved String manipulation methods to TextTools.
- Added possibility to generate a replacement via a StringReplacementGenerator implementation passed to replaceBoundedString methods.


Modified: components/common/trunk/common/src/main/java/org/gatein/common/text/TextTools.java
===================================================================
--- components/common/trunk/common/src/main/java/org/gatein/common/text/TextTools.java	2010-03-09 20:30:52 UTC (rev 2064)
+++ components/common/trunk/common/src/main/java/org/gatein/common/text/TextTools.java	2010-03-10 00:30:47 UTC (rev 2065)
@@ -22,6 +22,8 @@
  ******************************************************************************/
 package org.gatein.common.text;
 
+import org.gatein.common.util.ParameterValidation;
+
 /**
  * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
  * @version $Revision: 1.1 $
@@ -30,8 +32,8 @@
 {
 
    /**
-    * Returns true if the char c is alpha numeric i.e it belongs to one of the following ranges [0,9], [A,Z] or
-    * [a,z]
+    * Returns true if the char c is alpha numeric i.e it belongs to one of the following ranges [0,9], [A,Z] or [a,z]
+    *
     * @param c the char to test
     * @return true if c is alpha numeric
     */
@@ -42,6 +44,7 @@
 
    /**
     * Returns the hexadecimal value of the provided numeric value.
+    *
     * @param z the numeric value to convert
     * @return the hexadecimal char
     * @throws IllegalArgumentException if the value is not between 0 and 15
@@ -61,4 +64,139 @@
          throw new IllegalArgumentException("Wrong character");
       }
    }
+
+   /**
+    * Replace occurence in a string.
+    *
+    * @param string      the source string
+    * @param pattern     the replaced pattern
+    * @param replacement the replacement text
+    * @return the new string
+    */
+   public static String replace(String string, String pattern, String replacement)
+   {
+      StringBuffer buffer = new StringBuffer(string.length());
+      int previous = 0;
+      int current = string.indexOf(pattern);
+      while (current != -1)
+      {
+         buffer.append(string.substring(previous, current));
+         buffer.append(replacement);
+         previous = current + pattern.length();
+         current = string.indexOf(pattern, previous);
+      }
+      buffer.append(string.substring(previous));
+      return buffer.toString();
+   }
+
+   /**
+    * Same as replaceBoundedString(initial, prefix, suffix, replacement, true, false).
+    *
+    * @param initial
+    * @param prefix
+    * @param suffix
+    * @param replacement
+    * @return
+    */
+   public static String replaceAllInstancesOfBoundedString(String initial, String prefix, String suffix, String replacement)
+   {
+      return replaceBoundedString(initial, prefix, suffix, replacement, true, false);
+   }
+
+   public static String replaceBoundedString(String initial, String prefix, String suffix, String replacement,
+                                             boolean replaceIfBoundedStringEmpty, boolean keepBoundaries)
+   {
+      ParameterValidation.throwIllegalArgExceptionIfNull(replacement, "replacement");
+      return replaceBoundedString(initial, prefix, suffix, new ConstantStringReplacementGenerator(replacement),
+         replaceIfBoundedStringEmpty, keepBoundaries);
+   }
+
+   /**
+    * Replace instances of Strings delimited by the given prefix and suffix (hence, bounded) by a replacement String
+    * computed by the specified StringReplacementGenerator based on the current bounded String match. It is possible to
+    * specify whether the substitution will happen only if the delimited String is non-empty by setting
+    * <code>replaceIfBoundedStringEmpty</code> to <code>false</code>. It is also possible to keep the boundaries (prefix
+    * and suffix) after the substitution by setting <code>keepBoundaries</code> to <code>true</code>.
+    * <p/>
+    * See org.gatein.common.StringTestCase.testReplaceBoundedString() for usage details.
+    *
+    * @param initial                     the String in which we want to replace bounded Strings
+    * @param prefix                      the prefix used identify the beginning of the String targeted for replacement
+    * @param suffix                      the suffix used to identify the end of the String targeted for replacement
+    * @param generator                   the StringReplacementGeneraot generating replacements based on the current
+    *                                    bounded String match
+    * @param replaceIfBoundedStringEmpty <code>true</code> to allow replacement of empty Strings (in essence, insertion
+    *                                    of the replacement String between the prefix and suffix)
+    * @param keepBoundaries              <code>true</code> to keep the prefix and suffix markers in the resulting
+    *                                    String
+    * @return a String where the Strings marked by prefix and suffix have been replaced by replacement
+    */
+   public static String replaceBoundedString(String initial, String prefix, String suffix, StringReplacementGenerator generator,
+                                             boolean replaceIfBoundedStringEmpty, boolean keepBoundaries)
+   {
+      if (ParameterValidation.isNullOrEmpty(initial))
+      {
+         return initial;
+      }
+
+      ParameterValidation.throwIllegalArgExceptionIfNull(generator, "StringReplacementGenerator");
+
+      ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(prefix, "prefix", "Tools.replaceBoundedString");
+      ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(suffix, "suffix", "Tools.replaceBoundedString");
+
+      StringBuffer tmp = new StringBuffer(initial);
+      int prefixIndex = tmp.indexOf(prefix);
+      int suffixLength = suffix.length();
+      int prefixLength = prefix.length();
+
+      while (prefixIndex != -1)
+      {
+         int suffixIndex = tmp.indexOf(suffix, prefixIndex);
+
+         if (suffixIndex != -1)
+         {
+            // we don't care about empty bounded strings or prefix and suffix don't delimit an empty String => replace!
+            if (replaceIfBoundedStringEmpty || suffixIndex != prefixIndex + prefixLength)
+            {
+               String match;
+               if (keepBoundaries)
+               {
+                  match = tmp.substring(prefixIndex + prefixLength, suffixIndex);
+                  tmp.delete(prefixIndex + prefixLength, suffixIndex);
+                  tmp.insert(prefixIndex + prefixLength, generator.getReplacementFor(match));
+               }
+               else
+               {
+                  match = tmp.substring(prefixIndex, suffixIndex + suffixLength);
+                  tmp.delete(prefixIndex, suffixIndex + suffixLength);
+                  tmp.insert(prefixIndex, generator.getReplacementFor(match));
+               }
+            }
+         }
+
+         prefixIndex = tmp.indexOf(prefix, prefixIndex + prefixLength);
+      }
+
+      return tmp.toString();
+   }
+
+   public static interface StringReplacementGenerator
+   {
+      String getReplacementFor(String match);
+   }
+
+   public static class ConstantStringReplacementGenerator implements StringReplacementGenerator
+   {
+      private String replacement;
+
+      public ConstantStringReplacementGenerator(String replacement)
+      {
+         this.replacement = replacement;
+      }
+
+      public String getReplacementFor(String match)
+      {
+         return replacement;
+      }
+   }
 }

Modified: components/common/trunk/common/src/main/java/org/gatein/common/util/Tools.java
===================================================================
--- components/common/trunk/common/src/main/java/org/gatein/common/util/Tools.java	2010-03-09 20:30:52 UTC (rev 2064)
+++ components/common/trunk/common/src/main/java/org/gatein/common/util/Tools.java	2010-03-10 00:30:47 UTC (rev 2065)
@@ -22,6 +22,9 @@
  ******************************************************************************/
 package org.gatein.common.util;
 
+import org.gatein.common.logging.Logger;
+import org.gatein.common.logging.LoggerFactory;
+
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.io.Writer;
@@ -44,9 +47,6 @@
 import java.util.ResourceBundle;
 import java.util.Set;
 
-import org.gatein.common.logging.Logger;
-import org.gatein.common.logging.LoggerFactory;
-
 /**
  * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
  * @author <a href="mailto:theute at jboss.org">Thomas Heute</a>
@@ -750,30 +750,6 @@
    }
 
    /**
-    * Replace occurence in a string.
-    *
-    * @param string      the source string
-    * @param pattern     the replaced pattern
-    * @param replacement the replacement text
-    * @return the new string
-    */
-   public static String replace(String string, String pattern, String replacement)
-   {
-      StringBuffer buffer = new StringBuffer(string.length());
-      int previous = 0;
-      int current = string.indexOf(pattern);
-      while (current != -1)
-      {
-         buffer.append(string.substring(previous, current));
-         buffer.append(replacement);
-         previous = current + pattern.length();
-         current = string.indexOf(pattern, previous);
-      }
-      buffer.append(string.substring(previous));
-      return buffer.toString();
-   }
-
-   /**
     * Append an object to an array of objects. The original array is not modified. The returned array will be of the
     * same component type of the provided array and its first n elements where n is the size of the provided array will
     * be the elements of the provided array. The last element of the array will be the provided object to append.
@@ -808,11 +784,8 @@
    }
 
    /**
-    * Return true if
-    * <ul>
-    *    <li>o1 is null and o2 is null</li>
-    *    <li>o1 is not null and the invocation of <code>equals(Object o)</code> on o1 wit o2 as argument returns true</li>
-    * </ul>
+    * Return true if <ul> <li>o1 is null and o2 is null</li> <li>o1 is not null and the invocation of
+    * <code>equals(Object o)</code> on o1 wit o2 as argument returns true</li> </ul>
     *
     * @param o1 the first argument
     * @param o2 the second argument
@@ -831,94 +804,14 @@
    }
 
    /**
-    * Same as replaceBoundedString(initial, prefix, suffix, replacement, true, false).
-    *
-    * @param initial
-    * @param prefix
-    * @param suffix
-    * @param replacement
-    * @return
-    */
-   public static String replaceAllInstancesOfBoundedString(String initial, String prefix, String suffix, String replacement)
-   {
-      return replaceBoundedString(initial, prefix, suffix, replacement, true, false);
-   }
-
-   /**
-    * Replace instances of Strings delimited by the given prefix and suffix (hence, bounded) by the specified
-    * replacement String. It is possible to specify whether the substitution will happen only if the delimited String is
-    * non-empty by setting <code>replaceIfBoundedStringEmpty</code> to <code>false</code>. It is also possible to keep
-    * the boundaries (prefix and suffix) after the substitution by setting <code>keepBoundaries</code> to
-    * <code>true</code>.
-    * <p/>
-    * See org.gatein.common.StringTestCase.testReplaceBoundedString() for usage details.
-    *
-    * @param initial                     the String in which we want to replace bounded Strings
-    * @param prefix                      the prefix used identify the beginning of the String targeted for replacement
-    * @param suffix                      the suffix used to identify the end of the String targeted for replacement
-    * @param replacement                 the String to replace the bounded String with
-    * @param replaceIfBoundedStringEmpty <code>true</code> to allow replacement of empty Strings (in essence, insertion
-    *                                    of the replacement String between the prefix and suffix)
-    * @param keepBoundaries              <code>true</code> to keep the prefix and suffix markers in the resulting
-    *                                    String
-    * @return a String where the Strings marked by prefix and suffix have been replaced by replacement
-    */
-   public static String replaceBoundedString(String initial, String prefix, String suffix, String replacement,
-                                             boolean replaceIfBoundedStringEmpty, boolean keepBoundaries)
-   {
-      if (initial == null || initial.length() == 0)
-      {
-         return initial;
-      }
-
-      ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(prefix, "prefix", "Tools.replaceBoundedString");
-      ParameterValidation.throwIllegalArgExceptionIfNullOrEmpty(suffix, "suffix", "Tools.replaceBoundedString");
-      ParameterValidation.throwIllegalArgExceptionIfNull(replacement, "replacement");
-
-      StringBuffer tmp = new StringBuffer(initial);
-      int prefixIndex = tmp.indexOf(prefix);
-      int suffixLength = suffix.length();
-      int prefixLength = prefix.length();
-
-      while (prefixIndex != -1)
-      {
-         int suffixIndex = tmp.indexOf(suffix, prefixIndex);
-
-         if (suffixIndex != -1)
-         {
-            // we don't care about empty bounded strings or prefix and suffix don't delimit an empty String => replace!
-            if (replaceIfBoundedStringEmpty || suffixIndex != prefixIndex + prefixLength)
-            {
-               if (keepBoundaries)
-               {
-                  tmp.delete(prefixIndex + prefixLength, suffixIndex);
-                  tmp.insert(prefixIndex + prefixLength, replacement);
-               }
-               else
-               {
-                  tmp.delete(prefixIndex, suffixIndex + suffixLength);
-                  tmp.insert(prefixIndex, replacement);
-               }
-            }
-         }
-
-         prefixIndex = tmp.indexOf(prefix, prefixIndex + prefixLength);
-      }
-
-      return tmp.toString();
-   }
-
-   /**
     * Determines if value is contained in array.
     * <p/>
     * todo: correct this method contract in order to make it more reusable, it looks like for now like a method which
     * has a contract convenient only for some kind of callers.
     * <p/>
-    * <ol>
-    * <li>null value should be accepted (or the method should be called isContainedInButNotForNullValue ?)</li>
+    * <ol> <li>null value should be accepted (or the method should be called isContainedInButNotForNullValue ?)</li>
     * <li>null array should not be accepted (or the method should be called isContainedInExceptIfThePassedArrayIsNull
-    * ?)</li>
-    * </ol>
+    * ?)</li> </ol>
     *
     * @param value
     * @param array

Modified: components/common/trunk/common/src/test/java/org/gatein/common/StringTestCase.java
===================================================================
--- components/common/trunk/common/src/test/java/org/gatein/common/StringTestCase.java	2010-03-09 20:30:52 UTC (rev 2064)
+++ components/common/trunk/common/src/test/java/org/gatein/common/StringTestCase.java	2010-03-10 00:30:47 UTC (rev 2065)
@@ -23,7 +23,7 @@
 package org.gatein.common;
 
 import junit.framework.TestCase;
-import org.gatein.common.util.Tools;
+import org.gatein.common.text.TextTools;
 
 /**
  * @author <a href="mailto:julien at jboss.org">Julien Viet</a>
@@ -39,22 +39,22 @@
 
    public void testReplace()
    {
-      assertEquals("", Tools.replace("", "abc", "def"));
-      assertEquals("defg", Tools.replace("abc", "abc", "defg"));
-      assertEquals("_defg_", Tools.replace("_abc_", "abc", "defg"));
-      assertEquals("_defgdefg_", Tools.replace("_abcabc_", "abc", "defg"));
-      assertEquals("_defg_defg_", Tools.replace("_abc_abc_", "abc", "defg"));
+      assertEquals("", TextTools.replace("", "abc", "def"));
+      assertEquals("defg", TextTools.replace("abc", "abc", "defg"));
+      assertEquals("_defg_", TextTools.replace("_abc_", "abc", "defg"));
+      assertEquals("_defgdefg_", TextTools.replace("_abcabc_", "abc", "defg"));
+      assertEquals("_defg_defg_", TextTools.replace("_abc_abc_", "abc", "defg"));
    }
 
    public void testReplaceBoundedString()
    {
-      assertEquals("", Tools.replaceAllInstancesOfBoundedString("", "PREFIX", "SUFFIX", "REPLACEMENT"));
-      assertEquals("REPLACEMENT", Tools.replaceAllInstancesOfBoundedString("PREFIXSUFFIX", "PREFIX", "SUFFIX", "REPLACEMENT"));
-      assertEquals("PREFIXSUFFIX", Tools.replaceBoundedString("PREFIXSUFFIX", "PREFIX", "SUFFIX", "REPLACEMENT", false, true));
-      assertEquals("PREFIXSUFFIX", Tools.replaceBoundedString("PREFIXSUFFIX", "PREFIX", "SUFFIX", "REPLACEMENT", false, false));
-      assertEquals("aaaaREPLACEMENTccccc", Tools.replaceAllInstancesOfBoundedString("aaaaPREFIXbbbbbSUFFIXccccc", "PREFIX", "SUFFIX", "REPLACEMENT"));
-      assertEquals("aaaPREFIXbbbbSUFF", Tools.replaceAllInstancesOfBoundedString("aaaPREFIXbbbbSUFF", "PREFIX", "SUFFIX", "REPLACEMENT"));
-      assertEquals("aRcccReeeR", Tools.replaceAllInstancesOfBoundedString("aPbbScccPdSeeePS", "P", "S", "R"));
-      assertEquals("PSaPScccReeePS", Tools.replaceBoundedString("PSaPScccPdSeeePS", "P", "S", "R", false, false));
+      assertEquals("", TextTools.replaceAllInstancesOfBoundedString("", "PREFIX", "SUFFIX", "REPLACEMENT"));
+      assertEquals("REPLACEMENT", TextTools.replaceAllInstancesOfBoundedString("PREFIXSUFFIX", "PREFIX", "SUFFIX", "REPLACEMENT"));
+      assertEquals("PREFIXSUFFIX", TextTools.replaceBoundedString("PREFIXSUFFIX", "PREFIX", "SUFFIX", "REPLACEMENT", false, true));
+      assertEquals("PREFIXSUFFIX", TextTools.replaceBoundedString("PREFIXSUFFIX", "PREFIX", "SUFFIX", "REPLACEMENT", false, false));
+      assertEquals("aaaaREPLACEMENTccccc", TextTools.replaceAllInstancesOfBoundedString("aaaaPREFIXbbbbbSUFFIXccccc", "PREFIX", "SUFFIX", "REPLACEMENT"));
+      assertEquals("aaaPREFIXbbbbSUFF", TextTools.replaceAllInstancesOfBoundedString("aaaPREFIXbbbbSUFF", "PREFIX", "SUFFIX", "REPLACEMENT"));
+      assertEquals("aRcccReeeR", TextTools.replaceAllInstancesOfBoundedString("aPbbScccPdSeeePS", "P", "S", "R"));
+      assertEquals("PSaPScccReeePS", TextTools.replaceBoundedString("PSaPScccPdSeeePS", "P", "S", "R", false, false));
    }
 }



More information about the gatein-commits mailing list