dion 2002/11/16 02:15:46 Modified: jelly/src/test/org/apache/commons/jelly/util suite.jelly Added: jelly/src/java/org/apache/commons/jelly/tags/util ReplaceTag.java Log: Add a replace tag and some tests Revision Changes Path 1.4 +20 -0 jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/util/suite.jelly Index: suite.jelly =================================================================== RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/util/suite.jelly,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- suite.jelly 13 Nov 2002 12:59:35 -0000 1.3 +++ suite.jelly 16 Nov 2002 10:15:45 -0000 1.4 @@ -80,5 +80,25 @@ </util:available> </test:case> + + <test:case name="testReplace"> + + <util:replace oldChar="\" newChar="/" var="testString">A\B</util:replace> + <test:assertEquals expected="A/B" actual="${testString}"> + Should have replaced a back slash with a forward one + </test:assertEquals> + + <j:set var="testString2"><util:replace oldChar="\" newChar="/">A\B</util:replace></j:set> + <test:assertEquals expected="A/B" actual="${testString2}"> + Should have replaced a back slash with a forward one + and placed the result into output + </test:assertEquals> + + <util:replace oldChar="/" newChar="\" value="${testString}" var="testString3" /> + <test:assertEquals expected="A\B" actual="${testString3}"> + Should have replaced a slash with a back slash from a variable + and placed the result into a variable + </test:assertEquals> + </test:case> </test:suite> 1.1 jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/util/ReplaceTag.java Index: ReplaceTag.java =================================================================== /* * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 2002 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */ package org.apache.commons.jelly.tags.util; import org.apache.commons.jelly.expression.Expression; import org.apache.commons.jelly.MissingAttributeException; import org.apache.commons.jelly.TagSupport; import org.apache.commons.jelly.XMLOutput; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * A tag that replaces occurrences of strings in its body (or value) * and places the result into the context * * @author dion */ public class ReplaceTag extends TagSupport { /** The Log to which logging calls will be made. */ private static final Log log = LogFactory.getLog(ReplaceTag.class); /** The variable name to export. */ private String var; /** The expression to evaluate. */ private Expression value; /** the old character to be replaced */ private String oldChar; /** the new character that will replace the old */ private String newChar; // Tag interface //------------------------------------------------------------------------- public void doTag(XMLOutput output) throws Exception { // check required properties if (oldChar == null) { throw new MissingAttributeException("oldChar must be provided"); } if (newChar == null) { throw new MissingAttributeException("newChar must be provided"); } // get either the value or the body of the tag Object answer = null; if ( value != null ) { answer = value.evaluate(context); } else { answer = getBodyText(false); } // set the result in the context, or output it if (answer != null) { String stringAnswer = answer.toString().replace(oldChar.charAt(0), newChar.charAt(0)); if ( var != null ) { context.setVariable(var, stringAnswer); } else { output.write(stringAnswer); } } } /** * Returns the newChar used in replacing. Should only be a single * character. * @return String */ public String getNewChar() { return newChar; } /** * Returns the oldChar that will be replaced. Should only be a single * character. * @return String */ public String getOldChar() { return oldChar; } /** * Returns the value. * @return Expression */ public Expression getValue() { return value; } /** * Returns the var. * @return String */ public String getVar() { return var; } /** * Sets the newChar. * @param newChar The newChar to set */ public void setNewChar(String newChar) { this.newChar = newChar; } /** * Sets the oldChar. * @param oldChar The oldChar to set */ public void setOldChar(String oldChar) { this.oldChar = oldChar; } /** * Sets the value. * @param value The value to set */ public void setValue(Expression value) { this.value = value; } /** * Sets the var. * @param var The var to set */ public void setVar(String var) { this.var = var; } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>