001 /* $Id: ParserFeatureSetterFactory.java 729082 2008-12-23 20:04:44Z rahul $ 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 import javax.xml.validation.Schema; 028 029 import org.apache.commons.digester.parser.GenericParser; 030 import org.apache.commons.digester.parser.XercesParser; 031 032 import org.xml.sax.SAXException; 033 import org.xml.sax.SAXNotRecognizedException; 034 import org.xml.sax.SAXNotSupportedException; 035 036 /** 037 * Creates a <code>SAXParser</code> based on the underlying parser. 038 * Allows logical properties depending on logical parser versions 039 * to be set. 040 * 041 * @since 1.6 042 * @deprecated Create an XMLParser instance yourself, configure validation 043 * appropriately, and pass it as a parameter to the 044 * {@link Digester} constructor, or use 045 * {@link Digester#setXMLSchema(Schema)} for validation. 046 */ 047 public 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 }