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.view.save;
029:
030: import java.io.File;
031: import java.text.MessageFormat;
032:
033: import javax.swing.JOptionPane;
034:
035: import net.sf.jasperreports.engine.JRException;
036: import net.sf.jasperreports.engine.JRExporterParameter;
037: import net.sf.jasperreports.engine.JasperPrint;
038: import net.sf.jasperreports.engine.export.JRXmlExporter;
039: import net.sf.jasperreports.engine.export.JRXmlExporterParameter;
040: import net.sf.jasperreports.view.JRSaveContributor;
041:
042: /**
043: * @author Teodor Danciu (teodord@users.sourceforge.net)
044: * @version $Id: JRXmlSaveContributor.java 1229 2006-04-19 10:27:35Z teodord $
045: */
046: public class JRXmlSaveContributor extends JRSaveContributor {
047:
048: /**
049: *
050: */
051: private static final String EXTENSION_XML = ".xml";
052: private static final String EXTENSION_JRPXML = ".jrpxml";
053: public static final JRXmlSaveContributor INSTANCE = new JRXmlSaveContributor();
054:
055: /**
056: *
057: */
058: public static JRXmlSaveContributor getInstance() {
059: return INSTANCE;
060: }
061:
062: /**
063: *
064: */
065: public boolean accept(File file) {
066: if (file.isDirectory()) {
067: return true;
068: }
069: String name = file.getName().toLowerCase();
070: return (name.endsWith(EXTENSION_XML) || name
071: .endsWith(EXTENSION_JRPXML));
072: }
073:
074: /**
075: *
076: */
077: public String getDescription() {
078: return "XML (*.jrpxml, *.xml)";
079: }
080:
081: /**
082: *
083: */
084: public void save(JasperPrint jasperPrint, File file)
085: throws JRException {
086: if (!file.getName().endsWith(EXTENSION_XML)
087: && !file.getName().endsWith(EXTENSION_JRPXML)) {
088: file = new File(file.getAbsolutePath() + EXTENSION_JRPXML);
089: }
090:
091: if (!file.exists()
092: || JOptionPane.OK_OPTION == JOptionPane
093: .showConfirmDialog(
094: null,
095: MessageFormat
096: .format(
097: java.util.ResourceBundle
098: .getBundle(
099: "net/sf/jasperreports/view/viewer")
100: .getString(
101: "file.exists"),
102: new Object[] { file
103: .getName() }),
104: java.util.ResourceBundle
105: .getBundle(
106: "net/sf/jasperreports/view/viewer")
107: .getString("save"),
108: JOptionPane.OK_CANCEL_OPTION)) {
109: JRXmlExporter exporter = new JRXmlExporter();
110: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
111: jasperPrint);
112: exporter
113: .setParameter(JRExporterParameter.OUTPUT_FILE, file);
114: exporter.setParameter(
115: JRXmlExporterParameter.IS_EMBEDDING_IMAGES,
116: Boolean.FALSE);
117: exporter.exportReport();
118: }
119: }
120:
121: }
|