KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tomcat > util > digester > GenericParser


1 /* $Id: GenericParser.java 467222 2006-10-24 03:17:11Z markt $
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
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
20 package org.apache.tomcat.util.digester;
21
22 import java.util.Properties JavaDoc;
23
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.parsers.SAXParser JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.xml.sax.SAXException JavaDoc;
31 import org.xml.sax.SAXNotRecognizedException JavaDoc;
32
33 /**
34  * Create a <code>SAXParser</code> configured to support XML Schema and DTD.
35  *
36  * @since 1.6
37  */

38
39 public class GenericParser{
40
41     /**
42      * The Log to which all SAX event related logging calls will be made.
43      */

44     protected static Log log =
45         LogFactory.getLog("org.apache.commons.digester.Digester.sax");
46
47     /**
48      * The JAXP 1.2 property required to set up the schema location.
49      */

50     private static final String JavaDoc JAXP_SCHEMA_SOURCE =
51         "http://java.sun.com/xml/jaxp/properties/schemaSource";
52
53     /**
54      * The JAXP 1.2 property to set up the schemaLanguage used.
55      */

56     protected static String JavaDoc JAXP_SCHEMA_LANGUAGE =
57         "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
58
59     /**
60      * Create a <code>SAXParser</code> configured to support XML Scheman and DTD
61      * @param properties parser specific properties/features
62      * @return an XML Schema/DTD enabled <code>SAXParser</code>
63      */

64     public static SAXParser JavaDoc newSAXParser(Properties JavaDoc properties)
65             throws ParserConfigurationException JavaDoc,
66                    SAXException JavaDoc,
67                    SAXNotRecognizedException JavaDoc{
68
69         SAXParserFactory JavaDoc factory =
70                         (SAXParserFactory JavaDoc)properties.get("SAXParserFactory");
71         SAXParser JavaDoc parser = factory.newSAXParser();
72         String JavaDoc schemaLocation = (String JavaDoc)properties.get("schemaLocation");
73         String JavaDoc schemaLanguage = (String JavaDoc)properties.get("schemaLanguage");
74
75         try{
76             if (schemaLocation != null) {
77                 parser.setProperty(JAXP_SCHEMA_LANGUAGE, schemaLanguage);
78                 parser.setProperty(JAXP_SCHEMA_SOURCE, schemaLocation);
79             }
80         } catch (SAXNotRecognizedException JavaDoc e){
81             log.info(parser.getClass().getName() + ": "
82                                         + e.getMessage() + " not supported.");
83         }
84         return parser;
85     }
86
87 }
Popular Tags