KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > io > StringResource


1 package org.jicengine.io;
2
3 import java.io.*;
4
5 /**
6  * <p>
7  * A virtual resource whose content is a String, instead of a file on disc.
8  * </p>
9  * <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 JavaDoc 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 JavaDoc identifier, String JavaDoc text, Resource locatorResource)
42     {
43         super(identifier);
44         this.text = text;
45         this.locatorResource = locatorResource;
46     }
47
48     public String JavaDoc getText()
49     {
50         return this.text;
51     }
52
53     public boolean isAvailable()
54     {
55         return true;
56     }
57
58     public InputStream getInputStream() throws java.io.IOException JavaDoc
59     {
60         return new StringBufferInputStream(getText());
61     }
62
63     public Reader getReader() throws java.io.IOException JavaDoc
64     {
65         return new StringReader(getText());
66     }
67
68     public Resource getResource(String JavaDoc relativePath) throws IOException
69     {
70         return this.locatorResource.getResource(relativePath);
71     }
72
73 }
74
Popular Tags