001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.util;
029:
030: import java.io.File;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.net.URL;
034:
035: import javax.xml.parsers.DocumentBuilder;
036: import javax.xml.parsers.DocumentBuilderFactory;
037: import javax.xml.parsers.ParserConfigurationException;
038:
039: import net.sf.jasperreports.engine.JRException;
040:
041: import org.apache.commons.logging.Log;
042: import org.apache.commons.logging.LogFactory;
043: import org.w3c.dom.Document;
044: import org.w3c.dom.Node;
045: import org.xml.sax.InputSource;
046: import org.xml.sax.SAXException;
047:
048: /**
049: * XML parsing utilities.
050: *
051: * @author Lucian Chirita (lucianc@users.sourceforge.net)
052: * @version $Id: JRXmlUtils.java 1506 2006-11-23 15:10:30Z lucianc $
053: */
054: public class JRXmlUtils {
055: private static final Log log = LogFactory.getLog(JRXmlUtils.class);
056:
057: /**
058: * Parses an input source into a document.
059: *
060: * @param is the input source
061: * @return the parsed document
062: * @throws JRException
063: */
064: public static Document parse(InputSource is) throws JRException {
065: try {
066: return createDocumentBuilder().parse(is);
067: } catch (SAXException e) {
068: throw new JRException("Failed to parse the xml document", e);
069: } catch (IOException e) {
070: throw new JRException("Failed to parse the xml document", e);
071: }
072: }
073:
074: /**
075: * Parses a document specified by an URI.
076: *
077: * @param uri the URI
078: * @return the parsed document
079: * @throws JRException
080: */
081: public static Document parse(String uri) throws JRException {
082: return parse(new InputSource(uri));
083: }
084:
085: /**
086: * Parses a file into a document.
087: *
088: * @param file the XML file
089: * @return the document
090: * @throws JRException
091: */
092: public static Document parse(File file) throws JRException {
093: try {
094: return createDocumentBuilder().parse(file);
095: } catch (SAXException e) {
096: throw new JRException("Failed to parse the xmlf document",
097: e);
098: } catch (IOException e) {
099: throw new JRException("Failed to parse the xml document", e);
100: }
101: }
102:
103: /**
104: * Parses an input stream into a XML document.
105: *
106: * @param is the input stream
107: * @return the document
108: * @throws JRException
109: */
110: public static Document parse(InputStream is) throws JRException {
111: return parse(new InputSource(is));
112: }
113:
114: /**
115: * Parses an URL stream as a XML document.
116: *
117: * @param url the URL
118: * @return the document
119: * @throws JRException
120: */
121: public static Document parse(URL url) throws JRException {
122: InputStream is = null;
123: try {
124: is = url.openStream();
125: return createDocumentBuilder().parse(is);
126: } catch (SAXException e) {
127: throw new JRException("Failed to parse the xmlf document",
128: e);
129: } catch (IOException e) {
130: throw new JRException("Failed to parse the xml document", e);
131: } finally {
132: if (is != null) {
133: try {
134: is.close();
135: } catch (IOException e) {
136: log.warn("Error closing stream of URL " + url, e);
137: }
138: }
139: }
140: }
141:
142: /**
143: * Creates a XML document builder.
144: *
145: * @return a XML document builder
146: * @throws JRException
147: */
148: public static DocumentBuilder createDocumentBuilder()
149: throws JRException {
150: DocumentBuilderFactory dbf = DocumentBuilderFactory
151: .newInstance();
152: dbf.setValidating(false);
153: dbf.setIgnoringComments(true);
154:
155: try {
156: return dbf.newDocumentBuilder();
157: } catch (ParserConfigurationException e) {
158: throw new JRException(
159: "Failed to create a document builder factory", e);
160: }
161: }
162:
163: /**
164: * Creates a document having a node as root.
165: *
166: * @param sourceNode the node
167: * @return a document having the specified node as root
168: * @throws JRException
169: */
170: public static Document createDocument(Node sourceNode)
171: throws JRException {
172: Document doc = JRXmlUtils.createDocumentBuilder().newDocument();
173: Node source;
174: if (sourceNode.getNodeType() == Node.DOCUMENT_NODE) {
175: source = ((Document) sourceNode).getDocumentElement();
176: } else {
177: source = sourceNode;
178: }
179:
180: Node node = doc.importNode(source, true);
181: doc.appendChild(node);
182:
183: return doc;
184: }
185: }
|