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.

LoadResource.java 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.BufferedInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.io.Reader;
  24. import java.util.Vector;
  25. import org.apache.tools.ant.BuildException;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.Task;
  28. import org.apache.tools.ant.filters.util.ChainReaderHelper;
  29. import org.apache.tools.ant.types.FilterChain;
  30. import org.apache.tools.ant.types.Resource;
  31. import org.apache.tools.ant.types.ResourceCollection;
  32. import org.apache.tools.ant.util.FileUtils;
  33. /**
  34. * Load a resource into a property
  35. *
  36. * @since Ant 1.7
  37. * @ant.task category="utility"
  38. */
  39. public class LoadResource extends Task {
  40. /**
  41. * The resource to load.
  42. */
  43. private Resource src;
  44. /**
  45. * what to do when it goes pear-shaped
  46. */
  47. private boolean failOnError = true;
  48. /**
  49. * suppress error message if it goes pear-shaped, sets failOnError=false
  50. */
  51. private boolean quiet = false;
  52. /**
  53. * Encoding to use for filenames, defaults to the platform's default
  54. * encoding.
  55. */
  56. private String encoding = null;
  57. /**
  58. * name of property
  59. */
  60. private String property = null;
  61. /**
  62. * Holds FilterChains
  63. */
  64. private final Vector filterChains = new Vector();
  65. /**
  66. * Encoding to use for input, defaults to the platform's default
  67. * encoding. <p>
  68. *
  69. * For a list of possible values see
  70. * <a href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">
  71. * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
  72. * </a>.</p>
  73. *
  74. * @param encoding The new Encoding value
  75. */
  76. public final void setEncoding(final String encoding) {
  77. this.encoding = encoding;
  78. }
  79. /**
  80. * Property name to save to.
  81. *
  82. * @param property The new Property value
  83. */
  84. public final void setProperty(final String property) {
  85. this.property = property;
  86. }
  87. /**
  88. * If true, fail on load error.
  89. *
  90. * @param fail The new Failonerror value
  91. */
  92. public final void setFailonerror(final boolean fail) {
  93. failOnError = fail;
  94. }
  95. /**
  96. * If true, suppress the load error report and set the
  97. * the failonerror value to true.
  98. * @param quiet The new Quiet value
  99. */
  100. public void setQuiet(final boolean quiet) {
  101. this.quiet = quiet;
  102. if (quiet) {
  103. this.failOnError = false;
  104. }
  105. }
  106. /**
  107. * read in a source file to a property
  108. *
  109. * @exception BuildException if something goes wrong with the build
  110. */
  111. public final void execute()
  112. throws BuildException {
  113. //validation
  114. if (src == null) {
  115. throw new BuildException("source resource not defined");
  116. }
  117. if (property == null) {
  118. throw new BuildException("output property not defined");
  119. }
  120. if (quiet && failOnError) {
  121. throw new BuildException("quiet and failonerror cannot both be "
  122. + "set to true");
  123. }
  124. if (!src.isExists()) {
  125. String message = src + " doesn't exist";
  126. if (failOnError) {
  127. throw new BuildException(message);
  128. } else {
  129. log(message, quiet ? Project.MSG_WARN : Project.MSG_ERR);
  130. return;
  131. }
  132. }
  133. InputStream is = null;
  134. BufferedInputStream bis = null;
  135. Reader instream = null;
  136. log("loading " + src + " into property " + property,
  137. Project.MSG_VERBOSE);
  138. try {
  139. final long len = src.getSize();
  140. log("resource size = "
  141. + (len != Resource.UNKNOWN_SIZE ? String.valueOf(len)
  142. : "unknown"), Project.MSG_DEBUG);
  143. //discard most of really big resources
  144. final int size = (int) len;
  145. //open up the resource
  146. is = src.getInputStream();
  147. bis = new BufferedInputStream(is);
  148. if (encoding == null) {
  149. instream = new InputStreamReader(bis);
  150. } else {
  151. instream = new InputStreamReader(bis, encoding);
  152. }
  153. String text = "";
  154. if (size != 0) {
  155. ChainReaderHelper crh = new ChainReaderHelper();
  156. if (len != Resource.UNKNOWN_SIZE) {
  157. crh.setBufferSize(size);
  158. }
  159. crh.setPrimaryReader(instream);
  160. crh.setFilterChains(filterChains);
  161. crh.setProject(getProject());
  162. instream = crh.getAssembledReader();
  163. text = crh.readFully(instream);
  164. }
  165. if (text != null) {
  166. if (text.length() > 0) {
  167. getProject().setNewProperty(property, text);
  168. log("loaded " + text.length() + " characters",
  169. Project.MSG_VERBOSE);
  170. log(property + " := " + text, Project.MSG_DEBUG);
  171. }
  172. }
  173. } catch (final IOException ioe) {
  174. final String message = "Unable to load resource: "
  175. + ioe.toString();
  176. if (failOnError) {
  177. throw new BuildException(message, ioe, getLocation());
  178. } else {
  179. log(message, quiet ? Project.MSG_VERBOSE : Project.MSG_ERR);
  180. }
  181. } catch (final BuildException be) {
  182. if (failOnError) {
  183. throw be;
  184. } else {
  185. log(be.getMessage(),
  186. quiet ? Project.MSG_VERBOSE : Project.MSG_ERR);
  187. }
  188. } finally {
  189. FileUtils.close(is);
  190. }
  191. }
  192. /**
  193. * Add the FilterChain element.
  194. * @param filter the filter to add
  195. */
  196. public final void addFilterChain(FilterChain filter) {
  197. filterChains.addElement(filter);
  198. }
  199. /**
  200. * Set the source resource.
  201. * @param a the resource to load as a single element Resource collection.
  202. */
  203. public void addConfigured(ResourceCollection a) {
  204. if (a.size() != 1) {
  205. throw new BuildException("only single argument resource collections"
  206. + " are supported");
  207. }
  208. src = (Resource) a.iterator().next();
  209. }
  210. }