Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 379   Methods: 17
NCLOC: 164   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
ReplaceTokens.java 31% 52.5% 94.1% 51.1%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002-2003 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.filters;
 55   
 
 56   
 import java.io.IOException;
 57   
 import java.io.Reader;
 58   
 import java.util.Hashtable;
 59   
 import org.apache.tools.ant.types.Parameter;
 60   
 import org.apache.tools.ant.BuildException;
 61   
 
 62   
 /**
 63   
  * Replaces tokens in the original input with user-supplied values.
 64   
  *
 65   
  * Example:
 66   
  *
 67   
  * <pre>&lt;replacetokens begintoken=&quot;#&quot; endtoken=&quot;#&quot;&gt;
 68   
  *   &lt;token key=&quot;DATE&quot; value=&quot;${TODAY}&quot;/&gt;
 69   
  * &lt;/replacetokens&gt;</pre>
 70   
  *
 71   
  * Or:
 72   
  *
 73   
  * <pre>&lt;filterreader classname="org.apache.tools.ant.filters.ReplaceTokens"&gt;
 74   
  *   &lt;param type="tokenchar" name="begintoken" value="#"/&gt;
 75   
  *   &lt;param type="tokenchar" name="endtoken" value="#"/&gt;
 76   
  *   &lt;param type="token" name="DATE" value="${TODAY}"/&gt;
 77   
  * &lt;/filterreader&gt;</pre>
 78   
  *
 79   
  * @author Magesh Umasankar
 80   
  */
 81   
 public final class ReplaceTokens
 82   
     extends BaseParamFilterReader
 83   
     implements ChainableReader {
 84   
     /** Default "begin token" character. */
 85   
     private static final char DEFAULT_BEGIN_TOKEN = '@';
 86   
 
 87   
     /** Default "end token" character. */
 88   
     private static final char DEFAULT_END_TOKEN = '@';
 89   
 
 90   
     /** Data to be used before reading from stream again */
 91   
     private String queuedData = null;
 92   
 
 93   
     /** replacement test from a token */
 94   
     private String replaceData = null;
 95   
 
 96   
     /** Index into replacement data */
 97   
     private int replaceIndex = -1;
 98   
     
 99   
     /** Index into queue data */
 100   
     private int queueIndex = -1;
 101   
     
 102   
     /** Hashtable to hold the replacee-replacer pairs (String to String). */
 103   
     private Hashtable hash = new Hashtable();
 104   
 
 105   
     /** Character marking the beginning of a token. */
 106   
     private char beginToken = DEFAULT_BEGIN_TOKEN;
 107   
 
 108   
     /** Character marking the end of a token. */
 109   
     private char endToken = DEFAULT_END_TOKEN;
 110   
 
 111   
     /**
 112   
      * Constructor for "dummy" instances.
 113   
      * 
 114   
      * @see BaseFilterReader#BaseFilterReader()
 115   
      */
 116  6
     public ReplaceTokens() {
 117  6
         super();
 118   
     }
 119   
 
 120   
     /**
 121   
      * Creates a new filtered reader.
 122   
      *
 123   
      * @param in A Reader object providing the underlying stream.
 124   
      *           Must not be <code>null</code>.
 125   
      */
 126  7
     public ReplaceTokens(final Reader in) {
 127  7
         super(in);
 128   
     }
 129   
 
 130  268
     private int getNextChar() throws IOException {
 131  268
         if (queueIndex != -1) {
 132  0
             final int ch = queuedData.charAt(queueIndex++);
 133  0
             if (queueIndex >= queuedData.length()) {
 134  0
                 queueIndex = -1;
 135   
             }
 136  0
             return ch;
 137   
         }
 138   
         
 139  268
         return in.read();
 140   
     }
 141   
     
 142   
     /**
 143   
      * Returns the next character in the filtered stream, replacing tokens
 144   
      * from the original stream.
 145   
      * 
 146   
      * @return the next character in the resulting stream, or -1
 147   
      * if the end of the resulting stream has been reached
 148   
      * 
 149   
      * @exception IOException if the underlying stream throws an IOException
 150   
      * during reading     
 151   
      */
 152  293
     public final int read() throws IOException {
 153  293
         if (!getInitialized()) {
 154  0
             initialize();
 155  0
             setInitialized(true);
 156   
         }
 157   
 
 158  293
         if (replaceIndex != -1) {
 159  90
             final int ch = replaceData.charAt(replaceIndex++);
 160  90
             if (replaceIndex >= replaceData.length()) {
 161  9
                 replaceIndex = -1;
 162   
             }
 163  90
             return ch;
 164   
         }
 165   
         
 166  203
         int ch = getNextChar();
 167   
 
 168  203
         if (ch == beginToken) {
 169  9
             final StringBuffer key = new StringBuffer("");
 170  9
             do  {
 171  65
                 ch = getNextChar();
 172  65
                 if (ch != -1) {
 173  65
                     key.append((char) ch);
 174   
                 } else {
 175  0
                     break;
 176   
                 }
 177  65
             } while (ch != endToken);
 178   
 
 179  9
             if (ch == -1) {
 180  0
                 if (queuedData == null || queueIndex == -1) {
 181  0
                     queuedData = key.toString();
 182   
                 } else {
 183  0
                     queuedData 
 184   
                         = key.toString() + queuedData.substring(queueIndex);
 185   
                 }
 186  0
                 queueIndex = 0;
 187  0
                 return beginToken;
 188   
             } else {
 189  9
                 key.setLength(key.length() - 1);
 190   
                 
 191  9
                 final String replaceWith = (String) hash.get(key.toString());
 192  9
                 if (replaceWith != null) {
 193  9
                     replaceData = replaceWith;
 194  9
                     replaceIndex = 0;
 195  9
                     return read();
 196   
                 } else {
 197  0
                     String newData = key.toString() + endToken;
 198  0
                     if (queuedData == null || queueIndex == -1) {
 199  0
                         queuedData = newData;
 200   
                     } else {
 201  0
                         queuedData = newData + queuedData.substring(queueIndex);
 202   
                     }
 203  0
                     queueIndex = 0;
 204  0
                     return beginToken;
 205   
                 }
 206   
             }
 207   
         }
 208  194
         return ch;
 209   
     }
 210   
 
 211   
     /**
 212   
      * Sets the "begin token" character.
 213   
      * 
 214   
      * @param beginToken the character used to denote the beginning of a token
 215   
      */
 216  7
     public final void setBeginToken(final char beginToken) {
 217  7
         this.beginToken = beginToken;
 218   
     }
 219   
 
 220   
     /**
 221   
      * Returns the "begin token" character.
 222   
      * 
 223   
      * @return the character used to denote the beginning of a token
 224   
      */
 225  7
     private final char getBeginToken() {
 226  7
         return beginToken;
 227   
     }
 228   
 
 229   
     /**
 230   
      * Sets the "end token" character.
 231   
      * 
 232   
      * @param endToken the character used to denote the end of a token
 233   
      */
 234  7
     public final void setEndToken(final char endToken) {
 235  7
         this.endToken = endToken;
 236   
     }
 237   
 
 238   
     /**
 239   
      * Returns the "end token" character.
 240   
      * 
 241   
      * @return the character used to denote the end of a token
 242   
      */
 243  7
     private final char getEndToken() {
 244  7
         return endToken;
 245   
     }
 246   
 
 247   
     /**
 248   
      * Adds a token element to the map of tokens to replace.
 249   
      * 
 250   
      * @param token The token to add to the map of replacements.
 251   
      *              Must not be <code>null</code>.
 252   
      */
 253  9
     public final void addConfiguredToken(final Token token) {
 254  9
         hash.put(token.getKey(), token.getValue());
 255   
     }
 256   
 
 257   
     /**
 258   
      * Sets the map of tokens to replace.
 259   
      * 
 260   
      * @param hash A map (String->String) of token keys to replacement
 261   
      * values. Must not be <code>null</code>.
 262   
      */
 263  7
     private void setTokens(final Hashtable hash) {
 264  7
         this.hash = hash;
 265   
     }
 266   
 
 267   
     /**
 268   
      * Returns the map of tokens which will be replaced.
 269   
      * 
 270   
      * @return a map (String->String) of token keys to replacement
 271   
      * values
 272   
      */
 273  7
     private final Hashtable getTokens() {
 274  7
         return hash;
 275   
     }
 276   
 
 277   
     /**
 278   
      * Creates a new ReplaceTokens using the passed in
 279   
      * Reader for instantiation.
 280   
      * 
 281   
      * @param rdr A Reader object providing the underlying stream.
 282   
      *            Must not be <code>null</code>.
 283   
      * 
 284   
      * @return a new filter based on this configuration, but filtering
 285   
      *         the specified reader
 286   
      */
 287  7
     public final Reader chain(final Reader rdr) {
 288  7
         ReplaceTokens newFilter = new ReplaceTokens(rdr);
 289  7
         newFilter.setBeginToken(getBeginToken());
 290  7
         newFilter.setEndToken(getEndToken());
 291  7
         newFilter.setTokens(getTokens());
 292  7
         newFilter.setInitialized(true);
 293  7
         return newFilter;
 294   
     }
 295   
 
 296   
     /**
 297   
      * Initializes tokens and loads the replacee-replacer hashtable.
 298   
      */
 299  0
     private final void initialize() {
 300  0
         Parameter[] params = getParameters();
 301  0
         if (params != null) {
 302  0
             for (int i = 0; i < params.length; i++) {
 303  0
                 if (params[i] != null) {
 304  0
                     final String type = params[i].getType();
 305  0
                     if ("tokenchar".equals(type)) {
 306  0
                         final String name = params[i].getName();
 307  0
                         String value = params[i].getValue();
 308  0
                         if ("begintoken".equals(name)) {
 309  0
                             if (value.length() == 0) {
 310  0
                                 throw new BuildException("Begin token cannot " 
 311   
                                     + "be empty");
 312   
                             }
 313  0
                             beginToken = params[i].getValue().charAt(0);
 314  0
                         } else if ("endtoken".equals(name)) {
 315  0
                             if (value.length() == 0) {
 316  0
                                 throw new BuildException("End token cannot " 
 317   
                                     + "be empty");
 318   
                             }
 319  0
                             endToken = params[i].getValue().charAt(0);
 320   
                         }
 321  0
                     } else if ("token".equals(type)) {
 322  0
                         final String name = params[i].getName();
 323  0
                         final String value = params[i].getValue();
 324  0
                         hash.put(name, value);
 325   
                     }
 326   
                 }
 327   
             }
 328   
         }
 329   
     }
 330   
 
 331   
     /**
 332   
      * Holds a token
 333   
      */
 334   
     public static class Token {
 335   
 
 336   
         /** Token key */
 337   
         private String key;
 338   
 
 339   
         /** Token value */
 340   
         private String value;
 341   
 
 342   
         /**
 343   
          * Sets the token key
 344   
          * 
 345   
          * @param key The key for this token. Must not be <code>null</code>.
 346   
          */
 347  9
         public final void setKey(String key) {
 348  9
             this.key = key;
 349   
         }
 350   
 
 351   
         /**
 352   
          * Sets the token value
 353   
          * 
 354   
          * @param value The value for this token. Must not be <code>null</code>.
 355   
          */
 356  9
         public final void setValue(String value) {
 357  9
             this.value = value;
 358   
         }
 359   
 
 360   
         /**
 361   
          * Returns the key for this token.
 362   
          * 
 363   
          * @return the key for this token
 364   
          */
 365  9
         public final String getKey() {
 366  9
             return key;
 367   
         }
 368   
 
 369   
         /**
 370   
          * Returns the value for this token.
 371   
          * 
 372   
          * @return the value for this token
 373   
          */
 374  9
         public final String getValue() {
 375  9
             return value;
 376   
         }
 377   
     }
 378   
 }
 379