KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > LoadResource


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
20 import java.io.BufferedInputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.io.Reader JavaDoc;
25 import java.util.Vector JavaDoc;
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.Task;
29 import org.apache.tools.ant.filters.util.ChainReaderHelper;
30 import org.apache.tools.ant.types.FilterChain;
31 import org.apache.tools.ant.types.Resource;
32 import org.apache.tools.ant.types.ResourceCollection;
33 import org.apache.tools.ant.util.FileUtils;
34
35 /**
36  * Load a resource into a property
37  *
38  * @since Ant 1.7
39  * @ant.task category="utility"
40  */

41 public class LoadResource extends Task {
42
43     /**
44      * The resource to load.
45      */

46     private Resource src;
47
48     /**
49      * what to do when it goes pear-shaped
50      */

51     private boolean failOnError = true;
52
53     /**
54      * suppress error message if it goes pear-shaped, sets failOnError=false
55      */

56     private boolean quiet = false;
57
58     /**
59      * Encoding to use for filenames, defaults to the platform's default
60      * encoding.
61      */

62     private String JavaDoc encoding = null;
63
64     /**
65      * name of property
66      */

67     private String JavaDoc property = null;
68
69     /**
70      * Holds FilterChains
71      */

72     private final Vector JavaDoc filterChains = new Vector JavaDoc();
73
74     /**
75      * Encoding to use for input, defaults to the platform's default
76      * encoding. <p>
77      *
78      * For a list of possible values see
79      * <a HREF="http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html">
80      * http://java.sun.com/j2se/1.5.0/docs/guide/intl/encoding.doc.html
81      * </a>.</p>
82      *
83      * @param encoding The new Encoding value
84      */

85
86     public final void setEncoding(final String JavaDoc encoding) {
87         this.encoding = encoding;
88     }
89
90
91     /**
92      * Property name to save to.
93      *
94      * @param property The new Property value
95      */

96     public final void setProperty(final String JavaDoc property) {
97         this.property = property;
98     }
99
100     /**
101      * If true, fail on load error.
102      *
103      * @param fail The new Failonerror value
104      */

105     public final void setFailonerror(final boolean fail) {
106         failOnError = fail;
107     }
108
109     /**
110      * If true, suppress the load error report and set the
111      * the failonerror value to false.
112      * @param quiet The new Quiet value
113      */

114     public void setQuiet(final boolean quiet) {
115         this.quiet = quiet;
116         if (quiet) {
117             this.failOnError = false;
118         }
119     }
120
121     /**
122      * read in a source file to a property
123      *
124      * @exception BuildException if something goes wrong with the build
125      */

126     public final void execute()
127         throws BuildException {
128         //validation
129
if (src == null) {
130             throw new BuildException("source resource not defined");
131         }
132         if (property == null) {
133             throw new BuildException("output property not defined");
134         }
135         if (quiet && failOnError) {
136             throw new BuildException("quiet and failonerror cannot both be "
137                                      + "set to true");
138         }
139         if (!src.isExists()) {
140             String JavaDoc message = src + " doesn't exist";
141             if (failOnError) {
142                 throw new BuildException(message);
143             } else {
144                 log(message, quiet ? Project.MSG_WARN : Project.MSG_ERR);
145                 return;
146             }
147         }
148         InputStream JavaDoc is = null;
149         BufferedInputStream JavaDoc bis = null;
150         Reader JavaDoc instream = null;
151         log("loading " + src + " into property " + property,
152             Project.MSG_VERBOSE);
153         try {
154             final long len = src.getSize();
155             log("resource size = "
156                 + (len != Resource.UNKNOWN_SIZE ? String.valueOf(len)
157                    : "unknown"), 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 JavaDoc(is);
163             if (encoding == null) {
164                 instream = new InputStreamReader JavaDoc(bis);
165             } else {
166                 instream = new InputStreamReader JavaDoc(bis, encoding);
167             }
168
169             String JavaDoc 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 JavaDoc ioe) {
193             final String JavaDoc 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 : Project.MSG_ERR);
199             }
200         } catch (final BuildException be) {
201             if (failOnError) {
202                 throw be;
203             } else {
204                 log(be.getMessage(),
205                     quiet ? Project.MSG_VERBOSE : Project.MSG_ERR);
206             }
207         } finally {
208             FileUtils.close(is);
209         }
210     }
211
212     /**
213      * Add the FilterChain element.
214      * @param filter the filter to add
215      */

216     public final void addFilterChain(FilterChain filter) {
217         filterChains.addElement(filter);
218     }
219
220     /**
221      * Set the source resource.
222      * @param a the resource to load as a single element Resource collection.
223      */

224     public void addConfigured(ResourceCollection a) {
225         if (a.size() != 1) {
226             throw new BuildException("only single argument resource collections"
227                                      + " are supported");
228         }
229         src = (Resource) a.iterator().next();
230     }
231
232 }
233
Popular Tags