KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > util > xml > DocumentParser


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: DocumentParser.java 499 2006-05-23 16:41:31Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.util.xml;
27
28 import java.io.IOException JavaDoc;
29 import java.io.InputStreamReader JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLConnection JavaDoc;
33
34 import javax.xml.parsers.DocumentBuilder JavaDoc;
35 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37
38 import org.w3c.dom.Document JavaDoc;
39 import org.xml.sax.EntityResolver JavaDoc;
40 import org.xml.sax.InputSource JavaDoc;
41 import org.xml.sax.SAXException JavaDoc;
42
43 /**
44  * Allows to parse an xml file.
45  * @author Florent Benoit
46  */

47 public final class DocumentParser {
48
49     /**
50      * Utility class.
51      */

52     private DocumentParser() {
53
54     }
55
56     /**
57      * Builds a new Document for a given xml file.
58      * @param url the URL of the the XML file.
59      * @param isValidating validate or not the xml file ?
60      * @param entityResolver the entityResolver used to validate document (if
61      * validating = true)
62      * @throws DocumentParserException if creating of builder fails or parsing
63      * fails.
64      * @return an application object.
65      */

66     public static Document JavaDoc getDocument(final URL JavaDoc url, final boolean isValidating, final EntityResolver JavaDoc entityResolver)
67             throws DocumentParserException {
68         // build factory
69
DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
70
71         // Schema is used for persistence.xsd
72
factory.setNamespaceAware(true);
73         factory.setValidating(isValidating);
74
75         // ignore white space can only be set if parser is validating
76
if (isValidating) {
77             factory.setIgnoringElementContentWhitespace(isValidating);
78             factory.setAttribute("http://apache.org/xml/features/validation/schema", new Boolean JavaDoc(isValidating));
79             factory.setAttribute("http://apache.org/xml/features/validation/schema-full-checking", Boolean.valueOf(true));
80         }
81
82         // Add schema location
83
//TODO: FIXME: set other schemas
84
factory.setAttribute("http://apache.org/xml/properties/schema/external-schemaLocation",
85                 "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd");
86
87
88         // Build a document builder
89
DocumentBuilder JavaDoc builder = null;
90         try {
91             builder = factory.newDocumentBuilder();
92         } catch (ParserConfigurationException JavaDoc e) {
93             throw new DocumentParserException("Cannot build a document builder", e);
94         }
95
96         // Error handler (throwing exceptions)
97
builder.setErrorHandler(new EasyBeansErrorHandler());
98
99         // Add schema
100
if (factory.isValidating()) {
101             builder.setEntityResolver(entityResolver);
102         }
103
104         // Parse document
105
URLConnection JavaDoc urlConnection = null;
106         try {
107             urlConnection = url.openConnection();
108         } catch (IOException JavaDoc e) {
109             throw new DocumentParserException("Cannot open a connection on URL '" + url + "'", e);
110         }
111         urlConnection.setDefaultUseCaches(false);
112         Reader JavaDoc reader = null;
113         try {
114             reader = new InputStreamReader JavaDoc(urlConnection.getInputStream());
115         } catch (IOException JavaDoc e) {
116             throw new DocumentParserException("Cannot build an input stream reader on URL '" + url + "'", e);
117         }
118
119         InputSource JavaDoc inputSource = new InputSource JavaDoc(reader);
120         Document JavaDoc document = null;
121         try {
122             document = builder.parse(inputSource);
123         } catch (SAXException JavaDoc e) {
124             throw new DocumentParserException("Cannot parse the XML file '" + url + "'.", e);
125         } catch (IOException JavaDoc e) {
126             throw new DocumentParserException("Cannot parse the XML file '" + url + "'.", e);
127         } finally {
128             // close InputStream when parsing is finished
129
try {
130                 reader.close();
131             } catch (IOException JavaDoc e) {
132                 throw new DocumentParserException("Cannot close the inputsource of the XML file'" + url + "'.", e);
133             }
134         }
135
136         return document;
137     }
138
139 }
140
Popular Tags