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:
029: /*
030: * Contributors:
031: * Artur Biesiadowski - abies@users.sourceforge.net
032: */
033: package net.sf.jasperreports.engine.xml;
034:
035: import java.io.BufferedInputStream;
036: import java.io.ByteArrayInputStream;
037: import java.io.File;
038: import java.io.FileInputStream;
039: import java.io.FileNotFoundException;
040: import java.io.IOException;
041: import java.io.InputStream;
042: import java.net.URL;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046: import org.xml.sax.SAXException;
047:
048: import net.sf.jasperreports.engine.JRException;
049: import net.sf.jasperreports.engine.JRRuntimeException;
050: import net.sf.jasperreports.engine.JRTemplate;
051: import net.sf.jasperreports.engine.util.JRLoader;
052:
053: /**
054: * Utility class that loads {@link JRTemplate templates} from XML representations.
055: *
056: * @author Lucian Chirita (lucianc@users.sourceforge.net)
057: * @version $Id: JRXmlTemplateLoader.java 1763 2007-06-21 08:47:21Z lucianc $
058: */
059: public class JRXmlTemplateLoader {
060:
061: private static final Log log = LogFactory
062: .getLog(JRXmlTemplateLoader.class);
063:
064: protected JRXmlTemplateLoader() {
065: }
066:
067: /**
068: * Parses a template XML found at a specified location into a {@link JRTemplate template object}.
069: *
070: * @param location the template XML location.
071: * Can be a URL, a file path or a classloader resource name.
072: * @return the template object
073: * @throws JRException when the location cannot be resolved or read
074: * @see JRLoader#loadBytesFromLocation(String)
075: */
076: public static JRTemplate load(String location) throws JRException {
077: byte[] data = JRLoader.loadBytesFromLocation(location);
078: return load(new ByteArrayInputStream(data));
079: }
080:
081: /**
082: * Parses a template XML file into a {@link JRTemplate template object}.
083: *
084: * @param file the template XML file
085: * @return the template object
086: */
087: public static JRTemplate load(File file) {
088: BufferedInputStream fileIn;
089: try {
090: fileIn = new BufferedInputStream(new FileInputStream(file));
091: } catch (FileNotFoundException e) {
092: throw new JRRuntimeException("Template XML file not found",
093: e);
094: }
095:
096: try {
097: return load(fileIn);
098: } finally {
099: try {
100: fileIn.close();
101: } catch (IOException e) {
102: log.warn("Error closing XML file", e);
103: }
104: }
105: }
106:
107: /**
108: * Parses a template XML located at a URL into a {@link JRTemplate template object}.
109: *
110: * @param url the location of the template XML
111: * @return the template object
112: */
113: public static JRTemplate load(URL url) {
114: InputStream input;
115: try {
116: input = url.openStream();
117: } catch (IOException e) {
118: throw new JRRuntimeException(
119: "Error opening connection to template URL " + url,
120: e);
121: }
122:
123: try {
124: return load(input);
125: } finally {
126: try {
127: input.close();
128: } catch (IOException e) {
129: log.warn("Error closing connection to template URL "
130: + url, e);
131: }
132: }
133: }
134:
135: /**
136: * Parses a template XML data stream into a {@link JRTemplate template object}.
137: *
138: * @param data the data stream
139: * @return the template object
140: */
141: public static JRTemplate load(InputStream data) {
142: JRXmlTemplateLoader loader = new JRXmlTemplateLoader();
143: return loader.loadTemplate(data);
144: }
145:
146: protected JRTemplate loadTemplate(InputStream data) {
147: JRXmlDigester digester = JRXmlTemplateDigesterFactory
148: .instance().createDigester();
149: try {
150: JRTemplate template = (JRTemplate) digester.parse(data);
151: return template;
152: } catch (IOException e) {
153: throw new JRRuntimeException("Error reading template XML",
154: e);
155: } catch (SAXException e) {
156: throw new JRRuntimeException("Error parsing template XML",
157: e);
158: }
159: }
160:
161: }
|