1 16 package org.apache.cocoon.components.parser; 17 18 import java.io.IOException ; 19 20 import javax.xml.parsers.DocumentBuilder ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import javax.xml.parsers.ParserConfigurationException ; 23 import javax.xml.parsers.SAXParserFactory ; 24 25 import org.apache.avalon.excalibur.pool.Poolable; 26 import org.apache.avalon.framework.activity.Disposable; 27 import org.apache.avalon.framework.component.ComponentException; 28 import org.apache.avalon.framework.component.ComponentManager; 29 import org.apache.avalon.framework.component.Composable; 30 import org.apache.avalon.framework.parameters.ParameterException; 31 import org.apache.avalon.framework.parameters.Parameterizable; 32 import org.apache.avalon.framework.parameters.Parameters; 33 import org.apache.cocoon.components.resolver.Resolver; 34 import org.apache.cocoon.util.ClassUtils; 35 import org.apache.cocoon.xml.AbstractXMLProducer; 36 import org.w3c.dom.DOMImplementation ; 37 import org.w3c.dom.Document ; 38 import org.xml.sax.ErrorHandler ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.SAXParseException ; 42 import org.xml.sax.XMLReader ; 43 44 83 public class JaxpParser extends AbstractXMLProducer 84 implements Parser, ErrorHandler , Composable, Parameterizable, Disposable, Poolable { 85 86 87 protected SAXParserFactory factory; 88 89 90 protected DocumentBuilderFactory docFactory; 91 92 94 protected XMLReader reader; 95 96 98 protected DocumentBuilder docBuilder; 99 100 101 protected ComponentManager manager; 102 103 104 protected Resolver resolver; 105 106 107 protected boolean nsPrefixes; 108 109 110 protected boolean reuseParsers; 111 112 115 public void compose(ComponentManager manager) 116 throws ComponentException { 117 this.manager = manager; 118 if ( manager.hasComponent( Resolver.ROLE ) ) { 119 if (getLogger().isDebugEnabled()) { 120 getLogger().debug("Looking up " + Resolver.ROLE); 121 } 122 this.resolver = (Resolver)manager.lookup(Resolver.ROLE); 123 } 124 } 125 126 129 public void dispose() { 130 if (this.manager != null) { 131 this.manager.release( this.resolver ); 132 } 133 } 134 135 138 public void parameterize(Parameters params) 139 throws ParameterException { 140 boolean validate = params.getParameterAsBoolean("validate", false); 142 this.nsPrefixes = params.getParameterAsBoolean("namespace-prefixes", false); 143 this.reuseParsers = params.getParameterAsBoolean("reuse-parsers", true); 144 145 String className = params.getParameter("sax-parser-factory", null); 147 if (className == null) { 148 factory = SAXParserFactory.newInstance(); 149 } else { 150 try { 152 Class factoryClass = ClassUtils.loadClass(className); 153 factory = (SAXParserFactory )factoryClass.newInstance(); 154 } catch(Exception e) { 155 throw new ParameterException("Cannot load SAXParserFactory class " + className, e); 156 } 157 } 158 factory.setNamespaceAware(true); 159 factory.setValidating(validate); 160 161 className = params.getParameter("document-builder-factory", null); 163 if (className == null) { 164 this.docFactory = DocumentBuilderFactory.newInstance(); 165 } else { 166 try { 168 Class factoryClass = ClassUtils.loadClass(className); 169 this.docFactory = (DocumentBuilderFactory )factoryClass.newInstance(); 170 } catch(Exception e) { 171 throw new ParameterException("Cannot load DocumentBuilderFactory class " + className, e); 172 } 173 } 174 175 docFactory.setNamespaceAware(true); 176 docFactory.setValidating(validate); 177 } 178 179 public void parse(InputSource in) 180 throws SAXException , IOException { 181 setupXMLReader(); 182 try { 183 this.reader.setProperty("http://xml.org/sax/properties/lexical-handler", super.lexicalHandler); 184 } catch (SAXException e) { 185 getLogger().warn("SAX2 driver does not support property: "+ 186 "'http://xml.org/sax/properties/lexical-handler'"); 187 } 188 189 this.reader.setErrorHandler(this); 190 this.reader.setContentHandler(super.contentHandler); 191 if(this.resolver != null) { 192 reader.setEntityResolver(this.resolver); 193 } 194 195 XMLReader tmpReader = this.reader; 197 this.reader = null; 198 199 tmpReader.parse(in); 200 201 if (this.reuseParsers) 203 this.reader = tmpReader; 204 } 205 206 209 public Document newDocument() { 210 setupDocumentBuilder(); 211 return this.docBuilder.newDocument(); 212 } 213 214 217 public Document newDocument(String name) { 218 return this.newDocument(name, null, null); 219 } 220 221 225 public Document newDocument(String name, String publicId, String systemId) { 226 setupDocumentBuilder(); 227 DOMImplementation impl = this.docBuilder.newDocument().getImplementation(); 229 return impl.createDocument( 230 null, 231 name, 232 impl.createDocumentType(name, publicId, systemId) 233 ); 234 } 235 236 239 public Document parseDocument(InputSource input) throws SAXException , IOException { 240 setupDocumentBuilder(); 241 242 DocumentBuilder tmpBuilder = this.docBuilder; 244 this.docBuilder = null; 245 246 Document result = tmpBuilder.parse(input); 247 248 if (this.reuseParsers) 250 this.docBuilder = tmpBuilder; 251 252 return result; 253 } 254 255 258 protected void setupXMLReader() throws SAXException { 259 if (this.reader == null) { 260 try { 262 this.reader = factory.newSAXParser().getXMLReader(); 263 this.reader.setFeature("http://xml.org/sax/features/namespace-prefixes", nsPrefixes); 264 } catch(Exception e) { 265 getLogger().error("Cannot produce a valid parser", e); 266 throw new SAXException ("Cannot produce a valid parser", e); 267 } 268 } 269 } 270 271 274 protected void setupDocumentBuilder() { 275 if (this.docBuilder == null) { 276 try { 277 this.docBuilder = this.docFactory.newDocumentBuilder(); 278 } catch (ParserConfigurationException pce) { 279 getLogger().error("Could not create DocumentBuilder", pce); 280 throw new org.apache.avalon.framework.CascadingRuntimeException( 281 "Could not create DocumentBuilder", pce); 282 } 283 } 284 } 285 286 289 public void error(SAXParseException e) 290 throws SAXException { 291 throw new SAXException ("Error parsing "+e.getSystemId()+" (line "+ 292 e.getLineNumber()+" col. "+e.getColumnNumber()+ 293 "): "+e.getMessage(),e); 294 } 295 296 299 public void fatalError(SAXParseException e) 300 throws SAXException { 301 throw new SAXException ("Fatal error parsing "+e.getSystemId()+" (line "+ 302 e.getLineNumber()+" col. "+e.getColumnNumber()+ 303 "): "+e.getMessage(),e); 304 } 305 306 309 public void warning(SAXParseException e) 310 throws SAXException { 311 throw new SAXException ("Warning parsing "+e.getSystemId()+" (line "+ 312 e.getLineNumber()+" col. "+e.getColumnNumber()+ 313 "): "+e.getMessage(),e); 314 } 315 } 316 | Popular Tags |