01: /* $Id: ParserFeatureSetterFactory.java 476205 2006-11-17 16:43:10Z dennisl $
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package org.apache.commons.digester;
20:
21: import java.util.Properties;
22:
23: import javax.xml.parsers.ParserConfigurationException;
24: import javax.xml.parsers.SAXParser;
25: import javax.xml.parsers.SAXParserFactory;
26:
27: import org.apache.commons.digester.parser.GenericParser;
28: import org.apache.commons.digester.parser.XercesParser;
29:
30: import org.xml.sax.SAXException;
31: import org.xml.sax.SAXNotRecognizedException;
32: import org.xml.sax.SAXNotSupportedException;
33:
34: /**
35: * Creates a <code>SAXParser</code> based on the underlying parser.
36: * Allows logical properties depending on logical parser versions
37: * to be set.
38: *
39: * @since 1.6
40: */
41: public class ParserFeatureSetterFactory {
42:
43: /**
44: * <code>true</code> is Xerces is used.
45: */
46: private static boolean isXercesUsed;
47:
48: static {
49: try {
50: // Use reflection to avoid a build dependency with Xerces.
51: //
52: // Note that this does not detect Sun's repackaging of
53: // Xerces as com.sun.org.apache.xerces; perhaps it should?
54: SAXParserFactory factory = SAXParserFactory.newInstance();
55: if (factory.getClass().getName().startsWith(
56: "org.apache.xerces")) {
57: isXercesUsed = true;
58: }
59: } catch (Exception ex) {
60: isXercesUsed = false;
61: }
62: }
63:
64: /**
65: * Create a new <code>SAXParser</code>
66: * @param properties (logical) properties to be set on parser
67: * @return a <code>SAXParser</code> configured based on the underlying
68: * parser implementation.
69: */
70: public static SAXParser newSAXParser(Properties properties)
71: throws ParserConfigurationException, SAXException,
72: SAXNotRecognizedException, SAXNotSupportedException {
73:
74: if (isXercesUsed) {
75: return XercesParser.newSAXParser(properties);
76: } else {
77: return GenericParser.newSAXParser(properties);
78: }
79: }
80:
81: }
|