Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.xml » [javadoc | source]

    1   /**
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   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.slf4j.Logger;
   25   import org.slf4j.LoggerFactory;
   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   /**
   34    * @version $Rev: 653740 $ $Date: 2008-05-06 03:44:18 -0700 (Tue, 06 May 2008) $
   35    */
   36   public class ParserFactoryImpl implements ParserFactory {
   37       private static final Logger log = LoggerFactory.getLogger(ParserFactoryImpl.class);
   38   
   39       private final DocumentBuilderFactory factory;
   40       private EntityResolver entityResolver;
   41   
   42       public ParserFactoryImpl(EntityResolver entityResolver) {
   43           this.entityResolver = entityResolver;
   44           factory = XmlUtil.newDocumentBuilderFactory();
   45           //sets "http://xml.org/sax/features/namespaces"
   46           factory.setNamespaceAware(true);
   47           //sets "http://xml.org/sax/features/validation"
   48           factory.setValidating(true);
   49           factory.setAttribute(
   50                   "http://java.sun.com/xml/jaxp/properties/schemaLanguage",
   51                   "http://www.w3.org/2001/XMLSchema");
   52           factory.setAttribute("http://apache.org/xml/features/validation/schema",
   53                   Boolean.TRUE);
   54       }
   55   
   56       public DocumentBuilder getParser()
   57               throws ParserConfigurationException {
   58           DocumentBuilder builder = factory.newDocumentBuilder();
   59           builder.setEntityResolver(entityResolver);
   60           builder.setErrorHandler(new ErrorHandler() {
   61               public void error(SAXParseException exception) {
   62                   log.warn("SAX parse error (ignored)", exception);
   63                   //throw exception;
   64               }
   65   
   66               public void fatalError(SAXParseException exception) {
   67                   log.warn("Fatal SAX parse error (ignored)", exception);
   68                   //throw exception;
   69               }
   70   
   71               public void warning(SAXParseException exception) {
   72                   log.warn("SAX parse warning", exception);
   73               }
   74           });
   75           return builder;
   76       }
   77   
   78       public EntityResolver getEntityResolver() {
   79           return entityResolver;
   80       }
   81   
   82       public void setEntityResolver(EntityResolver entityResolver) {
   83           this.entityResolver = entityResolver;
   84       }
   85   
   86       public final static GBeanInfo GBEAN_INFO;
   87   
   88       static {
   89           GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Factory for constructing suitable configured xml parsers", ParserFactoryImpl.class);
   90   
   91           infoFactory.addOperation("getParser");
   92   
   93           infoFactory.addReference("EntityResolver", EntityResolver.class, "GBean");
   94   
   95           infoFactory.setConstructor(new String[]{"EntityResolver"});
   96   
   97           GBEAN_INFO = infoFactory.getBeanInfo();
   98       }
   99   
  100       public static GBeanInfo getGBeanInfo() {
  101           return GBEAN_INFO;
  102       }
  103   }

Home » geronimo-2.2-source-release » org.apache.geronimo.deployment.xml » [javadoc | source]