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