KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > integration > ant > util > ResourceUtils


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 JavaDoc;
23 import java.io.BufferedWriter JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.OutputStream JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLDecoder JavaDoc;
33 import java.util.Vector JavaDoc;
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,v 1.12 2004/02/29 10:19:57 vmassol Exp $
44  */

45 public final class ResourceUtils
46 {
47
48     // Constructors ------------------------------------------------------------
49

50     /**
51      * Private constructor to ensure static usage.
52      */

53     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     public static void copyResource(Project theProject, String JavaDoc theResourceName,
70         File JavaDoc theDestFile)
71         throws IOException JavaDoc
72     {
73         InputStream JavaDoc in =
74             ResourceUtils.class.getResourceAsStream(theResourceName);
75         if (in == null)
76         {
77             throw new IOException JavaDoc("Resource '" + theResourceName
78                 + "' not found");
79         }
80         
81         OutputStream JavaDoc out = null;
82         try
83         {
84             out = new FileOutputStream JavaDoc(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     /**
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     public static void copyResource(Project theProject, String JavaDoc theResourceName,
120         File JavaDoc theDestFile, FilterChain theFilterChain)
121         throws IOException JavaDoc
122     {
123         InputStream JavaDoc resource =
124             ResourceUtils.class.getResourceAsStream(theResourceName);
125         if (resource == null)
126         {
127             throw new IOException JavaDoc("Resource '" + theResourceName
128                 + "' not found");
129         }
130         
131         BufferedReader JavaDoc in = null;
132         BufferedWriter JavaDoc out = null;
133         try
134         {
135             ChainReaderHelper helper = new ChainReaderHelper();
136             helper.setBufferSize(8192);
137             helper.setPrimaryReader(new BufferedReader JavaDoc(
138                 new InputStreamReader JavaDoc(resource)));
139             Vector JavaDoc filterChains = new Vector JavaDoc();
140             filterChains.add(theFilterChain);
141             helper.setFilterChains(filterChains);
142             helper.setProject(theProject);
143             in = new BufferedReader JavaDoc(helper.getAssembledReader());
144
145             out = new BufferedWriter JavaDoc(new FileWriter JavaDoc(theDestFile));
146
147             String JavaDoc 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     /**
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     public static File JavaDoc getResourceLocation(String JavaDoc theResourceName)
185     {
186         File JavaDoc file = null;
187         URL JavaDoc url = ResourceUtils.class.getResource(theResourceName);
188         if (url != null)
189         {
190             String JavaDoc urlString = url.toString();
191             if (urlString.startsWith("jar:file:"))
192             {
193                 int pling = urlString.indexOf("!");
194                 String JavaDoc jar = urlString.substring(9, pling);
195                 file = new File JavaDoc(URLDecoder.decode(jar));
196             }
197             else if (urlString.startsWith("file:"))
198             {
199                 int tail = urlString.indexOf(theResourceName);
200                 String JavaDoc dir = urlString.substring(5, tail);
201                 file = new File JavaDoc(URLDecoder.decode(dir));
202             }
203         }
204         return file;
205     }
206
207 }
208
Popular Tags