1 17 18 package org.apache.geronimo.deployment.xml; 19 20 import javax.xml.parsers.DocumentBuilder ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import javax.xml.parsers.ParserConfigurationException ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.geronimo.gbean.GBeanInfo; 27 import org.apache.geronimo.gbean.GBeanInfoBuilder; 28 import org.apache.geronimo.kernel.util.XmlUtil; 29 import org.xml.sax.EntityResolver ; 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.SAXParseException ; 32 33 39 public class ParserFactoryImpl implements ParserFactory { 40 41 private static final Log log = LogFactory.getLog(ParserFactoryImpl.class); 42 43 private final DocumentBuilderFactory factory; 44 private EntityResolver entityResolver; 45 46 public ParserFactoryImpl(EntityResolver entityResolver) { 47 this.entityResolver = entityResolver; 48 factory = XmlUtil.newDocumentBuilderFactory(); 49 factory.setNamespaceAware(true); 51 factory.setValidating(true); 53 factory.setAttribute( 54 "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 55 "http://www.w3.org/2001/XMLSchema"); 56 factory.setAttribute("http://apache.org/xml/features/validation/schema", 57 Boolean.TRUE); 58 } 59 60 public DocumentBuilder getParser() 61 throws ParserConfigurationException { 62 DocumentBuilder builder = factory.newDocumentBuilder(); 63 builder.setEntityResolver(entityResolver); 64 builder.setErrorHandler(new ErrorHandler () { 65 public void error(SAXParseException exception) { 66 log.warn("SAX parse error (ignored)", exception); 67 } 69 70 public void fatalError(SAXParseException exception) { 71 log.warn("Fatal SAX parse error (ignored)", exception); 72 } 74 75 public void warning(SAXParseException exception) { 76 log.warn("SAX parse warning", exception); 77 } 78 }); 79 return builder; 80 } 81 82 public EntityResolver getEntityResolver() { 83 return entityResolver; 84 } 85 86 public void setEntityResolver(EntityResolver entityResolver) { 87 this.entityResolver = entityResolver; 88 } 89 90 public final static GBeanInfo GBEAN_INFO; 91 92 static { 93 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Factory for constructing suitable configured xml parsers", ParserFactoryImpl.class); 94 95 infoFactory.addOperation("getParser"); 96 97 infoFactory.addReference("EntityResolver", EntityResolver .class, "GBean"); 98 99 infoFactory.setConstructor(new String []{"EntityResolver"}); 100 101 GBEAN_INFO = infoFactory.getBeanInfo(); 102 } 103 104 public static GBeanInfo getGBeanInfo() { 105 return GBEAN_INFO; 106 } 107 } 108 | Popular Tags |