KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xquery > xdbc > AbstractXMLDocument


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xquery.xdbc;
24
25 import java.io.StringWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31
32 import org.w3c.dom.Document JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.xml.sax.*;
35 import org.xml.sax.ext.LexicalHandler JavaDoc;
36 import org.xquark.serialize.XMLSerializer;
37 import org.xquark.util.DOMBuilder;
38 import org.xquark.util.SAXConstants;
39 import org.xquark.xml.xdbc.*;
40
41
42 /**
43  * This class is an abstract class implementing XMLDocument.
44  * It describes a document object which provides access to XML data
45  * in several ways. When the underlying object is a stored document, it also
46  * supports a number of document management methods also available in the
47  * XMLCollection interface.<BR><BR>
48  */

49 public abstract class AbstractXMLDocument implements XMLDocument
50 {
51     private static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
52     private static final String JavaDoc RCSName = "$Name: $";
53     
54     private static DocumentBuilder JavaDoc DOMCreator;
55     
56     /**
57      * The XML collection.
58      */

59     private XMLCollection xmlCollection = null ;
60     
61     
62     /**
63      * The document identifier in the XML collection.
64      */

65     private String JavaDoc identifier = null;
66     private boolean readOnly = false;
67     
68     protected ContentHandler contentHandler ;
69     protected ErrorHandler errorHandler ;
70     protected LexicalHandler JavaDoc lexicalHandler ;
71     
72     protected AbstractXMLDocument()
73     {
74     }
75     
76     protected AbstractXMLDocument(boolean readOnly)
77     {
78         setReadOnly(readOnly);
79     }
80     
81     protected AbstractXMLDocument(String JavaDoc Id)
82     {
83         this(Id, null, false);
84     }
85     
86     protected AbstractXMLDocument(String JavaDoc Id, boolean readOnly)
87     {
88         this(Id, null, readOnly);
89     }
90     
91     protected AbstractXMLDocument(String JavaDoc Id, XMLCollection collection)
92     {
93         this(Id, collection, false);
94     }
95     
96     protected AbstractXMLDocument(String JavaDoc Id, XMLCollection collection, boolean readOnly)
97     {
98         identifier = Id;
99         xmlCollection = collection;
100         setReadOnly(readOnly);
101     }
102     
103     protected void setReadOnly(boolean readOnly)
104     {
105         this.readOnly = readOnly;
106     }
107     
108     /**
109      * Returns the document identifier in the XML collection.
110      * return the document identifier as a String, or null if the document is not a stored
111      * object.
112      */

113     public String JavaDoc getIdentifier()
114     {
115         return identifier ;
116     }
117     
118     /**
119      * Changes the document identifier in the XML collection.
120      * This change is persistent in the data source.
121      * Note: Depending on the auto-commit mode of the underlying data
122      * source connection, the change may only become definitive after a
123      * commit operation.
124      * @param the new document identifier
125      * @throws XMLDBCException if a data source access error occurs.
126      * @throws XMLDBCNotSupportedException if the renaming operation is not supported.
127      */

128     public void setIdentifier(String JavaDoc identifier) throws XMLDBCException, XMLDBCNotSupportedException
129     {
130         if (readOnly)
131             throw new XMLDBCNotSupportedException("This document is read-only.");
132         if (xmlCollection != null)
133             xmlCollection.renameDocument(this.identifier, identifier);
134         this.identifier = identifier;
135     }
136     
137     /**
138      * Returns the XML collection to which this document belongs. This may be null if
139      * the document is not a stored object.
140      * @return an XMLCollection object, or null if the document is not a stored object.
141      */

142     public XMLCollection getCollection()
143     {
144         return xmlCollection ;
145     }
146     
147     
148     /**
149      * Removes the document from the collection to which it belongs. The object should
150      * not be accessed any more after this method is called.
151      * Note: Depending on the auto-commit mode of the underlying data source connection, the
152      * change may only become definitive after a commit operation.
153      * @throws XMLDBCException if a data source access error occurs.
154      * @throws XMLDBCNotSupportedException if the remove operation is not supported.
155      */

156     public void remove() throws XMLDBCException, XMLDBCNotSupportedException
157     {
158         if (readOnly)
159             throw new XMLDBCNotSupportedException("This document is read-only.");
160         if (xmlCollection != null)
161         {
162             xmlCollection.removeDocument(getIdentifier());
163             xmlCollection = null;
164         }
165         else
166             throw new XMLDBCNotSupportedException("Document is not a stored object");
167     }
168     
169     /**
170      * Sets a (SAX2) content handler to intercept events produced when retrieving
171      * the document content.<BR>
172      * <P>*** ONLY FOR SAX2 ***</P>
173      * @param handler the content handler implementation (see org.xml.sax.ContentHandler)
174      */

175     public void setContentHandler(ContentHandler handler)
176     {
177         this.contentHandler = handler;
178     }
179     
180     /**
181      * Sets a (SAX2) lexical handler to intercept events produced when retrieving
182      * the document content.<BR>
183      * <P>*** ONLY FOR SAX2 ***</P>
184      * @param handler the lexical handler implementation (see org.xml.sax.ext.LexicalHandler)
185      */

186     public void setLexicalHandler(LexicalHandler JavaDoc handler)
187     {
188         this.lexicalHandler = handler ;
189     }
190     
191     /**
192      * Sets a (SAX2) error handler to intercept error events produced when retrieving
193      * the document content.<BR>
194      * <P>*** ONLY FOR SAX2 ***</P>
195      * @param handler the error handler implementation (see org.xml.sax.ErrorHandler)
196      */

197     public void setErrorHandler(ErrorHandler handler)
198     {
199         this.errorHandler = handler ;
200     }
201     
202     /**
203      * Retrieves the current (SAX2) lexical handler.<BR>
204      * <P>*** ONLY FOR SAX2 ***</P>
205      * @return the lexical handler implementation (see org.xml.sax.ext.LexicalHandler)
206      */

207     public LexicalHandler JavaDoc getLexicalHandler()
208     {
209         return lexicalHandler;
210     }
211     
212     /**
213      * Retrieves the current (SAX2) content handler.<BR>
214      * <P>*** ONLY FOR SAX2 ***</P>
215      * @return the content handler implementation (see org.xml.sax.ContentHandler)
216      */

217     public ContentHandler getContentHandler()
218     {
219         return contentHandler;
220     }
221     
222     /**
223      * SRetrieves the current (SAX2) error handler.<BR>
224      * <P>*** ONLY FOR SAX2 ***</P>
225      * @return the error handler implementation (see org.xml.sax.ErrorHandler)
226      */

227     public ErrorHandler getErrorHandler()
228     {
229         return errorHandler;
230     }
231     
232     /**
233      * Returns the document content as a DOM2 Document.
234      * @return the DOM2 Document containing the document data.
235      * @throws XMLDBCException if a data source access error occurs.
236      */

237     public Document JavaDoc getAsDocument() throws XMLDBCException
238     {
239         createDOMBuilder();
240         Document JavaDoc doc = DOMCreator.newDocument();
241         DOMBuilder builder = new DOMBuilder(doc);
242         getDocument(builder, builder);
243         return doc;
244     }
245     
246     public Document JavaDoc getAsDOM() throws XMLDBCException
247     {
248         return getAsDocument();
249     }
250     
251     public void getAsDOM(Element JavaDoc parent) throws XMLDBCException
252     {
253         DOMBuilder builder = new DOMBuilder(parent);
254         getDocument(builder, builder);
255     }
256     
257     /**
258      * Returns the document content as an XML string.
259      * @return the XML string containing the document data.
260      * @throws XMLDBCException if a data source access error occurs.
261      */

262     public String JavaDoc getAsString() throws XMLDBCException
263     {
264         StringWriter JavaDoc strWriter = new StringWriter JavaDoc() ;
265         getAsStream(strWriter);
266         return strWriter.toString() ;
267     }
268     
269     public void getAsStream(Writer JavaDoc out) throws XMLDBCException
270     {
271         XMLSerializer serializer = new XMLSerializer(out);
272         getDocument(serializer, serializer);
273     }
274     
275     ///////////////////////////////////////////////////////////////////////////
276
// PRIVATE UTILITIES
277
///////////////////////////////////////////////////////////////////////////
278
private void getDocument(ContentHandler contentHandler, LexicalHandler JavaDoc lexicalHandler)
279     throws XMLDBCException
280     {
281         // save handlers plugged by user
282
ContentHandler initialContentHandler = getContentHandler();
283         LexicalHandler JavaDoc initialLexicalHandler = getLexicalHandler();
284         
285         setContentHandler(contentHandler);
286         setLexicalHandler(lexicalHandler);
287         try
288         {
289             getAsSAX();
290         }
291         catch (SAXException e)
292         {
293             throw new XMLDBCException(e.getMessage(), e.getException());
294         }
295         finally
296         {
297             // restore handlers plugged by user
298
if (initialContentHandler != null)
299                 setContentHandler(initialContentHandler);
300             if (initialLexicalHandler != null)
301                 setLexicalHandler(initialLexicalHandler);
302         }
303     }
304     
305     protected void plugHandlers(XMLReader reader)
306     throws XMLDBCException
307     {
308         try
309         {
310             if (contentHandler != null)
311                 reader.setContentHandler(contentHandler);
312             if (errorHandler != null)
313                 reader.setErrorHandler(errorHandler);
314             if (lexicalHandler != null)
315                 reader.setProperty(SAXConstants.SAX_LEXICAL_PROPERTY, lexicalHandler);
316         }
317         catch (SAXException e)
318         {
319             throw new XMLDBCException("Could not plug handlers on repository XMLReader.", e);
320         }
321     }
322     
323     private synchronized void createDOMBuilder()
324     {
325         if (DOMCreator == null)
326         {
327             DocumentBuilderFactory JavaDoc DOMFactory = DocumentBuilderFactory.newInstance();
328             DOMFactory.setNamespaceAware(true);
329             try
330             {
331                 DOMCreator = DOMFactory.newDocumentBuilder();
332             }
333             catch (ParserConfigurationException JavaDoc pce)
334             {
335                 throw new RuntimeException JavaDoc("Could not create an empty DOM level 2 document using JAXP. Check a JAXP DOM implementation is in your classpath.") ;
336             }
337         }
338     }
339 }
340
Popular Tags