001/* $Id: ParserFeatureSetterFactory.java 992075 2010-09-02 19:43:25Z simonetripodi $
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
020package org.apache.commons.digester;
021
022import java.util.Properties;
023
024import javax.xml.parsers.ParserConfigurationException;
025import javax.xml.parsers.SAXParser;
026import javax.xml.parsers.SAXParserFactory;
027
028import org.apache.commons.digester.parser.GenericParser;
029import org.apache.commons.digester.parser.XercesParser;
030
031import org.xml.sax.SAXException;
032import org.xml.sax.SAXNotRecognizedException;
033import 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 * @deprecated Create an XMLParser instance yourself, configure validation
042 *             appropriately, and pass it as a parameter to the
043 *             {@link Digester} constructor, or use
044 *             {@link Digester#setXMLSchema(javax.xml.validation.Schema)} for validation.
045 */
046@Deprecated
047public class ParserFeatureSetterFactory {
048
049    /**
050     * <code>true</code> is Xerces is used.
051     */
052    private static boolean isXercesUsed; 
053
054    static {
055        try{
056            // Use reflection to avoid a build dependency with Xerces.
057            //
058            // Note that this does not detect Sun's repackaging of 
059            // Xerces as com.sun.org.apache.xerces; perhaps it should?
060            SAXParserFactory factory = SAXParserFactory.newInstance();
061            if (factory.getClass().getName().startsWith("org.apache.xerces")) {
062                isXercesUsed = true;
063            }
064        } catch (Exception ex) {
065            isXercesUsed = false;
066        }
067    }
068
069    /**
070     * Create a new <code>SAXParser</code>
071     * @param properties (logical) properties to be set on parser
072     * @return a <code>SAXParser</code> configured based on the underlying
073     * parser implementation.
074     */
075    public static SAXParser newSAXParser(Properties properties)
076            throws ParserConfigurationException, 
077                   SAXException,
078                   SAXNotRecognizedException, 
079                   SAXNotSupportedException {
080
081        if (isXercesUsed){
082            return XercesParser.newSAXParser(properties);
083        } else {
084            return GenericParser.newSAXParser(properties);
085        }
086    }
087
088}