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 7.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000,2002 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.io.BufferedReader;
  56. import java.io.BufferedWriter;
  57. import java.io.File;
  58. import java.io.FileReader;
  59. import java.io.FileWriter;
  60. import java.io.IOException;
  61. import java.util.Hashtable;
  62. import java.util.StringTokenizer;
  63. import org.apache.tools.ant.BuildException;
  64. import org.apache.tools.ant.Task;
  65. /**
  66. * Keyword substitution. Input file is written to output file.
  67. * Do not make input file same as output file.
  68. * Keywords in input files look like this: @foo@. See the docs for the
  69. * setKeys method to understand how to do the substitutions.
  70. *
  71. * @author Jon S. Stevens <a href="mailto:jon@clearink.com">jon@clearink.com</a>
  72. * @since Ant 1.1
  73. * @deprecated KeySubst is deprecated since Ant 1.1. Use Filter + Copy
  74. * instead.
  75. */
  76. public class KeySubst extends Task {
  77. private File source = null;
  78. private File dest = null;
  79. private String sep = "*";
  80. private Hashtable replacements = new Hashtable();
  81. /**
  82. Do the execution.
  83. */
  84. public void execute() throws BuildException {
  85. log("!! KeySubst is deprecated. Use Filter + Copy instead. !!");
  86. log("Performing Substitions");
  87. if (source == null || dest == null) {
  88. log("Source and destinations must not be null");
  89. return;
  90. }
  91. BufferedReader br = null;
  92. BufferedWriter bw = null;
  93. try {
  94. br = new BufferedReader(new FileReader(source));
  95. dest.delete();
  96. bw = new BufferedWriter(new FileWriter(dest));
  97. String line = null;
  98. String newline = null;
  99. line = br.readLine();
  100. while (line != null) {
  101. if (line.length() == 0) {
  102. bw.newLine();
  103. } else {
  104. newline = KeySubst.replace(line, replacements);
  105. bw.write(newline);
  106. bw.newLine();
  107. }
  108. line = br.readLine();
  109. }
  110. bw.flush();
  111. } catch (IOException ioe) {
  112. ioe.printStackTrace();
  113. } finally {
  114. if (bw != null) {
  115. try {
  116. bw.close();
  117. } catch (IOException e) {}
  118. }
  119. if (bw != null) {
  120. try {
  121. br.close();
  122. } catch (IOException e) {}
  123. }
  124. }
  125. }
  126. /**
  127. Set the source file.
  128. */
  129. public void setSrc(File s) {
  130. this.source = s;
  131. }
  132. /**
  133. Set the destination file.
  134. */
  135. public void setDest(File dest) {
  136. this.dest = dest;
  137. }
  138. /**
  139. Sets the seperator between name=value arguments
  140. in setKeys(). By default it is "*".
  141. */
  142. public void setSep(String sep) {
  143. this.sep = sep;
  144. }
  145. /**
  146. * Sets the keys.
  147. *
  148. Format string is like this:
  149. <p>
  150. name=value*name2=value
  151. <p>
  152. Names are case sensitive.
  153. <p>
  154. Use the setSep() method to change the * to something else
  155. if you need to use * as a name or value.
  156. */
  157. public void setKeys(String keys) {
  158. if (keys != null && keys.length() > 0) {
  159. StringTokenizer tok =
  160. new StringTokenizer(keys, this.sep, false);
  161. while (tok.hasMoreTokens()) {
  162. String token = tok.nextToken().trim();
  163. StringTokenizer itok =
  164. new StringTokenizer(token, "=", false);
  165. String name = itok.nextToken();
  166. String value = itok.nextToken();
  167. replacements.put(name, value);
  168. }
  169. }
  170. }
  171. public static void main(String[] args) {
  172. try {
  173. Hashtable hash = new Hashtable();
  174. hash.put("VERSION", "1.0.3");
  175. hash.put("b", "ffff");
  176. System.out.println(KeySubst.replace("$f ${VERSION} f ${b} jj $",
  177. hash));
  178. } catch (Exception e) {
  179. e.printStackTrace();
  180. }
  181. }
  182. /**
  183. Does replacement on text using the hashtable of keys.
  184. @return the string with the replacements in it.
  185. */
  186. public static String replace(String origString, Hashtable keys)
  187. throws BuildException {
  188. StringBuffer finalString = new StringBuffer();
  189. int index = 0;
  190. int i = 0;
  191. String key = null;
  192. while ((index = origString.indexOf("${", i)) > -1) {
  193. key = origString.substring(index + 2, origString.indexOf("}",
  194. index + 3));
  195. finalString.append (origString.substring(i, index));
  196. if (keys.containsKey(key)) {
  197. finalString.append (keys.get(key));
  198. } else {
  199. finalString.append ("${");
  200. finalString.append (key);
  201. finalString.append ("}");
  202. }
  203. i = index + 3 + key.length();
  204. }
  205. finalString.append (origString.substring(i));
  206. return finalString.toString();
  207. }
  208. }