source: release-kits/lirk3/resources/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/taskdefs/LoadFile.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 5.8 KB
Line 
1/*
2 * Copyright 2001-2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17package org.apache.tools.ant.taskdefs;
18
19import java.io.BufferedInputStream;
20import java.io.File;
21import java.io.FileInputStream;
22import java.io.IOException;
23import java.io.InputStreamReader;
24import java.io.Reader;
25import java.util.Vector;
26import org.apache.tools.ant.BuildException;
27import org.apache.tools.ant.Project;
28import org.apache.tools.ant.Task;
29import org.apache.tools.ant.filters.util.ChainReaderHelper;
30import org.apache.tools.ant.types.FilterChain;
31
32/**
33 * Load a file into a property
34 *
35 * @since Ant 1.5
36 * @ant.task category="utility"
37 */
38public class LoadFile extends Task {
39
40 /**
41 * source file, usually null
42 */
43 private File srcFile = null;
44
45 /**
46 * what to do when it goes pear-shaped
47 */
48 private boolean failOnError = true;
49
50 /**
51 * Encoding to use for filenames, defaults to the platform's default
52 * encoding.
53 */
54 private String encoding = null;
55
56 /**
57 * name of property
58 */
59 private String property = null;
60
61 /**
62 * Holds FilterChains
63 */
64 private final Vector filterChains = new Vector();
65
66 /**
67 * Encoding to use for input, defaults to the platform's default
68 * encoding. <p>
69 *
70 * For a list of possible values see
71 * <a href="http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html">
72 * http://java.sun.com/products/jdk/1.2/docs/guide/internat/encoding.doc.html
73 * </a>.</p>
74 *
75 * @param encoding The new Encoding value
76 */
77
78 public final void setEncoding(final String encoding) {
79 this.encoding = encoding;
80 }
81
82
83 /**
84 * Property name to save to.
85 *
86 * @param property The new Property value
87 */
88 public final void setProperty(final String property) {
89 this.property = property;
90 }
91
92
93 /**
94 * Sets the file to load.
95 *
96 * @param srcFile The new SrcFile value
97 */
98 public final void setSrcFile(final File srcFile) {
99 this.srcFile = srcFile;
100 }
101
102
103 /**
104 * If true, fail on load error.
105 *
106 * @param fail The new Failonerror value
107 */
108 public final void setFailonerror(final boolean fail) {
109 failOnError = fail;
110 }
111
112
113 /**
114 * read in a source file to a property
115 *
116 * @exception BuildException if something goes wrong with the build
117 */
118 public final void execute()
119 throws BuildException {
120 //validation
121 if (srcFile == null) {
122 throw new BuildException("source file not defined");
123 }
124 if (property == null) {
125 throw new BuildException("output property not defined");
126 }
127 FileInputStream fis = null;
128 BufferedInputStream bis = null;
129 Reader instream = null;
130 log("loading " + srcFile + " into property " + property,
131 Project.MSG_VERBOSE);
132 try {
133 final long len = srcFile.length();
134 log("file size = " + len, Project.MSG_DEBUG);
135 //discard most of really big files
136 final int size = (int) len;
137 //open up the file
138 fis = new FileInputStream(srcFile);
139 bis = new BufferedInputStream(fis);
140 if (encoding == null) {
141 instream = new InputStreamReader(bis);
142 } else {
143 instream = new InputStreamReader(bis, encoding);
144 }
145
146 String text = "";
147 if (size != 0) {
148 ChainReaderHelper crh = new ChainReaderHelper();
149 crh.setBufferSize(size);
150 crh.setPrimaryReader(instream);
151 crh.setFilterChains(filterChains);
152 crh.setProject(getProject());
153 instream = crh.getAssembledReader();
154
155 text = crh.readFully(instream);
156 }
157
158 if (text != null) {
159 if (text.length() > 0) {
160 getProject().setNewProperty(property, text);
161 log("loaded " + text.length() + " characters",
162 Project.MSG_VERBOSE);
163 log(property + " := " + text, Project.MSG_DEBUG);
164 }
165 }
166
167 } catch (final IOException ioe) {
168 final String message = "Unable to load file: " + ioe.toString();
169 if (failOnError) {
170 throw new BuildException(message, ioe, getLocation());
171 } else {
172 log(message, Project.MSG_ERR);
173 }
174 } catch (final BuildException be) {
175 if (failOnError) {
176 throw be;
177 } else {
178 log(be.getMessage(), Project.MSG_ERR);
179 }
180 } finally {
181 try {
182 if (fis != null) {
183 fis.close();
184 }
185 } catch (IOException ioex) {
186 //ignore
187 }
188 }
189 }
190
191 /**
192 * Add the FilterChain element.
193 * @param filter the filter to add
194 */
195 public final void addFilterChain(FilterChain filter) {
196 filterChains.addElement(filter);
197 }
198
199//end class
200}
Note: See TracBrowser for help on using the repository browser.