|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ResourceUtils.java | 16.7% | 23.1% | 50% | 22.5% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ========================================================================
|
|
| 3 |
*
|
|
| 4 |
* Copyright 2003 The Apache Software Foundation.
|
|
| 5 |
*
|
|
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 7 |
* you may not use this file except in compliance with the License.
|
|
| 8 |
* You may obtain a copy of the License at
|
|
| 9 |
*
|
|
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 11 |
*
|
|
| 12 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 15 |
* See the License for the specific language governing permissions and
|
|
| 16 |
* limitations under the License.
|
|
| 17 |
*
|
|
| 18 |
* ========================================================================
|
|
| 19 |
*/
|
|
| 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 |
/**
|
|
| 40 |
* Utility class that provides a couple of methods for extracting files stored
|
|
| 41 |
* as resource in a JAR.
|
|
| 42 |
*
|
|
| 43 |
* @version $Id: ResourceUtils.java 238812 2004-02-29 10:21:34Z vmassol $
|
|
| 44 |
*/
|
|
| 45 |
public final class ResourceUtils |
|
| 46 |
{
|
|
| 47 |
|
|
| 48 |
// Constructors ------------------------------------------------------------
|
|
| 49 |
|
|
| 50 |
/**
|
|
| 51 |
* Private constructor to ensure static usage.
|
|
| 52 |
*/
|
|
| 53 | 0 |
private ResourceUtils()
|
| 54 |
{
|
|
| 55 |
} |
|
| 56 |
|
|
| 57 |
// Public Static Methods ---------------------------------------------------
|
|
| 58 |
|
|
| 59 |
/**
|
|
| 60 |
* Copies a container resource from the JAR into the specified file.
|
|
| 61 |
*
|
|
| 62 |
* @param theProject The Ant project
|
|
| 63 |
* @param theResourceName The name of the resource, relative to the
|
|
| 64 |
* org.apache.cactus.integration.ant.container package
|
|
| 65 |
* @param theDestFile The file to which the contents of the resource should
|
|
| 66 |
* be copied
|
|
| 67 |
* @throws IOException If an I/O error occurs while copying the resource
|
|
| 68 |
*/
|
|
| 69 | 22 |
public static void copyResource(Project theProject, String theResourceName, |
| 70 |
File theDestFile) |
|
| 71 |
throws IOException
|
|
| 72 |
{
|
|
| 73 | 22 |
InputStream in = |
| 74 |
ResourceUtils.class.getResourceAsStream(theResourceName);
|
|
| 75 | 22 |
if (in == null) |
| 76 |
{
|
|
| 77 | 22 |
throw new IOException("Resource '" + theResourceName |
| 78 |
+ "' not found");
|
|
| 79 |
} |
|
| 80 |
|
|
| 81 | 0 |
OutputStream out = null;
|
| 82 | 0 |
try
|
| 83 |
{
|
|
| 84 | 0 |
out = new FileOutputStream(theDestFile);
|
| 85 |
|
|
| 86 | 0 |
byte buf[] = new byte[4096]; |
| 87 | 0 |
int numBytes = 0;
|
| 88 | 0 |
while ((numBytes = in.read(buf)) > 0)
|
| 89 |
{
|
|
| 90 | 0 |
out.write(buf, 0, numBytes); |
| 91 |
} |
|
| 92 |
} |
|
| 93 |
finally
|
|
| 94 |
{
|
|
| 95 | 0 |
if (in != null) |
| 96 |
{
|
|
| 97 | 0 |
in.close(); |
| 98 |
} |
|
| 99 | 0 |
if (out != null) |
| 100 |
{
|
|
| 101 | 0 |
out.close(); |
| 102 |
} |
|
| 103 |
} |
|
| 104 |
} |
|
| 105 |
|
|
| 106 |
/**
|
|
| 107 |
* Copies a container resource from the JAR into the specified file, thereby
|
|
| 108 |
* applying the specified filters.
|
|
| 109 |
*
|
|
| 110 |
* @param theProject The Ant project
|
|
| 111 |
* @param theResourceName The name of the resource, relative to the
|
|
| 112 |
* org.apache.cactus.integration.ant.container package
|
|
| 113 |
* @param theDestFile The file to which the contents of the resource should
|
|
| 114 |
* be copied
|
|
| 115 |
* @param theFilterChain The ordered list of filter readers that should be
|
|
| 116 |
* applied while copying
|
|
| 117 |
* @throws IOException If an I/O error occurs while copying the resource
|
|
| 118 |
*/
|
|
| 119 | 0 |
public static void copyResource(Project theProject, String theResourceName, |
| 120 |
File theDestFile, FilterChain theFilterChain) |
|
| 121 |
throws IOException
|
|
| 122 |
{
|
|
| 123 | 0 |
InputStream resource = |
| 124 |
ResourceUtils.class.getResourceAsStream(theResourceName);
|
|
| 125 | 0 |
if (resource == null) |
| 126 |
{
|
|
| 127 | 0 |
throw new IOException("Resource '" + theResourceName |
| 128 |
+ "' not found");
|
|
| 129 |
} |
|
| 130 |
|
|
| 131 | 0 |
BufferedReader in = null;
|
| 132 | 0 |
BufferedWriter out = null;
|
| 133 | 0 |
try
|
| 134 |
{
|
|
| 135 | 0 |
ChainReaderHelper helper = new ChainReaderHelper();
|
| 136 | 0 |
helper.setBufferSize(8192); |
| 137 | 0 |
helper.setPrimaryReader(new BufferedReader(
|
| 138 |
new InputStreamReader(resource)));
|
|
| 139 | 0 |
Vector filterChains = new Vector();
|
| 140 | 0 |
filterChains.add(theFilterChain); |
| 141 | 0 |
helper.setFilterChains(filterChains); |
| 142 | 0 |
helper.setProject(theProject); |
| 143 | 0 |
in = new BufferedReader(helper.getAssembledReader());
|
| 144 |
|
|
| 145 | 0 |
out = new BufferedWriter(new FileWriter(theDestFile)); |
| 146 |
|
|
| 147 | 0 |
String line = null;
|
| 148 | 0 |
while ((line = in.readLine()) != null) |
| 149 |
{
|
|
| 150 | 0 |
if (line.length() == 0)
|
| 151 |
{
|
|
| 152 | 0 |
out.newLine(); |
| 153 |
} |
|
| 154 |
else
|
|
| 155 |
{
|
|
| 156 | 0 |
out.write(line); |
| 157 | 0 |
out.newLine(); |
| 158 |
} |
|
| 159 |
} |
|
| 160 |
} |
|
| 161 |
finally
|
|
| 162 |
{
|
|
| 163 | 0 |
if (in != null) |
| 164 |
{
|
|
| 165 | 0 |
in.close(); |
| 166 |
} |
|
| 167 | 0 |
if (out != null) |
| 168 |
{
|
|
| 169 | 0 |
out.close(); |
| 170 |
} |
|
| 171 |
} |
|
| 172 |
} |
|
| 173 |
|
|
| 174 |
/**
|
|
| 175 |
* Search for the given resource and return the directory or archive
|
|
| 176 |
* that contains it.
|
|
| 177 |
*
|
|
| 178 |
* <p>Doesn't work for archives in JDK 1.1 as the URL returned by
|
|
| 179 |
* getResource doesn't contain the name of the archive.</p>
|
|
| 180 |
*
|
|
| 181 |
* @param theResourceName The name of the resource
|
|
| 182 |
* @return The directory or archive containing the specified resource
|
|
| 183 |
*/
|
|
| 184 | 105 |
public static File getResourceLocation(String theResourceName) |
| 185 |
{
|
|
| 186 | 105 |
File file = null;
|
| 187 | 105 |
URL url = ResourceUtils.class.getResource(theResourceName);
|
| 188 | 105 |
if (url != null) |
| 189 |
{
|
|
| 190 | 42 |
String urlString = url.toString(); |
| 191 | 42 |
if (urlString.startsWith("jar:file:")) |
| 192 |
{
|
|
| 193 | 42 |
int pling = urlString.indexOf("!"); |
| 194 | 42 |
String jar = urlString.substring(9, pling); |
| 195 | 42 |
file = new File(URLDecoder.decode(jar));
|
| 196 |
} |
|
| 197 | 0 |
else if (urlString.startsWith("file:")) |
| 198 |
{
|
|
| 199 | 0 |
int tail = urlString.indexOf(theResourceName);
|
| 200 | 0 |
String dir = urlString.substring(5, tail); |
| 201 | 0 |
file = new File(URLDecoder.decode(dir));
|
| 202 |
} |
|
| 203 |
} |
|
| 204 | 105 |
return file;
|
| 205 |
} |
|
| 206 |
|
|
| 207 |
} |
|
| 208 |
|
|
||||||||||