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 | 2320 | this(Thread.currentThread().getContextClassLoader()); |
19 | 2320 | } |
20 | |
|
21 | |
public LoadFromClasspath(Class<?> loadFromClass) { |
22 | 3 | this(loadFromClass.getClassLoader()); |
23 | 3 | } |
24 | |
|
25 | 2324 | public LoadFromClasspath(ClassLoader classLoader) { |
26 | 2324 | this.classLoader = classLoader; |
27 | 2324 | } |
28 | |
|
29 | |
public String loadResourceAsText(String resourcePath) { |
30 | 4 | InputStream stream = resourceAsStream(resourcePath); |
31 | |
try { |
32 | 3 | return IOUtils.toString(stream); |
33 | 1 | } catch (IOException e) { |
34 | 1 | throw new InvalidStoryResource(resourcePath, stream, e); |
35 | |
} |
36 | |
} |
37 | |
|
38 | |
public String loadStoryAsText(String storyPath) { |
39 | 4 | return loadResourceAsText(storyPath); |
40 | |
} |
41 | |
|
42 | |
protected InputStream resourceAsStream(String resourcePath) { |
43 | 4 | InputStream stream = classLoader.getResourceAsStream(resourcePath); |
44 | 4 | if (stream == null) { |
45 | 1 | throw new StoryResourceNotFound(resourcePath, classLoader); |
46 | |
} |
47 | 3 | return stream; |
48 | |
} |
49 | |
|
50 | |
@Override |
51 | |
public String toString() { |
52 | 1 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
53 | |
} |
54 | |
|
55 | |
} |