Coverage report

  %line %branch
org.apache.commons.jelly.tags.util.ReplaceTag
78% 
100% 

 1  
 /*
 2  
  * Copyright 2002,2004 The Apache Software Foundation.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 
 17  
 package org.apache.commons.jelly.tags.util;
 18  
 
 19  
 import org.apache.commons.jelly.JellyTagException;
 20  
 import org.apache.commons.jelly.MissingAttributeException;
 21  
 import org.apache.commons.jelly.TagSupport;
 22  
 import org.apache.commons.jelly.XMLOutput;
 23  
 import org.apache.commons.jelly.expression.Expression;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 
 27  
 import org.apache.commons.lang.StringUtils;
 28  
 
 29  
 import org.xml.sax.SAXException;
 30  
 
 31  
 /**
 32  
  * A tag that replaces occurrences of a character or string in its body or
 33  
  * (or value) and places the result into the context
 34  
  *
 35  
  * @author dion
 36  
  */
 37  6
 public class ReplaceTag extends TagSupport {
 38  
     /** The Log to which logging calls will be made. */
 39  1
     private static final Log log = LogFactory.getLog(ReplaceTag.class);
 40  
 
 41  
     /** The variable name to export. */
 42  
     private String var;
 43  
 
 44  
     /** The expression to evaluate. */
 45  
     private Expression value;
 46  
 
 47  
     /** the old character to be replaced */
 48  
     private String oldChar;
 49  
 
 50  
     /** the new character that will replace the old */
 51  
     private String newChar;
 52  
 
 53  
     /** the old string to be replace */
 54  
     private String oldString;
 55  
 
 56  
     /** the new string that will replace the old */
 57  
     private String newString;
 58  
 
 59  
     // Tag interface
 60  
     //-------------------------------------------------------------------------
 61  
     public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
 62  
         // check required properties
 63  5
         if (oldChar != null) {
 64  4
             oldString = oldChar.substring(0,1);
 65  
         }
 66  
 
 67  5
         if (newChar != null) {
 68  4
             newString = newChar.substring(0,1);
 69  
         }
 70  
 
 71  5
         if (oldString == null) {
 72  0
             throw new MissingAttributeException("oldChar or oldString must be provided");
 73  
         }
 74  
 
 75  5
         if (newString == null) {
 76  0
             throw new MissingAttributeException("newChar or newString must be provided");
 77  
         }
 78  
 
 79  
         // get either the value or the body of the tag
 80  5
         Object answer = null;
 81  5
         if ( value != null ) {
 82  3
             answer = value.evaluateAsString(context);
 83  
         } else {
 84  2
             answer = getBodyText(false);
 85  
         }
 86  
 
 87  
         // set the result in the context, or output it
 88  5
         if (answer != null) {
 89  5
             String stringAnswer = StringUtils.replace(answer.toString(), oldString, newString);
 90  5
             if ( var != null ) {
 91  4
                 context.setVariable(var, stringAnswer);
 92  
             } else {
 93  
                 try {
 94  1
                     output.write(stringAnswer);
 95  1
                 } catch (SAXException e) {
 96  0
                     throw new JellyTagException(e);
 97  
                 }
 98  
             }
 99  
         }
 100  5
     }
 101  
 
 102  
     /**
 103  
      * Returns the newChar used in replacing. Should only be a single
 104  
      * character.
 105  
      * @return String
 106  
      */
 107  
     public String getNewChar()
 108  
     {
 109  0
         return newChar;
 110  
     }
 111  
 
 112  
     /**
 113  
      * Returns the oldChar that will be replaced. Should only be a single
 114  
      * character.
 115  
      * @return String
 116  
      */
 117  
     public String getOldChar()
 118  
     {
 119  0
         return oldChar;
 120  
     }
 121  
 
 122  
     /**
 123  
      * Returns the newString that will be replaced.
 124  
      * @return String
 125  
      */
 126  
     public String getNew()
 127  
     {
 128  0
         return newString;
 129  
     }
 130  
 
 131  
     /**
 132  
      * Returns the oldString that will be replaced.
 133  
      * @return String
 134  
      */
 135  
     public String getOld()
 136  
     {
 137  0
         return oldString;
 138  
     }
 139  
 
 140  
     /**
 141  
      * Returns the value.
 142  
      * @return Expression
 143  
      */
 144  
     public Expression getValue()
 145  
     {
 146  0
         return value;
 147  
     }
 148  
 
 149  
     /**
 150  
      * Returns the var.
 151  
      * @return String
 152  
      */
 153  
     public String getVar()
 154  
     {
 155  0
         return var;
 156  
     }
 157  
 
 158  
     /**
 159  
      * Sets the newChar.
 160  
      * @param newChar The newChar to set
 161  
      */
 162  
     public void setNewChar(String newChar)
 163  
     {
 164  4
         this.newChar = newChar;
 165  4
     }
 166  
 
 167  
     /**
 168  
      * Sets the oldChar.
 169  
      * @param oldChar The oldChar to set
 170  
      */
 171  
     public void setOldChar(String oldChar)
 172  
     {
 173  4
         this.oldChar = oldChar;
 174  4
     }
 175  
 
 176  
     /**
 177  
      * Sets the newString.
 178  
      * @param newString The newString to set
 179  
      */
 180  
     public void setNew(String newString)
 181  
     {
 182  1
         this.newString = newString;
 183  1
     }
 184  
 
 185  
     /**
 186  
      * Sets the oldString.
 187  
      * @param oldString The oldString to set
 188  
      */
 189  
     public void setOld(String oldString)
 190  
     {
 191  1
         this.oldString = oldString;
 192  1
     }
 193  
 
 194  
     /**
 195  
      * Sets the value.
 196  
      * @param value The value to set
 197  
      */
 198  
     public void setValue(Expression value)
 199  
     {
 200  3
         this.value = value;
 201  3
     }
 202  
 
 203  
     /**
 204  
      * Sets the var.
 205  
      * @param var The var to set
 206  
      */
 207  
     public void setVar(String var)
 208  
     {
 209  4
         this.var = class="keyword">var;
 210  4
     }
 211  
 
 212  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.