View Javadoc

1   
2   /*
3    * Copyright (c) 2003 Peter Antman, Teknik i Media  <peter.antman@tim.se>
4    *
5    * $Id: DOMCachedStream.java,v 1.1.1.1 2004/05/19 12:07:30 pra Exp $
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2 of the License, or (at your option) any later version
11   * 
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   * 
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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           } // end of if ()
66           return dis;
67        } // end of if ()
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        } // end of if ()
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        } // end of if ()
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        } // end of if ()
100       
101       return props.getProperty(name);
102    }
103 }// DOMCachedStream