View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  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.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  /**
34   *
35   *
36   * @version $Rev: 428843 $ $Date: 2006-08-04 11:43:59 -0700 (Fri, 04 Aug 2006) $
37   *
38   * */
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          //sets "http://xml.org/sax/features/namespaces"
50          factory.setNamespaceAware(true);
51          //sets "http://xml.org/sax/features/validation"
52          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                  //throw exception;
68              }
69  
70              public void fatalError(SAXParseException exception) {
71                  log.warn("Fatal SAX parse error (ignored)", exception);
72                  //throw exception;
73              }
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 }