KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > common > JaxpUtils


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.common;
13
14 import javax.xml.parsers.SAXParserFactory JavaDoc;
15 import javax.xml.parsers.SAXParser JavaDoc;
16
17 import com.versant.core.common.BindingSupportImpl;
18
19 /**
20  * Utility methods to get hold of XML parsers.
21  */

22 public class JaxpUtils {
23
24     private static final String JavaDoc SAX_PARSER_FACTORY_SYSPROP = "versant.SAXParserFactory";
25     private static final String JavaDoc SAX_PARSER_FACTORY_SYSPROP_OLD = "jdogenie.SAXParserFactory";
26
27     /**
28      * Get a SAXParserFactory.
29      */

30     public static SAXParserFactory JavaDoc getSAXParserFactory() {
31         String JavaDoc parserFactoryName = System.getProperty(SAX_PARSER_FACTORY_SYSPROP);
32         if (parserFactoryName == null) {
33             parserFactoryName = System.getProperty(SAX_PARSER_FACTORY_SYSPROP_OLD);
34         }
35         SAXParserFactory JavaDoc parserFactory = null;
36         if (parserFactoryName != null) {
37             try {
38                 Class JavaDoc cls = Class.forName(parserFactoryName);
39                 parserFactory = (SAXParserFactory JavaDoc)cls.newInstance();
40             } catch (Throwable JavaDoc t) {
41                 throw BindingSupportImpl.getInstance().runtime("Unable to create SAXParserFactory '" +
42                     parserFactoryName + "': " + t, t);
43             }
44             System.out.println("Versant Open Access: Using " + parserFactory);
45         } else {
46             parserFactory = SAXParserFactory.newInstance();
47         }
48         parserFactory.setValidating(false);
49         parserFactory.setNamespaceAware(false);
50         return parserFactory;
51     }
52
53     /**
54      * Create a SAXParser from a factory. Throws a JDOFatalUserException if
55      * this fails for some reason.
56      */

57     public static SAXParser JavaDoc createSAXParser(SAXParserFactory JavaDoc parserFactory) {
58         try {
59             return parserFactory.newSAXParser();
60         } catch (Throwable JavaDoc t) {
61             throw BindingSupportImpl.getInstance().runtime(
62                 "Unable to create SAXParser from factory: " + parserFactory + "\n" +
63                 "Versant Open Access requires a JAXP 1.1 compliant parser. If this is not possible\n" +
64                 "in your environment (e.g. WebLogic 6.0) try invoking java with\n" +
65                 "-D" + SAX_PARSER_FACTORY_SYSPROP + "=<name of SAXParserFactory> to bypass JAXP\n" +
66                 t, t);
67         }
68     }
69
70 }
71
Popular Tags