KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > deployment > xml > ParserFactoryImpl


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 JavaDoc;
21 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
22 import javax.xml.parsers.ParserConfigurationException JavaDoc;
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 JavaDoc;
30 import org.xml.sax.ErrorHandler JavaDoc;
31 import org.xml.sax.SAXParseException JavaDoc;
32
33 /**
34  *
35  *
36  * @version $Rev: 486195 $ $Date: 2006-12-12 10:42:02 -0500 (Tue, 12 Dec 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 JavaDoc factory;
44     private EntityResolver JavaDoc entityResolver;
45
46     public ParserFactoryImpl(EntityResolver JavaDoc 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 JavaDoc getParser()
61             throws ParserConfigurationException JavaDoc {
62         DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
63         builder.setEntityResolver(entityResolver);
64         builder.setErrorHandler(new ErrorHandler JavaDoc() {
65             public void error(SAXParseException JavaDoc exception) {
66                 log.warn("SAX parse error (ignored)", exception);
67                 //throw exception;
68
}
69
70             public void fatalError(SAXParseException JavaDoc exception) {
71                 log.warn("Fatal SAX parse error (ignored)", exception);
72                 //throw exception;
73
}
74
75             public void warning(SAXParseException JavaDoc exception) {
76                 log.warn("SAX parse warning", exception);
77             }
78         });
79         return builder;
80     }
81
82     public EntityResolver JavaDoc getEntityResolver() {
83         return entityResolver;
84     }
85
86     public void setEntityResolver(EntityResolver JavaDoc 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 JavaDoc.class, "GBean");
98
99         infoFactory.setConstructor(new String JavaDoc[]{"EntityResolver"});
100
101         GBEAN_INFO = infoFactory.getBeanInfo();
102     }
103
104     public static GBeanInfo getGBeanInfo() {
105         return GBEAN_INFO;
106     }
107 }
108
Popular Tags