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