01: /*
02: * @(#)FileInputStreamGenerator.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.io.File;
32: import java.io.FileInputStream;
33:
34: /**
35: * A type of class which can generate a new InputStream based on just a
36: * partial name. Useful if a resource is loaded from a specific location
37: * (classpath resource, file system, URL, etc), and it references other
38: * resources which must be loaded relative to it.
39: *
40: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41: * @since 0.9.1d Alpha (sometime in 2001)
42: * @version $Date: 2003/02/10 22:52:45 $
43: */
44: public class FileInputStreamGenerator implements IInputStreamGenerator {
45: private String root;
46: private String orig;
47:
48: /**
49: * Accepts the name of the 1st file loaded. All files generated from
50: * this generator will be relative to its location. If the name is
51: * a directory, then all relative names will be in this directory.
52: */
53: public FileInputStreamGenerator(String originalName)
54: throws IOException {
55: this .orig = originalName;
56: File f = new File(originalName);
57: if (!f.isDirectory()) {
58: originalName = f.getParent();
59: if (originalName == null) {
60: originalName = "";
61: }
62: }
63: if (originalName.endsWith(File.separator)) {
64: originalName = originalName.substring(0, originalName
65: .length()
66: - File.separator.length());
67: }
68: this .root = originalName;
69: if (this .root.length() <= 0) {
70: this .root = ".";
71: }
72: }
73:
74: public File getFile(String relativeName) {
75: if (!this .orig.equals(relativeName)) {
76: if (!relativeName.startsWith("/")
77: && !relativeName.startsWith(File.separator)) {
78: // only adjust the relative name if it is relative.
79: relativeName = this .root + File.separator
80: + relativeName;
81: }
82: }
83: return new File(relativeName);
84: }
85:
86: public String getFullName(String relativeName) {
87: File f = getFile(relativeName);
88: try {
89: return f.getCanonicalPath();
90: } catch (IOException ioe) {
91: return f.getAbsolutePath();
92: }
93: }
94:
95: public InputStream createInputStream(String relativeName)
96: throws IOException {
97: return new FileInputStream(getFile(relativeName));
98: }
99: }
|