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 relative to a given class.
046: */
047: public class ClassResourceLocator extends CommonLocator {
048: private final Class clazz;
049:
050: private final String name;
051:
052: final private String resolvedName;
053:
054: /** The encoding of the designated source */
055: private String encoding;
056:
057: public ClassResourceLocator(Class clazz, String name) {
058: super ();
059: this .clazz = clazz;
060: this .name = name;
061: this .resolvedName = resolveName(name);
062: }
063:
064: /*
065: * (non-Javadoc)
066: *
067: * @see de.intarsys.tools.locator.ILocator#getChild(java.lang.String)
068: */
069: public ILocator getChild(String childName) {
070: String child = getResolvedName() + "/" + childName;
071: ClassResourceLocator result = new ClassResourceLocator(
072: getClazz(), child);
073: return result;
074: }
075:
076: /**
077: * The {@link Class} used to define this.
078: * <p>
079: * The resource will be accessed the "getResourceAsStream" of this
080: * {@link Class} object.
081: *
082: * @return The {@link Class} used to define this.
083: */
084: public Class getClazz() {
085: return clazz;
086: }
087:
088: /*
089: * (non-Javadoc)
090: *
091: * @see de.intarsys.tools.locator.ILocator#isDirectory()
092: */
093: public boolean isDirectory() {
094: return false;
095: }
096:
097: /*
098: * (non-Javadoc)
099: *
100: * @see de.intarsys.tools.locator.ILocator#getFullName()
101: */
102: public String getFullName() {
103: return getResolvedName();
104: }
105:
106: /*
107: * (non-Javadoc)
108: *
109: * @see de.intarsys.tools.locator.ILocator#getInputStream()
110: */
111: public InputStream getInputStream() throws IOException {
112: return getClazz().getResourceAsStream(getResolvedName());
113: }
114:
115: /*
116: * (non-Javadoc)
117: *
118: * @see de.intarsys.tools.locator.ILocator#getLocalName()
119: */
120: public String getLocalName() {
121: if (getName() == null) {
122: return "unknown";
123: } else {
124: return FileTools.getBaseName(getName());
125: }
126: }
127:
128: /**
129: * The resource name used when constructing this.
130: *
131: * @return The resource name used when constructing this.
132: */
133: public String getName() {
134: return name;
135: }
136:
137: /*
138: * (non-Javadoc)
139: *
140: * @see de.intarsys.tools.component.ISynchronizable#isOutOfSynch()
141: */
142: public boolean isOutOfSynch() {
143: return false;
144: }
145:
146: /*
147: * (non-Javadoc)
148: *
149: * @see de.intarsys.tools.locator.ILocator#getParent()
150: */
151: public ILocator getParent() {
152: int index = getResolvedName().lastIndexOf("/");
153: if (index > -1) {
154: String parentname = getResolvedName().substring(0, index);
155: return new ClassResourceLocator(getClazz(), 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 de.intarsys.tools.locator.ILocator#getTypedName()
213: */
214: public String getTypedName() {
215: if (getName() == null) {
216: return "<unknown>";
217: }
218: return new File(getName()).getName();
219: }
220:
221: /*
222: * (non-Javadoc)
223: *
224: * @see java.lang.Object#equals(java.lang.Object)
225: */
226: public boolean equals(Object obj) {
227: if (!(obj instanceof ClassResourceLocator)) {
228: return false;
229: }
230: return getResolvedName().equals(
231: ((ClassResourceLocator) obj).getResolvedName());
232: }
233:
234: /*
235: * (non-Javadoc)
236: *
237: * @see de.intarsys.tools.locator.ILocator#exists()
238: */
239: public boolean exists() {
240: return getClazz().getResource(getResolvedName()) != null;
241: }
242:
243: /*
244: * (non-Javadoc)
245: *
246: * @see java.lang.Object#hashCode()
247: */
248: public int hashCode() {
249: return getResolvedName().hashCode();
250: }
251:
252: /*
253: * (non-Javadoc)
254: *
255: * @see de.intarsys.tools.locator.ILocator#listLocators(de.intarsys.tools.locator.ILocatorNameFilter)
256: */
257: public ILocator[] listLocators(final ILocatorNameFilter filter)
258: throws IOException {
259: return new ILocator[0];
260: }
261:
262: /*
263: * (non-Javadoc)
264: *
265: * @see de.intarsys.tools.component.ISynchronizable#synch()
266: */
267: public void synch() {
268: // do nothing
269: }
270:
271: /*
272: * (non-Javadoc)
273: *
274: * @see java.lang.Object#toString()
275: */
276: public String toString() {
277: return getResolvedName();
278: }
279:
280: protected void setEncoding(String encoding) {
281: this .encoding = encoding;
282: }
283:
284: protected String getEncoding() {
285: return encoding;
286: }
287:
288: /**
289: * ..this is borrowed partially from java.lang.Class.
290: *
291: * @param pName
292: * The name to resolve.
293: *
294: * @return The resolved name.
295: */
296: private String resolveName(String pName) {
297: if (pName == null) {
298: return pName;
299: }
300: if (!pName.startsWith("/")) {
301: Class c = getClazz();
302: while (c.isArray()) {
303: c = c.getComponentType();
304: }
305: String baseName = c.getName();
306: int index = baseName.lastIndexOf('.');
307: if (index != -1) {
308: pName = "/"
309: + baseName.substring(0, index)
310: .replace('.', '/') + "/" + pName;
311: }
312: }
313: return pName;
314: }
315:
316: protected String getResolvedName() {
317: return resolvedName;
318: }
319:
320: /*
321: * (non-Javadoc)
322: *
323: * @see de.intarsys.tools.locator.ILocator#getOutputStream()
324: */
325: public OutputStream getOutputStream() throws IOException {
326: throw new UnsupportedOperationException();
327: }
328:
329: /*
330: * (non-Javadoc)
331: *
332: * @see de.intarsys.tools.locator.ILocator#getWriter()
333: */
334: public Writer getWriter() throws IOException {
335: throw new UnsupportedOperationException();
336: }
337:
338: /*
339: * (non-Javadoc)
340: *
341: * @see de.intarsys.tools.locator.ILocator#getWriter(java.lang.String)
342: */
343: public Writer getWriter(String pEncoding) throws IOException {
344: throw new UnsupportedOperationException();
345: }
346:
347: /*
348: * (non-Javadoc)
349: *
350: * @see de.intarsys.tools.locator.ILocator#toURL()
351: */
352: public URL toURL() {
353: return null;
354: }
355:
356: /*
357: * (non-Javadoc)
358: *
359: * @see de.intarsys.tools.locator.ILocator#getRandomAccessData()
360: */
361: public IRandomAccess getRandomAccess() throws IOException {
362: throw new UnsupportedOperationException();
363: }
364:
365: /*
366: * (non-Javadoc)
367: *
368: * @see de.intarsys.tools.locator.ILocator#isReadOnly()
369: */
370: public boolean isReadOnly() {
371: return true;
372: }
373: }
|