| 1 | |
package org.jbehave.core.io; |
| 2 | |
|
| 3 | |
import java.io.IOException; |
| 4 | |
import java.io.InputStream; |
| 5 | |
|
| 6 | |
import org.apache.commons.io.IOUtils; |
| 7 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 8 | |
import org.apache.commons.lang.builder.ToStringStyle; |
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
public class LoadFromClasspath implements ResourceLoader, StoryLoader { |
| 14 | |
|
| 15 | |
private final ClassLoader classLoader; |
| 16 | |
|
| 17 | |
public LoadFromClasspath() { |
| 18 | 1748 | this(Thread.currentThread().getContextClassLoader()); |
| 19 | 1748 | } |
| 20 | |
|
| 21 | |
public LoadFromClasspath(Class<?> loadFromClass) { |
| 22 | 1 | this(loadFromClass.getClassLoader()); |
| 23 | 1 | } |
| 24 | |
|
| 25 | 1750 | public LoadFromClasspath(ClassLoader classLoader) { |
| 26 | 1750 | this.classLoader = classLoader; |
| 27 | 1750 | } |
| 28 | |
|
| 29 | |
public String loadResourceAsText(String resourcePath) { |
| 30 | 3 | InputStream stream = classLoader.getResourceAsStream(resourcePath); |
| 31 | 3 | if (stream == null) { |
| 32 | 1 | throw new StoryResourceNotFound(resourcePath, classLoader); |
| 33 | |
} |
| 34 | |
try { |
| 35 | 2 | return IOUtils.toString(stream); |
| 36 | 1 | } catch (IOException e) { |
| 37 | 1 | throw new InvalidStoryResource(resourcePath, stream, e); |
| 38 | |
} |
| 39 | |
} |
| 40 | |
|
| 41 | |
public String loadStoryAsText(String storyPath) { |
| 42 | 3 | return loadResourceAsText(storyPath); |
| 43 | |
} |
| 44 | |
|
| 45 | |
@Override |
| 46 | |
public String toString() { |
| 47 | 1 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
| 48 | |
} |
| 49 | |
|
| 50 | |
} |