1 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 ; 32 import org.xml.sax.helpers.AttributesImpl ; 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 ; 39 import java.util.Map ; 40 41 63 public class XMLDBCollectionGenerator extends ServiceableGenerator 64 implements CacheableProcessingComponent, Configurable, Initializable { 65 66 protected static final String URI = 67 "http://apache.org/cocoon/xmldb/1.0"; 68 protected static final String PREFIX = "collection"; 69 protected static final String RESOURCE_COUNT_ATTR = "resources"; 70 protected static final String COLLECTION_COUNT_ATTR = "collections"; 71 protected static final String COLLECTION = "collection"; 72 protected static final String QCOLLECTION = PREFIX + ":collection"; 73 protected static final String RESOURCE = "resource"; 74 protected static final String QRESOURCE = PREFIX + ":resource"; 75 76 protected String driver; 77 protected String base; 78 protected String col; 79 protected Database database; 80 protected Collection collection; 81 protected final AttributesImpl attributes = new AttributesImpl (); 82 83 87 public void recycle() { 88 super.recycle(); 89 this.col = null; 90 this.collection = null; 91 } 92 93 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 117 public void initialize() throws Exception { 118 try { 119 Class 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 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 objectModel, 136 String src, 137 Parameters par) 138 throws ProcessingException, SAXException ,IOException { 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 146 public SourceValidity getValidity() { 147 return null; 148 } 149 150 153 public java.io.Serializable getKey() { 154 return null; 155 } 156 157 163 public void generate() 164 throws IOException , SAXException , ProcessingException { 165 167 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 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 195 public void collectionToSAX(Collection collection) 196 throws SAXException { 197 198 String ncollections; 199 String nresources; 200 String [] resources; 201 String [] 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 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 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 ("Collection listing failed: " + xde.getMessage()); 250 } 251 } 252 } 253 | Popular Tags |