[gatein-commits] gatein SVN: r4669 - in components/common/trunk/common/src: test/java/org/gatein/common and 1 other directory.
do-not-reply at jboss.org
do-not-reply at jboss.org
Thu Oct 14 08:39:35 EDT 2010
Author: chris.laprun at jboss.com
Date: 2010-10-14 08:39:34 -0400 (Thu, 14 Oct 2010)
New Revision: 4669
Modified:
components/common/trunk/common/src/main/java/org/gatein/common/text/TextTools.java
components/common/trunk/common/src/test/java/org/gatein/common/StringTestCase.java
Log:
- GTNCOMMON-13:
+ Added possibility of knowing whether the match was from a prefix only match.
+ Match is the remainder of the string if the match was from prefix only instead of simply the prefix.
+ Fixed an issue where the parser would miss matches if prefix and suffix are too close one to another.
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-10-14 09:48:30 UTC (rev 4668)
+++ components/common/trunk/common/src/main/java/org/gatein/common/text/TextTools.java 2010-10-14 12:39:34 UTC (rev 4669)
@@ -136,7 +136,7 @@
* 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>. <br/> Note that it
- * is possible to specify that the suffix is optional, in which case, the prefix will be passed on to the specified
+ * is possible to specify that the suffix is optional, will be passed as a match on to the specified
* StringReplacementGenerator and be replaced. <br/> See org.gatein.common.StringTestCase.testReplaceBoundedString()
* for usage details.
*
@@ -168,6 +168,7 @@
int prefixIndex = tmp.indexOf(prefix);
final int prefixLength = prefix.length();
boolean suffixAbsent = suffix == null;
+ int suffixLength = suffixAbsent ? 0 : suffix.length();
// nothing to do if didn't ask for an optional suffix and we have one and it's not present in our string
if (!suffixIsOptional && suffix != null && tmp.indexOf(suffix) == -1)
@@ -181,17 +182,19 @@
int suffixIndex;
if (suffixAbsent)
{
+ String match = tmp.substring(prefixIndex + prefixLength);
+
// replace prefix with replacement
if (keepBoundaries)
{
// just insert replacement for prefix
- tmp.insert(prefixIndex + prefixLength, generator.getReplacementFor(prefix, prefix, suffix));
+ tmp.insert(prefixIndex + prefixLength, generator.getReplacementFor(match, prefix, suffix, true));
}
else
{
// delete prefix then insert remplacement instead
tmp.delete(prefixIndex, prefixIndex + prefixLength);
- tmp.insert(prefixIndex, generator.getReplacementFor(prefix, prefix, suffix));
+ tmp.insert(prefixIndex, generator.getReplacementFor(match, prefix, suffix, true));
}
// new lookup starting position
@@ -215,12 +218,20 @@
// if suffix is optional, look for potential next prefix instance that we'd need to replace
int nextPrefixIndex = tmp.indexOf(prefix, prefixIndex + prefixLength);
+ // check that we're not matching the suffix, which can happen if they are close one to another as is the case with WSRP
+ if (nextPrefixIndex >= suffixIndex && nextPrefixIndex <= (suffixIndex + suffixLength))
+ {
+ // if that's the case, look for the next one instead
+ nextPrefixIndex = tmp.indexOf(prefix, suffixIndex + suffixLength);
+ }
+
if (nextPrefixIndex != -1 && nextPrefixIndex <= suffixIndex)
{
// we've found an in-between prefix, use it as the suffix for the current match
// delete prefix then insert remplacement instead
+ String match = tmp.substring(prefixIndex + prefixLength, nextPrefixIndex);
tmp.delete(prefixIndex, prefixIndex + prefixLength);
- String replacement = generator.getReplacementFor(prefix, prefix, suffix);
+ String replacement = generator.getReplacementFor(match, prefix, suffix, true);
tmp.insert(prefixIndex, replacement);
prefixIndex = nextPrefixIndex - prefixLength + replacement.length();
@@ -232,9 +243,15 @@
if (replaceIfBoundedStringEmpty || suffixIndex != prefixIndex + prefixLength)
{
String match = tmp.substring(prefixIndex + prefixLength, suffixIndex);
+ String replacement = generator.getReplacementFor(match, prefix, suffix, false);
+ int changeInLength = replacement.length() - match.length();
+
+ // compute the next lookup index from which we will look for the next prefix
+ int nextLookupIndex = prefixIndex + changeInLength;
+
if (keepBoundaries)
{
- if (suffix != null)
+ if (!suffixAbsent)
{
// delete only match
tmp.delete(prefixIndex + prefixLength, suffixIndex);
@@ -243,13 +260,12 @@
{
// delete nothing
}
- tmp.insert(prefixIndex + prefixLength, generator.getReplacementFor(match, prefix, suffix));
+ tmp.insert(prefixIndex + prefixLength, replacement);
+ nextLookupIndex += prefixLength + suffixLength;
}
else
{
- int suffixLength = suffix != null ? suffix.length() : 0;
-
- if (suffix != null)
+ if (!suffixAbsent)
{
// if we have a suffix, delete everything between start of prefix and end of suffix
tmp.delete(prefixIndex, suffixIndex + suffixLength);
@@ -259,12 +275,14 @@
// only delete prefix
tmp.delete(prefixIndex, prefixIndex + prefixLength);
}
- tmp.insert(prefixIndex, generator.getReplacementFor(match, prefix, suffix));
+ tmp.insert(prefixIndex, replacement);
}
+
+ prefixIndex = nextLookupIndex;
}
}
- prefixIndex = tmp.indexOf(prefix, prefixIndex + prefixLength);
+ prefixIndex = tmp.indexOf(prefix, prefixIndex + 1); // +1 to avoid infinite loop on border cases
}
}
@@ -274,7 +292,7 @@
public static interface StringReplacementGenerator
{
- String getReplacementFor(String match, String prefix, String suffix);
+ String getReplacementFor(String match, String prefix, String suffix, boolean matchedPrefixOnly);
}
public static class ConstantStringReplacementGenerator implements StringReplacementGenerator
@@ -286,7 +304,7 @@
this.replacement = replacement;
}
- public String getReplacementFor(String match, String prefix, String suffix)
+ public String getReplacementFor(String match, String prefix, String suffix, boolean matchedPrefixOnly)
{
return replacement;
}
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-10-14 09:48:30 UTC (rev 4668)
+++ components/common/trunk/common/src/test/java/org/gatein/common/StringTestCase.java 2010-10-14 12:39:34 UTC (rev 4669)
@@ -1,25 +1,25 @@
-/******************************************************************************
- * JBoss, a division of Red Hat *
- * Copyright 2009, Red Hat Middleware, LLC, and individual *
- * contributors as indicated by the @authors tag. See the *
- * copyright.txt in the distribution for a full listing of *
- * individual contributors. *
- * *
- * This is free software; you can redistribute it and/or modify it *
- * under the terms of the GNU Lesser General Public License as *
- * published by the Free Software Foundation; either version 2.1 of *
- * the License, or (at your option) any later version. *
- * *
- * This software is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
- * Lesser General Public License for more details. *
- * *
- * You should have received a copy of the GNU Lesser General Public *
- * License along with this software; if not, write to the Free *
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
- ******************************************************************************/
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
package org.gatein.common;
import junit.framework.TestCase;
@@ -56,13 +56,13 @@
assertEquals("", TextTools.replaceAllInstancesOfBoundedString("", PREFIX, "", REPLACEMENT));
assertEquals("", TextTools.replaceAllInstancesOfBoundedString("", "", "", REPLACEMENT));
- DEF_GEN.setExpectedMatch("");
+ DEF_GEN.setExpectedMatch(new Pair("", false));
assertEquals(REPLACEMENT, TextTools.replaceAllInstancesOfBoundedString("PREFIXSUF", PREFIX, SUFFIX, DEF_GEN));
- DEF_GEN.setExpectedMatch("bbbbb");
+ DEF_GEN.setExpectedMatch(new Pair("bbbbb", false));
assertEquals("aaaaREPLACEMENTccccc", TextTools.replaceAllInstancesOfBoundedString("aaaaPREFIXbbbbbSUFccccc", PREFIX, SUFFIX, DEF_GEN));
- DEF_GEN.setExpectedMatch(null);
+ DEF_GEN.setExpectedMatch((Pair)null);
assertEquals("aaaPREFIXbbbbSUFF", TextTools.replaceAllInstancesOfBoundedString("aaaPREFIXbbbbSUFF", PREFIX, "SUFFI", DEF_GEN));
assertEquals("aRcccReeeR", TextTools.replaceAllInstancesOfBoundedString("aPbbScccPdSeeePS", "P", "S", "R"));
}
@@ -73,22 +73,75 @@
replaceBoundedString(true);
}
+ public void testReplaceStringsWithNullSuffix()
+ {
+ replaceStringsWithOptionalSuffix(false, null);
+ replaceStringsWithOptionalSuffix(true, null);
+ }
+
+ public void testReplaceStringsWithOptionalSuffix()
+ {
+ replaceStringsWithOptionalSuffix(true, SUFFIX);
+ buildTest(REPLACEMENT + "blah" + REPLACEMENT + "foo", PREFIX + "blah" + PREFIX + "replaced" + SUFFIX + "foo", REPLACEMENT, PREFIX, SUFFIX, false, false, true, new Pair("blah", true), new Pair("replaced", false));
+ buildTest(REPLACEMENT + "blah" + PREFIX + REPLACEMENT + SUFFIX + "foo", PREFIX + "blah" + PREFIX + "replaced" + SUFFIX + "foo", REPLACEMENT, PREFIX, SUFFIX, false, true, true, new Pair("blah", true), new Pair("replaced", false));
+ }
+
+ /*public void testReplaceWSRPTokens()
+ {
+ String original = "<form method='post' action='wsrp_rewrite?wsrp-urlType=blockingAction&wsrp" +
+ "-interactionState=JBPNS_/wsrp_rewrite' id='wsrp_rewrite_portfolioManager'><table><tr><td>Stock symbol</t" +
+ "d><td><input name='symbol'/></td></tr><tr><td><input type='submit' value='Submit'></td></tr></table></form>";
+
+ String expected = "<form method='post' action='foo' id='namespaceportfolioManager'><table><tr><td>Stock symbol</t" +
+ "d><td><input name='symbol'/></td></tr><tr><td><input type='submit' value='Submit'></td></tr></table></form>";
+
+ String replaced = TextTools.replaceBoundedString(original, "wsrp_rewrite", "/wsrp_rewrite", new TextTools.StringReplacementGenerator()
+ {
+ public String getReplacementFor(String match, String prefix, String suffix, boolean matchedPrefixOnly)
+ {
+ if(matchedPrefixOnly)
+ {
+ return "namespace" + match.substring(1);
+ }
+ else
+ {
+ return "foo";
+ }
+ }
+ }, true, false, true);
+
+ assertEquals(expected, replaced);
+ }*/
+
private void replaceBoundedString(boolean suffixIsOptional)
{
buildTestWithDefaults(PREFIX + SUFFIX, PREFIX + SUFFIX, false, true, suffixIsOptional, "");
buildTestWithDefaults(PREFIX + SUFFIX, PREFIX + SUFFIX, false, false, suffixIsOptional, "");
buildTestWithDefaults(REPLACEMENT, PREFIX + SUFFIX, true, false, suffixIsOptional, "");
buildTestWithDefaults(PREFIX + REPLACEMENT + SUFFIX, PREFIX + SUFFIX, true, true, suffixIsOptional, "");
- buildTest(PREFIX + SUFFIX, PREFIX + SUFFIX, "", PREFIX, SUFFIX, false, false, suffixIsOptional, "");
- buildTest("PSaPScccReeePS", "PSaPScccPdSeeePS", "R", "P", "S", false, false, suffixIsOptional, "d");
+ buildTest(PREFIX + SUFFIX, PREFIX + SUFFIX, "", PREFIX, SUFFIX, false, false, suffixIsOptional, new Pair("", false));
+ buildTest("PSaPScccReeePS", "PSaPScccPdSeeePS", "R", "P", "S", false, false, suffixIsOptional, new Pair("d", false));
}
+ private void replaceStringsWithOptionalSuffix(boolean suffixIsOptional, String suffix)
+ {
+ buildTest(REPLACEMENT + "blah", PREFIX + "blah", REPLACEMENT, PREFIX, suffix, false, false, suffixIsOptional, new Pair("blah", true));
+ buildTest(PREFIX + REPLACEMENT + "blah", PREFIX + "blah", REPLACEMENT, PREFIX, suffix, false, true, suffixIsOptional, new Pair("blah", true));
+ buildTest(REPLACEMENT + "blah", PREFIX + "blah", REPLACEMENT, PREFIX, suffix, true, false, suffixIsOptional, new Pair("blah", true));
+ buildTest(REPLACEMENT, PREFIX, REPLACEMENT, PREFIX, suffix, true, false, suffixIsOptional, new Pair("", true));
+ }
+
private void buildTestWithDefaults(String expected, String initial, boolean replaceIfBoundedStringEmpty, boolean keepBoundaries, boolean suffixIsOptional, String... expectedMatches)
{
- buildTest(expected, initial, null, PREFIX, SUFFIX, replaceIfBoundedStringEmpty, keepBoundaries, suffixIsOptional, expectedMatches);
+ Pair[] pairs = new Pair[expectedMatches.length];
+ for (int i = 0; i < expectedMatches.length; i++)
+ {
+ pairs[i] = new Pair(expectedMatches[i], false);
+ }
+ buildTest(expected, initial, null, PREFIX, SUFFIX, replaceIfBoundedStringEmpty, keepBoundaries, suffixIsOptional, pairs);
}
- private void buildTest(String expected, String initial, String replacement, String prefix, String suffix, boolean replaceIfBoundedStringEmpty, boolean keepBoundaries, boolean suffixIsOptional, String... expectedMatches)
+ private void buildTest(String expected, String initial, String replacement, String prefix, String suffix, boolean replaceIfBoundedStringEmpty, boolean keepBoundaries, boolean suffixIsOptional, Pair... expectedMatches)
{
if (replacement == null)
{
@@ -100,31 +153,10 @@
assertEquals(expected, TextTools.replaceBoundedString(initial, prefix, suffix, gen, replaceIfBoundedStringEmpty, keepBoundaries, suffixIsOptional));
}
- public void testReplaceStringsWithNullSuffix()
- {
- replaceStringsWithOptionalSuffix(false, null);
- replaceStringsWithOptionalSuffix(true, null);
- }
-
- private void replaceStringsWithOptionalSuffix(boolean suffixIsOptional, String suffix)
- {
- buildTest(REPLACEMENT + "blah", PREFIX + "blah", REPLACEMENT, PREFIX, suffix, false, false, suffixIsOptional, PREFIX);
- buildTest(PREFIX + REPLACEMENT + "blah", PREFIX + "blah", REPLACEMENT, PREFIX, suffix, false, true, suffixIsOptional, PREFIX);
- buildTest(REPLACEMENT + "blah", PREFIX + "blah", REPLACEMENT, PREFIX, suffix, true, false, suffixIsOptional, PREFIX);
- buildTest(REPLACEMENT, PREFIX, REPLACEMENT, PREFIX, suffix, true, false, suffixIsOptional, PREFIX);
- }
-
- public void testReplaceStringsWithOptionalSuffix()
- {
- replaceStringsWithOptionalSuffix(true, SUFFIX);
- buildTest(REPLACEMENT + "blah" + REPLACEMENT + "foo", PREFIX + "blah" + PREFIX + "replaced" + SUFFIX + "foo", REPLACEMENT, PREFIX, SUFFIX, false, false, true, PREFIX, "replaced");
- buildTest(REPLACEMENT + "blah" + PREFIX + REPLACEMENT + SUFFIX + "foo", PREFIX + "blah" + PREFIX + "replaced" + SUFFIX + "foo", REPLACEMENT, PREFIX, SUFFIX, false, true, true, PREFIX, "replaced");
- }
-
static class TestStringReplacementGenerator implements TextTools.StringReplacementGenerator
{
private String replacement;
- private String[] expectedMatch;
+ private Pair[] expectedMatch;
private int invocationCount;
TestStringReplacementGenerator(String replacement)
@@ -132,20 +164,34 @@
this.replacement = replacement;
}
- public void setExpectedMatch(String... expectedMatch)
+ public void setExpectedMatch(Pair... expectedMatch)
{
this.expectedMatch = expectedMatch;
this.invocationCount = 0;
}
- public String getReplacementFor(String match, String prefix, String suffix)
+ public String getReplacementFor(String match, String prefix, String suffix, boolean matchedPrefixOnly)
{
if (expectedMatch == null)
{
fail("getReplacementFor shouldn't have been called");
}
- assertEquals("'" + expectedMatch[invocationCount++] + "'", "'" + match + "'");
+ Pair pair = expectedMatch[invocationCount++];
+ assertEquals("'" + pair.match + "'", "'" + match + "'");
+ assertEquals(matchedPrefixOnly, pair.matchedPrefix);
return replacement;
}
}
+
+ static class Pair
+ {
+ String match;
+ boolean matchedPrefix;
+
+ public Pair(String match, boolean matchedPrefix)
+ {
+ this.match = match;
+ this.matchedPrefix = matchedPrefix;
+ }
+ }
}
More information about the gatein-commits
mailing list