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