Source Code Cross Referenced for ResourceUtils.java in  » Testing » jakarta-cactus » org » apache » cactus » integration » ant » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » jakarta cactus » org.apache.cactus.integration.ant.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.