1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.BufferedInputStream ; 21 import java.io.ByteArrayInputStream ; 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.Reader ; 26 import java.util.Properties ; 27 import java.util.Vector ; 28 import org.apache.tools.ant.Project; 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Task; 31 import org.apache.tools.ant.filters.util.ChainReaderHelper; 32 import org.apache.tools.ant.types.Path; 33 import org.apache.tools.ant.types.Reference; 34 import org.apache.tools.ant.types.Resource; 35 import org.apache.tools.ant.types.ResourceCollection; 36 import org.apache.tools.ant.types.FilterChain; 37 import org.apache.tools.ant.types.resources.FileResource; 38 import org.apache.tools.ant.types.resources.JavaResource; 39 import org.apache.tools.ant.util.FileUtils; 40 41 47 public class LoadProperties extends Task { 48 49 52 private Resource src = null; 53 54 57 private final Vector filterChains = new Vector (); 58 59 62 private String encoding = null; 63 64 69 public final void setSrcFile(final File srcFile) { 70 addConfigured(new FileResource(srcFile)); 71 } 72 73 78 public void setResource(String resource) { 79 assertSrcIsJavaResource(); 80 ((JavaResource) src).setName(resource); 81 } 82 83 94 public final void setEncoding(final String encoding) { 95 this.encoding = encoding; 96 } 97 98 102 public void setClasspath(Path classpath) { 103 assertSrcIsJavaResource(); 104 ((JavaResource) src).setClasspath(classpath); 105 } 106 107 111 public Path createClasspath() { 112 assertSrcIsJavaResource(); 113 return ((JavaResource) src).createClasspath(); 114 } 115 116 121 public void setClasspathRef(Reference r) { 122 assertSrcIsJavaResource(); 123 ((JavaResource) src).setClasspathRef(r); 124 } 125 126 130 public Path getClasspath() { 131 assertSrcIsJavaResource(); 132 return ((JavaResource) src).getClasspath(); 133 } 134 135 140 public final void execute() throws BuildException { 141 if (src == null) { 143 throw new BuildException("A source resource is required."); 144 } 145 if (!src.isExists()) { 146 if (src instanceof JavaResource) { 147 log("Unable to find resource " + src, Project.MSG_WARN); 149 return; 150 } 151 throw new BuildException("Source resource does not exist: " + src); 152 } 153 154 BufferedInputStream bis = null; 155 Reader instream = null; 156 ByteArrayInputStream tis = null; 157 158 try { 159 bis = new BufferedInputStream (src.getInputStream()); 160 if (encoding == null) { 161 instream = new InputStreamReader (bis); 162 } else { 163 instream = new InputStreamReader (bis, encoding); 164 } 165 166 ChainReaderHelper crh = new ChainReaderHelper(); 167 crh.setPrimaryReader(instream); 168 crh.setFilterChains(filterChains); 169 crh.setProject(getProject()); 170 instream = crh.getAssembledReader(); 171 172 String text = crh.readFully(instream); 173 174 if (text != null) { 175 if (!text.endsWith("\n")) { 176 text = text + "\n"; 177 } 178 179 if (encoding == null) { 180 tis = new ByteArrayInputStream (text.getBytes()); 181 } else { 182 tis = new ByteArrayInputStream (text.getBytes(encoding)); 183 } 184 final Properties props = new Properties (); 185 props.load(tis); 186 187 Property propertyTask = new Property(); 188 propertyTask.bindToOwner(this); 189 propertyTask.addProperties(props); 190 } 191 192 } catch (final IOException ioe) { 193 final String message = "Unable to load file: " + ioe.toString(); 194 throw new BuildException(message, ioe, getLocation()); 195 } finally { 196 FileUtils.close(bis); 197 FileUtils.close(tis); 198 } 199 } 200 201 205 public final void addFilterChain(FilterChain filter) { 206 filterChains.addElement(filter); 207 } 208 209 214 public void addConfigured(ResourceCollection a) { 215 if (src != null) { 216 throw new BuildException("only a single source is supported"); 217 } 218 if (a.size() != 1) { 219 throw new BuildException("only single argument resource collections" 220 + " are supported"); 221 } 222 src = (Resource) a.iterator().next(); 223 } 224 225 private void assertSrcIsJavaResource() { 226 if (src == null) { 227 src = new JavaResource(); 228 src.setProject(getProject()); 229 } else if (!(src instanceof JavaResource)) { 230 throw new BuildException("expected a java resource as source"); 231 } 232 } 233 } 234 | Popular Tags |