1
/*FreeMind - A Program for creating and viewing Mindmaps
2
*Copyright (C) 2000-2006 Joerg Mueller, Daniel Polansky, Christian Foltin, Dimitri Polivaev and others.
4
*See COPYING for Details
6
*This program is free software; you can redistribute it and/or
7
*modify it under the terms of the GNU General Public License
8
*as published by the Free Software Foundation; either version 2
9
*of the License, or (at your option) any later version.
11
*This program is distributed in the hope that it will be useful,
12
*but WITHOUT ANY WARRANTY; without even the implied warranty of
13
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
*GNU General Public License for more details.
16
*You should have received a copy of the GNU General Public License
17
*along with this program; if not, write to the Free Software
18
*Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
* Created on 28.03.2004
24
package accessories.plugins;
26
import java.io.ByteArrayOutputStream;
28
import java.io.FileInputStream;
29
import java.io.FileOutputStream;
30
import java.io.IOException;
31
import java.io.InputStream;
32
import java.io.OutputStream;
33
import java.io.StringReader;
34
import java.io.StringWriter;
36
import java.text.DateFormat;
37
import java.util.Date;
38
import java.util.Properties;
39
import java.util.StringTokenizer;
40
import java.util.zip.ZipEntry;
41
import java.util.zip.ZipOutputStream;
43
import javax.xml.transform.Result;
44
import javax.xml.transform.Source;
45
import javax.xml.transform.Transformer;
46
import javax.xml.transform.TransformerFactory;
47
import javax.xml.transform.stream.StreamResult;
48
import javax.xml.transform.stream.StreamSource;
50
import freemind.extensions.ExportHook;
51
import freemind.main.FreeMind;
52
import freemind.main.FreeMindStarter;
53
import freemind.main.Resources;
54
import freemind.main.Tools;
60
public class ExportToOoWriter extends ExportHook {
61
private static java.util.logging.Logger logger = null;
66
public ExportToOoWriter() {
69
logger = freemind.main.Resources.getInstance().getLogger(
70
this.getClass().getName());
77
* @see freemind.extensions.MindMapHook#startupMapHook()
79
public void startupMapHook() {
80
super.startupMapHook();
81
File chosenFile = chooseFile(getResourceString("file_type"),
82
null /*getTranslatableResourceString("file_description")*/, null);
83
if (chosenFile == null) {
86
getController().getFrame().setWaitingCursor(true);
88
exportToOoWriter(chosenFile);
89
} catch (IOException e) {
90
freemind.main.Resources.getInstance().logException(e);
92
getController().getFrame().setWaitingCursor(false);
95
public boolean exportToOoWriter(File chosenFile) throws IOException {
97
StringWriter writer = new StringWriter();
99
getController().getMap().getFilteredXml(writer);
100
String xslts = getResourceString("files");
101
return exportToOoWriter(chosenFile, writer, xslts);
105
* @return true, if successful.
107
private boolean applyXsltFile(String xsltFileName, StringWriter writer,
108
Result result) throws IOException {
109
URL xsltUrl = getResource(xsltFileName);
110
if (xsltUrl == null) {
111
logger.severe("Can't find " + xsltFileName + " as resource.");
112
throw new IllegalArgumentException("Can't find " + xsltFileName
115
InputStream xsltStream = xsltUrl.openStream();
116
// System.out.println("set xsl");
117
Source xsltSource = new StreamSource(xsltStream);
119
// create an instance of TransformerFactory
121
StringReader reader = new StringReader(writer.getBuffer()
124
// System.out.println("make transform instance");
125
TransformerFactory transFact = TransformerFactory.newInstance();
127
Transformer trans = transFact.newTransformer(xsltSource);
128
trans.setParameter("date", DateFormat.getDateInstance(
129
DateFormat.SHORT).format(new Date()));
131
trans.transform(new StreamSource(reader), result);
133
} catch (Exception e) {
134
// System.err.println("error applying the xslt file "+e);
135
freemind.main.Resources.getInstance().logException(e);
140
public boolean exportToOoWriter(File file, StringWriter writer, String xslts) throws IOException {
141
boolean resultValue = true;
142
ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(file));
144
Result result = new StreamResult(zipout);
145
StringTokenizer tokenizer = new StringTokenizer(xslts, ",");
146
while (tokenizer.hasMoreTokens()) {
147
String token = tokenizer.nextToken();
148
String[] files = token.split("->");
149
if (files.length == 2) {
150
ZipEntry entry = new ZipEntry(files[1]);
151
zipout.putNextEntry(entry);
152
if(files[0].endsWith(".xsl")) {
153
logger.info("Transforming with xslt " + files[0] + " to file " + files[1]);
154
resultValue &= applyXsltFile(files[0], writer, result);
156
logger.info("Copying resource from " + files[0] + " to file " + files[1]);
157
resultValue &= copyFromResource(files[0], zipout);
167
* @return true, if successful.
169
private boolean copyFromResource(String fileName, OutputStream out) {
170
// adapted from http://javaalmanac.com/egs/java.io/CopyFile.html
171
// Copies src file to dst file.
172
// If the dst file does not exist, it is created
174
logger.finest("searching for " + fileName);
175
URL resource = getResource( fileName);
177
logger.severe("Cannot find resource: "+ fileName);
180
InputStream in = resource.openStream();
181
Tools.copyStream(in, out, false);
183
} catch (Exception e) {
184
logger.severe("File not found or could not be copied. " +
185
"Was earching for " + fileName + " and should go to "+out);
186
freemind.main.Resources.getInstance().logException(e);