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.xml;
029:
030: import java.io.BufferedOutputStream;
031: import java.io.FileNotFoundException;
032: import java.io.FileOutputStream;
033: import java.io.IOException;
034: import java.io.OutputStream;
035: import java.io.OutputStreamWriter;
036: import java.io.StringWriter;
037: import java.io.UnsupportedEncodingException;
038: import java.io.Writer;
039:
040: import net.sf.jasperreports.engine.JRRuntimeException;
041: import net.sf.jasperreports.engine.JRStyle;
042: import net.sf.jasperreports.engine.JRTemplate;
043: import net.sf.jasperreports.engine.JRTemplateReference;
044: import net.sf.jasperreports.engine.util.JRXmlWriteHelper;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048:
049: /**
050: * {@link JRTemplate Template} XML serializer.
051: *
052: * @author Lucian Chirita (lucianc@users.sourceforge.net)
053: * @version $Id: JRXmlTemplateWriter.java 1763 2007-06-21 08:47:21Z lucianc $
054: * @see JRXmlTemplateLoader
055: */
056: public class JRXmlTemplateWriter extends JRXmlBaseWriter {
057:
058: private static final Log log = LogFactory
059: .getLog(JRXmlTemplateWriter.class);
060:
061: /**
062: * Default XML output encoding.
063: */
064: public static final String DEFAULT_ENCODING = "UTF-8";
065:
066: /**
067: * Returns the XML representation of a template.
068: * <p/>
069: * Uses {@link #DEFAULT_ENCODING the default encoding}.
070: *
071: * @param template the template
072: * @return the XML representation of the template
073: */
074: public static String writeTemplate(JRTemplate template) {
075: return writeTemplate(template, DEFAULT_ENCODING);
076: }
077:
078: /**
079: * Returns the XML representation of a template.
080: *
081: * @param template the template
082: * @param encoding the XML encoding to use
083: * @return the XML representation of the template
084: */
085: public static String writeTemplate(JRTemplate template,
086: String encoding) {
087: StringWriter stringOut = new StringWriter();
088: try {
089: writeTemplate(template, stringOut, encoding);
090: } catch (IOException e) {
091: throw new JRRuntimeException(e);
092: }
093: return stringOut.toString();
094: }
095:
096: /**
097: * Writes the XML representation of a template to an output stream.
098: * <p/>
099: * Uses {@link #DEFAULT_ENCODING the default encoding}.
100: *
101: * @param template the template
102: * @param out the output stream
103: */
104: public static void writeTemplate(JRTemplate template,
105: OutputStream out) {
106: writeTemplate(template, out, DEFAULT_ENCODING);
107: }
108:
109: /**
110: * Writes the XML representation of a template to an output stream.
111: *
112: * @param template the template
113: * @param out the output stream
114: * @param encoding the XML encoding to use
115: */
116: public static void writeTemplate(JRTemplate template,
117: OutputStream out, String encoding) {
118: try {
119: Writer writer = new OutputStreamWriter(out, encoding);
120: writeTemplate(template, writer, encoding);
121: } catch (UnsupportedEncodingException e) {
122: throw new JRRuntimeException(e);
123: } catch (IOException e) {
124: throw new JRRuntimeException(e);
125: }
126: }
127:
128: /**
129: * Writes the XML representation of a template to a file.
130: * <p/>
131: * Uses {@link #DEFAULT_ENCODING the default encoding}.
132: *
133: * @param template the template
134: * @param outputFile the output file name
135: */
136: public static void writeTemplateToFile(JRTemplate template,
137: String outputFile) {
138: writeTemplateToFile(template, outputFile, DEFAULT_ENCODING);
139: }
140:
141: /**
142: * Writes the XML representation of a template to a file.
143: *
144: * @param template the template
145: * @param outputFile the output file name
146: * @param encoding the XML encoding to use
147: */
148: public static void writeTemplateToFile(JRTemplate template,
149: String outputFile, String encoding) {
150: BufferedOutputStream fileOut = null;
151: boolean close = true;
152: try {
153: fileOut = new BufferedOutputStream(new FileOutputStream(
154: outputFile));
155: writeTemplate(template, fileOut, encoding);
156:
157: fileOut.flush();
158: close = false;
159: fileOut.close();
160: } catch (FileNotFoundException e) {
161: throw new JRRuntimeException(e);
162: } catch (IOException e) {
163: throw new JRRuntimeException(e);
164: } finally {
165: if (fileOut != null && close) {
166: try {
167: fileOut.close();
168: } catch (IOException e) {
169: log.warn("Could not close file " + outputFile, e);
170: }
171: }
172: }
173: }
174:
175: protected static void writeTemplate(JRTemplate template,
176: Writer out, String encoding) throws IOException {
177: JRXmlTemplateWriter writer = new JRXmlTemplateWriter(template,
178: out, encoding);
179: writer.write();
180: out.flush();
181: }
182:
183: private final JRTemplate template;
184: private final String encoding;
185:
186: /**
187: * Creates an XML template writer.
188: *
189: * @param template the template to write
190: * @param out the ouput writer
191: * @param encoding the XML encoding to use
192: */
193: public JRXmlTemplateWriter(JRTemplate template, Writer out,
194: String encoding) {
195: this .template = template;
196: this .encoding = encoding;
197: useWriter(new JRXmlWriteHelper(out));
198: }
199:
200: /**
201: * Writes the template to the output writer.
202: *
203: * @throws IOException
204: */
205: public void write() throws IOException {
206: writer.writeProlog(encoding);
207: writer.writePublicDoctype(JRXmlConstants.TEMPLATE_ELEMENT_ROOT,
208: JRXmlConstants.JASPERTEMPLATE_PUBLIC_ID,
209: JRXmlConstants.JASPERTEMPLATE_SYSTEM_ID);
210:
211: writer.startElement(JRXmlConstants.TEMPLATE_ELEMENT_ROOT);
212: writeIncludedTemplates();
213: writeStyles();
214: writer.closeElement();
215: }
216:
217: protected void writeIncludedTemplates() throws IOException {
218: JRTemplateReference[] includedTemplates = template
219: .getIncludedTemplates();
220: if (includedTemplates != null) {
221: for (int i = 0; i < includedTemplates.length; i++) {
222: JRTemplateReference reference = includedTemplates[i];
223: writeIncludedTemplate(reference);
224: }
225: }
226: }
227:
228: protected void writeIncludedTemplate(JRTemplateReference reference)
229: throws IOException {
230: writer.writeCDATAElement(
231: JRXmlConstants.TEMPLATE_ELEMENT_INCLUDED_TEMPLATE,
232: reference.getLocation());
233:
234: }
235:
236: protected void writeStyles() throws IOException {
237: JRStyle[] styles = template.getStyles();
238: if (styles != null) {
239: for (int i = 0; i < styles.length; i++) {
240: JRStyle style = styles[i];
241: writeStyle(style);
242: }
243: }
244: }
245:
246: protected boolean toWriteConditionalStyles() {
247: return false;
248: }
249: }
|