KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > arooa > xml > JAXPUtils


1 /*
2  * This source code is heavily based on source code from the Apache
3  * Ant project. As such the following is included:
4  * ------------------------------------------------------------------
5  *
6  * The Apache Software License, Version 1.1
7  *
8  * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
9  * reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution, if
24  * any, must include the following acknowlegement:
25  * "This product includes software developed by the
26  * Apache Software Foundation (http://www.apache.org/)."
27  * Alternately, this acknowlegement may appear in the software itself,
28  * if and wherever such third-party acknowlegements normally appear.
29  *
30  * 4. The names "Ant" and "Apache Software
31  * Foundation" must not be used to endorse or promote products derived
32  * from this software without prior written permission. For written
33  * permission, please contact apache@apache.org.
34  *
35  * 5. Products derived from this software may not be called "Apache"
36  * nor may "Apache" appear in their names without prior written
37  * permission of the Apache Group.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of the Apache Software Foundation. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  */

58 package org.oddjob.arooa.xml;
59
60 import javax.xml.parsers.DocumentBuilder JavaDoc;
61 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
62 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
63 import javax.xml.parsers.ParserConfigurationException JavaDoc;
64 import javax.xml.parsers.SAXParser JavaDoc;
65 import javax.xml.parsers.SAXParserFactory JavaDoc;
66
67 import org.oddjob.arooa.ArooaException;
68 import org.xml.sax.SAXException JavaDoc;
69 import org.xml.sax.XMLReader JavaDoc;
70
71 /**
72  * Collection of helper methods that retrieve a ParserFactory or
73  * Parsers and Readers.
74  * <p>
75  * This class will create only a single factory instance.
76  * <p>
77  * Based on the original by <b>Stefan Bodewig</b>.
78  *
79  */

80 public class JAXPUtils {
81
82
83     /**
84      * Parser factory to use to create parsers.
85      * @see #getParserFactory
86      *
87      */

88     private static SAXParserFactory JavaDoc parserFactory = null;
89
90     /**
91      * Parser Factory to create Namespace aware parsers.
92      *
93      */

94     private static SAXParserFactory JavaDoc nsParserFactory = null;
95
96     /**
97      * Parser factory to use to create document builders.
98      *
99      */

100     private static DocumentBuilderFactory JavaDoc builderFactory = null;
101
102     /**
103      * Returns the parser factory to use. Only one parser factory is
104      * ever created by this method and is then cached for future use.
105      *
106      * @return a SAXParserFactory to use
107      *
108      */

109     public static synchronized SAXParserFactory JavaDoc getParserFactory()
110         throws ArooaException {
111
112         if (parserFactory == null) {
113             parserFactory = newParserFactory();
114         }
115         return parserFactory;
116     }
117
118     /**
119      * Returns the parser factory to use to create namespace aware parsers.
120      *
121      * @return a SAXParserFactory to use which supports manufacture of
122      * namespace aware parsers
123      *
124      */

125     public static synchronized SAXParserFactory JavaDoc getNSParserFactory()
126         throws ArooaException {
127
128         if (nsParserFactory == null) {
129             nsParserFactory = newParserFactory();
130             nsParserFactory.setNamespaceAware(true);
131         }
132         return nsParserFactory;
133     }
134
135     /**
136      * Returns a new parser factory instance.
137      *
138      */

139     public static SAXParserFactory JavaDoc newParserFactory() throws ArooaException {
140
141         try {
142             return SAXParserFactory.newInstance();
143         } catch (FactoryConfigurationError JavaDoc e) {
144             throw new ArooaException("XML parser factory has not been "
145                                      + "configured correctly: "
146                                      + e.getMessage(), e);
147         }
148     }
149
150     /**
151      * Returns a newly created SAX 2 XMLReader, using the default parser
152      * factory.
153      *
154      * @return a SAX 2 XMLReader.
155      * @see #getParserFactory
156      */

157     public static XMLReader JavaDoc getXMLReader() throws ArooaException {
158         try {
159             return newSAXParser(getParserFactory()).getXMLReader();
160         } catch (SAXException JavaDoc e) {
161             throw convertToArooaException(e);
162         }
163     }
164
165     /**
166      * Returns a newly created SAX 2 XMLReader, which is namespace aware
167      *
168      * @return a SAX 2 XMLReader.
169      * @see #getParserFactory
170      */

171     public static XMLReader JavaDoc getNamespaceXMLReader() throws ArooaException {
172         try {
173             return newSAXParser(getNSParserFactory()).getXMLReader();
174         } catch (SAXException JavaDoc e) {
175             throw convertToArooaException(e);
176         }
177     }
178
179     /**
180      * Returns a newly created DocumentBuilder.
181      *
182      * @return a DocumentBuilder
183      */

184     public static DocumentBuilder JavaDoc getDocumentBuilder() throws ArooaException {
185         try {
186             return getDocumentBuilderFactory().newDocumentBuilder();
187         } catch (ParserConfigurationException JavaDoc e) {
188             throw new ArooaException(e);
189         }
190     }
191
192     /**
193      * @return a new SAXParser instance as helper for getParser and
194      * getXMLReader.
195      *
196      */

197     private static SAXParser JavaDoc newSAXParser(SAXParserFactory JavaDoc factory)
198          throws ArooaException {
199         try {
200             return factory.newSAXParser();
201         } catch (ParserConfigurationException JavaDoc e) {
202             throw new ArooaException("Cannot create parser for the given "
203                                      + "configuration: " + e.getMessage(), e);
204         } catch (SAXException JavaDoc e) {
205             throw convertToArooaException(e);
206         }
207     }
208
209     /**
210      * Translate a SAXException into a ArooaException
211      *
212      */

213     private static ArooaException convertToArooaException(SAXException JavaDoc e) {
214         Exception JavaDoc nested = e.getException();
215         if (nested != null) {
216             return new ArooaException(nested);
217         } else {
218             return new ArooaException(e);
219         }
220     }
221
222     /**
223      * Obtains the default builder factory if not already.
224      *
225      */

226     private static synchronized
227         DocumentBuilderFactory JavaDoc getDocumentBuilderFactory()
228         throws ArooaException {
229         if (builderFactory == null) {
230             try {
231                 builderFactory = DocumentBuilderFactory.newInstance();
232             } catch (FactoryConfigurationError JavaDoc e) {
233                 throw new ArooaException("Document builder factory has not "
234                                          + "been configured correctly: "
235                                          + e.getMessage(), e);
236             }
237         }
238         return builderFactory;
239     }
240
241 }
242
Popular Tags