001 /* $Id: ParserFeatureSetterFactory.java 476205 2006-11-17 16:43:10Z dennisl $ 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 019 020 package org.apache.commons.digester; 021 022 import java.util.Properties; 023 024 import javax.xml.parsers.ParserConfigurationException; 025 import javax.xml.parsers.SAXParser; 026 import javax.xml.parsers.SAXParserFactory; 027 028 import org.apache.commons.digester.parser.GenericParser; 029 import org.apache.commons.digester.parser.XercesParser; 030 031 import org.xml.sax.SAXException; 032 import org.xml.sax.SAXNotRecognizedException; 033 import org.xml.sax.SAXNotSupportedException; 034 035 /** 036 * Creates a <code>SAXParser</code> based on the underlying parser. 037 * Allows logical properties depending on logical parser versions 038 * to be set. 039 * 040 * @since 1.6 041 */ 042 public class ParserFeatureSetterFactory { 043 044 /** 045 * <code>true</code> is Xerces is used. 046 */ 047 private static boolean isXercesUsed; 048 049 static { 050 try{ 051 // Use reflection to avoid a build dependency with Xerces. 052 // 053 // Note that this does not detect Sun's repackaging of 054 // Xerces as com.sun.org.apache.xerces; perhaps it should? 055 SAXParserFactory factory = SAXParserFactory.newInstance(); 056 if (factory.getClass().getName().startsWith("org.apache.xerces")) { 057 isXercesUsed = true; 058 } 059 } catch (Exception ex) { 060 isXercesUsed = false; 061 } 062 } 063 064 /** 065 * Create a new <code>SAXParser</code> 066 * @param properties (logical) properties to be set on parser 067 * @return a <code>SAXParser</code> configured based on the underlying 068 * parser implementation. 069 */ 070 public static SAXParser newSAXParser(Properties properties) 071 throws ParserConfigurationException, 072 SAXException, 073 SAXNotRecognizedException, 074 SAXNotSupportedException { 075 076 if (isXercesUsed){ 077 return XercesParser.newSAXParser(properties); 078 } else { 079 return GenericParser.newSAXParser(properties); 080 } 081 } 082 083 }