001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.tools.locator;
031:
032: import java.io.File;
033: import java.io.IOException;
034: import java.io.InputStream;
035: import java.io.InputStreamReader;
036: import java.io.OutputStream;
037: import java.io.Reader;
038: import java.io.Writer;
039: import java.net.URL;
040:
041: import de.intarsys.tools.file.FileTools;
042: import de.intarsys.tools.randomaccess.IRandomAccess;
043:
044: /**
045: * An {@link ILocator} for java resources identified directly by a class loader.
046: *
047: */
048: public class ClassLoaderResourceLocator extends CommonLocator {
049: private final ClassLoader classLoader;
050:
051: private final String name;
052:
053: /** The encoding of the designated source */
054: private String encoding;
055:
056: public ClassLoaderResourceLocator(ClassLoader classLoader,
057: String name) {
058: super ();
059: this .classLoader = classLoader;
060: this .name = name;
061: }
062:
063: /*
064: * (non-Javadoc)
065: *
066: * @see de.intarsys.tools.locator.ILocator#getChild(java.lang.String)
067: */
068: public ILocator getChild(String childName) {
069: String child = getName() + "/" + childName;
070: ClassLoaderResourceLocator result = new ClassLoaderResourceLocator(
071: getClassLoader(), child);
072: return result;
073: }
074:
075: /*
076: * (non-Javadoc)
077: *
078: * @see de.intarsys.tools.locator.ILocator#isDirectory()
079: */
080: public boolean isDirectory() {
081: return false;
082: }
083:
084: /*
085: * (non-Javadoc)
086: *
087: * @see de.intarsys.tools.locator.ILocator#getFullName()
088: */
089: public String getFullName() {
090: return getName();
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see de.intarsys.tools.locator.ILocator#getInputStream()
097: */
098: public InputStream getInputStream() throws IOException {
099: return getClassLoader().getResourceAsStream(getName());
100: }
101:
102: /*
103: * (non-Javadoc)
104: *
105: * @see de.intarsys.tools.locator.ILocator#getLocalName()
106: */
107: public String getLocalName() {
108: if (getName() == null) {
109: return "unknown";
110: } else {
111: return FileTools.getBaseName(getName());
112: }
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see de.intarsys.tools.locator.ILocator#getTypedName()
119: */
120: public String getTypedName() {
121: if (getName() == null) {
122: return "<unknown>";
123: }
124: return new File(getName()).getName();
125: }
126:
127: /**
128: * The resource name defined when constructing this.
129: *
130: * @return The resource name defined when constructing this.
131: */
132: public String getName() {
133: return name;
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see de.intarsys.tools.component.ISynchronizable#isOutOfSynch()
140: */
141: public boolean isOutOfSynch() {
142: return false;
143: }
144:
145: /*
146: * (non-Javadoc)
147: *
148: * @see de.intarsys.tools.locator.ILocator#getParent()
149: */
150: public ILocator getParent() {
151: int index = getName().lastIndexOf("/");
152: if (index > -1) {
153: String parentname = getName().substring(0, index);
154: return new ClassLoaderResourceLocator(getClassLoader(),
155: parentname);
156: } else {
157: return null;
158: }
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see de.intarsys.tools.locator.ILocator#getReader()
165: */
166: public Reader getReader() throws IOException {
167: if (getEncoding() == null) {
168: return new InputStreamReader(getInputStream());
169: } else {
170: return new InputStreamReader(getInputStream(),
171: getEncoding());
172: }
173: }
174:
175: /*
176: * (non-Javadoc)
177: *
178: * @see de.intarsys.tools.locator.ILocator#getReader(java.lang.String)
179: */
180: public Reader getReader(String newEncoding) throws IOException {
181: if ((newEncoding == null) || newEncoding.equals("")) {
182: return getReader();
183: } else {
184: return new InputStreamReader(getInputStream(), newEncoding);
185: }
186: }
187:
188: /*
189: * (non-Javadoc)
190: *
191: * @see de.intarsys.tools.component.ISynchronizable#isSynchSynchronous()
192: */
193: public boolean isSynchSynchronous() {
194: return false;
195: }
196:
197: /*
198: * (non-Javadoc)
199: *
200: * @see de.intarsys.tools.locator.ILocator#getType()
201: */
202: public String getType() {
203: if (getName() == null) {
204: return "<unknown>";
205: }
206: return FileTools.getExtension(new File(getName()));
207: }
208:
209: /*
210: * (non-Javadoc)
211: *
212: * @see java.lang.Object#equals(java.lang.Object)
213: */
214: public boolean equals(Object obj) {
215: if (!(obj instanceof ClassLoaderResourceLocator)) {
216: return false;
217: }
218: return getName().equals(
219: ((ClassLoaderResourceLocator) obj).getName());
220: }
221:
222: /*
223: * (non-Javadoc)
224: *
225: * @see de.intarsys.tools.locator.ILocator#exists()
226: */
227: public boolean exists() {
228: return getClassLoader().getResource(getName()) != null;
229: }
230:
231: /*
232: * (non-Javadoc)
233: *
234: * @see java.lang.Object#hashCode()
235: */
236: public int hashCode() {
237: return getName().hashCode();
238: }
239:
240: /*
241: * (non-Javadoc)
242: *
243: * @see de.intarsys.tools.locator.ILocator#listLocators(de.intarsys.tools.locator.ILocatorNameFilter)
244: */
245: public ILocator[] listLocators(final ILocatorNameFilter filter)
246: throws IOException {
247: return new ILocator[0];
248: }
249:
250: /*
251: * (non-Javadoc)
252: *
253: * @see de.intarsys.tools.component.ISynchronizable#synch()
254: */
255: public void synch() {
256: // do nothing
257: }
258:
259: /*
260: * (non-Javadoc)
261: *
262: * @see java.lang.Object#toString()
263: */
264: public String toString() {
265: return getName();
266: }
267:
268: protected void setEncoding(String encoding) {
269: this .encoding = encoding;
270: }
271:
272: protected String getEncoding() {
273: return encoding;
274: }
275:
276: /*
277: * (non-Javadoc)
278: *
279: * @see de.intarsys.tools.locator.ILocator#getOutputStream()
280: */
281: public OutputStream getOutputStream() throws IOException {
282: throw new UnsupportedOperationException();
283: }
284:
285: /*
286: * (non-Javadoc)
287: *
288: * @see de.intarsys.tools.locator.ILocator#getWriter()
289: */
290: public Writer getWriter() throws IOException {
291: throw new UnsupportedOperationException();
292: }
293:
294: /*
295: * (non-Javadoc)
296: *
297: * @see de.intarsys.tools.locator.ILocator#getWriter(java.lang.String)
298: */
299: public Writer getWriter(String pEncoding) throws IOException {
300: throw new UnsupportedOperationException();
301: }
302:
303: /**
304: * The {@link ClassLoader} used to access the resource.
305: *
306: * @return The {@link ClassLoader} used to access the resource.
307: */
308: public ClassLoader getClassLoader() {
309: return classLoader;
310: }
311:
312: /*
313: * (non-Javadoc)
314: *
315: * @see de.intarsys.tools.locator.ILocator#toURL()
316: */
317: public URL toURL() {
318: return null;
319: }
320:
321: /*
322: * (non-Javadoc)
323: *
324: * @see de.intarsys.tools.locator.ILocator#getRandomAccessData()
325: */
326: public IRandomAccess getRandomAccess() throws IOException {
327: throw new UnsupportedOperationException();
328: }
329:
330: /*
331: * (non-Javadoc)
332: *
333: * @see de.intarsys.tools.locator.ILocator#isReadOnly()
334: */
335: public boolean isReadOnly() {
336: return true;
337: }
338: }
|