01: /*
02: * @(#)URLInputStreamGenerator.java
03: *
04: * Copyright (C) 2001,,2003 2002 Matt Albrecht
05: * groboclown@users.sourceforge.net
06: * http://groboutils.sourceforge.net
07: *
08: * Permission is hereby granted, free of charge, to any person obtaining a
09: * copy of this software and associated documentation files (the "Software"),
10: * to deal in the Software without restriction, including without limitation
11: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12: * and/or sell copies of the Software, and to permit persons to whom the
13: * Software is furnished to do so, subject to the following conditions:
14: *
15: * The above copyright notice and this permission notice shall be included in
16: * all copies or substantial portions of the Software.
17: *
18: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24: * DEALINGS IN THE SOFTWARE.
25: */
26:
27: package net.sourceforge.groboutils.util.io.v1;
28:
29: import java.io.InputStream;
30: import java.io.IOException;
31: import java.net.URL;
32:
33: /**
34: * A type of class which can generate a new InputStream based on just a
35: * partial name. Useful if a resource is loaded from a specific location
36: * (classpath resource, file system, URL, etc), and it references other
37: * resources which must be loaded relative to it.
38: *
39: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
40: * @since 0.9.1d Alpha (sometime in 2001)
41: * @version $Date: 2003/02/10 22:52:45 $
42: */
43: public class URLInputStreamGenerator implements IInputStreamGenerator {
44: private URL root;
45: private String orig;
46:
47: /**
48: * Accepts the name of the 1st file loaded. All files generated from
49: * this generator will be relative to its location.
50: */
51: public URLInputStreamGenerator(String originalName)
52: throws IOException {
53: this .orig = originalName;
54: URL url = new URL(originalName);
55:
56: // JDK 1.3 allows for getPath()...
57: String path = url.getFile();
58: int lastPos = path.lastIndexOf('/');
59: if (lastPos >= 0) {
60: path = path.substring(0, lastPos + 1);
61: }
62: this .root = new URL(url.getProtocol(), url.getHost(), url
63: .getPort(), path);
64: }
65:
66: public URL getURL(String relativeName) throws IOException {
67: URL url;
68: if (this .orig.equals(relativeName)) {
69: url = new URL(relativeName);
70: } else {
71: url = new URL(this .root, relativeName);
72: }
73: return url;
74: }
75:
76: public String getFullName(String relativeName) {
77: try {
78: return getURL(relativeName).toString();
79: } catch (IOException e) {
80: return e.toString();
81: }
82: }
83:
84: public InputStream createInputStream(String relativeName)
85: throws IOException {
86: return getURL(relativeName).openStream();
87: }
88: }
|