KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.sax.helpers.AttributesImpl JavaDoc;
33 import org.xmldb.api.DatabaseManager;
34 import org.xmldb.api.base.Collection;
35 import org.xmldb.api.base.Database;
36 import org.xmldb.api.base.XMLDBException;
37
38 import java.io.IOException JavaDoc;
39 import java.util.Map JavaDoc;
40
41 /**
42  * This class implements generation of a XML:DB collection
43  * contents as a directory listing.
44  *
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: XMLDBCollectionGenerator.java 157288 2005-03-12 22:42:07Z sylvain $
61  * @deprecated Use the XML:DB pseudo protocol instead.
62  */

63 public class XMLDBCollectionGenerator extends ServiceableGenerator
64         implements CacheableProcessingComponent, Configurable, Initializable {
65
66     protected static final String JavaDoc URI =
67             "http://apache.org/cocoon/xmldb/1.0";
68     protected static final String JavaDoc PREFIX = "collection";
69     protected static final String JavaDoc RESOURCE_COUNT_ATTR = "resources";
70     protected static final String JavaDoc COLLECTION_COUNT_ATTR = "collections";
71     protected static final String JavaDoc COLLECTION = "collection";
72     protected static final String JavaDoc QCOLLECTION = PREFIX + ":collection";
73     protected static final String JavaDoc RESOURCE = "resource";
74     protected static final String JavaDoc QRESOURCE = PREFIX + ":resource";
75
76     protected String JavaDoc driver;
77     protected String JavaDoc base;
78     protected String JavaDoc col;
79     protected Database database;
80     protected Collection collection;
81     protected final AttributesImpl JavaDoc attributes = new AttributesImpl JavaDoc();
82
83     /**
84      * Recycle the component, keep only the configuration variables
85      * and the database instance for reuse.
86      */

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

107    public void configure(Configuration conf) throws ConfigurationException {
108        this.driver = conf.getChild("driver").getValue();
109        this.base = conf.getChild("base").getValue();
110    }
111
112    /**
113     * Initialize the component getting a database instance.
114     *
115     * @exception Exception if an error occurs
116     */

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

146     public SourceValidity getValidity() {
147         return null;
148     }
149
150     /**
151      * The component isn't cached (yet)
152      */

153     public java.io.Serializable JavaDoc getKey() {
154         return null;
155     }
156
157     /**
158      * Parse the requested URI, connect to the XML:DB database
159      * and fetch the requested resource.
160      *
161      * @exception ProcessingException something unexpected happened with the DB
162      */

163     public void generate()
164             throws IOException JavaDoc, SAXException JavaDoc, ProcessingException {
165         //String col = "/";
166

167         //if (source.indexOf('/') != -1)
168
col = source;
169
170         try {
171             collection = DatabaseManager.getCollection(base + col);
172             if (collection == null) {
173                 throw new ResourceNotFoundException("Collection " + col +
174                                                     " not found");
175             }
176
177             collectionToSAX(collection);
178             collection.close();
179         } catch (XMLDBException xde) {
180             throw new ProcessingException("Unable to fetch content '"
181                                           + source + "':" + xde.getMessage());
182         } catch (NullPointerException JavaDoc npe) {
183             getLogger().error("The XML:DB driver raised an exception");
184             getLogger().error("probably the document was not found");
185             throw new ProcessingException("Null pointer exception while " +
186                                           "retrieving document : " + npe.getMessage());
187         }
188     }
189
190     /**
191      * Output SAX events listing the collection.
192      *
193      * @exception SAXException
194      */

195     public void collectionToSAX(Collection collection)
196             throws SAXException JavaDoc {
197
198         String JavaDoc ncollections;
199         String JavaDoc nresources;
200         String JavaDoc[] resources;
201         String JavaDoc[] collections;
202
203         try {
204             ncollections = Integer.toString(collection.getChildCollectionCount());
205             nresources = Integer.toString(collection.getResourceCount());
206
207             attributes.clear();
208             attributes.addAttribute("", RESOURCE_COUNT_ATTR,
209                                     RESOURCE_COUNT_ATTR, "CDATA", nresources);
210             attributes.addAttribute("", COLLECTION_COUNT_ATTR,
211                                     COLLECTION_COUNT_ATTR, "CDATA", ncollections);
212
213             collections = collection.listChildCollections();
214             resources = collection.listResources();
215
216             this.xmlConsumer.startDocument();
217             this.xmlConsumer.startPrefixMapping(PREFIX, URI);
218
219             this.xmlConsumer.startElement(URI, "collections",
220                                           "collection:collections", attributes);
221
222             // Print child collections
223

224             for (int i = 0; i < collections.length; i++) {
225                 attributes.clear();
226                 attributes.addAttribute("", "name", "name", "CDATA", collections[i]);
227                 this.xmlConsumer.startElement(URI, COLLECTION,
228                                               QCOLLECTION, attributes);
229                 this.xmlConsumer.endElement(URI, COLLECTION, COLLECTION);
230             }
231
232             // Print child resources
233

234             for (int i = 0; i < resources.length; i++) {
235                 attributes.clear();
236                 attributes.addAttribute("", "name", "name", "CDATA", resources[i]);
237                 this.xmlConsumer.startElement(URI, RESOURCE,
238                                               QRESOURCE, attributes);
239                 this.xmlConsumer.endElement(URI, RESOURCE, RESOURCE);
240             }
241
242             this.xmlConsumer.endElement(URI, "collections",
243                                         "collection:collections");
244
245             this.xmlConsumer.endPrefixMapping(PREFIX);
246             this.xmlConsumer.endDocument();
247         } catch (XMLDBException xde) {
248             getLogger().warn("Collection listing failed: " + xde.getMessage());
249             throw new SAXException JavaDoc("Collection listing failed: " + xde.getMessage());
250         }
251     }
252 }
253
Popular Tags