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.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 ; 39 import java.util.Map ; 40 41 63 public class XMLDBGenerator extends ServiceableGenerator 64 implements CacheableProcessingComponent, Configurable,Initializable { 65 66 protected String driver; 67 protected String base; 68 protected String col; 69 protected String res; 70 protected Database database; 71 protected Collection collection; 72 protected XMLResource xmlResource; 73 74 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 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 110 public void initialize() throws Exception { 111 try { 112 Class 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 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 objectModel, 129 String src, 130 Parameters par) 131 throws ProcessingException, SAXException ,IOException { 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 139 public SourceValidity getValidity() { 140 return null; 141 } 142 143 146 public java.io.Serializable getKey() { 147 return null; 148 } 149 150 156 public void generate() 157 throws IOException , SAXException , ProcessingException { 158 String 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 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 |