1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.creadur.tentacles;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStreamReader;
24 import java.io.Reader;
25 import java.net.URL;
26
27 public class TentaclesResources {
28
29 private final IOSystem ioSystem;
30
31 public TentaclesResources(final IOSystem ioSystem) {
32 super();
33 this.ioSystem = ioSystem;
34 }
35
36 public Reader read(final String resourceName) throws IOException {
37 final URL resourceUrl = toUrl(resourceName);
38 final InputStreamReader templateReader =
39 new InputStreamReader(resourceUrl.openStream());
40 return templateReader;
41 }
42
43 public String readText(final String resourcePath) throws IOException {
44 final String text = this.ioSystem.slurp(toUrl(resourcePath));
45 return text;
46 }
47
48 public void copyTo(final String resourcePath, final File to)
49 throws IOException {
50 this.ioSystem.copy(toUrl(resourcePath).openStream(), to);
51 }
52
53 private URL toUrl(final String resourcePath) {
54 final URL resourceUrl =
55 this.getClass().getClassLoader().getResource(resourcePath);
56 if (resourceUrl == null) {
57 throw new IllegalStateException(
58 "Tentacles expects the classpath to contain "
59 + resourcePath);
60 }
61 return resourceUrl;
62 }
63 }