1 /* $Id: ParserFeatureSetterFactory.java 476205 2006-11-17 16:43:10Z dennisl $
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one or more
4 * contributor license agreements. See the NOTICE file distributed with
5 * this work for additional information regarding copyright ownership.
6 * The ASF licenses this file to You under the Apache License, Version 2.0
7 * (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 *
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
20 package org.apache.commons.digester;
21
22 import java.util.Properties;
23
24 import javax.xml.parsers.ParserConfigurationException;
25 import javax.xml.parsers.SAXParser;
26 import javax.xml.parsers.SAXParserFactory;
27
28 import org.apache.commons.digester.parser.GenericParser;
29 import org.apache.commons.digester.parser.XercesParser;
30
31 import org.xml.sax.SAXException;
32 import org.xml.sax.SAXNotRecognizedException;
33 import org.xml.sax.SAXNotSupportedException;
34
35 /**
36 * Creates a <code>SAXParser</code> based on the underlying parser.
37 * Allows logical properties depending on logical parser versions
38 * to be set.
39 *
40 * @since 1.6
41 */
42 public class ParserFeatureSetterFactory {
43
44 /**
45 * <code>true</code> is Xerces is used.
46 */
47 private static boolean isXercesUsed;
48
49 static {
50 try{
51 // Use reflection to avoid a build dependency with Xerces.
52 //
53 // Note that this does not detect Sun's repackaging of
54 // Xerces as com.sun.org.apache.xerces; perhaps it should?
55 SAXParserFactory factory = SAXParserFactory.newInstance();
56 if (factory.getClass().getName().startsWith("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,
72 SAXException,
73 SAXNotRecognizedException,
74 SAXNotSupportedException {
75
76 if (isXercesUsed){
77 return XercesParser.newSAXParser(properties);
78 } else {
79 return GenericParser.newSAXParser(properties);
80 }
81 }
82
83 }