KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > generation > XMLDBGenerator


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.generation;
17
18 import org.apache.avalon.framework.activity.Initializable;
19 import org.apache.avalon.framework.configuration.Configurable;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.parameters.Parameters;
23
24 import org.apache.cocoon.ProcessingException;
25 import org.apache.cocoon.ResourceNotFoundException;
26 import org.apache.cocoon.caching.CacheableProcessingComponent;
27 import org.apache.cocoon.environment.SourceResolver;
28 import org.apache.cocoon.util.Deprecation;
29
30 import org.apache.excalibur.source.SourceValidity;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xmldb.api.DatabaseManager;
33 import org.xmldb.api.base.Collection;
34 import org.xmldb.api.base.Database;
35 import org.xmldb.api.base.XMLDBException;
36 import org.xmldb.api.modules.XMLResource;
37
38 import java.io.IOException JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * This class implements generation of XML documents from a
43  * XML:DB compliant database.
44  * It must to be configured as follows:
45  * <pre>
46  * &lt;driver&gt;
47  * (a valid DB:XML compliant driver)
48  * &lt;/driver&gt;
49  * &lt;base&gt;
50  * xmldb:yourdriver://host/an/optional/path/to/be/prepended
51  * &lt;/base&gt;
52  * </pre>
53  *
54  * NOTE: the driver can be any DB:XML compliant driver (although this
55  * component has been tested only with
56  * <a HREF="http://www.dbxml.org">dbXML</a>, and the trailing
57  * slash in the base tag is important!
58  *
59  * @author <a HREF="mailto:gianugo@rabellino.it">Gianugo Rabellino</a>
60  * @version CVS $Id: XMLDBGenerator.java 157288 2005-03-12 22:42:07Z sylvain $
61  * @deprecated Use the XML:DB pseudo protocol instead.
62  */

63 public class XMLDBGenerator extends ServiceableGenerator
64         implements CacheableProcessingComponent, Configurable,Initializable {
65
66     protected String JavaDoc driver;
67     protected String JavaDoc base;
68     protected String JavaDoc col;
69     protected String JavaDoc res;
70     protected Database database;
71     protected Collection collection;
72     protected XMLResource xmlResource;
73
74     /**
75      * Recycle the component, keep only the configuration variables
76      * and the database instance for reuse.
77      */

78     public void recycle() {
79         super.recycle();
80         this.col = null;
81         this.res = null;
82         this.xmlResource = null;
83         this.collection = null;
84     }
85
86     /**
87      * Configure the component. This class is expecting a configuration
88      * like the following one:
89      * <pre>
90      * &lt;driver&gt;org.dbxml.client.xmldb.DatabaseImpl&lt;/driver&gt;
91      * &lt;base&gt;xmldb:dbxml:///db/&lt;/base&gt;
92      * </pre>
93      * NOTE: the driver can be any DB:XML compliant driver (although this
94      * component has been tested only with
95      * <a HREF="http://www.dbxml.org">dbXML</a>, and the trailing
96      * slash in the base tag is important!
97      *
98      * @exception ConfigurationException (configuration invalid or missing)
99      */

100     public void configure(Configuration conf) throws ConfigurationException {
101         this.driver = conf.getChild("driver").getValue();
102         this.base = conf.getChild("base").getValue();
103     }
104
105     /**
106      * Initialize the component getting a database instance.
107      *
108      * @exception Exception if an error occurs
109      */

110     public void initialize() throws Exception JavaDoc {
111         try {
112             Class JavaDoc c = Class.forName(driver);
113             database = (Database)c.newInstance();
114             DatabaseManager.registerDatabase(database);
115         } catch (XMLDBException xde) {
116             getLogger().error("Unable to connect to the XML:DB database");
117             throw new ProcessingException("Unable to connect to the XMLDB database: "
118                                           + xde.getMessage());
119         } catch (Exception JavaDoc e) {
120             getLogger().error("There was a problem setting up the connection");
121             getLogger().error("Make sure that your driver is available");
122             throw new ProcessingException("Problem setting up the connection: "
123                                           + e.getMessage());
124         }
125     }
126
127     public void setup(SourceResolver resolver,
128                       Map JavaDoc objectModel,
129                       String JavaDoc src,
130                       Parameters par)
131     throws ProcessingException, SAXException JavaDoc,IOException JavaDoc {
132         Deprecation.logger.warn("The XMLDBGenerator is deprecated. Use the XML:DB pseudo protocol instead");
133         super.setup(resolver, objectModel, src, par);
134     }
135
136     /**
137      * The component isn't cached (yet)
138      */

139     public SourceValidity getValidity() {
140         return null;
141     }
142
143     /**
144      * The component isn't cached (yet)
145      */

146     public java.io.Serializable JavaDoc getKey() {
147         return null;
148     }
149
150     /**
151      * Parse the requested URI, connect to the XML:DB database
152      * and fetch the requested resource.
153      *
154      * @exception ProcessingException something unexpected happened with the DB
155      */

156     public void generate()
157             throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
158         String JavaDoc col = "/";
159
160         if (source.indexOf('/') != -1)
161             col = "/" + source.substring(0, source.lastIndexOf('/'));
162         res = source.substring(source.lastIndexOf('/') + 1);
163
164         try {
165             collection = DatabaseManager.getCollection(base + col);
166             xmlResource = (XMLResource) collection.getResource(res);
167             if (xmlResource == null) {
168                 throw new ResourceNotFoundException("Document " + col + "/" + res +
169                                                     " not found");
170             }
171
172             xmlResource.getContentAsSAX(this.xmlConsumer);
173             collection.close();
174         } catch (XMLDBException xde) {
175             throw new ProcessingException("Unable to fetch content: " +
176                                           xde.getMessage());
177         } catch (NullPointerException JavaDoc npe) {
178             getLogger().error("The XML:DB driver raised an exception");
179             getLogger().error("probably the document was not found");
180             throw new ProcessingException("Null pointer exception while " +
181                                           "retrieving document : " + npe.getMessage());
182         }
183     }
184 }
185
Popular Tags