KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > KeySubst


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.BufferedWriter JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.FileReader JavaDoc;
25 import java.io.FileWriter JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.StringTokenizer JavaDoc;
29 import org.apache.tools.ant.BuildException;
30 import org.apache.tools.ant.Task;
31
32 /**
33  * Keyword substitution. Input file is written to output file.
34  * Do not make input file same as output file.
35  * Keywords in input files look like this: @foo@. See the docs for the
36  * setKeys method to understand how to do the substitutions.
37  *
38  * @since Ant 1.1
39  * @deprecated KeySubst is deprecated since Ant 1.1. Use Filter + Copy
40  * instead.
41  */

42 public class KeySubst extends Task {
43     private File JavaDoc source = null;
44     private File JavaDoc dest = null;
45     private String JavaDoc sep = "*";
46     private Hashtable JavaDoc replacements = new Hashtable JavaDoc();
47
48     /**
49      * Do the execution.
50      * @throws BuildException on error
51      */

52     public void execute() throws BuildException {
53         log("!! KeySubst is deprecated. Use Filter + Copy instead. !!");
54         log("Performing Substitutions");
55         if (source == null || dest == null) {
56             log("Source and destinations must not be null");
57             return;
58         }
59         BufferedReader JavaDoc br = null;
60         BufferedWriter JavaDoc bw = null;
61         try {
62             br = new BufferedReader JavaDoc(new FileReader JavaDoc(source));
63             dest.delete();
64             bw = new BufferedWriter JavaDoc(new FileWriter JavaDoc(dest));
65
66             String JavaDoc line = null;
67             String JavaDoc newline = null;
68             line = br.readLine();
69             while (line != null) {
70                 if (line.length() == 0) {
71                     bw.newLine();
72                 } else {
73                     newline = KeySubst.replace(line, replacements);
74                     bw.write(newline);
75                     bw.newLine();
76                 }
77                 line = br.readLine();
78             }
79             bw.flush();
80         } catch (IOException JavaDoc ioe) {
81             ioe.printStackTrace();
82         } finally {
83             if (bw != null) {
84                 try {
85                     bw.close();
86                 } catch (IOException JavaDoc e) {
87                     // ignore
88
}
89             }
90             if (br != null) {
91                 try {
92                     br.close();
93                 } catch (IOException JavaDoc e) {
94                     // ignore
95
}
96             }
97         }
98     }
99
100     /**
101      * Set the source file.
102      * @param s the source file
103      */

104     public void setSrc(File JavaDoc s) {
105         this.source = s;
106     }
107
108     /**
109      * Set the destination file.
110      * @param dest the destination file
111      */

112     public void setDest(File JavaDoc dest) {
113         this.dest = dest;
114     }
115
116     /**
117      * Sets the separator between name=value arguments
118      * in setKeys(). By default it is "*".
119      * @param sep the separator string
120      */

121     public void setSep(String JavaDoc sep) {
122         this.sep = sep;
123     }
124     /**
125      * Sets the keys.
126      *
127      * Format string is like this:
128      * <p>
129      * name=value*name2=value
130      * <p>
131      * Names are case sensitive.
132      * <p>
133      * Use the setSep() method to change the * to something else
134      * if you need to use * as a name or value.
135      * @param keys a <code>String</code> value
136      */

137     public void setKeys(String JavaDoc keys) {
138         if (keys != null && keys.length() > 0) {
139             StringTokenizer JavaDoc tok =
140             new StringTokenizer JavaDoc(keys, this.sep, false);
141             while (tok.hasMoreTokens()) {
142                 String JavaDoc token = tok.nextToken().trim();
143                 StringTokenizer JavaDoc itok =
144                 new StringTokenizer JavaDoc(token, "=", false);
145
146                 String JavaDoc name = itok.nextToken();
147                 String JavaDoc value = itok.nextToken();
148                 replacements.put(name, value);
149             }
150         }
151     }
152
153
154     /**
155      * A test method.
156      * @param args not used
157      */

158     public static void main(String JavaDoc[] args) {
159         try {
160             Hashtable JavaDoc hash = new Hashtable JavaDoc();
161             hash.put("VERSION", "1.0.3");
162             hash.put("b", "ffff");
163             System.out.println(KeySubst.replace("$f ${VERSION} f ${b} jj $",
164                                                 hash));
165         } catch (Exception JavaDoc e) {
166             e.printStackTrace();
167         }
168     }
169
170     /**
171      * Does replacement on text using the hashtable of keys.
172      * @param origString an input string
173      * @param keys mapping of keys to values
174      * @return the string with the replacements in it.
175      * @throws BuildException on error
176      */

177     public static String JavaDoc replace(String JavaDoc origString, Hashtable JavaDoc keys)
178         throws BuildException {
179         StringBuffer JavaDoc finalString = new StringBuffer JavaDoc();
180         int index = 0;
181         int i = 0;
182         String JavaDoc key = null;
183         while ((index = origString.indexOf("${", i)) > -1) {
184             key = origString.substring(index + 2, origString.indexOf("}",
185                                        index + 3));
186             finalString.append (origString.substring(i, index));
187             if (keys.containsKey(key)) {
188                 finalString.append (keys.get(key));
189             } else {
190                 finalString.append ("${");
191                 finalString.append (key);
192                 finalString.append ("}");
193             }
194             i = index + 3 + key.length();
195         }
196         finalString.append (origString.substring(i));
197         return finalString.toString();
198     }
199 }
200
Popular Tags