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