001: /**
002: * EasyBeans
003: * Copyright (C) 2006-2007 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: DocumentParser.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.util.xml;
025:
026: import java.io.IOException;
027: import java.io.InputStreamReader;
028: import java.io.Reader;
029: import java.net.URL;
030: import java.net.URLConnection;
031:
032: import javax.xml.parsers.DocumentBuilder;
033: import javax.xml.parsers.DocumentBuilderFactory;
034: import javax.xml.parsers.ParserConfigurationException;
035:
036: import org.w3c.dom.Document;
037: import org.xml.sax.EntityResolver;
038: import org.xml.sax.InputSource;
039: import org.xml.sax.SAXException;
040:
041: /**
042: * Allows to parse an xml file.
043: * @author Florent Benoit
044: */
045: public final class DocumentParser {
046:
047: /**
048: * Utility class.
049: */
050: private DocumentParser() {
051:
052: }
053:
054: /**
055: * Builds a new Document for a given xml file.
056: * @param url the URL of the the XML file.
057: * @param isValidating validate or not the xml file ?
058: * @param entityResolver the entityResolver used to validate document (if
059: * validating = true)
060: * @throws DocumentParserException if creating of builder fails or parsing
061: * fails.
062: * @return an application object.
063: */
064: public static Document getDocument(final URL url,
065: final boolean isValidating,
066: final EntityResolver entityResolver)
067: throws DocumentParserException {
068: // build factory
069: DocumentBuilderFactory factory = DocumentBuilderFactory
070: .newInstance();
071:
072: // XML files use schemas.
073: factory.setNamespaceAware(true);
074: factory.setValidating(isValidating);
075:
076: // ignore white space can only be set if parser is validating
077: if (isValidating) {
078: factory.setIgnoringElementContentWhitespace(true);
079: factory.setAttribute(
080: "http://apache.org/xml/features/validation/schema",
081: Boolean.valueOf(isValidating));
082: factory
083: .setAttribute(
084: "http://apache.org/xml/features/validation/schema-full-checking",
085: Boolean.valueOf(true));
086: }
087:
088: // Add schema location
089: //TODO: FIXME: set other schemas ?
090: if (isValidating) {
091: factory
092: .setAttribute(
093: "http://apache.org/xml/properties/schema/external-schemaLocation",
094: "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd");
095: }
096:
097: // Build a document builder
098: DocumentBuilder builder = null;
099: try {
100: builder = factory.newDocumentBuilder();
101: } catch (ParserConfigurationException e) {
102: throw new DocumentParserException(
103: "Cannot build a document builder", e);
104: }
105:
106: // Error handler (throwing exceptions)
107: builder.setErrorHandler(new EasyBeansErrorHandler());
108:
109: // Dummy entity resolver if there is none
110: if (entityResolver == null) {
111: builder.setEntityResolver(new EmptyEntityResolver());
112: } else {
113: builder.setEntityResolver(entityResolver);
114: }
115:
116: // Parse document
117: URLConnection urlConnection = null;
118: try {
119: urlConnection = url.openConnection();
120: } catch (IOException e) {
121: throw new DocumentParserException(
122: "Cannot open a connection on URL '" + url + "'", e);
123: }
124: urlConnection.setDefaultUseCaches(false);
125: Reader reader = null;
126: try {
127: reader = new InputStreamReader(urlConnection
128: .getInputStream());
129: } catch (IOException e) {
130: throw new DocumentParserException(
131: "Cannot build an input stream reader on URL '"
132: + url + "'", e);
133: }
134:
135: InputSource inputSource = new InputSource(reader);
136: Document document = null;
137: try {
138: document = builder.parse(inputSource);
139: } catch (SAXException e) {
140: throw new DocumentParserException(
141: "Cannot parse the XML file '" + url + "'.", e);
142: } catch (IOException e) {
143: throw new DocumentParserException(
144: "Cannot parse the XML file '" + url + "'.", e);
145: } finally {
146: // close InputStream when parsing is finished
147: try {
148: reader.close();
149: } catch (IOException e) {
150: throw new DocumentParserException(
151: "Cannot close the inputsource of the XML file'"
152: + url + "'.", e);
153: }
154: }
155:
156: return document;
157: }
158:
159: }
|