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