Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 222   Methods: 7
NCLOC: 115   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
KeySubst.java 0% 0% 0% 0%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000,2002 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   
 
 55   
 package org.apache.tools.ant.taskdefs;
 56   
 
 57   
 import java.io.BufferedReader;
 58   
 import java.io.BufferedWriter;
 59   
 import java.io.File;
 60   
 import java.io.FileReader;
 61   
 import java.io.FileWriter;
 62   
 import java.io.IOException;
 63   
 import java.util.Hashtable;
 64   
 import java.util.StringTokenizer;
 65   
 import org.apache.tools.ant.BuildException;
 66   
 import org.apache.tools.ant.Task;
 67   
 
 68   
 /**
 69   
  * Keyword substitution. Input file is written to output file.
 70   
  * Do not make input file same as output file.
 71   
  * Keywords in input files look like this: @foo@. See the docs for the 
 72   
  * setKeys method to understand how to do the substitutions.
 73   
  *
 74   
  * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
 75   
  * @since Ant 1.1
 76   
  * @deprecated KeySubst is deprecated since Ant 1.1. Use Filter + Copy
 77   
  * instead.
 78   
  */
 79   
 public class KeySubst extends Task {
 80   
     private File source = null;
 81   
     private File dest = null;
 82   
     private String sep = "*";
 83   
     private Hashtable replacements = new Hashtable();
 84   
     
 85   
     /**
 86   
         Do the execution.
 87   
     */
 88  0
     public void execute() throws BuildException {
 89  0
         log("!! KeySubst is deprecated. Use Filter + Copy instead. !!");
 90  0
         log("Performing Substitions");
 91  0
         if (source == null || dest == null) {
 92  0
             log("Source and destinations must not be null");
 93  0
             return;            
 94   
         }
 95  0
         BufferedReader br = null;
 96  0
         BufferedWriter bw = null;
 97  0
         try {
 98  0
             br = new BufferedReader(new FileReader(source));
 99  0
             dest.delete();
 100  0
             bw = new BufferedWriter(new FileWriter(dest));
 101   
 
 102  0
             String line = null;
 103  0
             String newline = null;
 104  0
             line = br.readLine();
 105  0
             while (line != null) {
 106  0
                 if (line.length() == 0) {
 107  0
                     bw.newLine();
 108   
                 } else {
 109  0
                     newline = KeySubst.replace(line, replacements);
 110  0
                     bw.write(newline);
 111  0
                     bw.newLine();
 112   
                 }
 113  0
                 line = br.readLine();
 114   
             }
 115  0
             bw.flush();
 116   
         } catch (IOException ioe) {
 117  0
             ioe.printStackTrace();
 118   
         } finally {
 119  0
             if (bw != null) {
 120  0
                 try {
 121  0
                     bw.close();
 122   
                 } catch (IOException e) {}
 123   
             }
 124  0
             if (bw != null) {
 125  0
                 try {
 126  0
                     br.close();
 127   
                 } catch (IOException e) {}
 128   
             }
 129   
         }
 130   
     }
 131   
 
 132   
     /**
 133   
         Set the source file.
 134   
     */
 135  0
     public void setSrc(File s) {
 136  0
         this.source = s;
 137   
     }
 138   
 
 139   
     /**
 140   
         Set the destination file.
 141   
     */
 142  0
     public void setDest(File dest) {
 143  0
         this.dest = dest;
 144   
     }
 145   
 
 146   
     /**
 147   
         Sets the seperator between name=value arguments
 148   
         in setKeys(). By default it is "*".
 149   
     */
 150  0
     public void setSep(String sep) {
 151  0
         this.sep = sep;
 152   
     }
 153   
     /**
 154   
      * Sets the keys.
 155   
      * 
 156   
         Format string is like this:
 157   
         <p>
 158   
         name=value*name2=value
 159   
         <p>
 160   
         Names are case sensitive.
 161   
         <p>
 162   
         Use the setSep() method to change the * to something else
 163   
         if you need to use * as a name or value.
 164   
     */
 165  0
     public void setKeys(String keys) {
 166  0
         if (keys != null && keys.length() > 0) {
 167  0
             StringTokenizer tok =
 168   
             new StringTokenizer(keys, this.sep, false);
 169  0
             while (tok.hasMoreTokens()) {
 170  0
                 String token = tok.nextToken().trim();
 171  0
                 StringTokenizer itok =
 172   
                 new StringTokenizer(token, "=", false);
 173   
                 
 174  0
                 String name = itok.nextToken();
 175  0
                 String value = itok.nextToken();
 176  0
                 replacements.put(name, value);
 177   
             }
 178   
         }
 179   
     }
 180   
         
 181   
 
 182  0
     public static void main(String[] args) {
 183  0
         try {
 184  0
             Hashtable hash = new Hashtable();
 185  0
             hash.put("VERSION", "1.0.3");
 186  0
             hash.put("b", "ffff");
 187  0
             System.out.println(KeySubst.replace("$f ${VERSION} f ${b} jj $", 
 188   
                                                 hash));
 189   
         } catch (Exception e) {
 190  0
             e.printStackTrace();
 191   
         }
 192   
     }
 193   
 
 194   
     /**
 195   
         Does replacement on text using the hashtable of keys.
 196   
         
 197   
         @return the string with the replacements in it.
 198   
     */
 199  0
     public static String replace(String origString, Hashtable keys)
 200   
         throws BuildException {
 201  0
         StringBuffer finalString = new StringBuffer();
 202  0
         int index = 0;
 203  0
         int i = 0;
 204  0
         String key = null;
 205  0
         while ((index = origString.indexOf("${", i)) > -1) {
 206  0
             key = origString.substring(index + 2, origString.indexOf("}", 
 207   
                                        index + 3));
 208  0
             finalString.append (origString.substring(i, index));
 209  0
             if (keys.containsKey(key)) {
 210  0
                 finalString.append (keys.get(key));
 211   
             } else {
 212  0
                 finalString.append ("${");
 213  0
                 finalString.append (key);
 214  0
                 finalString.append ("}");
 215   
             }
 216  0
             i = index + 3 + key.length();
 217   
         }
 218  0
         finalString.append (origString.substring(i));
 219  0
         return finalString.toString();
 220   
     }
 221   
 }
 222