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.

Property.java 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 1999 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", "Tomcat", 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.*;
  56. import java.io.*;
  57. import java.util.*;
  58. /**
  59. * Will set a Project property. Used to be a hack in ProjectHelper
  60. * Will not override values set by the command line or parent projects.
  61. *
  62. * @author costin@dnt.ro
  63. * @author Sam Ruby <rubys@us.ibm.com>
  64. * @author Glenn McAllister <glennm@ca.ibm.com>
  65. */
  66. public class Property extends Task {
  67. String name;
  68. String value;
  69. String file;
  70. String resource;
  71. boolean userProperty=false; // set read-only properties
  72. public void setName(String name) {
  73. this.name = name;
  74. }
  75. public String getName() {
  76. return name;
  77. }
  78. public void setValue(String value) {
  79. this.value = value;
  80. }
  81. public String getValue() {
  82. return value;
  83. }
  84. public void setFile(String file) {
  85. this.file = file;
  86. }
  87. public void setResource(String resource) {
  88. this.resource = resource;
  89. }
  90. public void init() throws BuildException {
  91. try {
  92. if ((name != null) && (value != null)) {
  93. addProperty(name, value);
  94. }
  95. if (file != null) loadFile(file);
  96. if (resource != null) loadResource(resource);
  97. } catch (Exception e) {
  98. throw new BuildException(e);
  99. }
  100. }
  101. private void loadFile (String name) throws BuildException {
  102. Properties props = new Properties();
  103. log("Loading " + name, Project.MSG_VERBOSE);
  104. try {
  105. if (new File(name).exists()) {
  106. props.load(new FileInputStream(name));
  107. addProperties(props);
  108. } else {
  109. log("Unable to find " + name, Project.MSG_VERBOSE);
  110. }
  111. } catch(Exception ex) {
  112. throw new BuildException(ex.getMessage(), ex, location);
  113. }
  114. }
  115. private void loadResource( String name ) {
  116. Properties props = new Properties();
  117. log("Resource Loading " + name, Project.MSG_VERBOSE);
  118. try {
  119. InputStream is = this.getClass().getResourceAsStream(name);
  120. if (is != null) {
  121. props.load(is);
  122. addProperties(props);
  123. }
  124. } catch (Exception ex) {
  125. ex.printStackTrace();
  126. }
  127. }
  128. private void addProperties(Properties props) {
  129. resolveAllProperties(props);
  130. Enumeration e = props.keys();
  131. while (e.hasMoreElements()) {
  132. String name = (String) e.nextElement();
  133. String value = (String) props.getProperty(name);
  134. String v = ProjectHelper.replaceProperties(value, project.getProperties());
  135. addProperty(name, value);
  136. }
  137. }
  138. public void setUserProperty( boolean userP ) {
  139. userProperty=userP;
  140. }
  141. private void addProperty(String n, String v) {
  142. if( userProperty ) {
  143. if (project.getUserProperty(n) == null) {
  144. project.setUserProperty(n, v);
  145. } else {
  146. log("Override ignored for " + name, Project.MSG_VERBOSE);
  147. }
  148. } else {
  149. if (project.getProperty(n) == null) {
  150. project.setProperty(n, v);
  151. } else {
  152. log("Override ignored for " + name, Project.MSG_VERBOSE);
  153. }
  154. }
  155. }
  156. private void resolveAllProperties(Hashtable props) {
  157. Hashtable toResolve = new Hashtable();
  158. Enumeration e = props.keys();
  159. boolean more = true;
  160. while (more) {
  161. while (e.hasMoreElements()) {
  162. Vector propsInValue = new Vector();
  163. String name = (String) e.nextElement();
  164. String value = (String) props.get(name);
  165. if (extractProperties(value, propsInValue)) {
  166. for (int i=0; i < propsInValue.size(); i++) {
  167. String elem = (String) propsInValue.elementAt(i);
  168. if (project.getProperties().containsKey(elem) ||
  169. props.containsKey(elem)) {
  170. toResolve.put(name, value);
  171. break;
  172. }
  173. }
  174. }
  175. if (toResolve.size() > 0) {
  176. Enumeration tre = toResolve.keys();
  177. while (tre.hasMoreElements()) {
  178. String name2 = (String) tre.nextElement();
  179. String value2 = (String) toResolve.get(name2);
  180. String v = ProjectHelper.replaceProperties(value2,
  181. project.getProperties());
  182. v = ProjectHelper.replaceProperties(v, props);
  183. props.put(name, v);
  184. }
  185. toResolve.clear();
  186. e = props.keys();
  187. } else {
  188. more = false;
  189. }
  190. }
  191. }
  192. }
  193. private boolean extractProperties(String source, Vector properties) {
  194. // This is an abreviated version of
  195. // ProjectHelper.replaceProperties method
  196. int i=0;
  197. int prev=0;
  198. int pos;
  199. while( (pos=source.indexOf( "$", prev )) >= 0 ) {
  200. if( pos == (source.length() - 1)) {
  201. prev = pos + 1;
  202. } else if (source.charAt( pos + 1 ) != '{' ) {
  203. prev=pos+2;
  204. } else {
  205. int endName=source.indexOf( '}', pos );
  206. String n=source.substring( pos+2, endName );
  207. properties.addElement(n);
  208. prev=endName+1;
  209. }
  210. }
  211. return (properties.size() > 0);
  212. }
  213. }