View Javadoc

1   
2   /*
3    * Copyright (c) 2003 Peter Antman, Teknik i Media  <peter.antman@tim.se>
4    *
5    * $Id: DOMCachedInputStream.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 org.w3c.dom.Document;
24  import java.util.Properties;
25  import java.util.Iterator;
26  import java.util.Map;
27  import java.util.Map.Entry;
28  /***
29   * A cached input stream that reads from a {@link DOMInputStream}.
30   *
31   *
32   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
33   * @version $Revision: 1.1.1.1 $
34   */
35  
36  public class DOMCachedInputStream extends CachedInputStream{
37     Document doc;
38     private Properties props;
39     public DOMCachedInputStream (DOMCachedOutputStream cout) throws IOException{
40        this((Document)cout.getSink());
41     }
42     public DOMCachedInputStream (Document doc){
43        super( new DOMInputStream(doc));
44        this.doc = doc;
45        setProperties();
46     }
47     
48     public void setProperties(Properties props) {
49        this.props = props;
50        setProperties();
51     }
52     
53     private void setProperties() {
54        if ( props != null) {
55           DOMInputStream dis = (DOMInputStream)in;
56           Iterator entries = props.entrySet().iterator();
57           while ( entries.hasNext()) {
58              Map.Entry e = (Map.Entry)entries.next();
59              dis.setProperty(
60                              (String)e.getKey(), 
61                              (String)e.getValue() );
62           } // end of while ()
63           
64        } // end of if ()
65     }
66  
67     public Document getDocument() {
68        return doc;
69     }
70  
71     /***
72      * Get the underlying DOMInputStream.
73      */
74     public DOMInputStream getStream() {
75        return (DOMInputStream)in;
76     }
77     
78     /***
79      * Resets all buffers and opens a new DOMInputStream.
80      */
81     public void rewind() throws IOException {
82        try {
83           // for now the DOMIS does not show any hiddens exceptions in close!
84           in.close();
85        } catch (Exception ignore) {
86           ;//Ingnore
87        } // end of try-catch
88        buf = new byte[buf.length];
89        count = 0;
90        pos = 0;
91        markpos = -1;
92        in = new DOMInputStream(doc);
93        setProperties();
94     }
95     
96  }// DOMCachedInputStream