|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ResourceUtils.java | 16.7% | 23.1% | 50% | 22.5% |
|
1 |
/*
|
|
2 |
* ====================================================================
|
|
3 |
*
|
|
4 |
* The Apache Software License, Version 1.1
|
|
5 |
*
|
|
6 |
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
|
7 |
* reserved.
|
|
8 |
*
|
|
9 |
* Redistribution and use in source and binary forms, with or without
|
|
10 |
* modification, are permitted provided that the following conditions
|
|
11 |
* are met:
|
|
12 |
*
|
|
13 |
* 1. Redistributions of source code must retain the above copyright
|
|
14 |
* notice, this list of conditions and the following disclaimer.
|
|
15 |
*
|
|
16 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer in
|
|
18 |
* the documentation and/or other materials provided with the
|
|
19 |
* distribution.
|
|
20 |
*
|
|
21 |
* 3. The end-user documentation included with the redistribution, if
|
|
22 |
* any, must include the following acknowlegement:
|
|
23 |
* "This product includes software developed by the
|
|
24 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
25 |
* Alternately, this acknowlegement may appear in the software itself,
|
|
26 |
* if and wherever such third-party acknowlegements normally appear.
|
|
27 |
*
|
|
28 |
* 4. The names "The Jakarta Project", "Cactus" and "Apache Software
|
|
29 |
* Foundation" must not be used to endorse or promote products
|
|
30 |
* derived from this software without prior written permission. For
|
|
31 |
* written permission, please contact apache@apache.org.
|
|
32 |
*
|
|
33 |
* 5. Products derived from this software may not be called "Apache"
|
|
34 |
* nor may "Apache" appear in their names without prior written
|
|
35 |
* permission of the Apache Group.
|
|
36 |
*
|
|
37 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
38 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
39 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
40 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
41 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
42 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
43 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
44 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
45 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
46 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
47 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
48 |
* SUCH DAMAGE.
|
|
49 |
* ====================================================================
|
|
50 |
*
|
|
51 |
* This software consists of voluntary contributions made by many
|
|
52 |
* individuals on behalf of the Apache Software Foundation. For more
|
|
53 |
* information on the Apache Software Foundation, please see
|
|
54 |
* <http://www.apache.org/>.
|
|
55 |
*
|
|
56 |
*/
|
|
57 |
package org.apache.cactus.integration.ant.util;
|
|
58 |
|
|
59 |
import java.io.BufferedReader;
|
|
60 |
import java.io.BufferedWriter;
|
|
61 |
import java.io.File;
|
|
62 |
import java.io.FileOutputStream;
|
|
63 |
import java.io.FileWriter;
|
|
64 |
import java.io.IOException;
|
|
65 |
import java.io.InputStream;
|
|
66 |
import java.io.InputStreamReader;
|
|
67 |
import java.io.OutputStream;
|
|
68 |
import java.net.URL;
|
|
69 |
import java.net.URLDecoder;
|
|
70 |
import java.util.Vector;
|
|
71 |
|
|
72 |
import org.apache.tools.ant.Project;
|
|
73 |
import org.apache.tools.ant.filters.util.ChainReaderHelper;
|
|
74 |
import org.apache.tools.ant.types.FilterChain;
|
|
75 |
|
|
76 |
/**
|
|
77 |
* Utility class that provides a couple of methods for extracting files stored
|
|
78 |
* as resource in a JAR.
|
|
79 |
*
|
|
80 |
* @author <a href="mailto:cmlenz@apache.org">Christopher Lenz</a>
|
|
81 |
*
|
|
82 |
* @version $Id: ResourceUtils.java,v 1.10 2003/06/09 10:13:09 cmlenz Exp $
|
|
83 |
*/
|
|
84 |
public final class ResourceUtils |
|
85 |
{ |
|
86 |
|
|
87 |
// Constructors ------------------------------------------------------------
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Private constructor to ensure static usage.
|
|
91 |
*/
|
|
92 | 0 |
private ResourceUtils()
|
93 |
{ |
|
94 |
} |
|
95 |
|
|
96 |
// Public Static Methods ---------------------------------------------------
|
|
97 |
|
|
98 |
/**
|
|
99 |
* Copies a container resource from the JAR into the specified file.
|
|
100 |
*
|
|
101 |
* @param theProject The Ant project
|
|
102 |
* @param theResourceName The name of the resource, relative to the
|
|
103 |
* org.apache.cactus.integration.ant.container package
|
|
104 |
* @param theDestFile The file to which the contents of the resource should
|
|
105 |
* be copied
|
|
106 |
* @throws IOException If an I/O error occurs while copying the resource
|
|
107 |
*/
|
|
108 | 16 |
public static void copyResource(Project theProject, String theResourceName, |
109 |
File theDestFile) |
|
110 |
throws IOException
|
|
111 |
{ |
|
112 | 16 |
InputStream in = |
113 |
ResourceUtils.class.getResourceAsStream(theResourceName);
|
|
114 | 16 |
if (in == null) |
115 |
{ |
|
116 | 16 |
throw new IOException("Resource '" + theResourceName |
117 |
+ "' not found");
|
|
118 |
} |
|
119 |
|
|
120 | 0 |
OutputStream out = null;
|
121 | 0 |
try
|
122 |
{ |
|
123 | 0 |
out = new FileOutputStream(theDestFile);
|
124 |
|
|
125 | 0 |
byte buf[] = new byte[4096]; |
126 | 0 |
int numBytes = 0;
|
127 | 0 |
while ((numBytes = in.read(buf)) > 0)
|
128 |
{ |
|
129 | 0 |
out.write(buf, 0, numBytes); |
130 |
} |
|
131 |
} |
|
132 |
finally
|
|
133 |
{ |
|
134 | 0 |
if (in != null) |
135 |
{ |
|
136 | 0 |
in.close(); |
137 |
} |
|
138 | 0 |
if (out != null) |
139 |
{ |
|
140 | 0 |
out.close(); |
141 |
} |
|
142 |
} |
|
143 |
} |
|
144 |
|
|
145 |
/**
|
|
146 |
* Copies a container resource from the JAR into the specified file, thereby
|
|
147 |
* applying the specified filters.
|
|
148 |
*
|
|
149 |
* @param theProject The Ant project
|
|
150 |
* @param theResourceName The name of the resource, relative to the
|
|
151 |
* org.apache.cactus.integration.ant.container package
|
|
152 |
* @param theDestFile The file to which the contents of the resource should
|
|
153 |
* be copied
|
|
154 |
* @param theFilterChain The ordered list of filter readers that should be
|
|
155 |
* applied while copying
|
|
156 |
* @throws IOException If an I/O error occurs while copying the resource
|
|
157 |
*/
|
|
158 | 0 |
public static void copyResource(Project theProject, String theResourceName, |
159 |
File theDestFile, FilterChain theFilterChain) |
|
160 |
throws IOException
|
|
161 |
{ |
|
162 | 0 |
InputStream resource = |
163 |
ResourceUtils.class.getResourceAsStream(theResourceName);
|
|
164 | 0 |
if (resource == null) |
165 |
{ |
|
166 | 0 |
throw new IOException("Resource '" + theResourceName |
167 |
+ "' not found");
|
|
168 |
} |
|
169 |
|
|
170 | 0 |
BufferedReader in = null;
|
171 | 0 |
BufferedWriter out = null;
|
172 | 0 |
try
|
173 |
{ |
|
174 | 0 |
ChainReaderHelper helper = new ChainReaderHelper();
|
175 | 0 |
helper.setBufferSize(8192); |
176 | 0 |
helper.setPrimaryReader(new BufferedReader(
|
177 |
new InputStreamReader(resource)));
|
|
178 | 0 |
Vector filterChains = new Vector();
|
179 | 0 |
filterChains.add(theFilterChain); |
180 | 0 |
helper.setFilterChains(filterChains); |
181 | 0 |
helper.setProject(theProject); |
182 | 0 |
in = new BufferedReader(helper.getAssembledReader());
|
183 |
|
|
184 | 0 |
out = new BufferedWriter(new FileWriter(theDestFile)); |
185 |
|
|
186 | 0 |
String line = null;
|
187 | 0 |
while ((line = in.readLine()) != null) |
188 |
{ |
|
189 | 0 |
if (line.length() == 0)
|
190 |
{ |
|
191 | 0 |
out.newLine(); |
192 |
} |
|
193 |
else
|
|
194 |
{ |
|
195 | 0 |
out.write(line); |
196 | 0 |
out.newLine(); |
197 |
} |
|
198 |
} |
|
199 |
} |
|
200 |
finally
|
|
201 |
{ |
|
202 | 0 |
if (in != null) |
203 |
{ |
|
204 | 0 |
in.close(); |
205 |
} |
|
206 | 0 |
if (out != null) |
207 |
{ |
|
208 | 0 |
out.close(); |
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
|
|
213 |
/**
|
|
214 |
* Search for the given resource and return the directory or archive
|
|
215 |
* that contains it.
|
|
216 |
*
|
|
217 |
* <p>Doesn't work for archives in JDK 1.1 as the URL returned by
|
|
218 |
* getResource doesn't contain the name of the archive.</p>
|
|
219 |
*
|
|
220 |
* @param theResourceName The name of the resource
|
|
221 |
* @return The directory or archive containing the specified resource
|
|
222 |
*/
|
|
223 | 75 |
public static File getResourceLocation(String theResourceName) |
224 |
{ |
|
225 | 75 |
File file = null;
|
226 | 75 |
URL url = ResourceUtils.class.getResource(theResourceName);
|
227 | 75 |
if (url != null) |
228 |
{ |
|
229 | 30 |
String urlString = url.toString(); |
230 | 30 |
if (urlString.startsWith("jar:file:")) |
231 |
{ |
|
232 | 30 |
int pling = urlString.indexOf("!"); |
233 | 30 |
String jar = urlString.substring(9, pling); |
234 | 30 |
file = new File(URLDecoder.decode(jar));
|
235 |
} |
|
236 | 0 |
else if (urlString.startsWith("file:")) |
237 |
{ |
|
238 | 0 |
int tail = urlString.indexOf(theResourceName);
|
239 | 0 |
String dir = urlString.substring(5, tail); |
240 | 0 |
file = new File(URLDecoder.decode(dir));
|
241 |
} |
|
242 |
} |
|
243 | 75 |
return file;
|
244 |
} |
|
245 |
|
|
246 |
} |
|
247 |
|
|