1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.backsource.utils.io;
22 import java.io.IOException;
23 import java.util.Properties;
24 import org.w3c.dom.Document;
25 /***
26 * A StreamCache that caches data in a DOM Document.
27 *
28 * <p>It is important to notice that the streams returned by this class will onky work if given valid XML as data. The cached stream classes used by this cache uses {@link DOMInputStream} and {@link DOMOutputStream} so any thing valid for them is also vallid from streams achived from this cache.</p>
29 *
30 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
31 * @version $Revision: 1.1.1.1 $
32 */
33
34 public class DOMCachedStream extends ByteCachedStream {
35 private Properties props;
36 public DOMCachedStream (){
37 }
38 /***
39 * Get a CachedOutputStream that will write to a DOM tree.
40 *
41 * <p>The typical usecase for this is to later use it to get
42 * a CachedInputStream from it, it is also possible to get the DOM Document from it by using the getSink() method:</p>
43 * <pre>
44 CachedOutputStream cos = cache.getOutputStream();
45 //Write to it
46 if ( cos instanceof DOMCachedOutputStream) {
47 Document doc = (Document)cos.getSink();
48 } // end of if ()
49
50 */
51 public CachedOutputStream getOutputStream() throws IOException {
52 return new DOMCachedOutputStream();
53 }
54
55 /***
56 * Get a rewindable input stream from the given CachedOutputStream.
57 *
58 * @throws IOException of the CachedOutputStream was not a DOMCachedOutputStream.
59 */
60 public CachedInputStream getInputStream(CachedOutputStream cout)throws IOException {
61 if ( cout instanceof DOMCachedOutputStream ) {
62 DOMCachedInputStream dis = new DOMCachedInputStream((DOMCachedOutputStream)cout);
63 if ( props != null) {
64 dis.setProperties(props);
65 }
66 return dis;
67 }
68 throw new IOException("The CachedOutputStream is not retrieved through this cache; it is not a DOMCachedOutputStream");
69 }
70
71 /***
72 * A DOM specific method that returns a stream configured with any properties set on this CachedStream.
73 */
74 public CachedInputStream getInputStream(Document doc) throws IOException {
75 DOMCachedInputStream dis = new DOMCachedInputStream(doc);
76 if ( props != null) {
77 dis.setProperties(props);
78 }
79 return dis;
80 }
81
82 /***
83 * Set a property,see {@link DOMInputStream} for the possible properties.
84 */
85 public synchronized void setProperty(String name, String value) {
86 if ( props == null) {
87 props = new Properties();
88 }
89
90 props.setProperty(name,value);
91 }
92
93 /***
94 * Chech if property is set.
95 */
96 public String getProperty(String name){
97 if ( props == null) {
98 props = new Properties();
99 }
100
101 return props.getProperty(name);
102 }
103 }