001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs;
019:
020: import java.io.BufferedInputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.io.Reader;
025: import java.util.Vector;
026: import org.apache.tools.ant.BuildException;
027: import org.apache.tools.ant.Project;
028: import org.apache.tools.ant.Task;
029: import org.apache.tools.ant.filters.util.ChainReaderHelper;
030: import org.apache.tools.ant.types.FilterChain;
031: import org.apache.tools.ant.types.Resource;
032: import org.apache.tools.ant.types.ResourceCollection;
033: import org.apache.tools.ant.util.FileUtils;
034:
035: /**
036: * Load a resource into a property
037: *
038: * @since Ant 1.7
039: * @ant.task category="utility"
040: */
041: public class LoadResource extends Task {
042:
043: /**
044: * The resource to load.
045: */
046: private Resource src;
047:
048: /**
049: * what to do when it goes pear-shaped
050: */
051: private boolean failOnError = true;
052:
053: /**
054: * suppress error message if it goes pear-shaped, sets failOnError=false
055: */
056: private boolean quiet = false;
057:
058: /**
059: * Encoding to use for filenames, defaults to the platform's default
060: * encoding.
061: */
062: private String encoding = null;
063:
064: /**
065: * name of property
066: */
067: private String property = null;
068:
069: /**
070: * Holds FilterChains
071: */
072: private final Vector filterChains = new Vector();
073:
074: /**
075: * Encoding to use for input, defaults to the platform's default
076: * encoding. <p>
077: *
078: * For a list of possible values see
079: * <a href="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">
080: * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
081: * </a>.</p>
082: *
083: * @param encoding The new Encoding value
084: */
085:
086: public final void setEncoding(final String encoding) {
087: this .encoding = encoding;
088: }
089:
090: /**
091: * Property name to save to.
092: *
093: * @param property The new Property value
094: */
095: public final void setProperty(final String property) {
096: this .property = property;
097: }
098:
099: /**
100: * If true, fail on load error.
101: *
102: * @param fail The new Failonerror value
103: */
104: public final void setFailonerror(final boolean fail) {
105: failOnError = fail;
106: }
107:
108: /**
109: * If true, suppress the load error report and set the
110: * the failonerror value to false.
111: * @param quiet The new Quiet value
112: */
113: public void setQuiet(final boolean quiet) {
114: this .quiet = quiet;
115: if (quiet) {
116: this .failOnError = false;
117: }
118: }
119:
120: /**
121: * read in a source file to a property
122: *
123: * @exception BuildException if something goes wrong with the build
124: */
125: public final void execute() throws BuildException {
126: //validation
127: if (src == null) {
128: throw new BuildException("source resource not defined");
129: }
130: if (property == null) {
131: throw new BuildException("output property not defined");
132: }
133: if (quiet && failOnError) {
134: throw new BuildException(
135: "quiet and failonerror cannot both be "
136: + "set to true");
137: }
138: if (!src.isExists()) {
139: String message = src + " doesn't exist";
140: if (failOnError) {
141: throw new BuildException(message);
142: } else {
143: log(message, quiet ? Project.MSG_WARN : Project.MSG_ERR);
144: return;
145: }
146: }
147: InputStream is = null;
148: BufferedInputStream bis = null;
149: Reader instream = null;
150: log("loading " + src + " into property " + property,
151: Project.MSG_VERBOSE);
152: try {
153: final long len = src.getSize();
154: log("resource size = "
155: + (len != Resource.UNKNOWN_SIZE ? String
156: .valueOf(len) : "unknown"),
157: Project.MSG_DEBUG);
158: //discard most of really big resources
159: final int size = (int) len;
160: //open up the resource
161: is = src.getInputStream();
162: bis = new BufferedInputStream(is);
163: if (encoding == null) {
164: instream = new InputStreamReader(bis);
165: } else {
166: instream = new InputStreamReader(bis, encoding);
167: }
168:
169: String text = "";
170: if (size != 0) {
171: ChainReaderHelper crh = new ChainReaderHelper();
172: if (len != Resource.UNKNOWN_SIZE) {
173: crh.setBufferSize(size);
174: }
175: crh.setPrimaryReader(instream);
176: crh.setFilterChains(filterChains);
177: crh.setProject(getProject());
178: instream = crh.getAssembledReader();
179:
180: text = crh.readFully(instream);
181: }
182:
183: if (text != null) {
184: if (text.length() > 0) {
185: getProject().setNewProperty(property, text);
186: log("loaded " + text.length() + " characters",
187: Project.MSG_VERBOSE);
188: log(property + " := " + text, Project.MSG_DEBUG);
189: }
190: }
191:
192: } catch (final IOException ioe) {
193: final String message = "Unable to load resource: "
194: + ioe.toString();
195: if (failOnError) {
196: throw new BuildException(message, ioe, getLocation());
197: } else {
198: log(message, quiet ? Project.MSG_VERBOSE
199: : Project.MSG_ERR);
200: }
201: } catch (final BuildException be) {
202: if (failOnError) {
203: throw be;
204: } else {
205: log(be.getMessage(), quiet ? Project.MSG_VERBOSE
206: : Project.MSG_ERR);
207: }
208: } finally {
209: FileUtils.close(is);
210: }
211: }
212:
213: /**
214: * Add the FilterChain element.
215: * @param filter the filter to add
216: */
217: public final void addFilterChain(FilterChain filter) {
218: filterChains.addElement(filter);
219: }
220:
221: /**
222: * Set the source resource.
223: * @param a the resource to load as a single element Resource collection.
224: */
225: public void addConfigured(ResourceCollection a) {
226: if (a.size() != 1) {
227: throw new BuildException(
228: "only single argument resource collections"
229: + " are supported");
230: }
231: src = (Resource) a.iterator().next();
232: }
233:
234: }
|