Author: scolebourne Date: Mon Aug 7 16:06:41 2006 New Revision: 429507 URL: http://svn.apache.org/viewvc?rev=429507&view=rev Log: Give more power to StrSubstitutor subclasses
Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrSubstitutor.java jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrSubstitutorTest.java Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrSubstitutor.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrSubstitutor.java?rev=429507&r1=429506&r2=429507&view=diff ============================================================================== --- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrSubstitutor.java (original) +++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/text/StrSubstitutor.java Mon Aug 7 16:06:41 2006 @@ -126,8 +126,8 @@ * Replaces all the occurrences of variables in the given source object with * their matching values from the map. * - * @param source the source text containing the variables to substitute - * @param valueMap the map with the values + * @param source the source text containing the variables to substitute, null returns null + * @param valueMap the map with the values, may be null * @return the result of the replace operation */ public static String replace(Object source, Map valueMap) { @@ -139,11 +139,12 @@ * their matching values from the map. This method allows to specifiy a * custom variable prefix and suffix * - * @param source the source text containing the variables to substitute - * @param valueMap the map with the values - * @param prefix the prefix of variables - * @param suffix the suffix of variables + * @param source the source text containing the variables to substitute, null returns null + * @param valueMap the map with the values, may be null + * @param prefix the prefix of variables, not null + * @param suffix the suffix of variables, not null * @return the result of the replace operation + * @throws IllegalArgumentException if the prefix or suffix is null */ public static String replace(Object source, Map valueMap, String prefix, String suffix) { return new StrSubstitutor(valueMap, prefix, suffix).replace(source); @@ -153,7 +154,7 @@ * Replaces all the occurrences of variables in the given source object with * their matching values from the system properties. * - * @param source the source text containing the variables to substitute + * @param source the source text containing the variables to substitute, null returns null * @return the result of the replace operation */ public static String replaceSystemProperties(Object source) { @@ -365,14 +366,20 @@ //----------------------------------------------------------------------- /** - * Main method for substituting variables. + * Internal method that substitutes the variables. + * <p> + * Most users of this class do not need to call this method. This method will + * be called automatically by another (public) method. + * <p> + * Writers of subclasses can override this method if they need access to + * the substitution process at the start or end. * * @param buf the string builder to substitute into, not null * @param offset the start offset within the builder, must be valid * @param length the length within the builder to be processed, must be valid * @return true if altered */ - private boolean substitute(StrBuilder buf, int offset, int length) { + protected boolean substitute(StrBuilder buf, int offset, int length) { return substitute(buf, offset, length, null) > 0; } @@ -439,7 +446,7 @@ priorVariables.add(varName); // resolve the variable - String varValue = resolveVariable(varName); + String varValue = resolveVariable(varName, buf, startPos, endPos); if (varValue != null) { // recursive replace int varLen = varValue.length(); @@ -486,21 +493,28 @@ } /** - * Resolves the specified variable. This method is called whenever a variable - * reference is detected in the source text. It is passed the variable's name - * and must return the corresponding value. This implementation accesses the - * value map using the variable's name as key. Derived classes may override - * this method to implement a different strategy for resolving variables. - * - * @param varName the name of the variable + * Internal method that resolves the value of a variable. + * <p> + * Most users of this class do not need to call this method. This method is + * called automatically by the substitution process. + * <p> + * Writers of subclasses can override this method if they need to alter + * how each substitution occurs. The method is passed the variable's name + * and must return the corresponding value. This implementation uses the + * [EMAIL PROTECTED] #getVariableResolver()} with the variable's name as the key. + * + * @param variableName the name of the variable, not null + * @param buf the buffer where the substitution is occurring, not null + * @param startPos the start position of the variable including the prefix, valid + * @param endPos the end position of the variable including the suffix, valid * @return the variable's value or <b>null</b> if the variable is unknown */ - protected String resolveVariable(String varName) { + protected String resolveVariable(String variableName, StrBuilder buf, int startPos, int endPos) { VariableResolver lookup = getVariableResolver(); if (lookup == null) { return null; } - return lookup.resolveVariable(varName); + return lookup.resolveVariable(variableName); } // Escape Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrSubstitutorTest.java URL: http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrSubstitutorTest.java?rev=429507&r1=429506&r2=429507&view=diff ============================================================================== --- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrSubstitutorTest.java (original) +++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/text/StrSubstitutorTest.java Mon Aug 7 16:06:41 2006 @@ -24,8 +24,6 @@ import junit.framework.TestSuite; import junit.textui.TestRunner; -import org.apache.commons.lang.text.StrSubstitutor.MapVariableResolver; - /** * Test class for StrSubstitutor. * @@ -356,42 +354,60 @@ } //----------------------------------------------------------------------- -// /** -// * Tests source texts with nothing to replace. -// */ -// public void testReplaceNothing() { -// assertNull(this.getFormat().replace((char[]) null)); -// assertNull(this.getFormat().replace((String) null)); -// assertNull(this.getFormat().replace((Object) null)); -// assertEquals("Nothing to replace.", this.getFormat().replace("Nothing to replace.")); -// assertEquals("42", this.getFormat().replace(new Integer(42))); -// assertEquals(0, this.getFormat().replace((StrBuilder) null)); -// } -// -//// /** -//// * Tests operating on objects. -//// */ -//// public void testReplaceObject() { -//// this.getValueMap().put("value", new Integer(42)); -//// assertEquals(new Integer(42), this.getFormat().replaceObject("${value}")); -//// assertEquals("The answer is 42.", this.getFormat().replaceObject("The answer is ${value}.")); -//// } -// -// /** -// * Tests interpolation with system properties. -// */ -// public void testReplaceSystemProperties() { -// StringBuffer buf = new StringBuffer(); -// buf.append("Hi ").append(System.getProperty("user.name")); -// buf.append(", you are working with "); -// buf.append(System.getProperty("os.name")); -// buf.append(", your home directory is "); -// buf.append(System.getProperty("user.home")).append('.'); -// assertEquals(buf.toString(), StrSubstitutor.replaceSystemProperties("Hi ${user.name}, you are " -// + "working with ${os.name}, your home " -// + "directory is ${user.home}.")); -// } -// + /** + * Tests protected. + */ + public void testResolveVariable() { + final StrBuilder builder = new StrBuilder("Hi ${name}!"); + Map map = new HashMap(); + map.put("name", "commons"); + StrSubstitutor sub = new StrSubstitutor(map) { + protected String resolveVariable(String variableName, StrBuilder buf, int startPos, int endPos) { + assertEquals("name", variableName); + assertSame(builder, buf); + assertEquals(3, startPos); + assertEquals(10, endPos); + return "jakarta"; + } + }; + sub.replace(builder); + assertEquals("Hi jakarta!", builder.toString()); + } + + //----------------------------------------------------------------------- + /** + * Tests static. + */ + public void testStaticReplace() { + Map map = new HashMap(); + map.put("name", "commons"); + assertEquals("Hi commons!", StrSubstitutor.replace("Hi ${name}!", map)); + } + + /** + * Tests static. + */ + public void testStaticReplacePrefixSuffix() { + Map map = new HashMap(); + map.put("name", "commons"); + assertEquals("Hi commons!", StrSubstitutor.replace("Hi <name>!", map, "<", ">")); + } + + /** + * Tests interpolation with system properties. + */ + public void testStaticReplaceSystemProperties() { + StrBuilder buf = new StrBuilder(); + buf.append("Hi ").append(System.getProperty("user.name")); + buf.append(", you are working with "); + buf.append(System.getProperty("os.name")); + buf.append(", your home directory is "); + buf.append(System.getProperty("user.home")).append('.'); + assertEquals(buf.toString(), StrSubstitutor.replaceSystemProperties("Hi ${user.name}, you are " + + "working with ${os.name}, your home " + + "directory is ${user.home}.")); + } + //----------------------------------------------------------------------- private void doTestReplace(String expectedResult, String replaceTemplate, boolean substring) { String expectedShortResult = expectedResult.substring(1, expectedResult.length() - 1); --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]