001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.BufferedReader;
022: import java.io.BufferedWriter;
023: import java.io.File;
024: import java.io.FileReader;
025: import java.io.FileWriter;
026: import java.io.IOException;
027: import java.util.Hashtable;
028: import java.util.StringTokenizer;
029: import org.apache.tools.ant.BuildException;
030: import org.apache.tools.ant.Task;
031:
032: /**
033: * Keyword substitution. Input file is written to output file.
034: * Do not make input file same as output file.
035: * Keywords in input files look like this: @foo@. See the docs for the
036: * setKeys method to understand how to do the substitutions.
037: *
038: * @since Ant 1.1
039: * @deprecated KeySubst is deprecated since Ant 1.1. Use Filter + Copy
040: * instead.
041: */
042: public class KeySubst extends Task {
043: private File source = null;
044: private File dest = null;
045: private String sep = "*";
046: private Hashtable replacements = new Hashtable();
047:
048: /**
049: * Do the execution.
050: * @throws BuildException on error
051: */
052: public void execute() throws BuildException {
053: log("!! KeySubst is deprecated. Use Filter + Copy instead. !!");
054: log("Performing Substitutions");
055: if (source == null || dest == null) {
056: log("Source and destinations must not be null");
057: return;
058: }
059: BufferedReader br = null;
060: BufferedWriter bw = null;
061: try {
062: br = new BufferedReader(new FileReader(source));
063: dest.delete();
064: bw = new BufferedWriter(new FileWriter(dest));
065:
066: String line = null;
067: String newline = null;
068: line = br.readLine();
069: while (line != null) {
070: if (line.length() == 0) {
071: bw.newLine();
072: } else {
073: newline = KeySubst.replace(line, replacements);
074: bw.write(newline);
075: bw.newLine();
076: }
077: line = br.readLine();
078: }
079: bw.flush();
080: } catch (IOException ioe) {
081: ioe.printStackTrace();
082: } finally {
083: if (bw != null) {
084: try {
085: bw.close();
086: } catch (IOException e) {
087: // ignore
088: }
089: }
090: if (br != null) {
091: try {
092: br.close();
093: } catch (IOException e) {
094: // ignore
095: }
096: }
097: }
098: }
099:
100: /**
101: * Set the source file.
102: * @param s the source file
103: */
104: public void setSrc(File 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 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 sep) {
122: this .sep = sep;
123: }
124:
125: /**
126: * Sets the keys.
127: *
128: * Format string is like this:
129: * <p>
130: * name=value*name2=value
131: * <p>
132: * Names are case sensitive.
133: * <p>
134: * Use the setSep() method to change the * to something else
135: * if you need to use * as a name or value.
136: * @param keys a <code>String</code> value
137: */
138: public void setKeys(String keys) {
139: if (keys != null && keys.length() > 0) {
140: StringTokenizer tok = new StringTokenizer(keys, this .sep,
141: false);
142: while (tok.hasMoreTokens()) {
143: String token = tok.nextToken().trim();
144: StringTokenizer itok = new StringTokenizer(token, "=",
145: false);
146:
147: String name = itok.nextToken();
148: String value = itok.nextToken();
149: replacements.put(name, value);
150: }
151: }
152: }
153:
154: /**
155: * A test method.
156: * @param args not used
157: */
158: public static void main(String[] args) {
159: try {
160: Hashtable hash = new Hashtable();
161: hash.put("VERSION", "1.0.3");
162: hash.put("b", "ffff");
163: System.out.println(KeySubst.replace(
164: "$f ${VERSION} f ${b} jj $", hash));
165: } catch (Exception 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 replace(String origString, Hashtable keys)
178: throws BuildException {
179: StringBuffer finalString = new StringBuffer();
180: int index = 0;
181: int i = 0;
182: String 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: }
|