KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > digester > parser > GenericParser


1 /* $Id: GenericParser.java 179714 2005-06-03 03:53:39Z skitching $
2  *
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18
19 package org.apache.commons.digester.parser;
20
21 import java.util.Properties JavaDoc;
22
23 import javax.xml.parsers.ParserConfigurationException JavaDoc;
24 import javax.xml.parsers.SAXParser JavaDoc;
25 import javax.xml.parsers.SAXParserFactory JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.SAXNotRecognizedException JavaDoc;
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 =
44         LogFactory.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 JavaDoc JAXP_SCHEMA_SOURCE =
50         "http://java.sun.com/xml/jaxp/properties/schemaSource";
51
52     /**
53      * The JAXP 1.2 property to set up the schemaLanguage used.
54      */

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

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