1 17 package org.apache.forrest.xni; 18 19 import org.apache.cocoon.generation.ServiceableGenerator; 20 import org.apache.cocoon.ProcessingException; 21 import org.apache.cocoon.ResourceNotFoundException; 22 import org.apache.cocoon.components.source.SourceUtil; 23 import org.apache.cocoon.environment.SourceResolver; 24 import org.apache.cocoon.caching.CacheableProcessingComponent; 25 import org.apache.avalon.excalibur.pool.Recyclable; 26 import org.apache.excalibur.xml.EntityResolver; 27 import org.apache.avalon.framework.parameters.Parameters; 28 import org.apache.avalon.framework.parameters.ParameterException; 29 import org.apache.xerces.xni.parser.XMLParserConfiguration; 30 import org.apache.xerces.xni.parser.XMLConfigurationException; 31 import org.apache.xerces.util.EntityResolverWrapper; 32 import org.apache.xerces.parsers.AbstractSAXParser; 33 import org.apache.excalibur.source.*; 34 35 import org.xml.sax.*; 36 37 import java.util.Map ; 38 import java.io.IOException ; 39 40 41 64 public class XNIConfigurableFileGenerator 65 extends ServiceableGenerator implements CacheableProcessingComponent, Recyclable 66 { 67 68 71 public XNIConfigurableFileGenerator() 72 { 73 } 74 75 public static final String CONFIGCLASS_PARAMETER = "config-class"; 76 public static final String FULL_ENTITY_RESOLVER_PROPERTY_URI = 77 org.apache.xerces.impl.Constants.XERCES_PROPERTY_PREFIX + 78 org.apache.xerces.impl.Constants.ENTITY_RESOLVER_PROPERTY; 79 80 private Source inputSource; 81 82 83 XMLParserConfiguration parserConfig; 84 85 89 public void recycle() { 90 if (this.inputSource != null) { 91 this.resolver.release(inputSource); 92 this.inputSource = null; 93 } 94 super.recycle(); 95 } 96 97 100 public void setup(SourceResolver resolver, Map objectModel, String src, Parameters par) 101 throws ProcessingException, SAXException, IOException { 102 super.setup(resolver, objectModel, src, par); 103 String parserName = null; 104 105 try { 106 this.inputSource = resolver.resolveURI(super.source); 107 parserName = par.getParameter(CONFIGCLASS_PARAMETER); 108 parserConfig = (XMLParserConfiguration)Class.forName(parserName).newInstance(); 109 } catch(ParameterException e) { 110 getLogger().error("Missing parameter " + CONFIGCLASS_PARAMETER, e); 111 throw new ProcessingException("XNIConfigurable.setup()",e); 112 } catch(InstantiationException e) { 113 getLogger().error("Can not make instance of " + parserName, e); 114 throw new ProcessingException("XNIConfigurable.setup()",e); 115 } catch(IllegalAccessException e) { 116 getLogger().error("Can not access constructor of " + parserName, e); 117 throw new ProcessingException("XNIConfigurable.setup()",e); 118 } catch(ClassNotFoundException e) { 119 getLogger().error("Can not find " + parserName, e); 120 throw new ProcessingException("XNIConfigurable.setup()",e); 121 } catch (SourceException e) { 122 getLogger().error("Can not resolve " + super.source); 123 throw SourceUtil.handle("Unable to resolve " + super.source, e); 124 } 125 } 126 127 135 public java.io.Serializable getKey() { 136 return this.inputSource.getURI(); 137 } 138 139 public java.io.Serializable generateKey() { 141 return getKey(); 142 } 143 144 152 public SourceValidity getValidity() { 153 if (this.inputSource.getLastModified() != 0) { 154 this.inputSource.getValidity(); 155 } 156 return null; 157 } 158 159 public SourceValidity generateValidity() { 161 return getValidity(); 162 } 163 164 167 public void generate() 168 throws IOException , SAXException, ProcessingException { 169 EntityResolver catalogResolver = null; 170 final String [] extendRecognizedProperties = {FULL_ENTITY_RESOLVER_PROPERTY_URI}; 171 try { 172 getLogger().debug("XNIConfigurable generator start generate()"); 173 174 catalogResolver = (EntityResolver)this.manager.lookup(EntityResolver.ROLE); 182 parserConfig.addRecognizedProperties(extendRecognizedProperties); 183 parserConfig.setProperty(FULL_ENTITY_RESOLVER_PROPERTY_URI, new EntityResolverWrapper(catalogResolver)); 184 final XMLReader parser = new AbstractSAXParser(parserConfig){}; 185 parser.setFeature("http://xml.org/sax/features/namespaces", true); 186 parser.setFeature("http://xml.org/sax/features/namespace-prefixes", true); 187 parser.setContentHandler(this.contentHandler); 188 parser.parse(new InputSource(this.inputSource.getInputStream())); 189 190 } catch (IOException e){ 191 getLogger().warn("XNIConfigurable.generate()", e); 192 throw new ResourceNotFoundException("Could not get resource to process:\n[" 193 + "src = " + this.inputSource.getURI() + "]\n", e); 194 } catch (SAXException e){ 195 getLogger().error("XNIConfigurable.generate()", e); 196 throw e; 197 } catch (XMLConfigurationException e) { 198 getLogger().error( "Misconfig " + e.getType(), e); 199 throw new ProcessingException("XNIConfigurable.generate()",e); 200 } catch (Exception e){ 201 getLogger().error("Some strange thing just happened!!", e); 202 throw new ProcessingException("XNIConfigurable.generate()",e); 203 } finally { 204 this.manager.release(catalogResolver); 205 } 206 } 207 } 208 | Popular Tags |