001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.deployment.xml;
019    
020    import javax.xml.parsers.DocumentBuilder;
021    import javax.xml.parsers.DocumentBuilderFactory;
022    import javax.xml.parsers.ParserConfigurationException;
023    
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    import org.apache.geronimo.gbean.GBeanInfo;
027    import org.apache.geronimo.gbean.GBeanInfoBuilder;
028    import org.apache.geronimo.kernel.util.XmlUtil;
029    import org.xml.sax.EntityResolver;
030    import org.xml.sax.ErrorHandler;
031    import org.xml.sax.SAXParseException;
032    
033    /**
034     *
035     *
036     * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 2006) $
037     *
038     * */
039    public class ParserFactoryImpl implements ParserFactory {
040    
041        private static final Log log = LogFactory.getLog(ParserFactoryImpl.class);
042    
043        private final DocumentBuilderFactory factory;
044        private EntityResolver entityResolver;
045    
046        public ParserFactoryImpl(EntityResolver entityResolver) {
047            this.entityResolver = entityResolver;
048            factory = XmlUtil.newDocumentBuilderFactory();
049            //sets "http://xml.org/sax/features/namespaces"
050            factory.setNamespaceAware(true);
051            //sets "http://xml.org/sax/features/validation"
052            factory.setValidating(true);
053            factory.setAttribute(
054                    "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
055                    "http://www.w3.org/2001/XMLSchema");
056            factory.setAttribute("http://apache.org/xml/features/validation/schema",
057                    Boolean.TRUE);
058        }
059    
060        public DocumentBuilder getParser()
061                throws ParserConfigurationException {
062            DocumentBuilder builder = factory.newDocumentBuilder();
063            builder.setEntityResolver(entityResolver);
064            builder.setErrorHandler(new ErrorHandler() {
065                public void error(SAXParseException exception) {
066                    log.warn("SAX parse error (ignored)", exception);
067                    //throw exception;
068                }
069    
070                public void fatalError(SAXParseException exception) {
071                    log.warn("Fatal SAX parse error (ignored)", exception);
072                    //throw exception;
073                }
074    
075                public void warning(SAXParseException exception) {
076                    log.warn("SAX parse warning", exception);
077                }
078            });
079            return builder;
080        }
081    
082        public EntityResolver getEntityResolver() {
083            return entityResolver;
084        }
085    
086        public void setEntityResolver(EntityResolver entityResolver) {
087            this.entityResolver = entityResolver;
088        }
089    
090        public final static GBeanInfo GBEAN_INFO;
091    
092        static {
093            GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Factory for constructing suitable configured xml parsers", ParserFactoryImpl.class);
094    
095            infoFactory.addOperation("getParser");
096    
097            infoFactory.addReference("EntityResolver", EntityResolver.class, "GBean");
098    
099            infoFactory.setConstructor(new String[]{"EntityResolver"});
100    
101            GBEAN_INFO = infoFactory.getBeanInfo();
102        }
103    
104        public static GBeanInfo getGBeanInfo() {
105            return GBEAN_INFO;
106        }
107    }