Home | History | Annotate | Download | only in resourceloader
      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.File;
     22 import java.io.IOException;
     23 import java.io.InputStream;
     24 import java.io.InputStreamReader;
     25 import java.io.Reader;
     26 
     27 /**
     28  * Loads resources from classpath.
     29  *
     30  * <p>
     31  * For example, suppose the classpath contains:
     32  *
     33  * <pre>
     34  * com/foo/my-template.cs
     35  * com/foo/subdir/another-template.cs
     36  * </pre>
     37  *
     38  * <p>
     39  * You can access the resources like this:
     40  *
     41  * <pre>
     42  * ResourceLoader loader =
     43  *     new ClassPathResourceLoader(getClassLoader(), "com/foo");
     44  * loader.open("my-template.cs");
     45  * loader.open("subdir/my-template.cs");
     46  * </pre>
     47  *
     48  * @see ResourceLoader
     49  * @see ClassResourceLoader
     50  */
     51 public class ClassLoaderResourceLoader extends BufferedResourceLoader {
     52 
     53   private final ClassLoader classLoader;
     54   private String basePath;
     55 
     56   public ClassLoaderResourceLoader(ClassLoader classLoader, String basePath) {
     57     this.classLoader = classLoader;
     58     this.basePath = basePath;
     59   }
     60 
     61   public ClassLoaderResourceLoader(ClassLoader classLoader) {
     62     this(classLoader, ".");
     63   }
     64 
     65   @Override
     66   public Reader open(String name) throws IOException {
     67     String path = basePath + '/' + name;
     68     InputStream stream = classLoader.getResourceAsStream(path);
     69     return stream == null ? null : buffer(new InputStreamReader(stream, getCharacterSet()));
     70   }
     71 
     72   @Override
     73   public Reader openOrFail(String name) throws JSilverTemplateNotFoundException, IOException {
     74     Reader reader = open(name);
     75     if (reader == null) {
     76       throw new JSilverTemplateNotFoundException("No class loader resource '" + name + "' in '"
     77           + basePath + "'");
     78     } else {
     79       return reader;
     80     }
     81   }
     82 
     83 }
     84