1 20 package org.apache.cactus.integration.ant.util; 21 22 import java.io.BufferedReader ; 23 import java.io.BufferedWriter ; 24 import java.io.File ; 25 import java.io.FileOutputStream ; 26 import java.io.FileWriter ; 27 import java.io.IOException ; 28 import java.io.InputStream ; 29 import java.io.InputStreamReader ; 30 import java.io.OutputStream ; 31 import java.net.URL ; 32 import java.net.URLDecoder ; 33 import java.util.Vector ; 34 35 import org.apache.tools.ant.Project; 36 import org.apache.tools.ant.filters.util.ChainReaderHelper; 37 import org.apache.tools.ant.types.FilterChain; 38 39 45 public final class ResourceUtils 46 { 47 48 50 53 private ResourceUtils() 54 { 55 } 56 57 59 69 public static void copyResource(Project theProject, String theResourceName, 70 File theDestFile) 71 throws IOException 72 { 73 InputStream in = 74 ResourceUtils.class.getResourceAsStream(theResourceName); 75 if (in == null) 76 { 77 throw new IOException ("Resource '" + theResourceName 78 + "' not found"); 79 } 80 81 OutputStream out = null; 82 try 83 { 84 out = new FileOutputStream (theDestFile); 85 86 byte buf[] = new byte[4096]; 87 int numBytes = 0; 88 while ((numBytes = in.read(buf)) > 0) 89 { 90 out.write(buf, 0, numBytes); 91 } 92 } 93 finally 94 { 95 if (in != null) 96 { 97 in.close(); 98 } 99 if (out != null) 100 { 101 out.close(); 102 } 103 } 104 } 105 106 119 public static void copyResource(Project theProject, String theResourceName, 120 File theDestFile, FilterChain theFilterChain) 121 throws IOException 122 { 123 InputStream resource = 124 ResourceUtils.class.getResourceAsStream(theResourceName); 125 if (resource == null) 126 { 127 throw new IOException ("Resource '" + theResourceName 128 + "' not found"); 129 } 130 131 BufferedReader in = null; 132 BufferedWriter out = null; 133 try 134 { 135 ChainReaderHelper helper = new ChainReaderHelper(); 136 helper.setBufferSize(8192); 137 helper.setPrimaryReader(new BufferedReader ( 138 new InputStreamReader (resource))); 139 Vector filterChains = new Vector (); 140 filterChains.add(theFilterChain); 141 helper.setFilterChains(filterChains); 142 helper.setProject(theProject); 143 in = new BufferedReader (helper.getAssembledReader()); 144 145 out = new BufferedWriter (new FileWriter (theDestFile)); 146 147 String line = null; 148 while ((line = in.readLine()) != null) 149 { 150 if (line.length() == 0) 151 { 152 out.newLine(); 153 } 154 else 155 { 156 out.write(line); 157 out.newLine(); 158 } 159 } 160 } 161 finally 162 { 163 if (in != null) 164 { 165 in.close(); 166 } 167 if (out != null) 168 { 169 out.close(); 170 } 171 } 172 } 173 174 184 public static File getResourceLocation(String theResourceName) 185 { 186 File file = null; 187 URL url = ResourceUtils.class.getResource(theResourceName); 188 if (url != null) 189 { 190 String urlString = url.toString(); 191 if (urlString.startsWith("jar:file:")) 192 { 193 int pling = urlString.indexOf("!"); 194 String jar = urlString.substring(9, pling); 195 file = new File (URLDecoder.decode(jar)); 196 } 197 else if (urlString.startsWith("file:")) 198 { 199 int tail = urlString.indexOf(theResourceName); 200 String dir = urlString.substring(5, tail); 201 file = new File (URLDecoder.decode(dir)); 202 } 203 } 204 return file; 205 } 206 207 } 208 | Popular Tags |