View Javadoc

1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: DocumentInputSource.java,v 1.6 2004/06/25 08:03:36 maartenc Exp $
8    */
9   
10  package org.dom4j.io;
11  
12  import java.io.IOException;
13  import java.io.Reader;
14  import java.io.StringReader;
15  import java.io.StringWriter;
16  
17  import org.dom4j.Document;
18  import org.xml.sax.InputSource;
19  
20  
21  /*** <p><code>DocumentInputSource</code> implements a SAX {@link InputSource}
22    * for a {@link Document}.</p>
23    *
24    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
25    * @version $Revision: 1.6 $
26    */
27  class DocumentInputSource extends InputSource {
28  
29      /*** The document source */
30      private Document document;
31      
32  
33      public DocumentInputSource() {
34      }
35  
36      public DocumentInputSource(Document document) {
37          this.document = document;
38          setSystemId( document.getName() );
39      }
40  
41  
42      // Properties
43      //-------------------------------------------------------------------------                
44  
45      /*** @return the document which is being used as the SAX {@link InputSource}
46        */
47      public Document getDocument() {
48          return document;
49      }
50  
51      /*** Sets the document used as the SAX {@link InputSource}
52        */
53      public void setDocument(Document document) {
54          this.document = document;
55          setSystemId( document.getName() );
56      }
57  
58  
59      // Overloaded methods
60      //-------------------------------------------------------------------------                
61  
62      /*** This method is not supported as this source is always a 
63        * {@link Document} instance.
64        *
65        * @throws UnsupportedOperationException as this method is unsupported
66        */
67      public void setCharacterStream(Reader characterStream) 
68              throws UnsupportedOperationException {
69          throw new UnsupportedOperationException();
70      }
71  
72      /*** Note this method is quite inefficent, it turns the in memory XML tree
73        * object model into a single block of text which can then be read by
74        * other XML parsers. Should only be used with care.
75        */
76      public Reader getCharacterStream() {
77          try {
78              StringWriter out = new StringWriter();
79              XMLWriter writer = new XMLWriter( out );
80              writer.write( document );
81              writer.flush();
82              return new StringReader( out.toString() );
83          }
84          catch (final IOException e) {
85              // this should never really happen 
86              // but for completeness we'll return a Reader
87              // with the embedded exception inside it
88              return new Reader() {
89                  public int read(char ch[], int offset, int length) 
90                      throws IOException {
91                      throw e;
92                  }
93                  public void close() throws IOException {
94                  }
95              };
96          }
97      }
98  }
99  
100 
101 
102 
103 
104 
105 
106 /*
107  * Redistribution and use of this software and associated documentation
108  * ("Software"), with or without modification, are permitted provided
109  * that the following conditions are met:
110  *
111  * 1. Redistributions of source code must retain copyright
112  *    statements and notices.  Redistributions must also contain a
113  *    copy of this document.
114  *
115  * 2. Redistributions in binary form must reproduce the
116  *    above copyright notice, this list of conditions and the
117  *    following disclaimer in the documentation and/or other
118  *    materials provided with the distribution.
119  *
120  * 3. The name "DOM4J" must not be used to endorse or promote
121  *    products derived from this Software without prior written
122  *    permission of MetaStuff, Ltd.  For written permission,
123  *    please contact dom4j-info@metastuff.com.
124  *
125  * 4. Products derived from this Software may not be called "DOM4J"
126  *    nor may "DOM4J" appear in their names without prior written
127  *    permission of MetaStuff, Ltd. DOM4J is a registered
128  *    trademark of MetaStuff, Ltd.
129  *
130  * 5. Due credit should be given to the DOM4J Project - 
131  *    http://www.dom4j.org
132  *
133  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
134  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
135  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
136  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
137  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
138  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
139  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
140  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
141  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
142  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
143  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
144  * OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
147  *
148  * $Id: DocumentInputSource.java,v 1.6 2004/06/25 08:03:36 maartenc Exp $
149  */