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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000 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 "The Jakarta Project", "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 org.apache.tools.ant.Task;
  56. import org.apache.tools.ant.BuildException;
  57. import java.io.File;
  58. import java.io.BufferedReader;
  59. import java.io.BufferedWriter;
  60. import java.io.FileReader;
  61. import java.io.FileWriter;
  62. import java.io.IOException;
  63. import java.util.Hashtable;
  64. import java.util.StringTokenizer;
  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. *
  73. * @deprecated KeySubst is deprecated. Use Filter + CopyDir instead.
  74. */
  75. public class KeySubst extends Task {
  76. private File source = null;
  77. private File dest = null;
  78. private String sep = "*";
  79. private Hashtable replacements = new Hashtable();
  80. /**
  81. Do the execution.
  82. */
  83. public void execute() throws BuildException {
  84. log("!! KeySubst is deprecated. Use Filter + CopyDir instead. !!");
  85. log("Performing Substitions");
  86. if ( source == null || dest == null ) {
  87. log("Source and destinations must not be null");
  88. return;
  89. }
  90. BufferedReader br = null;
  91. BufferedWriter bw = null;
  92. try {
  93. br = new BufferedReader(new FileReader(source));
  94. dest.delete();
  95. bw = new BufferedWriter(new FileWriter(dest));
  96. String line = null;
  97. String newline = null;
  98. int length;
  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. bw.close();
  112. br.close();
  113. } catch (IOException ioe) {
  114. ioe.printStackTrace();
  115. }
  116. }
  117. /**
  118. Set the source file.
  119. */
  120. public void setSrc(File s) {
  121. this.source = s;
  122. }
  123. /**
  124. Set the destination file.
  125. */
  126. public void setDest(File dest) {
  127. this.dest = dest;
  128. }
  129. /**
  130. Sets the seperator between name=value arguments
  131. in setKeys(). By default it is "*".
  132. */
  133. public void setSep(String sep) {
  134. this.sep = sep;
  135. }
  136. /**
  137. Format string is like this:
  138. <p>
  139. name=value*name2=value
  140. <p>
  141. Names are case sensitive.
  142. <p>
  143. Use the setSep() method to change the * to something else
  144. if you need to use * as a name or value.
  145. */
  146. public void setKeys(String keys) {
  147. if (keys != null && keys.length() > 0) {
  148. StringTokenizer tok =
  149. new StringTokenizer(keys, this.sep, false);
  150. while (tok.hasMoreTokens()) {
  151. String token = tok.nextToken().trim();
  152. StringTokenizer itok =
  153. new StringTokenizer(token, "=", false);
  154. String name = itok.nextToken();
  155. String value = itok.nextToken();
  156. // log ( "Name: " + name );
  157. // log ( "Value: " + value );
  158. replacements.put ( name, value );
  159. }
  160. }
  161. }
  162. public static void main(String[] args)
  163. {
  164. try{
  165. Hashtable hash = new Hashtable();
  166. hash.put ( "VERSION", "1.0.3" );
  167. hash.put ( "b", "ffff" );
  168. System.out.println ( KeySubst.replace ( "$f ${VERSION} f ${b} jj $", hash ) );
  169. }catch ( Exception e)
  170. {
  171. e.printStackTrace();
  172. }
  173. }
  174. /**
  175. Does replacement on text using the hashtable of keys.
  176. @returns the string with the replacements in it.
  177. */
  178. public static String replace ( String origString, Hashtable keys )
  179. throws BuildException
  180. {
  181. StringBuffer finalString=new StringBuffer();
  182. int index=0;
  183. int i = 0;
  184. String key = null;
  185. while ((index = origString.indexOf("${", i)) > -1) {
  186. key = origString.substring(index + 2, origString.indexOf("}", index+3));
  187. finalString.append (origString.substring(i, index));
  188. if ( keys.containsKey ( key ) ) {
  189. finalString.append (keys.get(key));
  190. } else {
  191. finalString.append ( "${" );
  192. finalString.append ( key );
  193. finalString.append ( "}" );
  194. }
  195. i = index + 3 + key.length();
  196. }
  197. finalString.append (origString.substring(i));
  198. return finalString.toString();
  199. }
  200. }