1 | /*
|
---|
2 | * Copyright 2000,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 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import java.io.BufferedReader;
|
---|
21 | import java.io.BufferedWriter;
|
---|
22 | import java.io.File;
|
---|
23 | import java.io.FileReader;
|
---|
24 | import java.io.FileWriter;
|
---|
25 | import java.io.IOException;
|
---|
26 | import java.util.Hashtable;
|
---|
27 | import java.util.StringTokenizer;
|
---|
28 | import org.apache.tools.ant.BuildException;
|
---|
29 | import org.apache.tools.ant.Task;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Keyword substitution. Input file is written to output file.
|
---|
33 | * Do not make input file same as output file.
|
---|
34 | * Keywords in input files look like this: @foo@. See the docs for the
|
---|
35 | * setKeys method to understand how to do the substitutions.
|
---|
36 | *
|
---|
37 | * @since Ant 1.1
|
---|
38 | * @deprecated KeySubst is deprecated since Ant 1.1. Use Filter + Copy
|
---|
39 | * instead.
|
---|
40 | */
|
---|
41 | public class KeySubst extends Task {
|
---|
42 | private File source = null;
|
---|
43 | private File dest = null;
|
---|
44 | private String sep = "*";
|
---|
45 | private Hashtable replacements = new Hashtable();
|
---|
46 |
|
---|
47 | /**
|
---|
48 | Do the execution.
|
---|
49 | */
|
---|
50 | public void execute() throws BuildException {
|
---|
51 | log("!! KeySubst is deprecated. Use Filter + Copy instead. !!");
|
---|
52 | log("Performing Substitutions");
|
---|
53 | if (source == null || dest == null) {
|
---|
54 | log("Source and destinations must not be null");
|
---|
55 | return;
|
---|
56 | }
|
---|
57 | BufferedReader br = null;
|
---|
58 | BufferedWriter bw = null;
|
---|
59 | try {
|
---|
60 | br = new BufferedReader(new FileReader(source));
|
---|
61 | dest.delete();
|
---|
62 | bw = new BufferedWriter(new FileWriter(dest));
|
---|
63 |
|
---|
64 | String line = null;
|
---|
65 | String newline = null;
|
---|
66 | line = br.readLine();
|
---|
67 | while (line != null) {
|
---|
68 | if (line.length() == 0) {
|
---|
69 | bw.newLine();
|
---|
70 | } else {
|
---|
71 | newline = KeySubst.replace(line, replacements);
|
---|
72 | bw.write(newline);
|
---|
73 | bw.newLine();
|
---|
74 | }
|
---|
75 | line = br.readLine();
|
---|
76 | }
|
---|
77 | bw.flush();
|
---|
78 | } catch (IOException ioe) {
|
---|
79 | ioe.printStackTrace();
|
---|
80 | } finally {
|
---|
81 | if (bw != null) {
|
---|
82 | try {
|
---|
83 | bw.close();
|
---|
84 | } catch (IOException e) {
|
---|
85 | // ignore
|
---|
86 | }
|
---|
87 | }
|
---|
88 | if (br != null) {
|
---|
89 | try {
|
---|
90 | br.close();
|
---|
91 | } catch (IOException e) {
|
---|
92 | // ignore
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | Set the source file.
|
---|
100 | */
|
---|
101 | public void setSrc(File s) {
|
---|
102 | this.source = s;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | Set the destination file.
|
---|
107 | */
|
---|
108 | public void setDest(File dest) {
|
---|
109 | this.dest = dest;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | Sets the separator between name=value arguments
|
---|
114 | in setKeys(). By default it is "*".
|
---|
115 | */
|
---|
116 | public void setSep(String sep) {
|
---|
117 | this.sep = sep;
|
---|
118 | }
|
---|
119 | /**
|
---|
120 | * Sets the keys.
|
---|
121 | *
|
---|
122 | Format string is like this:
|
---|
123 | <p>
|
---|
124 | name=value*name2=value
|
---|
125 | <p>
|
---|
126 | Names are case sensitive.
|
---|
127 | <p>
|
---|
128 | Use the setSep() method to change the * to something else
|
---|
129 | if you need to use * as a name or value.
|
---|
130 | */
|
---|
131 | public void setKeys(String keys) {
|
---|
132 | if (keys != null && keys.length() > 0) {
|
---|
133 | StringTokenizer tok =
|
---|
134 | new StringTokenizer(keys, this.sep, false);
|
---|
135 | while (tok.hasMoreTokens()) {
|
---|
136 | String token = tok.nextToken().trim();
|
---|
137 | StringTokenizer itok =
|
---|
138 | new StringTokenizer(token, "=", false);
|
---|
139 |
|
---|
140 | String name = itok.nextToken();
|
---|
141 | String value = itok.nextToken();
|
---|
142 | replacements.put(name, value);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | public static void main(String[] args) {
|
---|
149 | try {
|
---|
150 | Hashtable hash = new Hashtable();
|
---|
151 | hash.put("VERSION", "1.0.3");
|
---|
152 | hash.put("b", "ffff");
|
---|
153 | System.out.println(KeySubst.replace("$f ${VERSION} f ${b} jj $",
|
---|
154 | hash));
|
---|
155 | } catch (Exception e) {
|
---|
156 | e.printStackTrace();
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | Does replacement on text using the hashtable of keys.
|
---|
162 |
|
---|
163 | @return the string with the replacements in it.
|
---|
164 | */
|
---|
165 | public static String replace(String origString, Hashtable keys)
|
---|
166 | throws BuildException {
|
---|
167 | StringBuffer finalString = new StringBuffer();
|
---|
168 | int index = 0;
|
---|
169 | int i = 0;
|
---|
170 | String key = null;
|
---|
171 | while ((index = origString.indexOf("${", i)) > -1) {
|
---|
172 | key = origString.substring(index + 2, origString.indexOf("}",
|
---|
173 | index + 3));
|
---|
174 | finalString.append (origString.substring(i, index));
|
---|
175 | if (keys.containsKey(key)) {
|
---|
176 | finalString.append (keys.get(key));
|
---|
177 | } else {
|
---|
178 | finalString.append ("${");
|
---|
179 | finalString.append (key);
|
---|
180 | finalString.append ("}");
|
---|
181 | }
|
---|
182 | i = index + 3 + key.length();
|
---|
183 | }
|
---|
184 | finalString.append (origString.substring(i));
|
---|
185 | return finalString.toString();
|
---|
186 | }
|
---|
187 | }
|
---|