dion 2004/08/31 02:16:28 Modified: jelly/jelly-tags/util project.xml jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util suite.jelly jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util ReplaceTag.java Log: Apply JELLY-77 with a slightly different project.xml change Revision Changes Path 1.12 +5 -0 jakarta-commons/jelly/jelly-tags/util/project.xml Index: project.xml =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/util/project.xml,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- project.xml 12 Aug 2004 03:40:32 -0000 1.11 +++ project.xml 31 Aug 2004 09:16:28 -0000 1.12 @@ -45,6 +45,11 @@ <version>SNAPSHOT</version> </dependency> + <dependency> + <id>commons-lang</id> + <version>1.0</version> + </dependency> + <!-- END for compilation --> <!-- START for test --> 1.5 +27 -15 jakarta-commons/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/suite.jelly Index: suite.jelly =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/util/src/test/org/apache/commons/jelly/tags/util/suite.jelly,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- suite.jelly 1 Mar 2004 12:47:53 -0000 1.4 +++ suite.jelly 31 Aug 2004 09:16:28 -0000 1.5 @@ -1,19 +1,19 @@ <?xml version="1.0"?>
-<!-- - Copyright 2002-2004 The Apache Software Foundation - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. ---> +<!-- + Copyright 2002-2004 The Apache Software Foundation + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> <test:suite xmlns:j="jelly:core" xmlns:util="jelly:util" @@ -113,6 +113,18 @@ Should have replaced a slash with a back slash from a variable and placed the result into a variable </test:assertEquals> + + <util:replace oldChar="ABC" newChar="123" value="CBABC" var="testString4" /> + <test:assertEquals expected="CB1BC" actual="${testString4}"> + Should have only substituted the 1 for the A, since the + old/newChar attributes were used. + </test:assertEquals> + + <util:replace old="brown" new="black" value="The quick brown fox" var="testString6" /> + <test:assertEquals expected="The quick black fox" actual="${testString6}"> + Should have substituted the string "black" for "brown" + </test:assertEquals> + </test:case> <test:case name="testProperties"> 1.6 +62 -10 jakarta-commons/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/ReplaceTag.java Index: ReplaceTag.java =================================================================== RCS file: /home/cvs/jakarta-commons/jelly/jelly-tags/util/src/java/org/apache/commons/jelly/tags/util/ReplaceTag.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- ReplaceTag.java 25 Feb 2004 01:31:53 -0000 1.5 +++ ReplaceTag.java 31 Aug 2004 09:16:28 -0000 1.6 @@ -24,11 +24,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.commons.lang.StringUtils; + import org.xml.sax.SAXException; /** - * A tag that replaces occurrences of a character in its body (or value) - * and places the result into the context + * A tag that replaces occurrences of a character or string in its body or + * (or value) and places the result into the context * * @author dion */ @@ -47,18 +49,33 @@ /** the new character that will replace the old */ private String newChar; + + /** the old string to be replace */ + private String oldString; + + /** the new string that will replace the old */ + private String newString; // Tag interface //------------------------------------------------------------------------- public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException { // check required properties - if (oldChar == null) { - throw new MissingAttributeException("oldChar must be provided"); + if (oldChar != null) { + oldString = oldChar.substring(0,1); + } + + if (newChar != null) { + newString = newChar.substring(0,1); } - if (newChar == null) { - throw new MissingAttributeException("newChar must be provided"); + + if (oldString == null) { + throw new MissingAttributeException("oldChar or oldString must be provided"); } - + + if (newString == null) { + throw new MissingAttributeException("newChar or newString must be provided"); + } + // get either the value or the body of the tag Object answer = null; if ( value != null ) { @@ -69,8 +86,7 @@ // set the result in the context, or output it if (answer != null) { - String stringAnswer = answer.toString().replace(oldChar.charAt(0), - newChar.charAt(0)); + String stringAnswer = StringUtils.replace(answer.toString(), oldString, newString); if ( var != null ) { context.setVariable(var, stringAnswer); } else { @@ -104,6 +120,24 @@ } /** + * Returns the newString that will be replaced. + * @return String + */ + public String getNew() + { + return newString; + } + + /** + * Returns the oldString that will be replaced. + * @return String + */ + public String getOld() + { + return oldString; + } + + /** * Returns the value. * @return Expression */ @@ -140,6 +174,24 @@ } /** + * Sets the newString. + * @param newString The newString to set + */ + public void setNew(String newString) + { + this.newString = newString; + } + + /** + * Sets the oldString. + * @param oldString The oldString to set + */ + public void setOld(String oldString) + { + this.oldString = oldString; + } + + /** * Sets the value. * @param value The value to set */ @@ -157,4 +209,4 @@ this.var = var; } -} \ No newline at end of file +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]