1 package org.apache.fulcrum.yaafi.framework.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import org.apache.avalon.framework.logger.Logger;
28 import org.apache.avalon.framework.logger.NullLogger;
29
30 /**
31 * Helper for locating a file name and returning an input stream.
32 *
33 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
34 */
35
36 public class InputStreamLocator
37 {
38 /** the root directory of our search */
39 private File rootDir;
40
41 /** the logger to be used */
42 private Logger logger;
43
44 /**
45 * Constructor
46 */
47 public InputStreamLocator()
48 {
49 this.rootDir = new File( new File("").getAbsolutePath() );
50 this.logger = new NullLogger();
51 }
52
53 /**
54 * Constructor
55 *
56 * @param rootDir the root directory to start the search */
57 public InputStreamLocator( File rootDir )
58 {
59 this( rootDir, new NullLogger() );
60 }
61
62 /**
63 * Constructor
64 *
65 * @param rootDir the root directory to start the search
66 * @param logger the logger to be used
67 */
68 public InputStreamLocator( File rootDir, Logger logger )
69 {
70 this.rootDir = rootDir;
71 this.logger = logger;
72 }
73
74 /**
75 * Locate the file with the given position using the following steps
76 *
77 * @param location the location of the source to be loaded
78 */
79 public InputStream locate( String location ) throws IOException
80 {
81 if( ( location == null ) || ( location.length() == 0 ) )
82 {
83 return null;
84 }
85
86 String baseName = null;
87 File file = null;
88 InputStream is = null;
89
90
91
92
93 if( is == null )
94 {
95 file = new File( this.rootDir, location );
96
97 this.getLogger().debug("Looking for " + location + " in the root directory");
98
99 if( file.exists() )
100 {
101 is = new FileInputStream( file );
102 this.getLogger().debug("Found " + location + " as " + file.getAbsolutePath() );
103 }
104 }
105
106
107
108
109 if( is == null )
110 {
111 file = new File( location );
112
113 this.getLogger().debug("Looking for " + location + " as absolute file location");
114
115 if( file.isAbsolute() && file.exists() )
116 {
117 is = new FileInputStream( file );
118 this.getLogger().debug("Found " + location + " as " + file.getAbsolutePath() );
119 }
120 }
121
122
123
124
125 if( ( is == null ) && ( location.startsWith( "/" ) == true ) )
126 {
127 this.getLogger().debug("Looking for " + location + " using the class loader");
128 is = getClass().getResourceAsStream( location );
129 }
130
131 if( is == null )
132 {
133 this.getLogger().warn("Unable to locate " + location);
134 }
135 else
136 {
137 this.getLogger().debug("Successfully located " + location);
138 }
139
140
141
142
143
144 if( ( is == null ) && ( location.startsWith( "/" ) == false ) )
145 {
146 baseName = '/' + new File(location).getName();
147 this.getLogger().debug("Looking for " + baseName + " using the class loader");
148 is = getClass().getResourceAsStream( baseName );
149 }
150
151 if( is == null )
152 {
153 this.getLogger().warn("Unable to locate " + baseName);
154 }
155 else
156 {
157 this.getLogger().debug("Successfully located " + baseName);
158 }
159
160 return is;
161 }
162
163 /**
164 * @return Returns the logger.
165 */
166 protected Logger getLogger()
167 {
168 return logger;
169 }
170
171 /**
172 * @return Returns the rootDir.
173 */
174 protected File getRootDir()
175 {
176 return rootDir;
177 }
178
179
180 }