1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.BufferedInputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.Reader ; 25 import java.util.Vector ; 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 41 public class LoadResource extends Task { 42 43 46 private Resource src; 47 48 51 private boolean failOnError = true; 52 53 56 private boolean quiet = false; 57 58 62 private String encoding = null; 63 64 67 private String property = null; 68 69 72 private final Vector filterChains = new Vector (); 73 74 85 86 public final void setEncoding(final String encoding) { 87 this.encoding = encoding; 88 } 89 90 91 96 public final void setProperty(final String property) { 97 this.property = property; 98 } 99 100 105 public final void setFailonerror(final boolean fail) { 106 failOnError = fail; 107 } 108 109 114 public void setQuiet(final boolean quiet) { 115 this.quiet = quiet; 116 if (quiet) { 117 this.failOnError = false; 118 } 119 } 120 121 126 public final void execute() 127 throws BuildException { 128 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 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 is = null; 149 BufferedInputStream bis = null; 150 Reader 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 final int size = (int) len; 160 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 : 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 216 public final void addFilterChain(FilterChain filter) { 217 filterChains.addElement(filter); 218 } 219 220 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 |