KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > configuration > JAXPConfigurator


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.configuration;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.logicalcobwebs.proxool.ProxoolException;
11 import org.xml.sax.InputSource JavaDoc;
12 import org.xml.sax.SAXException JavaDoc;
13 import org.xml.sax.SAXNotRecognizedException JavaDoc;
14 import org.xml.sax.SAXNotSupportedException JavaDoc;
15 import org.xml.sax.XMLReader JavaDoc;
16
17 import javax.xml.parsers.ParserConfigurationException JavaDoc;
18 import javax.xml.parsers.SAXParser JavaDoc;
19 import javax.xml.parsers.SAXParserFactory JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.FileReader JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.Reader JavaDoc;
24
25 /**
26  * Configurator that uses JAXP to get a parser for Proxool configuration xml. The parser relies on JAXP version 1.1 or higher
27  * and is namespace aware.
28  * <p>
29  * See {@link XMLConfigurator} for the Proxool xml configuration format.
30  * </p>
31  * <p>
32  * All the <code>configure</code> methods of this class takes a boolean argument describing whether the
33  * xml should be validated or not. If you want your xml to be validated be sure to read the
34  * <a HREF="XMLConfigurator.html#validation">Validation</a> chapter in the JavaDoc for {@link XMLConfigurator}.
35  * </p>
36  * @version $Revision: 1.12 $, $Date: 2006/01/18 14:39:58 $
37  * @author Christian Nedregaard (christian_nedregaard@email.com)
38  * @author $Author: billhorsman $ (current maintainer)
39  * @since Proxool 0.6
40  */

41 public class JAXPConfigurator {
42     private static final Log LOG = LogFactory.getLog(JAXPConfigurator.class);
43     private static final boolean NAMESPACE_AWARE = true;
44
45     /**
46      * Configure Proxool with xml from the given file.
47      * @param xmlFileName the file to read xml from.
48      * @param validate <code>true</code> if the parsel shall be validating, and <code>false</code> otherwise.
49      * @throws ProxoolException if the configuration fails.
50      */

51     public static void configure(String JavaDoc xmlFileName, boolean validate) throws ProxoolException {
52         try {
53             if (LOG.isDebugEnabled()) {
54                 LOG.debug("Configuring from xml file: " + xmlFileName);
55             }
56             configure(new InputSource JavaDoc(new FileReader JavaDoc(xmlFileName)), validate);
57         } catch (FileNotFoundException JavaDoc e) {
58             throw new ProxoolException(e);
59         }
60     }
61
62     /**
63      * Configure Proxool with xml from the given InputSource.
64      * @param inputSource the InputSource to read xml from.
65      * @param validate <code>true</code> if the parsel shall be validating, and <code>false</code> otherwise.
66      * @throws ProxoolException if the configuration fails.
67      */

68     public static void configure(InputSource JavaDoc inputSource, boolean validate) throws ProxoolException {
69         try {
70             SAXParserFactory JavaDoc saxParserFactory = SAXParserFactory.newInstance();
71             if (LOG.isDebugEnabled()) {
72                 LOG.debug("SAXParserFactory class: " + saxParserFactory.getClass().getName());
73             }
74             saxParserFactory.setValidating(validate);
75             final SAXParser JavaDoc saxParser = saxParserFactory.newSAXParser();
76             if (LOG.isDebugEnabled()) {
77                 LOG.debug("sax parser class" + saxParser.getClass().getName());
78             }
79             final XMLReader JavaDoc xmlReader = saxParser.getXMLReader();
80             if (LOG.isDebugEnabled()) {
81                 LOG.debug("XML reader class: " + xmlReader.getClass().getName());
82             }
83             final XMLConfigurator xmlConfigurator = new XMLConfigurator();
84             xmlReader.setErrorHandler(xmlConfigurator);
85             setSAXFeature(xmlReader, "http://xml.org/sax/features/namespaces", NAMESPACE_AWARE);
86             setSAXFeature(xmlReader, "http://xml.org/sax/features/namespace-prefixes", !NAMESPACE_AWARE);
87             saxParser.parse(inputSource, xmlConfigurator);
88         } catch (ParserConfigurationException JavaDoc pce) {
89             throw new ProxoolException("Parser configuration failed", pce);
90         } catch (SAXException JavaDoc se) {
91             throw new ProxoolException("Parsing failed.", se);
92         } catch (IOException JavaDoc ioe) {
93             throw new ProxoolException("Parsing failed.", ioe);
94         }
95     }
96
97     /**
98      * Configure Proxool with xml from the given reader.
99      * @param reader the reader to read xml from.
100      * @param validate <code>true</code> if the parsel shall be validating, and <code>false</code> otherwise.
101      * @throws ProxoolException if the configuration fails.
102      */

103     public static void configure(Reader JavaDoc reader, boolean validate) throws ProxoolException {
104         if (LOG.isDebugEnabled()) {
105             LOG.debug("Configuring from reader: " + reader);
106         }
107         configure(new InputSource JavaDoc(reader), validate);
108     }
109
110     // only log warning on problems with recognition and support of features
111
// we'll probably pull through anyway...
112
private static void setSAXFeature(XMLReader JavaDoc xmlReader, String JavaDoc feature, boolean state) {
113         if (LOG.isDebugEnabled()) {
114             LOG.debug("Setting sax feature: '" + feature + "'. State: " + state + ".");
115         }
116         try {
117             xmlReader.setFeature(feature, state);
118         } catch (SAXNotRecognizedException JavaDoc e) {
119             LOG.warn("Feature: '" + feature + "' not recognised by xml reader " + xmlReader + ".", e);
120         } catch (SAXNotSupportedException JavaDoc e) {
121             LOG.warn("Feature: '" + feature + "' not supported by xml reader " + xmlReader + ".", e);
122         }
123     }
124 }
125
126 /*
127  Revision history:
128  $Log: JAXPConfigurator.java,v $
129  Revision 1.12 2006/01/18 14:39:58 billhorsman
130  Unbundled Jakarta's Commons Logging.
131
132  Revision 1.11 2004/05/14 21:15:47 brenuart
133  Fix type in method name
134
135  Revision 1.10 2003/03/10 23:43:15 billhorsman
136  reapplied checkstyle that i'd inadvertently let
137  IntelliJ change...
138
139  Revision 1.9 2003/03/10 15:26:54 billhorsman
140  refactoringn of concurrency stuff (and some import
141  optimisation)
142
143  Revision 1.8 2003/03/03 11:12:00 billhorsman
144  fixed licence
145
146  Revision 1.7 2003/02/06 17:41:05 billhorsman
147  now uses imported logging
148
149  Revision 1.6 2003/01/27 18:26:42 billhorsman
150  refactoring of ProxyConnection and ProxyStatement to
151  make it easier to write JDK 1.2 patch
152
153  Revision 1.5 2002/12/18 23:31:57 chr32
154  Expanded doc.
155
156  Revision 1.4 2002/12/16 11:46:59 billhorsman
157  checkstyle
158
159  Revision 1.3 2002/12/16 02:38:47 chr32
160  Updated to new driver-properties xml format.
161
162  Revision 1.2 2002/12/15 19:43:11 chr32
163  Style fixes.
164
165  Revision 1.1 2002/12/15 18:49:55 chr32
166  Init rev.
167
168 */
Popular Tags