~ubuntu-branches/ubuntu/utopic/freemind/utopic

« back to all changes in this revision

Viewing changes to freemind/accessories/plugins/ExportToOoWriter.java

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Drung
  • Date: 2010-01-03 14:19:19 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20100103141919-m5az7dkicy21hqop
Tags: 0.9.0~rc6+dfsg-1ubuntu1
* Merge from Debian unstable (LP: #182927), remaining changes:
  - debian/copyright: add license/copyright for
    freemind/freemind/main/ExampleFileFilter.java

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*FreeMind - A Program for creating and viewing Mindmaps
 
2
 *Copyright (C) 2000-2006 Joerg Mueller, Daniel Polansky, Christian Foltin, Dimitri Polivaev and others.
 
3
 *
 
4
 *See COPYING for Details
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 */
 
20
/*
 
21
 * Created on 28.03.2004
 
22
 *
 
23
 */
 
24
package accessories.plugins;
 
25
 
 
26
import java.io.ByteArrayOutputStream;
 
27
import java.io.File;
 
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;
 
35
import java.net.URL;
 
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;
 
42
 
 
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;
 
49
 
 
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;
 
55
 
 
56
/**
 
57
 * @author foltin
 
58
 * 
 
59
 */
 
60
public class ExportToOoWriter extends ExportHook {
 
61
        private static java.util.logging.Logger logger = null;
 
62
 
 
63
        /**
 
64
         * 
 
65
         */
 
66
        public ExportToOoWriter() {
 
67
                super();
 
68
                if (logger == null) {
 
69
                        logger = freemind.main.Resources.getInstance().getLogger(
 
70
                                        this.getClass().getName());
 
71
                }
 
72
        }
 
73
 
 
74
        /*
 
75
         * (non-Javadoc)
 
76
         * 
 
77
         * @see freemind.extensions.MindMapHook#startupMapHook()
 
78
         */
 
79
        public void startupMapHook() {
 
80
                super.startupMapHook();
 
81
                File chosenFile = chooseFile(getResourceString("file_type"),
 
82
                                null /*getTranslatableResourceString("file_description")*/, null);
 
83
                if (chosenFile == null) {
 
84
                        return;
 
85
                }
 
86
                getController().getFrame().setWaitingCursor(true);
 
87
                try {
 
88
                        exportToOoWriter(chosenFile);
 
89
                } catch (IOException e) {
 
90
                        freemind.main.Resources.getInstance().logException(e);
 
91
                }
 
92
                getController().getFrame().setWaitingCursor(false);
 
93
        }
 
94
 
 
95
        public boolean exportToOoWriter(File chosenFile) throws IOException {
 
96
                // get output:
 
97
                StringWriter writer = new StringWriter();
 
98
                // get XML
 
99
                getController().getMap().getFilteredXml(writer);
 
100
                String xslts = getResourceString("files");
 
101
                return exportToOoWriter(chosenFile, writer, xslts);
 
102
        }
 
103
 
 
104
        /**
 
105
         * @return true, if successful.
 
106
         */
 
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
 
113
                                        + " as resource.");
 
114
                }
 
115
                InputStream xsltStream = xsltUrl.openStream();
 
116
                // System.out.println("set xsl");
 
117
                Source xsltSource = new StreamSource(xsltStream);
 
118
 
 
119
                // create an instance of TransformerFactory
 
120
                try {
 
121
                        StringReader reader = new StringReader(writer.getBuffer()
 
122
                                        .toString());
 
123
 
 
124
                        // System.out.println("make transform instance");
 
125
                        TransformerFactory transFact = TransformerFactory.newInstance();
 
126
 
 
127
                        Transformer trans = transFact.newTransformer(xsltSource);
 
128
                trans.setParameter("date", DateFormat.getDateInstance(
 
129
                                        DateFormat.SHORT).format(new Date()));
 
130
                        
 
131
                        trans.transform(new StreamSource(reader), result);
 
132
                        return true;
 
133
                } catch (Exception e) {
 
134
                        // System.err.println("error applying the xslt file "+e);
 
135
                        freemind.main.Resources.getInstance().logException(e);
 
136
                        return false;
 
137
                }
 
138
        }
 
139
 
 
140
        public boolean exportToOoWriter(File file, StringWriter writer, String xslts) throws IOException {
 
141
                boolean resultValue = true;
 
142
                ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(file));
 
143
 
 
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);
 
155
                                } else {
 
156
                                        logger.info("Copying resource from " + files[0] + " to file " + files[1]);
 
157
                                        resultValue &= copyFromResource(files[0], zipout);
 
158
                                }
 
159
                                zipout.closeEntry();
 
160
                        }
 
161
                }
 
162
                zipout.close();
 
163
                return resultValue;
 
164
        }
 
165
        
 
166
    /**
 
167
     * @return true, if successful.
 
168
     */
 
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
 
173
            try {
 
174
                logger.finest("searching for "  + fileName);
 
175
                URL resource = getResource( fileName);
 
176
                if(resource==null){
 
177
                        logger.severe("Cannot find resource: "+ fileName);
 
178
                        return false;
 
179
                }
 
180
                InputStream in = resource.openStream();
 
181
                Tools.copyStream(in, out, false);
 
182
                return true;
 
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);
 
187
                return false;
 
188
            }
 
189
 
 
190
        
 
191
    }
 
192
 
 
193
}