KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > kernel > digester > SaxParserConfigurer


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2003 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.kernel.digester;
65
66 import com.jcorporate.expresso.kernel.util.ClassLocator;
67 import org.apache.log4j.Logger;
68 import org.xml.sax.SAXException JavaDoc;
69
70 /**
71  * Class that sets the current SAX parser properties for the use by the
72  * javax.*Factory methods. This is basically used once before the Digester
73  * is loaded and used.
74  *
75  * @author Michael Rimov, Shash Chatterjee
76  */

77
78 public class SaxParserConfigurer {
79     static boolean configured = false;
80
81     private static final Logger log = Logger.getLogger(SaxParserConfigurer.class);
82
83     public SaxParserConfigurer() {
84         setSAXParser();
85     }
86
87     /*
88      * Goes through a pre-determined list of SAX parsers,
89      * the first one found is used to parse XML config files.
90      */

91     synchronized void setSAXParser() {
92         synchronized (SaxParserConfigurer.class) {
93             if (configured) {
94                 return;
95             }
96
97             //
98
//Check for previously explicitly defined parsers. If they're set,
99
//obey the law of the land.
100
//
101
String JavaDoc curParser = System.getProperty("javax.xml.parsers.SAXParserFactory", null);
102             if (curParser == null) {
103                 curParser = System.getProperty("org.xml.sax.parser", null);
104             }
105
106             if (curParser != null && curParser.length() > 0) {
107                 System.out.println("SAX Parser already explicitly defined. It is: " +
108                         curParser);
109                 configured = true;
110                 return;
111             }
112
113
114
115             // The class names and descriptions of parsers to look for
116
MySAXParser[] parserList =
117                     {
118                         new MySAXParser("org.apache.xerces.jaxp.SAXParserFactoryImpl",
119                                 "org.apache.xerces.parsers.SAXParser",
120                                 "Apache Xerces"),
121
122                         new MySAXParser("org.apache.crimson.jaxp.SAXParserFactoryImpl",
123                                 "org.apache.crimson.parser.XMLReaderImpl",
124                                 "Apache Crimson"),
125
126                         //
127
//http://www.xmlmind.com/xpforjaxp.html
128
//JAXP Capable modification to James Clark's XP
129
//
130
new MySAXParser("com.jclark.xml.jaxp.SAXParserFactoryImpl",
131                                 "com.jclark.xml.sax.Driver",
132                                 "James Clark's XP with SAX Additions"),
133
134                         //
135
//GNU JAXP Implementation
136
//
137
new MySAXParser("gnu.xml.aelfred2.JAXPFactory",
138                                 "gnu.xml.dom.JAXPFactory",
139                                 "GNU JAXP"),
140
141                     };
142
143             // Search through each element of parser list to see
144
// if parser exists in classpath
145
int idx = 0;
146             while ((idx < parserList.length) &&
147                     (!parserList[idx].existsInClasspath())) {
148                 idx++;
149             }
150
151             // If parser found set the system property that SAX uses to load the parser
152
// otherwise, print an error message
153
if (idx < parserList.length) {
154                 log.info("Setting default XML parser factory to " + parserList[idx].getDesc());
155                 System.setProperty("javax.xml.parsers.SAXParserFactory", parserList[idx].getName());
156                 System.setProperty("org.xml.sax.driver", parserList[idx].getSAXDriverName());
157             } else {
158                 log.info("Couldn't find a suitable SAX/JAXP-compliant XML parser");
159                 log.info("Please put Apache Crimson or Xerces in classpath");
160             }
161
162             configured = true;
163         }
164     }
165
166     /*
167      * A small class to hold the name/desc pair of SAXParsers
168      * Provides utility function to check if specified parser is in classpath
169      * Added By Shash Chatterjee 7/20/02
170      */

171     class MySAXParser {
172         // The classname of the class
173
private String JavaDoc className = null;
174
175         // The classname of the class
176
private String JavaDoc saxDriverClassName = null;
177
178         // The description of the Parser
179
private String JavaDoc classDesc = null;
180
181         // Only public constructor
182
public MySAXParser(String JavaDoc cName, String JavaDoc sdcName, String JavaDoc cDesc) {
183             className = cName;
184             saxDriverClassName = sdcName;
185             classDesc = cDesc;
186         }
187
188         // Disallow use of the default constructor
189
private MySAXParser() {
190         }
191
192         public String JavaDoc getName() {
193             return className;
194         }
195
196         public String JavaDoc getSAXDriverName() {
197             return saxDriverClassName;
198         }
199
200         public String JavaDoc getDesc() {
201             return classDesc;
202         }
203
204         // Check if this SAX parser exists in the classpath
205
public boolean existsInClasspath() {
206             boolean returnValue = true;
207             String JavaDoc oldProperty = null;
208             try {
209                 Class JavaDoc clazz = ClassLocator.loadClass(getName());
210
211                 //
212
//Ok so far, now let's try actually getting a parser factory with
213
//this information and see what happens
214
//
215
//
216
//Ok so far, now let's try actually getting a parser factory with
217
//this information and see what happens
218
//
219
if (oldProperty == null) {
220                     oldProperty = System.getProperty("javax.xml.parsers.SAXParserFactory");
221                 }
222                 try {
223                     System.setProperty("javax.xml.parsers.SAXParserFactory", getName());
224                     javax.xml.parsers.SAXParserFactory JavaDoc spf = javax.xml.parsers.SAXParserFactory.newInstance();
225                     javax.xml.parsers.SAXParser JavaDoc sp = spf.newSAXParser();
226                 } catch (java.security.AccessControlException JavaDoc ex) {
227                     log.warn("App Server would not let us set the SAX Parser to another parser.");
228                 }
229             } catch (ClassNotFoundException JavaDoc cnfe) {
230                 returnValue = false;
231             } catch (javax.xml.parsers.ParserConfigurationException JavaDoc ex) {
232                 System.err.println(
233                         "Found class " + getName() + " but unable to instantiate SAX Parser " + ex.getMessage());
234                 ex.printStackTrace(System.err);
235                 returnValue = false;
236             } catch (SAXException JavaDoc ex) {
237                 System.err.println(
238                         "Found class " + getName() + " but unable to instantiate SAX Parser " + ex.getMessage());
239                 ex.printStackTrace(System.err);
240                 returnValue = false;
241             } catch (javax.xml.parsers.FactoryConfigurationError JavaDoc ex) {
242                 System.err.println(
243                         "Found class " + getName() + " but unable to instantiate ClassFactory " + ex.getMessage());
244                 ex.printStackTrace(System.err);
245                 returnValue = false;
246             }
247
248             //Ok, we've done mucking with things, set the old SAXParserFactory
249
//back
250
if (oldProperty != null) {
251                 System.setProperty("javax.xml.parsers.SAXParserFactory", oldProperty);
252             }
253
254             return returnValue;
255         }
256     }
257 }
Popular Tags