001: /*
002: * ========================================================================
003: *
004: * Copyright 2003 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: * ========================================================================
019: */
020: package org.apache.cactus.integration.ant.util;
021:
022: import java.io.BufferedReader;
023: import java.io.BufferedWriter;
024: import java.io.File;
025: import java.io.FileOutputStream;
026: import java.io.FileWriter;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030: import java.io.OutputStream;
031: import java.net.URL;
032: import java.net.URLDecoder;
033: import java.util.Vector;
034:
035: import org.apache.tools.ant.Project;
036: import org.apache.tools.ant.filters.util.ChainReaderHelper;
037: import org.apache.tools.ant.types.FilterChain;
038:
039: /**
040: * Utility class that provides a couple of methods for extracting files stored
041: * as resource in a JAR.
042: *
043: * @version $Id: ResourceUtils.java 238812 2004-02-29 10:21:34Z vmassol $
044: */
045: public final class ResourceUtils {
046:
047: // Constructors ------------------------------------------------------------
048:
049: /**
050: * Private constructor to ensure static usage.
051: */
052: private ResourceUtils() {
053: }
054:
055: // Public Static Methods ---------------------------------------------------
056:
057: /**
058: * Copies a container resource from the JAR into the specified file.
059: *
060: * @param theProject The Ant project
061: * @param theResourceName The name of the resource, relative to the
062: * org.apache.cactus.integration.ant.container package
063: * @param theDestFile The file to which the contents of the resource should
064: * be copied
065: * @throws IOException If an I/O error occurs while copying the resource
066: */
067: public static void copyResource(Project theProject,
068: String theResourceName, File theDestFile)
069: throws IOException {
070: InputStream in = ResourceUtils.class
071: .getResourceAsStream(theResourceName);
072: if (in == null) {
073: throw new IOException("Resource '" + theResourceName
074: + "' not found");
075: }
076:
077: OutputStream out = null;
078: try {
079: out = new FileOutputStream(theDestFile);
080:
081: byte buf[] = new byte[4096];
082: int numBytes = 0;
083: while ((numBytes = in.read(buf)) > 0) {
084: out.write(buf, 0, numBytes);
085: }
086: } finally {
087: if (in != null) {
088: in.close();
089: }
090: if (out != null) {
091: out.close();
092: }
093: }
094: }
095:
096: /**
097: * Copies a container resource from the JAR into the specified file, thereby
098: * applying the specified filters.
099: *
100: * @param theProject The Ant project
101: * @param theResourceName The name of the resource, relative to the
102: * org.apache.cactus.integration.ant.container package
103: * @param theDestFile The file to which the contents of the resource should
104: * be copied
105: * @param theFilterChain The ordered list of filter readers that should be
106: * applied while copying
107: * @throws IOException If an I/O error occurs while copying the resource
108: */
109: public static void copyResource(Project theProject,
110: String theResourceName, File theDestFile,
111: FilterChain theFilterChain) throws IOException {
112: InputStream resource = ResourceUtils.class
113: .getResourceAsStream(theResourceName);
114: if (resource == null) {
115: throw new IOException("Resource '" + theResourceName
116: + "' not found");
117: }
118:
119: BufferedReader in = null;
120: BufferedWriter out = null;
121: try {
122: ChainReaderHelper helper = new ChainReaderHelper();
123: helper.setBufferSize(8192);
124: helper.setPrimaryReader(new BufferedReader(
125: new InputStreamReader(resource)));
126: Vector filterChains = new Vector();
127: filterChains.add(theFilterChain);
128: helper.setFilterChains(filterChains);
129: helper.setProject(theProject);
130: in = new BufferedReader(helper.getAssembledReader());
131:
132: out = new BufferedWriter(new FileWriter(theDestFile));
133:
134: String line = null;
135: while ((line = in.readLine()) != null) {
136: if (line.length() == 0) {
137: out.newLine();
138: } else {
139: out.write(line);
140: out.newLine();
141: }
142: }
143: } finally {
144: if (in != null) {
145: in.close();
146: }
147: if (out != null) {
148: out.close();
149: }
150: }
151: }
152:
153: /**
154: * Search for the given resource and return the directory or archive
155: * that contains it.
156: *
157: * <p>Doesn't work for archives in JDK 1.1 as the URL returned by
158: * getResource doesn't contain the name of the archive.</p>
159: *
160: * @param theResourceName The name of the resource
161: * @return The directory or archive containing the specified resource
162: */
163: public static File getResourceLocation(String theResourceName) {
164: File file = null;
165: URL url = ResourceUtils.class.getResource(theResourceName);
166: if (url != null) {
167: String urlString = url.toString();
168: if (urlString.startsWith("jar:file:")) {
169: int pling = urlString.indexOf("!");
170: String jar = urlString.substring(9, pling);
171: file = new File(URLDecoder.decode(jar));
172: } else if (urlString.startsWith("file:")) {
173: int tail = urlString.indexOf(theResourceName);
174: String dir = urlString.substring(5, tail);
175: file = new File(URLDecoder.decode(dir));
176: }
177: }
178: return file;
179: }
180:
181: }
|