KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > util > JRXmlUtils


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.util;
29
30 import java.io.File JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.net.URL JavaDoc;
34
35 import javax.xml.parsers.DocumentBuilder JavaDoc;
36 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38
39 import net.sf.jasperreports.engine.JRException;
40
41 import org.apache.commons.logging.Log;
42 import org.apache.commons.logging.LogFactory;
43 import org.w3c.dom.Document JavaDoc;
44 import org.w3c.dom.Node JavaDoc;
45 import org.xml.sax.InputSource JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
47
48 /**
49  * XML parsing utilities.
50  *
51  * @author Lucian Chirita (lucianc@users.sourceforge.net)
52  * @version $Id: JRXmlUtils.java 1506 2006-11-23 17:10:30 +0200 (Thu, 23 Nov 2006) lucianc $
53  */

54 public class JRXmlUtils
55 {
56     private static final Log log = LogFactory.getLog(JRXmlUtils.class);
57     
58     
59     /**
60      * Parses an input source into a document.
61      *
62      * @param is the input source
63      * @return the parsed document
64      * @throws JRException
65      */

66     public static Document JavaDoc parse(InputSource JavaDoc is) throws JRException
67     {
68         try
69         {
70             return createDocumentBuilder().parse(is);
71         }
72         catch (SAXException JavaDoc e)
73         {
74             throw new JRException("Failed to parse the xml document", e);
75         }
76         catch (IOException JavaDoc e)
77         {
78             throw new JRException("Failed to parse the xml document", e);
79         }
80     }
81     
82     
83     /**
84      * Parses a document specified by an URI.
85      *
86      * @param uri the URI
87      * @return the parsed document
88      * @throws JRException
89      */

90     public static Document JavaDoc parse(String JavaDoc uri) throws JRException
91     {
92         return parse(new InputSource JavaDoc(uri));
93     }
94
95     
96     /**
97      * Parses a file into a document.
98      *
99      * @param file the XML file
100      * @return the document
101      * @throws JRException
102      */

103     public static Document JavaDoc parse(File JavaDoc file) throws JRException
104     {
105         try
106         {
107             return createDocumentBuilder().parse(file);
108         }
109         catch (SAXException JavaDoc e)
110         {
111             throw new JRException("Failed to parse the xmlf document", e);
112         }
113         catch (IOException JavaDoc e)
114         {
115             throw new JRException("Failed to parse the xml document", e);
116         }
117     }
118
119     
120     /**
121      * Parses an input stream into a XML document.
122      *
123      * @param is the input stream
124      * @return the document
125      * @throws JRException
126      */

127     public static Document JavaDoc parse(InputStream JavaDoc is) throws JRException
128     {
129         return parse(new InputSource JavaDoc(is));
130     }
131
132     
133     /**
134      * Parses an URL stream as a XML document.
135      *
136      * @param url the URL
137      * @return the document
138      * @throws JRException
139      */

140     public static Document JavaDoc parse(URL JavaDoc url) throws JRException
141     {
142         InputStream JavaDoc is = null;
143         try
144         {
145             is = url.openStream();
146             return createDocumentBuilder().parse(is);
147         }
148         catch (SAXException JavaDoc e)
149         {
150             throw new JRException("Failed to parse the xmlf document", e);
151         }
152         catch (IOException JavaDoc e)
153         {
154             throw new JRException("Failed to parse the xml document", e);
155         }
156         finally
157         {
158             if (is != null)
159             {
160                 try
161                 {
162                     is.close();
163                 }
164                 catch (IOException JavaDoc e)
165                 {
166                     log.warn("Error closing stream of URL " + url, e);
167                 }
168             }
169         }
170     }
171
172     
173     /**
174      * Creates a XML document builder.
175      *
176      * @return a XML document builder
177      * @throws JRException
178      */

179     public static DocumentBuilder JavaDoc createDocumentBuilder() throws JRException
180     {
181         DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
182         dbf.setValidating(false);
183         dbf.setIgnoringComments(true);
184
185         try
186         {
187             return dbf.newDocumentBuilder();
188         }
189         catch (ParserConfigurationException JavaDoc e)
190         {
191             throw new JRException("Failed to create a document builder factory", e);
192         }
193     }
194
195     
196     /**
197      * Creates a document having a node as root.
198      *
199      * @param sourceNode the node
200      * @return a document having the specified node as root
201      * @throws JRException
202      */

203     public static Document JavaDoc createDocument(Node JavaDoc sourceNode) throws JRException
204     {
205         Document JavaDoc doc = JRXmlUtils.createDocumentBuilder().newDocument();
206         Node JavaDoc source;
207         if (sourceNode.getNodeType() == Node.DOCUMENT_NODE) {
208             source = ((Document JavaDoc) sourceNode).getDocumentElement();
209         } else {
210             source = sourceNode;
211         }
212
213         Node JavaDoc node = doc.importNode(source, true);
214         doc.appendChild(node);
215         
216         return doc;
217     }
218 }
219
Popular Tags