You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-dev@jakarta.apache.org by ba...@apache.org on 2003/02/14 08:14:19 UTC

cvs commit: jakarta-taglibs/string/src/org/apache/taglibs/string ReplaceTag.java

bayard      2003/02/13 23:14:18

  Modified:    string/src/org/apache/taglibs/string Tag: string-1-0
                        ReplaceTag.java
  Log:
  Added a series of hacks to allow newline replacement to be possible despite the
  best attempts of JSP to the otherwise.
  
  Revision  Changes    Path
  No                   revision
  
  
  No                   revision
  
  
  1.3.2.1   +75 -9     jakarta-taglibs/string/src/org/apache/taglibs/string/ReplaceTag.java
  
  Index: ReplaceTag.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/string/src/org/apache/taglibs/string/ReplaceTag.java,v
  retrieving revision 1.3
  retrieving revision 1.3.2.1
  diff -u -r1.3 -r1.3.2.1
  --- ReplaceTag.java	28 Jul 2002 00:11:15 -0000	1.3
  +++ ReplaceTag.java	14 Feb 2003 07:14:18 -0000	1.3.2.1
  @@ -62,6 +62,7 @@
   
   import org.apache.commons.lang.StringUtils;
   import org.apache.commons.lang.NumberUtils;
  +import javax.servlet.jsp.JspException;
   
   /**
    * Replace a specified substring with another string.
  @@ -81,15 +82,28 @@
    *             Number of times to replace.
    *             Default is all matches.
    * </dd>
  + * <dt>newlineToken</dt><dd>
  + *             Token to use as a newline.
  + *             This gets around the pain of haivng a literal newline in the jsp.
  + *             By default this is ignored.
  + * </dd>
  + * <dt>carriageToken</dt><dd>
  + *             Token to use as a carriage return.
  + *             This gets around the pain of haivng a literal carriage return 
  + *             in the jsp. 
  + *             By default this is ignored.
  + * </dd>
    * </dl>
    * 
    * @author bayard@generationjava.com
    */
   public class ReplaceTag extends StringTagSupport {
   
  -    private int count;
  +    private String count;
       private String replace;
       private String with;
  +    private String newlineToken;
  +    private String carriageToken;
   
       public ReplaceTag() {
           super();
  @@ -113,6 +127,42 @@
           this.replace = replace;
       }
   
  +    /**
  +     * Get the newlineToken property
  +     *
  +     * @return String property
  +     */
  +    public String getNewlineToken() {
  +        return this.newlineToken;
  +    }
  +
  +    /**
  +     * Set the newlineToken property
  +     *
  +     * @param newlineToken String property
  +     */
  +    public void setNewlineToken(String newlineToken) {
  +        this.newlineToken = newlineToken;
  +    }
  +
  +    /**
  +     * Get the carriageToken property
  +     *
  +     * @return String property
  +     */
  +    public String getCarriageToken() {
  +        return this.carriageToken;
  +    }
  +
  +    /**
  +     * Set the carriageToken property
  +     *
  +     * @param carriageToken String property
  +     */
  +    public void setCarriageToken(String carriageToken) {
  +        this.carriageToken = carriageToken;
  +    }
  +
   
       /**
        * Get the with property
  @@ -139,7 +189,7 @@
        * @return String property
        */
       public String getCount() {
  -        return ""+this.count;
  +        return this.count;
       }
   
       /**
  @@ -148,13 +198,27 @@
        * @param count String property
        */
       public void setCount(String count) {
  -        this.count = NumberUtils.stringToInt(count);
  +        this.count = count;
       }
   
   
   
       public String changeString(String text) {
  -        return StringUtils.replace(text, replace, with, count);
  +        String repl = replace;
  +        String wit = with;
  +        if(this.carriageToken != null) {
  +            repl = StringUtils.replace(replace, this.carriageToken, "\r");
  +            wit = StringUtils.replace(with, this.carriageToken, "\r");
  +        }
  +        if(this.newlineToken != null) {
  +            repl = StringUtils.replace(replace, this.newlineToken, "\n");
  +            wit = StringUtils.replace(with, this.newlineToken, "\n");
  +        }
  +        int ct = -1;
  +        if(count != null) {
  +            ct = NumberUtils.stringToInt(count);
  +        }
  +        return StringUtils.replace(text, repl, wit, ct);
       }
   
       public void initAttributes() {
  @@ -163,9 +227,11 @@
   
           this.with = null;
   
  -        this.count = -1;
  +        this.count = null;
   
  -    }
  +        this.newlineToken = null;
  +        this.carriageToken = null;
   
  +    }
   
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org