01: /*
02: * Copyright 2006-2007 The Scriptella Project Team.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package scriptella.configuration;
17:
18: import scriptella.spi.Resource;
19:
20: import java.io.IOException;
21: import java.io.Reader;
22: import java.io.StringReader;
23:
24: /**
25: * Represents String as a resource.
26: * <p>This implementation is intended to represent
27: * small text blocks.
28: * <p>This resource is immutable and can be cached based
29: * on object identity.
30: *
31: * @author Fyodor Kupolov
32: * @version 1.0
33: */
34: public class StringResource implements Resource {
35: private final String string;
36: private final String description;
37:
38: /**
39: * Creates a resource based on String content.
40: *
41: * @param string resource content.
42: */
43: public StringResource(String string) {
44: this (string, null);
45: }
46:
47: /**
48: * Creates a resource based on String content.
49: *
50: * @param string resource content.
51: * @param description resource description.
52: */
53: public StringResource(String string, String description) {
54: this .string = string;
55: this .description = description;
56: }
57:
58: public Reader open() throws IOException {
59: return new StringReader(string);
60: }
61:
62: /**
63: * Returns the string wrapped by this resource.
64: */
65: public String getString() {
66: return string;
67: }
68:
69: public String toString() {
70: return description == null ? "Text block" : description;
71: }
72: }
|