001 /* $Id: GenericParser.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.parser;
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.Digester;
030 import org.apache.commons.logging.Log;
031 import org.apache.commons.logging.LogFactory;
032 import org.xml.sax.SAXException;
033 import org.xml.sax.SAXNotRecognizedException;
034
035 /**
036 * Create a <code>SAXParser</code> configured to support XML Schema and DTD.
037 *
038 * @since 1.6
039 * @deprecated Create an XMLParser instance yourself, configure validation
040 * appropriately, and pass it as a parameter to the
041 * {@link Digester} constructor, or use
042 * {@link Digester#setXMLSchema(Schema)} for validation.
043 */
044
045 public class GenericParser{
046
047 /**
048 * The Log to which all SAX event related logging calls will be made.
049 */
050 protected static Log log =
051 LogFactory.getLog("org.apache.commons.digester.Digester.sax");
052
053 /**
054 * The JAXP 1.2 property required to set up the schema location.
055 */
056 private static final String JAXP_SCHEMA_SOURCE =
057 "http://java.sun.com/xml/jaxp/properties/schemaSource";
058
059 /**
060 * The JAXP 1.2 property to set up the schemaLanguage used.
061 */
062 protected static String JAXP_SCHEMA_LANGUAGE =
063 "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
064
065 /**
066 * Create a <code>SAXParser</code> configured to support XML Scheman and DTD
067 * @param properties parser specific properties/features
068 * @return an XML Schema/DTD enabled <code>SAXParser</code>
069 */
070 public static SAXParser newSAXParser(Properties properties)
071 throws ParserConfigurationException,
072 SAXException,
073 SAXNotRecognizedException{
074
075 SAXParserFactory factory =
076 (SAXParserFactory)properties.get("SAXParserFactory");
077 SAXParser parser = factory.newSAXParser();
078 String schemaLocation = (String)properties.get("schemaLocation");
079 String schemaLanguage = (String)properties.get("schemaLanguage");
080
081 try{
082 if (schemaLocation != null) {
083 parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
084 parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
085 }
086 } catch (SAXNotRecognizedException e){
087 log.info(parser.getClass().getName() + ": "
088 + e.getMessage() + " not supported.");
089 }
090 return parser;
091 }
092
093 }