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 StoryLoader { |
14 | |
|
15 | |
private final ClassLoader classLoader; |
16 | |
|
17 | |
public LoadFromClasspath() { |
18 | 400 | this(Thread.currentThread().getContextClassLoader()); |
19 | 400 | } |
20 | |
|
21 | |
public LoadFromClasspath(Class<?> loadFromClass) { |
22 | 1 | this(loadFromClass.getClassLoader()); |
23 | 1 | } |
24 | |
|
25 | 402 | public LoadFromClasspath(ClassLoader classLoader) { |
26 | 402 | this.classLoader = classLoader; |
27 | 402 | } |
28 | |
|
29 | |
public String loadStoryAsText(String storyPath) { |
30 | 3 | InputStream stream = classLoader.getResourceAsStream(storyPath); |
31 | 3 | if (stream == null) { |
32 | 1 | throw new StoryResourceNotFound(storyPath, classLoader); |
33 | |
} |
34 | |
try { |
35 | 2 | return IOUtils.toString(stream); |
36 | 1 | } catch (IOException e) { |
37 | 1 | throw new InvalidStoryResource(storyPath, stream, e); |
38 | |
} |
39 | |
} |
40 | |
|
41 | |
@Override |
42 | |
public String toString() { |
43 | 1 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
44 | |
} |
45 | |
} |