LoadFromClasspath.java

  1. package org.jbehave.core.io;

  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.nio.charset.Charset;
  5. import java.nio.charset.StandardCharsets;

  6. import org.apache.commons.io.IOUtils;
  7. import org.apache.commons.lang3.builder.ToStringBuilder;
  8. import org.apache.commons.lang3.builder.ToStringStyle;

  9. /**
  10.  * Loads story resources from classpath
  11.  */
  12. public class LoadFromClasspath implements StoryLoader {

  13.     private final Charset charset;

  14.     protected final ClassLoader classLoader;

  15.     /**
  16.      * Uses default encoding UTF-8.
  17.      * @see #LoadFromClasspath(Charset)
  18.      */
  19.     public LoadFromClasspath() {
  20.         this(StandardCharsets.UTF_8);
  21.     }
  22.    
  23.     /**
  24.      * Uses encoding provided.
  25.      * @param charset the Charset
  26.      * @see #LoadFromClasspath(ClassLoader,Charset)
  27.      */
  28.     public LoadFromClasspath(Charset charset) {
  29.         this(Thread.currentThread().getContextClassLoader(), charset);
  30.     }

  31.     /**
  32.      * Uses a class to get the ClassLoader
  33.      * @param loadFromClass the Class to get the ClassLoader from
  34.      * @see #LoadFromClasspath(ClassLoader)
  35.      */
  36.     public LoadFromClasspath(Class<?> loadFromClass) {
  37.         this(loadFromClass.getClassLoader());
  38.     }

  39.     /**
  40.      * Uses default encoding UTF-8
  41.      * @param classLoader the ClassLoader
  42.      */
  43.     public LoadFromClasspath(ClassLoader classLoader) {
  44.         this(classLoader, StandardCharsets.UTF_8);
  45.     }
  46.    
  47.     /**
  48.      * Uses classloader and encoding provided.
  49.      * @param classLoader the ClassLoader
  50.      * @param charset the Charset
  51.      */
  52.     public LoadFromClasspath(ClassLoader classLoader, Charset charset) {
  53.         this.classLoader = classLoader;
  54.         this.charset = charset;
  55.     }

  56.     @Override
  57.     public String loadResourceAsText(String resourcePath) {
  58.         try (InputStream stream = resourceAsStream(resourcePath)) {
  59.             return IOUtils.toString(stream, charset);
  60.         } catch (IOException e) {
  61.             throw new InvalidStoryResource(resourcePath, e);
  62.         }
  63.     }

  64.     @Override
  65.     public String loadStoryAsText(String storyPath) {
  66.         return loadResourceAsText(storyPath);
  67.     }

  68.     protected InputStream resourceAsStream(String resourcePath) {
  69.         InputStream stream = classLoader.getResourceAsStream(resourcePath);
  70.         if (stream == null) {
  71.             throw new StoryResourceNotFound(resourcePath, classLoader);
  72.         }
  73.         return stream;
  74.     }

  75.     @Override
  76.     public String toString() {
  77.         return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  78.     }
  79. }