odt: Change default output file name to "pspp.odt".
[pspp-builds.git] / 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 /* A driver for creating OpenDocument Format text files from PSPP's output */
20
21 #include <libpspp/assertion.h>
22 #include <libpspp/cast.h>
23 #include <libpspp/str.h>
24 #include <libpspp/version.h>
25 #include <output/driver-provider.h>
26 #include <output/options.h>
27 #include <output/tab.h>
28 #include <output/table-item.h>
29 #include <output/table-provider.h>
30 #include <output/text-item.h>
31
32 #include <libgen.h>
33 #include <libxml/xmlwriter.h>
34 #include <pwd.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <time.h>
38 #include <unistd.h>
39
40 #include "xalloc.h"
41 #include "error.h"
42
43 #include "gettext.h"
44 #define _(msgid) gettext (msgid)
45
46 #define _xml(X) (const xmlChar *)(X)
47
48 struct odt_driver
49 {
50   struct output_driver driver;
51
52   char *file_name;            /* Output file name. */
53   bool debug;
54
55   /* The name of the temporary directory used to construct the ODF */
56   char *dirname;
57
58   /* Writer for the content.xml file */
59   xmlTextWriterPtr content_wtr;
60
61   /* Writer fot the manifest.xml file */
62   xmlTextWriterPtr manifest_wtr;
63
64   /* Number of tables so far. */
65   int table_num;
66 };
67
68 static struct odt_driver *
69 odt_driver_cast (struct output_driver *driver)
70 {
71   assert (driver->class == &odt_class);
72   return UP_CAST (driver, struct odt_driver, driver);
73 }
74
75 /* Create the "mimetype" file needed by ODF */
76 static void
77 create_mimetype (const char *dirname)
78 {
79   FILE *fp;
80   struct string filename;
81   ds_init_cstr (&filename, dirname);
82   ds_put_cstr (&filename, "/mimetype");
83   fp = fopen (ds_cstr (&filename), "w");
84   ds_destroy (&filename);
85
86   assert (fp);
87   fprintf (fp, "application/vnd.oasis.opendocument.text");
88   fclose (fp);
89 }
90
91 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
92 static xmlTextWriterPtr
93 create_writer (const struct odt_driver *driver, const char *filename)
94 {
95   char *copy = NULL;
96   xmlTextWriterPtr w;
97   struct string str;
98   ds_init_cstr (&str, driver->dirname);
99   ds_put_cstr (&str, "/");
100   ds_put_cstr (&str, filename);
101
102   /* dirname modifies its argument, so we must copy it */
103   copy = xstrdup (ds_cstr (&str));
104   mkdir (dirname (copy), 0700);
105   free (copy);
106
107   w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
108
109   ds_destroy (&str);
110
111   xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
112
113   return w;
114 }
115
116
117 static void
118 register_file (struct odt_driver *odt, const char *filename)
119 {
120   assert (odt->manifest_wtr);
121   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
122   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("text/xml"));
123   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml (filename));
124   xmlTextWriterEndElement (odt->manifest_wtr);
125 }
126
127 static void
128 write_style_data (struct odt_driver *odt)
129 {
130   xmlTextWriterPtr w = create_writer (odt, "styles.xml");
131   register_file (odt, "styles.xml");
132
133   xmlTextWriterStartElement (w, _xml ("office:document-styles"));
134   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
135                                _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
136
137   xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
138                                _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
139
140   xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
141                                _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
142
143   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml ("1.1"));
144                                
145
146
147   xmlTextWriterStartElement (w, _xml ("office:styles"));
148
149
150   {
151     xmlTextWriterStartElement (w, _xml ("style:style"));
152     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
153                                  _xml ("Standard"));
154
155     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
156                                  _xml ("paragraph"));
157
158     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
159                                  _xml ("text"));
160
161     xmlTextWriterEndElement (w); /* style:style */
162   }
163
164   {
165     xmlTextWriterStartElement (w, _xml ("style:style"));
166     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
167                                  _xml ("Table_20_Contents"));
168
169     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
170                                  _xml ("Table Contents"));
171
172     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
173                                  _xml ("paragraph"));
174
175     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
176                                  _xml ("Standard"));
177
178     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
179                                  _xml ("extra"));
180
181     xmlTextWriterEndElement (w); /* style:style */
182   }
183
184   {
185     xmlTextWriterStartElement (w, _xml ("style:style"));
186     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
187                                  _xml ("Table_20_Heading"));
188
189     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
190                                  _xml ("Table Heading"));
191
192     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
193                                  _xml ("paragraph"));
194
195     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
196                                  _xml ("Table_20_Contents"));
197
198     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
199                                  _xml ("extra"));
200
201
202     xmlTextWriterStartElement (w, _xml ("style:text-properties"));
203     xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
204     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
205     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
206     xmlTextWriterEndElement (w); /* style:text-properties */
207
208     xmlTextWriterEndElement (w); /* style:style */
209   }
210
211
212   xmlTextWriterEndElement (w); /* office:styles */
213   xmlTextWriterEndElement (w); /* office:document-styles */
214
215   xmlTextWriterEndDocument (w);
216   xmlFreeTextWriter (w);
217 }
218
219 static void
220 write_meta_data (struct odt_driver *odt)
221 {
222   xmlTextWriterPtr w = create_writer (odt, "meta.xml");
223   register_file (odt, "meta.xml");
224
225   xmlTextWriterStartElement (w, _xml ("office:document-meta"));
226   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
227   xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"),  _xml ("http://purl.org/dc/elements/1.1/"));
228   xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
229   xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
230   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml("1.1"));
231
232   xmlTextWriterStartElement (w, _xml ("office:meta"));
233   {
234     xmlTextWriterStartElement (w, _xml ("meta:generator"));
235     xmlTextWriterWriteString (w, _xml (stat_version));
236     xmlTextWriterEndElement (w);
237   }
238
239
240   {
241     char buf[30];
242     struct passwd *pw = getpwuid (getuid ());
243     time_t t = time (NULL);
244     struct tm *tm =  localtime (&t);
245
246     strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
247
248     xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
249     xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
250     xmlTextWriterEndElement (w);
251
252     xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
253     xmlTextWriterWriteString (w, _xml (buf));
254     xmlTextWriterEndElement (w);
255
256     xmlTextWriterStartElement (w, _xml ("dc:creator"));
257     xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
258
259     xmlTextWriterEndElement (w);
260
261     xmlTextWriterStartElement (w, _xml ("dc:date"));
262     xmlTextWriterWriteString (w, _xml (buf));
263     xmlTextWriterEndElement (w);
264   }
265
266   xmlTextWriterEndElement (w);
267   xmlTextWriterEndElement (w);
268   xmlTextWriterEndDocument (w);
269   xmlFreeTextWriter (w);
270 }
271
272 enum
273 {
274   output_file_arg,
275   boolean_arg,
276 };
277
278 static struct driver_option *
279 opt (struct output_driver *d, struct string_map *options, const char *key,
280      const char *default_value)
281 {
282   return driver_option_get (d, options, key, default_value);
283 }
284
285 static struct output_driver *
286 odt_create (const char *name, enum output_device_type device_type,
287             struct string_map *o)
288 {
289   struct output_driver *d;
290   struct odt_driver *odt;
291
292   odt = xzalloc (sizeof *odt);
293   d = &odt->driver;
294   output_driver_init (d, &odt_class, name, device_type);
295
296   odt->file_name = parse_string (opt (d, o, "output-file", "pspp.odt"));
297   odt->debug = parse_boolean (opt (d, o, "debug", "false"));
298
299   odt->dirname = xstrdup ("odt-XXXXXX");
300   mkdtemp (odt->dirname);
301
302   create_mimetype (odt->dirname);
303
304   /* Create the manifest */
305   odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml");
306
307   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
308   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
309                                _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
310
311
312   /* Add a manifest entry for the document as a whole */
313   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
314   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("application/vnd.oasis.opendocument.text"));
315   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml("/"));
316   xmlTextWriterEndElement (odt->manifest_wtr);
317
318
319   write_meta_data (odt);
320   write_style_data (odt);
321
322   odt->content_wtr = create_writer (odt, "content.xml");
323   register_file (odt, "content.xml");
324
325
326   /* Some necessary junk at the start */
327   xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
328   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
329                                _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
330
331   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
332                                _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
333
334   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
335                                _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
336
337   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
338
339   xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
340   xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
341
342
343
344   /* Close the manifest */
345   xmlTextWriterEndElement (odt->manifest_wtr);
346   xmlTextWriterEndDocument (odt->manifest_wtr);
347   xmlFreeTextWriter (odt->manifest_wtr);
348
349   return d;
350 }
351
352 static void
353 odt_destroy (struct output_driver *driver)
354 {
355   struct odt_driver *odt = odt_driver_cast (driver);
356
357   struct string zip_cmd;
358   struct string rm_cmd;
359
360   xmlTextWriterEndElement (odt->content_wtr); /* office:text */
361   xmlTextWriterEndElement (odt->content_wtr); /* office:body */
362   xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
363
364   xmlTextWriterEndDocument (odt->content_wtr);
365   xmlFreeTextWriter (odt->content_wtr);
366
367   /* Zip up the directory */
368   ds_init_empty (&zip_cmd);
369   ds_put_format (&zip_cmd,
370                  "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../pspp.odt .",
371                  odt->dirname, odt->file_name, odt->file_name);
372   system (ds_cstr (&zip_cmd));
373   ds_destroy (&zip_cmd);
374
375
376   if ( !odt->debug )
377     {
378       /* Remove the temp dir */
379       ds_init_empty (&rm_cmd);
380       ds_put_format (&rm_cmd, "rm -r %s", odt->dirname);
381       system (ds_cstr (&rm_cmd));
382       ds_destroy (&rm_cmd);
383     }
384   else
385     fprintf (stderr, "Not removing directory %s\n", odt->dirname);
386
387   free (odt->dirname);
388   free (odt);
389 }
390
391 static void
392 odt_submit_table (struct odt_driver *odt, struct table_item *item)
393 {
394   const struct table *tab = table_item_get_table (item);
395   const char *caption = table_item_get_caption (item);
396   int r, c;
397
398   /* Write a heading for the table */
399   if (caption != NULL)
400     {
401       xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
402       xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
403                                          "%d", 2);
404       xmlTextWriterWriteString (odt->content_wtr,
405                                 _xml (table_item_get_caption (item)) );
406       xmlTextWriterEndElement (odt->content_wtr);
407     }
408
409   /* Start table */
410   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
411   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"), 
412                                      "TABLE-%d", odt->table_num++);
413
414
415   /* Start column definitions */
416   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
417   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
418   xmlTextWriterEndElement (odt->content_wtr);
419
420
421   /* Deal with row headers */
422   if ( table_ht (tab) > 0)
423     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
424     
425
426   /* Write all the rows */
427   for (r = 0 ; r < table_nr (tab); ++r)
428     {
429       /* Start row definition */
430       xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
431
432       /* Write all the columns */
433       for (c = 0 ; c < table_nc (tab) ; ++c)
434         {
435           struct table_cell cell;
436
437           table_get_cell (tab, c, r, &cell);
438
439           if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
440             {
441               int colspan = table_cell_colspan (&cell);
442               int rowspan = table_cell_rowspan (&cell);
443
444               xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
445               xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
446
447               if (colspan > 1)
448                 xmlTextWriterWriteFormatAttribute (
449                   odt->content_wtr, _xml("table:number-columns-spanned"),
450                   "%d", colspan);
451
452               if (rowspan > 1)
453                 xmlTextWriterWriteFormatAttribute (
454                   odt->content_wtr, _xml("table:number-rows-spanned"),
455                   "%d", rowspan);
456
457               xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
458
459               if ( r < table_ht (tab) || c < table_hl (tab) )
460                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
461               else
462                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
463
464               xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
465
466               xmlTextWriterEndElement (odt->content_wtr); /* text:p */
467               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
468             }
469           else
470             {
471               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
472               xmlTextWriterEndElement (odt->content_wtr);
473             }
474
475           table_cell_free (&cell);
476         }
477   
478       xmlTextWriterEndElement (odt->content_wtr); /* row */
479
480       if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
481         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
482     }
483
484   xmlTextWriterEndElement (odt->content_wtr); /* table */
485 }
486
487 /* Submit a table to the ODT driver */
488 static void
489 odt_submit (struct output_driver *driver,
490             const struct output_item *output_item)
491 {
492   struct odt_driver *odt = odt_driver_cast (driver);
493   if (is_table_item (output_item))
494     odt_submit_table (odt, to_table_item (output_item));
495   else if (is_text_item (output_item))
496     {
497       const struct text_item *text_item = to_text_item (output_item);
498       const char *text = text_item_get_text (text_item);
499
500       /* XXX apply different styles based on text_item's type.  */
501       xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
502       xmlTextWriterWriteString (odt->content_wtr, _xml(text));
503       xmlTextWriterEndElement (odt->content_wtr);
504     }
505 }
506
507 /* ODT driver class. */
508 const struct output_driver_class odt_class =
509 {
510   "odf",
511   odt_create,
512   odt_destroy,
513   odt_submit,
514   NULL,
515 };