01: /*
02: * Janino - An embedded Java[TM] compiler
03: *
04: * Copyright (c) 2001-2007, Arno Unkrig
05: * All rights reserved.
06: *
07: * Redistribution and use in source and binary forms, with or without
08: * modification, are permitted provided that the following conditions
09: * are met:
10: *
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above
14: * copyright notice, this list of conditions and the following
15: * disclaimer in the documentation and/or other materials
16: * provided with the distribution.
17: * 3. The name of the author may not be used to endorse or promote
18: * products derived from this software without specific prior
19: * written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31: * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32: */
33:
34: package org.codehaus.janino.util.resource;
35:
36: import java.io.*;
37:
38: /**
39: * Finds a resource by name.
40: * <p>
41: * Notice that there is a symmetrical concept
42: * {@link org.codehaus.janino.util.resource.ResourceCreator} that creates resources for
43: * writing.
44: *
45: * @see org.codehaus.janino.util.resource.ResourceCreator
46: */
47: public abstract class ResourceFinder {
48:
49: /**
50: * Find a resource by name and open it for reading.
51: *
52: * @param resourceName Designates the resource; typically structured by slashes ("/") like "<code>com/foo/pkg/Bar.class</code>"
53: * @return <code>null</code> if the resource could not be found
54: * @throws IOException The resource was found, but there are problems opening it
55: */
56: public final InputStream findResourceAsStream(String resourceName)
57: throws IOException {
58: Resource resource = this .findResource(resourceName);
59: if (resource == null)
60: return null;
61: return resource.open();
62: }
63:
64: /**
65: * Find a resource by name and return it as a {@link Resource} object.
66: *
67: * @param resourceName Designates the resource; typically structured by slashes ("/") like "<code>com/foo/pkg/Bar.class</code>"
68: * @return <code>null</code> if the resource could not be found
69: */
70: public abstract Resource findResource(String resourceName);
71:
72: /**
73: * This one's useful when a resource finder is required, but cannot be created
74: * for some reason.
75: */
76: public static final ResourceFinder EMPTY_RESOURCE_FINDER = new ResourceFinder() {
77: public Resource findResource(String resourceName) {
78: return null;
79: }
80:
81: public String toString() {
82: return "invalid entry";
83: }
84: };
85: }
|