View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  }