You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

KeySubst.java 6.3 kB

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