01: package org.jicengine.io;
02:
03: import java.io.*;
04:
05: /**
06: * <p>
07: * A virtual resource whose content is a String, instead of a file on disc.
08: * </p>
09: * <p>
10: * StringResource makes it possible to create resource-content dynamically
11: * and feed it to applications like any other resource.
12: * </p>
13: *
14: * <h4> Neighbouring resources of a StringResource </h4>
15: * <p>
16: * StringResource needs a 'locator resource' that specifies the 'location' of
17: * this virtual resource. StringResource doesn't implement the getResource()
18: * method by itself but forwards the call to its locator resource.
19: * </p>
20: *
21: * <p>
22: * Copyright (C) 2004 Timo Laitinen
23: * </p>
24: * @author Timo Laitinen
25: * @created 2004-09-20
26: * @since JICE-0.10
27: * @version 1.0
28: */
29:
30: public class StringResource extends AbstractResource {
31:
32: private String text;
33: private Resource locatorResource;
34:
35: /**
36: * @param locatorResource a resource that gives this virtual resource a location.
37: * if the StringResource is created from some source resources through some kind
38: * of a transformation, these source resources would be good candidates for
39: * a locatorResource.
40: */
41: public StringResource(String identifier, String text,
42: Resource locatorResource) {
43: super (identifier);
44: this .text = text;
45: this .locatorResource = locatorResource;
46: }
47:
48: public String getText() {
49: return this .text;
50: }
51:
52: public boolean isAvailable() {
53: return true;
54: }
55:
56: public InputStream getInputStream() throws java.io.IOException {
57: return new StringBufferInputStream(getText());
58: }
59:
60: public Reader getReader() throws java.io.IOException {
61: return new StringReader(getText());
62: }
63:
64: public Resource getResource(String relativePath) throws IOException {
65: return this.locatorResource.getResource(relativePath);
66: }
67:
68: }
|