01: /* $Id: GenericParser.java 467222 2006-10-24 03:17:11Z markt $
02: *
03: * Licensed to the Apache Software Foundation (ASF) under one or more
04: * contributor license agreements. See the NOTICE file distributed with
05: * this work for additional information regarding copyright ownership.
06: * The ASF licenses this file to You under the Apache License, Version 2.0
07: * (the "License"); you may not use this file except in compliance with
08: * the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing, software
13: * distributed under the License is distributed on an "AS IS" BASIS,
14: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: * See the License for the specific language governing permissions and
16: * limitations under the License.
17: */
18:
19: package org.apache.tomcat.util.digester;
20:
21: import java.util.Properties;
22:
23: import javax.xml.parsers.ParserConfigurationException;
24: import javax.xml.parsers.SAXParser;
25: import javax.xml.parsers.SAXParserFactory;
26:
27: import org.apache.juli.logging.Log;
28: import org.apache.juli.logging.LogFactory;
29: import org.xml.sax.SAXException;
30: import org.xml.sax.SAXNotRecognizedException;
31:
32: /**
33: * Create a <code>SAXParser</code> configured to support XML Schema and DTD.
34: *
35: * @since 1.6
36: */
37:
38: public class GenericParser {
39:
40: /**
41: * The Log to which all SAX event related logging calls will be made.
42: */
43: protected static Log log = LogFactory
44: .getLog("org.apache.commons.digester.Digester.sax");
45:
46: /**
47: * The JAXP 1.2 property required to set up the schema location.
48: */
49: private static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource";
50:
51: /**
52: * The JAXP 1.2 property to set up the schemaLanguage used.
53: */
54: protected static String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
55:
56: /**
57: * Create a <code>SAXParser</code> configured to support XML Scheman and DTD
58: * @param properties parser specific properties/features
59: * @return an XML Schema/DTD enabled <code>SAXParser</code>
60: */
61: public static SAXParser newSAXParser(Properties properties)
62: throws ParserConfigurationException, SAXException,
63: SAXNotRecognizedException {
64:
65: SAXParserFactory factory = (SAXParserFactory) properties
66: .get("SAXParserFactory");
67: SAXParser parser = factory.newSAXParser();
68: String schemaLocation = (String) properties
69: .get("schemaLocation");
70: String schemaLanguage = (String) properties
71: .get("schemaLanguage");
72:
73: try {
74: if (schemaLocation != null) {
75: parser
76: .setProperty(JAXP_SCHEMA_LANGUAGE,
77: schemaLanguage);
78: parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
79: }
80: } catch (SAXNotRecognizedException e) {
81: log.info(parser.getClass().getName() + ": "
82: + e.getMessage() + " not supported.");
83: }
84: return parser;
85: }
86:
87: }
|