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