KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > mapper > storage > DocumentProducer


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 /*
24  * DocumentGenerator.java
25  *
26  * Created on 12 mars 2001, 18:01
27  */

28 package org.xquark.mapper.storage;
29
30 import java.io.IOException JavaDoc;
31 import java.sql.SQLException JavaDoc;
32
33 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
34 import javax.xml.parsers.ParserConfigurationException JavaDoc;
35
36 import org.w3c.dom.Document JavaDoc;
37 import org.xml.sax.*;
38 import org.xml.sax.ext.LexicalHandler JavaDoc;
39 import org.xquark.mapper.RepositoryConnection;
40 import org.xquark.mapper.RepositoryException;
41 import org.xquark.mapper.metadata.PathNode;
42 import org.xquark.mapper.metadata.PathSet;
43 import org.xquark.util.DOMBuilder;
44 import org.xquark.xml.xdbc.XMLDBCException;
45
46 /** This object is used to generate an XML document stored in a repository
47  * with an XML schema using the SAX2 interface.
48  *
49  * <p>Generation is guided by the schema and indecisions are removed by
50  * document instance information (stored in the structure table).</p>
51  * @see org.xquark.mapper.XMLCollection
52  */

53 public class DocumentProducer extends BaseXMLReaderImpl
54 implements Locator
55 {
56     private static final String JavaDoc RCSRevision = "$Revision: 1.2 $";
57     private static final String JavaDoc RCSName = "$Name: $";
58     
59     public static DocumentBuilderFactory JavaDoc DOMFactory;
60     static
61     {
62         DOMFactory = DocumentBuilderFactory.newInstance();
63         DOMFactory.setNamespaceAware(true);
64     }
65     
66     private final static String JavaDoc DEFAULT_PREFIX = "ns";
67
68     private DocumentExplorer docTuplePool;
69     
70     private PathSet pathSet;
71     private String JavaDoc Id;
72     
73     /** Creates new DocumentGenerator */
74     public DocumentProducer(_RepositoryCollection collection) throws XMLDBCException
75     {
76         super(collection);
77         try
78         {
79             /* Preparing database queries */
80             tuplePool = docTuplePool = new DocumentExplorer(collection);
81         }
82         catch (SQLException JavaDoc e)
83         {
84             throw new RepositoryException(RepositoryException.DB_ERROR,
85                                     "JDBC error while initializing reader.");
86         }
87         generateDBPrefix = !collection.getFeature(RepositoryConnection.USE_SCHEMA_PREFIXES_FEATURE);
88         prefixProvider = new PrefixProvider();
89         pathSet = collection.getMetadata().getPathSet();
90         try {
91             docTuplePool.initDocReading(collection.getRepositoryConnection().getConnection());
92         }
93         catch (SQLException JavaDoc e) {
94             throw new RepositoryException(RepositoryException.DB_ERROR,
95             "JDBC error while initializing prepared statements", e);
96         }
97     }
98     
99     ////////////////////////////////////////////////////////////////////////////////
100
// XMLReader IMPLEMENTATION
101
////////////////////////////////////////////////////////////////////////////////
102

103     /**
104      * Parse an XML document from a system identifier (URI).
105      *
106      * @param systemId The system identifier (URI).
107      * @exception org.xml.sax.SAXException Any SAX exception, possibly
108      * wrapping another exception.
109      * @exception java.io.IOException An IO exception from the parser,
110      * possibly from a byte stream or character stream
111      * supplied by the application.
112      * @see #parse(org.xml.sax.InputSource)
113      */

114     public void parse(String JavaDoc systemId)
115     throws IOException JavaDoc, SAXException
116     {
117         if (systemId == null)
118             throw new IOException JavaDoc("A string ID must be provided in order to retrieve the document.");
119         
120         // remove useless spaces
121
Id = systemId.trim();
122         try
123         {
124             try
125             {
126                 docTuplePool.initDocumentExploration(Id);
127                 
128                 // initiate row lookup
129
if (!docTuplePool.lookup())
130                     throw new RepositoryException(RepositoryException.NOT_EXISTS,
131                     "Document with ID " + Id + " not found in this collection");
132                 
133                 contentHandler.setDocumentLocator(this);
134                 
135                 ////////////////////////
136
// SAX startDocument
137
////////////////////////
138
contentHandler.startDocument();
139                 
140                 browseBranch
141                         (
142                         0,
143                         Long.MAX_VALUE,
144                         -1,
145                         -1,
146                         pathSet.createIterator
147                                 (
148                                 (PathNode)pathSet.getRoot()
149                                 ),
150                         0,
151                         false
152                         );
153                 
154                 ////////////////////////
155
// SAX endDocument
156
////////////////////////
157
contentHandler.endDocument();
158                 reset();
159             }
160             catch(SQLException JavaDoc e)
161             {
162                 throw new SAXException("JDBC error raised while generating SAX events.", e);
163             }
164             catch(XMLDBCException e)
165             {
166                 throw new SAXException("Repository error raised while generating SAX events.", e);
167             }
168         }
169         catch (SAXException e)
170         {
171             if (getErrorHandler() != null)
172                 getErrorHandler().fatalError(
173                 new SAXParseException(e.getMessage(), this, e.getException()));
174             try {
175                 reset();
176             }
177             catch (SQLException JavaDoc ex) {
178             }catch (RepositoryException ex) {
179                 // no op
180
}
181             throw e;
182         }
183     }
184     
185     /** Builds a DOM Level 2 document from an XML document stored in the
186      * repository base on an empty document passed as a paramater.
187      * @param ID The name used to identify the document in the repository
188      * that can be, for instance, the document URL.
189      * @return a DOM Level 2 document.
190      * @throws RepositoryException Application exception.
191      */

192     public Document JavaDoc getDocument(String JavaDoc ID) throws RepositoryException
193     {
194         Document JavaDoc document = null;
195         try {
196             document = DOMFactory.newDocumentBuilder().newDocument();
197         }
198         catch (ParserConfigurationException JavaDoc e) {
199             throw new RepositoryException(RepositoryException.PARSER_ERROR, "Could not create an empty DOM level 2 document using JAXP. Check a JAXP DOM implementation is in your classpath.");
200         }
201         
202         // SAX to DOM converter instantiation
203
DOMBuilder builder = new DOMBuilder(document);
204         
205         // save handlers plugged by user
206
ContentHandler initialContentHandler = getContentHandler();
207         LexicalHandler JavaDoc initialLexicalHandler;
208          try {
209             initialLexicalHandler = (LexicalHandler JavaDoc)getProperty(SAX_LEXICAL_PROPERTY);
210         }
211         catch (SAXException e) {
212             throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Couldn't access the lexical handler in XMLReaderImpl.",e);
213         }
214         
215         setContentHandler(builder);
216         try {
217             setProperty(SAX_LEXICAL_PROPERTY, builder);
218         }
219         catch (SAXException e) {
220             throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Couldn't access the lexical handler in XMLReaderImpl.",e);
221         }
222         
223         try {
224             parse(ID);
225         }
226         catch (IOException JavaDoc e) {
227             throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Error while processing internal SAX parsing.",e);
228         }
229         catch (SAXException e) {
230             throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Error while processing internal SAX parsing.",e);
231         }
232         
233         // restore handlers plugged by user
234
setContentHandler(initialContentHandler);
235         try {
236             setProperty(SAX_LEXICAL_PROPERTY, initialLexicalHandler);
237         }
238         catch (SAXException e) {
239             throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Couldn't access the lexical handler in XMLReaderImpl.",e);
240         }
241         
242         return document;
243     }
244     
245     public void reset() throws SQLException JavaDoc, RepositoryException
246     {
247         Id = null;
248         super.reset();
249     }
250     
251     public java.lang.String JavaDoc getSystemId(){return Id;}
252     public java.lang.String JavaDoc getPublicId(){return null;}
253     public int getLineNumber() {return -1;}
254     public int getColumnNumber() {return -1;}
255     
256 }
257
Popular Tags