Add comments and cleanup
[pspp] / src / output / odt.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19
20 /* A driver for creating OpenDocument Format text files from PSPP's output */
21
22 #include <libpspp/assertion.h>
23 #include <libpspp/start-date.h>
24 #include <libpspp/version.h>
25 #include <output/afm.h>
26 #include <output/chart-provider.h>
27 #include <output/manager.h>
28 #include <output/output.h>
29 #include <output/table.h>
30
31 #include <sys/stat.h>
32 #include <sys/types.h>
33
34 #include <libgen.h>
35
36 #include <libxml/xmlwriter.h>
37
38 #include <stdlib.h>
39
40 #include "error.h"
41 #include "intprops.h"
42 #include "minmax.h"
43 #include "xalloc.h"
44
45 #define _xml(X) (const xmlChar *)(X)
46
47 struct odt_driver_ext 
48 {
49   /* The name of the temporary directory used to construct the ODF */
50   char *dirname;
51
52   /* Writer for the content.xml file */
53   xmlTextWriterPtr content_wtr;
54
55   /* Writer fot the manifest.xml file */
56   xmlTextWriterPtr manifest_wtr;
57 };
58
59
60
61 /* Create the "mimetype" file needed by ODF */
62 static void
63 create_mimetype (const char *dirname)
64 {
65   FILE *fp;
66   struct string filename;
67   ds_init_cstr (&filename, dirname);
68   ds_put_cstr (&filename, "/mimetype");
69   fp = fopen (ds_cstr (&filename), "w");
70   ds_destroy (&filename);
71
72   assert (fp);
73   fprintf (fp, "application/vnd.oasis.opendocument.text");
74   fclose (fp);
75 }
76
77 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
78 static xmlTextWriterPtr
79 create_writer (const struct odt_driver_ext *driver, const char *filename)
80 {
81   char *copy = NULL;
82   xmlTextWriterPtr w;
83   struct string str;
84   ds_init_cstr (&str, driver->dirname);
85   ds_put_cstr (&str, "/");
86   ds_put_cstr (&str, filename);
87
88   /* dirname modifies its argument, so we must copy it */
89   copy = xstrdup (ds_cstr (&str));
90   mkdir (dirname (copy), 0700);
91   free (copy);
92
93   w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
94
95   ds_destroy (&str);
96
97   xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
98
99   return w;
100 }
101
102 static bool
103 odt_open_driver (const char *name, int types, struct substring option_string)
104 {
105   struct odt_driver_ext *x;
106   struct outp_driver *this = outp_allocate_driver (&odt_class, name, types);
107
108   this->ext = x = xmalloc (sizeof *x);
109
110   outp_register_driver (this);
111
112   x->dirname = xstrdup ("odt-XXXXXX");
113   mkdtemp (x->dirname);
114
115   create_mimetype (x->dirname);
116
117   /* Create the manifest */
118   x->manifest_wtr = create_writer (x, "META-INF/manifest.xml");
119
120   xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:manifest"));
121   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("xmlns:manifest"),
122                                _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
123
124
125   /* Add a manifest entry for the document as a whole */
126   xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:file-entry"));
127   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:media-type"),  _xml("application/vnd.oasis.opendocument.text"));
128   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:full-path"),  _xml("/"));
129   xmlTextWriterEndElement (x->manifest_wtr);
130
131
132   x->content_wtr = create_writer (x, "content.xml");
133
134   /* Add a manifest entry for content.xml */
135   xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:file-entry"));
136   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:media-type"),  _xml("text/xml"));
137   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:full-path"),  _xml("content.xml"));
138   xmlTextWriterEndElement (x->manifest_wtr);
139
140
141   /* Some necessary junk at the start */
142   xmlTextWriterStartElement (x->content_wtr, _xml("office:document-content"));
143   xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:office"),
144                                _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
145
146   xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:text"),
147                                _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
148
149   xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:table"),
150                                _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
151
152   xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:version"), _xml("1.1"));
153
154   xmlTextWriterStartElement (x->content_wtr, _xml("office:body"));
155   xmlTextWriterStartElement (x->content_wtr, _xml("office:text"));
156
157
158
159   /* Close the manifest */
160   xmlTextWriterEndElement (x->manifest_wtr);
161   xmlTextWriterEndDocument (x->manifest_wtr);
162   xmlFreeTextWriter (x->manifest_wtr);
163
164   return true;
165 }
166
167 static bool
168 odt_close_driver (struct outp_driver *this)
169 {
170   struct string zip_cmd;
171   struct string rm_cmd;
172   struct odt_driver_ext *x = this->ext;
173
174   xmlTextWriterEndElement (x->content_wtr); /* office:text */
175   xmlTextWriterEndElement (x->content_wtr); /* office:body */
176   xmlTextWriterEndElement (x->content_wtr); /* office:document-content */
177
178   xmlTextWriterEndDocument (x->content_wtr);
179   xmlFreeTextWriter (x->content_wtr);
180
181   /* Zip up the directory */
182   ds_init_empty (&zip_cmd);
183   ds_put_format (&zip_cmd, "cd %s ; zip -r ../pspp.odt . > /dev/null", x->dirname);
184   system (ds_cstr (&zip_cmd));
185   ds_destroy (&zip_cmd);
186
187   /* Remove the temp dir */
188   ds_init_empty (&rm_cmd);
189   ds_put_format (&rm_cmd, "rm -r %s", x->dirname);
190   system (ds_cstr (&rm_cmd));
191   ds_destroy (&rm_cmd);
192
193   free (x->dirname);
194   free (x);
195
196   return true;
197 }
198
199 static void
200 odt_open_page (struct outp_driver *this)
201 {
202 }
203
204 static void
205 odt_close_page (struct outp_driver *this)
206 {
207 }
208
209 static void
210 odt_output_chart (struct outp_driver *this, const struct chart *chart)
211 {
212  printf ("%s\n", __FUNCTION__);
213 }
214
215
216 /* Submit a table to the ODT driver */
217 static void
218 odt_submit (struct outp_driver *this, struct som_entity *e)
219 {
220   int r, c;
221   
222   struct odt_driver_ext *x = this->ext;
223   struct tab_table *tab = e->ext;
224
225
226   /* Write a heading for the table */
227   xmlTextWriterStartElement (x->content_wtr, _xml("text:h"));
228   xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("text:level"), "%d", e->subtable_num == 1 ? 2 : 3);
229   xmlTextWriterWriteString (x->content_wtr, _xml (tab->title) );
230   xmlTextWriterEndElement (x->content_wtr);
231
232   /* Start table */
233   xmlTextWriterStartElement (x->content_wtr, _xml("table:table"));
234   xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:name"), 
235                                      "TABLE-%d.%d", e->table_num, e->subtable_num);
236
237
238   /* Start column definitions */
239   xmlTextWriterStartElement (x->content_wtr, _xml("table:table-column"));
240   xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:number-columns-repeated"), "%d", tab->nc);
241   xmlTextWriterEndElement (x->content_wtr);
242
243
244   /* Deal with row headers */
245   if ( tab->t > 0)
246     xmlTextWriterStartElement (x->content_wtr, _xml("table:table-header-rows"));
247     
248
249   /* Write all the rows */
250   for (r = 0 ; r < tab->nr; ++r)
251     {
252       /* Start row definition */
253       xmlTextWriterStartElement (x->content_wtr, _xml("table:table-row"));
254
255       /* Write all the columns */
256       for (c = 0 ; c < tab->nc ; ++c)
257         {
258           int opts = tab->ct[tab->nc * r + c];
259           xmlTextWriterStartElement (x->content_wtr, _xml("table:table-cell"));
260           xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:value-type"), _xml("string"));
261
262           if (! (opts & TAB_EMPTY) ) 
263             {
264               char *s = ss_xstrdup (tab->cc[tab->nc * r + c]);
265               xmlTextWriterStartElement (x->content_wtr, _xml("text:p"));
266               if ( r < tab->t || c < tab->l )
267                 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
268               else
269                 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
270
271               xmlTextWriterWriteString (x->content_wtr, _xml (s));
272           
273               xmlTextWriterEndElement (x->content_wtr);
274               free (s);
275             }
276           xmlTextWriterEndElement (x->content_wtr);
277         }
278   
279       xmlTextWriterEndElement (x->content_wtr); /* row */
280
281       if ( tab->t > 0 && r == tab->t - 1)
282         xmlTextWriterEndElement (x->content_wtr); /* table-header-rows */
283     }
284
285   xmlTextWriterEndElement (x->content_wtr); /* table */
286 }
287
288
289 /* ODT driver class. */
290 const struct outp_class odt_class =
291 {
292   "odf",
293   1,
294
295   odt_open_driver,
296   odt_close_driver,
297
298   odt_open_page,
299   odt_close_page,
300   NULL,
301
302   odt_output_chart,
303   odt_submit,
304
305   NULL,
306   NULL,
307   NULL,
308 };