1 /*
2  * Copyright (C) 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.clearsilver.jsilver.resourceloader;
18 
19 import com.google.clearsilver.jsilver.exceptions.JSilverTemplateNotFoundException;
20 
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.InputStreamReader;
24 import java.io.Reader;
25 
26 /**
27  * Loads resources from classpath, alongside a given class.
28  *
29  * <p>For example, suppose the classpath contains:
30  * <pre>
31  * com/foo/SomeThing.class
32  * com/foo/my-template.cs
33  * com/foo/subdir/another-template.cs
34  * </pre>
35  *
36  * <p>You can access the resources in the class's package like this:
37  * <pre>
38  * ResourceLoader loader = new ClassResourceLoader(SomeThing.class);
39  * loader.open("my-template.cs");
40  * loader.open("subdir/my-template.cs");
41  * </pre>
42  * Or by using a relative path:
43  * <pre>
44  * ResourceLoader loader = new ClassResourceLoader(Something.class, "subdir");
45  * loader.open("my-template.cs");
46  * </pre>
47  *
48  * @see ResourceLoader
49  * @see ClassLoaderResourceLoader
50  */
51 public class ClassResourceLoader extends BufferedResourceLoader {
52 
53   private final Class<?> cls;
54   private final String basePath;
55 
ClassResourceLoader(Class<?> cls)56   public ClassResourceLoader(Class<?> cls) {
57     this.cls = cls;
58     this.basePath = "/" + cls.getPackage().getName().replace('.', '/');
59   }
60 
61   /**
62    * Load resources from the given subdirectory {@code basePath},
63    * relative to the .class file of {@code cls}.
64    */
ClassResourceLoader(Class<?> cls, String basePath)65   public ClassResourceLoader(Class<?> cls, String basePath) {
66     this.cls = cls;
67     this.basePath = basePath;
68   }
69 
70   @Override
open(String name)71   public Reader open(String name) throws IOException {
72     InputStream stream = cls.getResourceAsStream(basePath + '/' + name);
73     return stream == null ? null : buffer(new InputStreamReader(stream, getCharacterSet()));
74   }
75 
76   @Override
openOrFail(String name)77   public Reader openOrFail(String name) throws JSilverTemplateNotFoundException, IOException {
78     Reader reader = open(name);
79     if (reader == null) {
80       throw new JSilverTemplateNotFoundException("No '" + name + "' as class resource of "
81           + cls.getName());
82     } else {
83       return reader;
84     }
85   }
86 
87 }
88