a96958b9e8e5d30273cf2a2282402d44272cfc8c
[pspp] / src / output / odt.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009-2014 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 #ifdef HAVE_PWD_H
25 #include <pwd.h>
26 #endif
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <time.h>
30 #include <unistd.h>
31
32 #include "libpspp/assertion.h"
33 #include "libpspp/cast.h"
34 #include "libpspp/message.h"
35 #include "libpspp/str.h"
36 #include "libpspp/temp-file.h"
37 #include "libpspp/version.h"
38 #include "libpspp/zip-writer.h"
39 #include "data/file-handle-def.h"
40 #include "output/driver-provider.h"
41 #include "output/options.h"
42 #include "output/output-item.h"
43 #include "output/pivot-table.h"
44 #include "output/pivot-output.h"
45 #include "output/table-provider.h"
46
47 #include "gl/xalloc.h"
48
49 #include "gettext.h"
50 #define _(msgid) gettext (msgid)
51
52 #define _xml(X) (CHAR_CAST (const xmlChar *, X))
53
54 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
55 #define H TABLE_HORZ
56 #define V TABLE_VERT
57
58 struct odt_driver
59 {
60   struct output_driver driver;
61
62   struct zip_writer *zip;     /* ZIP file writer. */
63   struct file_handle *handle; /* Handle for 'file_name'. */
64   char *file_name;            /* Output file name. */
65
66   /* content.xml */
67   xmlTextWriterPtr content_wtr; /* XML writer. */
68   FILE *content_file;           /* Temporary file. */
69
70   /* manifest.xml */
71   xmlTextWriterPtr manifest_wtr; /* XML writer. */
72   FILE *manifest_file;           /* Temporary file. */
73
74   /* Number of tables so far. */
75   int table_num;
76 };
77
78 static const struct output_driver_class odt_driver_class;
79
80 static struct odt_driver *
81 odt_driver_cast (struct output_driver *driver)
82 {
83   assert (driver->class == &odt_driver_class);
84   return UP_CAST (driver, struct odt_driver, driver);
85 }
86
87 /* Creates a new temporary file and stores it in *FILE, then creates an XML
88    writer for it and stores it in *W. */
89 static void
90 create_writer (FILE **file, xmlTextWriterPtr *w)
91 {
92   /* XXX this can fail */
93   *file = create_temp_file ();
94   *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
95
96   xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
97 }
98
99
100 static void
101 register_file (struct odt_driver *odt, const char *filename)
102 {
103   assert (odt->manifest_wtr);
104   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
105   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("text/xml"));
106   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml (filename));
107   xmlTextWriterEndElement (odt->manifest_wtr);
108 }
109
110 static void
111 write_style_data (struct odt_driver *odt)
112 {
113   xmlTextWriterPtr w;
114   FILE *file;
115
116   create_writer (&file, &w);
117   register_file (odt, "styles.xml");
118
119   xmlTextWriterStartElement (w, _xml ("office:document-styles"));
120   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
121                                _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
122
123   xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
124                                _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
125
126   xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
127                                _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"));
128
129   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml ("1.1"));
130
131
132
133   xmlTextWriterStartElement (w, _xml ("office:styles"));
134
135
136   {
137     xmlTextWriterStartElement (w, _xml ("style:style"));
138     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
139                                  _xml ("Standard"));
140
141     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
142                                  _xml ("paragraph"));
143
144     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
145                                  _xml ("text"));
146
147     xmlTextWriterEndElement (w); /* style:style */
148   }
149
150   {
151     xmlTextWriterStartElement (w, _xml ("style:style"));
152     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
153                                  _xml ("Table_20_Contents"));
154
155     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
156                                  _xml ("Table Contents"));
157
158     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
159                                  _xml ("paragraph"));
160
161     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
162                                  _xml ("Standard"));
163
164     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
165                                  _xml ("extra"));
166
167     xmlTextWriterEndElement (w); /* style:style */
168   }
169
170   {
171     xmlTextWriterStartElement (w, _xml ("style:style"));
172     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
173                                  _xml ("Table_20_Heading"));
174
175     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
176                                  _xml ("Table Heading"));
177
178     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
179                                  _xml ("paragraph"));
180
181     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
182                                  _xml ("Table_20_Contents"));
183
184     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
185                                  _xml ("extra"));
186
187
188     xmlTextWriterStartElement (w, _xml ("style:text-properties"));
189     xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
190     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
191     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
192     xmlTextWriterEndElement (w); /* style:text-properties */
193
194     xmlTextWriterEndElement (w); /* style:style */
195   }
196
197   {
198     xmlTextWriterStartElement (w, _xml ("style:style"));
199     xmlTextWriterWriteAttribute (w, _xml ("style:name"), _xml ("superscript"));
200     xmlTextWriterWriteAttribute (w, _xml ("style:family"), _xml ("text"));
201
202     xmlTextWriterStartElement (w, _xml ("style:text-properties"));
203     xmlTextWriterWriteAttribute (w, _xml ("style:text-position"),
204                                  _xml ("super 58%"));
205     xmlTextWriterEndElement (w); /* style:text-properties */
206
207     xmlTextWriterEndElement (w); /* style:style */
208   }
209
210   xmlTextWriterEndElement (w); /* office:styles */
211   xmlTextWriterEndElement (w); /* office:document-styles */
212
213   xmlTextWriterEndDocument (w);
214   xmlFreeTextWriter (w);
215   zip_writer_add (odt->zip, file, "styles.xml");
216   close_temp_file (file);
217 }
218
219 static void
220 write_meta_data (struct odt_driver *odt)
221 {
222   xmlTextWriterPtr w;
223   FILE *file;
224
225   create_writer (&file, &w);
226   register_file (odt, "meta.xml");
227
228   xmlTextWriterStartElement (w, _xml ("office:document-meta"));
229   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
230   xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"),  _xml ("http://purl.org/dc/elements/1.1/"));
231   xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
232   xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
233   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml("1.1"));
234
235   xmlTextWriterStartElement (w, _xml ("office:meta"));
236   {
237     xmlTextWriterStartElement (w, _xml ("meta:generator"));
238     xmlTextWriterWriteString (w, _xml (version));
239     xmlTextWriterEndElement (w);
240   }
241
242
243   {
244     char buf[30];
245     time_t t = time (NULL);
246     struct tm *tm =  localtime (&t);
247
248     strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
249
250     xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
251     xmlTextWriterWriteString (w, _xml (buf));
252     xmlTextWriterEndElement (w);
253
254     xmlTextWriterStartElement (w, _xml ("dc:date"));
255     xmlTextWriterWriteString (w, _xml (buf));
256     xmlTextWriterEndElement (w);
257   }
258
259 #ifdef HAVE_PWD_H
260   {
261     struct passwd *pw = getpwuid (getuid ());
262     if (pw != NULL)
263       {
264         xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
265         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
266         xmlTextWriterEndElement (w);
267
268         xmlTextWriterStartElement (w, _xml ("dc:creator"));
269         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
270         xmlTextWriterEndElement (w);
271       }
272   }
273 #endif
274
275   xmlTextWriterEndElement (w);
276   xmlTextWriterEndElement (w);
277   xmlTextWriterEndDocument (w);
278   xmlFreeTextWriter (w);
279   zip_writer_add (odt->zip, file, "meta.xml");
280   close_temp_file (file);
281 }
282
283 static struct output_driver *
284 odt_create (struct file_handle *fh, enum settings_output_devices device_type,
285             struct string_map *o UNUSED)
286 {
287   struct output_driver *d;
288   struct odt_driver *odt;
289   struct zip_writer *zip;
290   const char *file_name = fh_get_file_name (fh);
291
292   zip = zip_writer_create (file_name);
293   if (zip == NULL)
294     return NULL;
295
296   odt = xzalloc (sizeof *odt);
297   d = &odt->driver;
298
299   output_driver_init (d, &odt_driver_class, file_name, device_type);
300
301   odt->zip = zip;
302   odt->handle = fh;
303   odt->file_name = xstrdup (file_name);
304
305   zip_writer_add_string (zip, "mimetype",
306                          "application/vnd.oasis.opendocument.text");
307
308   /* Create the manifest */
309   create_writer (&odt->manifest_file, &odt->manifest_wtr);
310
311   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
312   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
313                                _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
314
315
316   /* Add a manifest entry for the document as a whole */
317   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
318   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("application/vnd.oasis.opendocument.text"));
319   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml("/"));
320   xmlTextWriterEndElement (odt->manifest_wtr);
321
322
323   write_meta_data (odt);
324   write_style_data (odt);
325
326   create_writer (&odt->content_file, &odt->content_wtr);
327   register_file (odt, "content.xml");
328
329
330   /* Some necessary junk at the start */
331   xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
332   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
333                                _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
334
335   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
336                                _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
337
338   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
339                                _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
340
341   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
342
343   xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
344   xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
345
346
347
348   /* Close the manifest */
349   xmlTextWriterEndElement (odt->manifest_wtr);
350   xmlTextWriterEndDocument (odt->manifest_wtr);
351   xmlFreeTextWriter (odt->manifest_wtr);
352   zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
353   close_temp_file (odt->manifest_file);
354
355   return d;
356 }
357
358 static void
359 odt_destroy (struct output_driver *driver)
360 {
361   struct odt_driver *odt = odt_driver_cast (driver);
362
363   if (odt->content_wtr != NULL)
364     {
365       xmlTextWriterEndElement (odt->content_wtr); /* office:text */
366       xmlTextWriterEndElement (odt->content_wtr); /* office:body */
367       xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
368
369       xmlTextWriterEndDocument (odt->content_wtr);
370       xmlFreeTextWriter (odt->content_wtr);
371       zip_writer_add (odt->zip, odt->content_file, "content.xml");
372       close_temp_file (odt->content_file);
373
374       zip_writer_close (odt->zip);
375     }
376
377   fh_unref (odt->handle);
378   free (odt->file_name);
379   free (odt);
380 }
381
382 static void
383 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
384 {
385   xmlTextWriterPtr writer = odt->content_wtr;
386
387   if (!strchr (line_, '\n'))
388     xmlTextWriterWriteString (writer, _xml(line_));
389   else
390     {
391       char *line = xstrdup (line_);
392       char *newline;
393       char *p;
394
395       for (p = line; *p; p = newline + 1)
396         {
397           newline = strchr (p, '\n');
398
399           if (!newline)
400             {
401               xmlTextWriterWriteString (writer, _xml(p));
402               free (line);
403               return;
404             }
405
406           if (newline > p && newline[-1] == '\r')
407             newline[-1] = '\0';
408           else
409             *newline = '\0';
410           xmlTextWriterWriteString (writer, _xml(p));
411           xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
412         }
413     }
414 }
415
416 static void
417 write_footnotes (struct odt_driver *odt,
418                  const struct pivot_table *pt,
419                  const size_t *footnote_indexes,
420                  size_t n_footnotes)
421 {
422   for (size_t i = 0; i < n_footnotes; i++)
423     {
424       const struct pivot_footnote *f = pt->footnotes[footnote_indexes[i]];
425       if (f->show)
426         {
427           xmlTextWriterStartElement (odt->content_wtr, _xml("text:span"));
428           xmlTextWriterWriteAttribute (odt->content_wtr,
429                                        _xml("text:style-name"),
430                                        _xml("superscript"));
431           char *s = pivot_footnote_marker_string (f, pt);
432           write_xml_with_line_breaks (odt, s);
433           free (s);
434           xmlTextWriterEndElement (odt->content_wtr);
435         }
436     }
437 }
438
439 static void
440 write_table_item_cell (struct odt_driver *odt,
441                        const struct pivot_table *pt,
442                        const struct table_cell *cell)
443 {
444   struct string body = DS_EMPTY_INITIALIZER;
445   pivot_value_format_body (cell->value, pt, &body);
446   xmlTextWriterWriteString (odt->content_wtr, _xml (ds_cstr (&body)));
447   ds_destroy (&body);
448
449   const struct pivot_value_ex *ex = pivot_value_ex (cell->value);
450   write_footnotes (odt, pt, ex->footnote_indexes, ex->n_footnotes);
451 }
452
453 static void
454 write_table__ (struct odt_driver *odt, const struct pivot_table *pt,
455                const struct table *t)
456 {
457   if (t)
458     {
459       for (size_t y = 0; y < t->n[V]; y++)
460         {
461           xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
462           xmlTextWriterWriteFormatAttribute (odt->content_wtr,
463                                              _xml("text:outline-level"), "%d", 2);
464
465           struct table_cell cell;
466           table_get_cell (t, 0, y, &cell);
467           write_table_item_cell (odt, pt, &cell);
468
469           xmlTextWriterEndElement (odt->content_wtr);
470         }
471     }
472 }
473
474 static void
475 write_table_layer (struct odt_driver *odt, const struct pivot_table *pt,
476                    const size_t *layer_indexes)
477 {
478   struct table *title, *layers, *body, *caption, *footnotes;
479   pivot_output (pt, layer_indexes, true, &title, &layers, &body,
480                 &caption, &footnotes, NULL, NULL);
481
482   /* Write a heading for the table */
483   write_table__ (odt, pt, title);
484   write_table__ (odt, pt, layers);
485
486   /* Start table */
487   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
488   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
489                                      "TABLE-%d", odt->table_num++);
490
491
492   /* Start column definitions */
493   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
494   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", body->n[H]);
495   xmlTextWriterEndElement (odt->content_wtr);
496
497
498   /* Deal with row headers */
499   if (body->h[V][0] > 0)
500     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
501
502
503   /* Write all the rows */
504   for (int r = 0 ; r < body->n[V]; ++r)
505     {
506       /* Start row definition */
507       xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
508
509       /* Write all the columns */
510       for (int c = 0 ; c < body->n[H] ; ++c)
511         {
512           struct table_cell cell;
513
514           table_get_cell (body, c, r, &cell);
515
516           if (c == cell.d[H][0] && r == cell.d[V][0])
517             {
518               int colspan = table_cell_colspan (&cell);
519               int rowspan = table_cell_rowspan (&cell);
520
521               xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
522               xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
523
524               if (colspan > 1)
525                 xmlTextWriterWriteFormatAttribute (
526                   odt->content_wtr, _xml("table:number-columns-spanned"),
527                   "%d", colspan);
528
529               if (rowspan > 1)
530                 xmlTextWriterWriteFormatAttribute (
531                   odt->content_wtr, _xml("table:number-rows-spanned"),
532                   "%d", rowspan);
533
534               xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
535
536               if (r < body->h[V][0] || c < body->h[H][0])
537                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
538               else
539                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
540
541               write_table_item_cell (odt, pt, &cell);
542
543               xmlTextWriterEndElement (odt->content_wtr); /* text:p */
544               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
545             }
546           else
547             {
548               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
549               xmlTextWriterEndElement (odt->content_wtr);
550             }
551         }
552
553       xmlTextWriterEndElement (odt->content_wtr); /* row */
554
555       int ht = body->h[V][0];
556       if (ht > 0 && r == ht - 1)
557         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
558     }
559
560   xmlTextWriterEndElement (odt->content_wtr); /* table */
561
562   /* Write a caption for the table */
563   write_table__ (odt, pt, caption);
564   write_table__ (odt, pt, footnotes);
565
566   table_unref (title);
567   table_unref (layers);
568   table_unref (body);
569   table_unref (caption);
570   table_unref (footnotes);
571 }
572
573 static void
574 write_table (struct odt_driver *odt, const struct pivot_table *pt)
575 {
576   size_t *layer_indexes;
577   PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, pt, true)
578     write_table_layer (odt, pt, layer_indexes);
579 }
580
581 static void
582 odt_output_text (struct odt_driver *odt, const char *text)
583 {
584   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
585   xmlTextWriterWriteString (odt->content_wtr, _xml(text));
586   xmlTextWriterEndElement (odt->content_wtr);
587 }
588
589 /* Submit a table to the ODT driver */
590 static void
591 odt_submit (struct output_driver *driver, const struct output_item *item)
592 {
593   struct odt_driver *odt = odt_driver_cast (driver);
594
595   switch (item->type)
596     {
597     case OUTPUT_ITEM_CHART:
598       break;
599
600     case OUTPUT_ITEM_GROUP:
601       break;
602
603     case OUTPUT_ITEM_IMAGE:
604       break;
605
606     case OUTPUT_ITEM_MESSAGE:
607       {
608         char *s = msg_to_string (item->message);
609         odt_output_text (odt, s);
610         free (s);
611       }
612       break;
613
614     case OUTPUT_ITEM_PAGE_BREAK:
615       break;
616
617     case OUTPUT_ITEM_TABLE:
618       write_table (odt, item->table);
619       break;
620
621     case OUTPUT_ITEM_TEXT:
622       {
623         char *text = text_item_get_plain_text (item);
624         odt_output_text (odt, text);
625         free (text);
626       }
627       break;
628     }
629 }
630
631 struct output_driver_factory odt_driver_factory =
632   { "odt", "pspp.odf", odt_create };
633
634 static const struct output_driver_class odt_driver_class =
635 {
636   .name = "odf",
637   .destroy = odt_destroy,
638   .submit = odt_submit,
639 };