KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > JAXPUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 package org.apache.tools.ant.util;
19
20 import java.io.File JavaDoc;
21 import javax.xml.parsers.DocumentBuilder JavaDoc;
22 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
23 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.parsers.SAXParser JavaDoc;
26 import javax.xml.parsers.SAXParserFactory JavaDoc;
27 import org.apache.tools.ant.BuildException;
28 import org.xml.sax.Parser JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.XMLReader JavaDoc;
31
32 // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
33

34 /**
35  * Collection of helper methods that retrieve a ParserFactory or
36  * Parsers and Readers.
37  *
38  * <p>This class will create only a single factory instance.</p>
39  *
40  * @since Ant 1.5
41  */

42 public class JAXPUtils {
43
44     /**
45      * Helper for systemId.
46      *
47      * @since Ant 1.6
48      */

49     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
50
51     /**
52      * Parser factory to use to create parsers.
53      * @see #getParserFactory
54      *
55      * @since Ant 1.5
56      */

57     private static SAXParserFactory JavaDoc parserFactory = null;
58
59     /**
60      * Parser Factory to create Namespace aware parsers.
61      *
62      * @since Ant 1.6
63      */

64     private static SAXParserFactory JavaDoc nsParserFactory = null;
65
66     /**
67      * Parser factory to use to create document builders.
68      *
69      * @since Ant 1.6
70      */

71     private static DocumentBuilderFactory JavaDoc builderFactory = null;
72
73     /**
74      * Returns the parser factory to use. Only one parser factory is
75      * ever created by this method and is then cached for future use.
76      *
77      * @return a SAXParserFactory to use.
78      * @throws BuildException on error.
79      *
80      * @since Ant 1.5
81      */

82     public static synchronized SAXParserFactory JavaDoc getParserFactory()
83         throws BuildException {
84
85         if (parserFactory == null) {
86             parserFactory = newParserFactory();
87         }
88         return parserFactory;
89     }
90
91     /**
92      * Returns the parser factory to use to create namespace aware parsers.
93      *
94      * @return a SAXParserFactory to use which supports manufacture of
95      * namespace aware parsers.
96      * @throws BuildException on error.
97      *
98      * @since Ant 1.6
99      */

100     public static synchronized SAXParserFactory JavaDoc getNSParserFactory()
101         throws BuildException {
102
103         if (nsParserFactory == null) {
104             nsParserFactory = newParserFactory();
105             nsParserFactory.setNamespaceAware(true);
106         }
107         return nsParserFactory;
108     }
109
110     /**
111      * Returns a new parser factory instance.
112      *
113      * @return the parser factory.
114      * @throws BuildException on error.
115      * @since Ant 1.5
116      */

117     public static SAXParserFactory JavaDoc newParserFactory() throws BuildException {
118
119         try {
120             return SAXParserFactory.newInstance();
121         } catch (FactoryConfigurationError JavaDoc e) {
122             throw new BuildException("XML parser factory has not been "
123                                      + "configured correctly: "
124                                      + e.getMessage(), e);
125         }
126     }
127
128     /**
129      * Returns a newly created SAX 1 Parser, using the default parser
130      * factory.
131      *
132      * @return a SAX 1 Parser.
133      * @throws BuildException on error.
134      * @see #getParserFactory
135      * @since Ant 1.5
136      */

137     public static Parser JavaDoc getParser() throws BuildException {
138         try {
139             return newSAXParser(getParserFactory()).getParser();
140         } catch (SAXException JavaDoc e) {
141             throw convertToBuildException(e);
142         }
143     }
144
145     /**
146      * Returns a newly created SAX 2 XMLReader, using the default parser
147      * factory.
148      *
149      * @return a SAX 2 XMLReader.
150      * @throws BuildException on error.
151      * @see #getParserFactory
152      * @since Ant 1.5
153      */

154     public static XMLReader JavaDoc getXMLReader() throws BuildException {
155         try {
156             return newSAXParser(getParserFactory()).getXMLReader();
157         } catch (SAXException JavaDoc e) {
158             throw convertToBuildException(e);
159         }
160     }
161
162     /**
163      * Returns a newly created SAX 2 XMLReader, which is namespace aware
164      *
165      * @return a SAX 2 XMLReader.
166      * @throws BuildException on error.
167      * @see #getParserFactory
168      * @since Ant 1.6
169      */

170     public static XMLReader JavaDoc getNamespaceXMLReader() throws BuildException {
171         try {
172             return newSAXParser(getNSParserFactory()).getXMLReader();
173         } catch (SAXException JavaDoc e) {
174             throw convertToBuildException(e);
175         }
176     }
177
178     /**
179      * This is a best attempt to provide a URL.toExternalForm() from
180      * a file URL. Some parsers like Crimson choke on uri that are made of
181      * backslashed paths (ie windows) as it is does not conform
182      * URI specifications.
183      * @param file the file to create the system id from.
184      * @return the systemid corresponding to the given file.
185      * @since Ant 1.5.2
186      */

187     public static String JavaDoc getSystemId(File JavaDoc file) {
188         return FILE_UTILS.toURI(file.getAbsolutePath());
189     }
190
191     /**
192      * Returns a newly created DocumentBuilder.
193      *
194      * @return a DocumentBuilder.
195      * @throws BuildException on error.
196      * @since Ant 1.6
197      */

198     public static DocumentBuilder JavaDoc getDocumentBuilder() throws BuildException {
199         try {
200             return getDocumentBuilderFactory().newDocumentBuilder();
201         } catch (ParserConfigurationException JavaDoc e) {
202             throw new BuildException(e);
203         }
204     }
205
206     /**
207      * @return a new SAXParser instance as helper for getParser and
208      * getXMLReader.
209      *
210      * @since Ant 1.5
211      */

212     private static SAXParser JavaDoc newSAXParser(SAXParserFactory JavaDoc factory)
213          throws BuildException {
214         try {
215             return factory.newSAXParser();
216         } catch (ParserConfigurationException JavaDoc e) {
217             throw new BuildException("Cannot create parser for the given "
218                                      + "configuration: " + e.getMessage(), e);
219         } catch (SAXException JavaDoc e) {
220             throw convertToBuildException(e);
221         }
222     }
223
224     /**
225      * Translate a SAXException into a BuildException
226      *
227      * @since Ant 1.5
228      */

229     private static BuildException convertToBuildException(SAXException JavaDoc e) {
230         Exception JavaDoc nested = e.getException();
231         if (nested != null) {
232             return new BuildException(nested);
233         } else {
234             return new BuildException(e);
235         }
236     }
237
238     /**
239      * Obtains the default builder factory if not already.
240      *
241      * @since Ant 1.6
242      */

243     private static synchronized
244         DocumentBuilderFactory JavaDoc getDocumentBuilderFactory()
245         throws BuildException {
246         if (builderFactory == null) {
247             try {
248                 builderFactory = DocumentBuilderFactory.newInstance();
249             } catch (FactoryConfigurationError JavaDoc e) {
250                 throw new BuildException("Document builder factory has not "
251                                          + "been configured correctly: "
252                                          + e.getMessage(), e);
253             }
254         }
255         return builderFactory;
256     }
257
258 }
259
Popular Tags