output: Introduce pivot tables.
[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/message-item.h"
42 #include "output/options.h"
43 #include "output/table-item.h"
44 #include "output/table-provider.h"
45 #include "output/text-item.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 struct odt_driver
55 {
56   struct output_driver driver;
57
58   struct zip_writer *zip;     /* ZIP file writer. */
59   char *file_name;            /* Output file name. */
60
61   /* content.xml */
62   xmlTextWriterPtr content_wtr; /* XML writer. */
63   FILE *content_file;           /* Temporary file. */
64
65   /* manifest.xml */
66   xmlTextWriterPtr manifest_wtr; /* XML writer. */
67   FILE *manifest_file;           /* Temporary file. */
68
69   /* Number of tables so far. */
70   int table_num;
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 (struct zip_writer *zip)
85 {
86   FILE *fp;
87
88   fp = create_temp_file ();
89   if (fp == NULL)
90     {
91       msg_error (errno, _("error creating temporary file"));
92       return false;
93     }
94
95   fprintf (fp, "application/vnd.oasis.opendocument.text");
96   zip_writer_add (zip, fp, "mimetype");
97   close_temp_file (fp);
98
99   return true;
100 }
101
102 /* Creates a new temporary file and stores it in *FILE, then creates an XML
103    writer for it and stores it in *W. */
104 static void
105 create_writer (FILE **file, xmlTextWriterPtr *w)
106 {
107   /* XXX this can fail */
108   *file = create_temp_file ();
109   *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
110
111   xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
112 }
113
114
115 static void
116 register_file (struct odt_driver *odt, const char *filename)
117 {
118   assert (odt->manifest_wtr);
119   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
120   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("text/xml"));
121   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml (filename));
122   xmlTextWriterEndElement (odt->manifest_wtr);
123 }
124
125 static void
126 write_style_data (struct odt_driver *odt)
127 {
128   xmlTextWriterPtr w;
129   FILE *file;
130
131   create_writer (&file, &w);
132   register_file (odt, "styles.xml");
133
134   xmlTextWriterStartElement (w, _xml ("office:document-styles"));
135   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
136                                _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
137
138   xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
139                                _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
140
141   xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
142                                _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
143
144   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml ("1.1"));
145
146
147
148   xmlTextWriterStartElement (w, _xml ("office:styles"));
149
150
151   {
152     xmlTextWriterStartElement (w, _xml ("style:style"));
153     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
154                                  _xml ("Standard"));
155
156     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
157                                  _xml ("paragraph"));
158
159     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
160                                  _xml ("text"));
161
162     xmlTextWriterEndElement (w); /* style:style */
163   }
164
165   {
166     xmlTextWriterStartElement (w, _xml ("style:style"));
167     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
168                                  _xml ("Table_20_Contents"));
169
170     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
171                                  _xml ("Table Contents"));
172
173     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
174                                  _xml ("paragraph"));
175
176     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
177                                  _xml ("Standard"));
178
179     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
180                                  _xml ("extra"));
181
182     xmlTextWriterEndElement (w); /* style:style */
183   }
184
185   {
186     xmlTextWriterStartElement (w, _xml ("style:style"));
187     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
188                                  _xml ("Table_20_Heading"));
189
190     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
191                                  _xml ("Table Heading"));
192
193     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
194                                  _xml ("paragraph"));
195
196     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
197                                  _xml ("Table_20_Contents"));
198
199     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
200                                  _xml ("extra"));
201
202
203     xmlTextWriterStartElement (w, _xml ("style:text-properties"));
204     xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
205     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
206     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
207     xmlTextWriterEndElement (w); /* style:text-properties */
208
209     xmlTextWriterEndElement (w); /* style:style */
210   }
211
212
213   xmlTextWriterEndElement (w); /* office:styles */
214   xmlTextWriterEndElement (w); /* office:document-styles */
215
216   xmlTextWriterEndDocument (w);
217   xmlFreeTextWriter (w);
218   zip_writer_add (odt->zip, file, "styles.xml");
219   close_temp_file (file);
220 }
221
222 static void
223 write_meta_data (struct odt_driver *odt)
224 {
225   xmlTextWriterPtr w;
226   FILE *file;
227
228   create_writer (&file, &w);
229   register_file (odt, "meta.xml");
230
231   xmlTextWriterStartElement (w, _xml ("office:document-meta"));
232   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
233   xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"),  _xml ("http://purl.org/dc/elements/1.1/"));
234   xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
235   xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
236   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml("1.1"));
237
238   xmlTextWriterStartElement (w, _xml ("office:meta"));
239   {
240     xmlTextWriterStartElement (w, _xml ("meta:generator"));
241     xmlTextWriterWriteString (w, _xml (version));
242     xmlTextWriterEndElement (w);
243   }
244
245
246   {
247     char buf[30];
248     time_t t = time (NULL);
249     struct tm *tm =  localtime (&t);
250
251     strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
252
253     xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
254     xmlTextWriterWriteString (w, _xml (buf));
255     xmlTextWriterEndElement (w);
256
257     xmlTextWriterStartElement (w, _xml ("dc:date"));
258     xmlTextWriterWriteString (w, _xml (buf));
259     xmlTextWriterEndElement (w);
260   }
261
262 #ifdef HAVE_PWD_H
263   {
264     struct passwd *pw = getpwuid (getuid ());
265     if (pw != NULL)
266       {
267         xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
268         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
269         xmlTextWriterEndElement (w);
270
271         xmlTextWriterStartElement (w, _xml ("dc:creator"));
272         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
273         xmlTextWriterEndElement (w);
274       }
275   }
276 #endif
277
278   xmlTextWriterEndElement (w);
279   xmlTextWriterEndElement (w);
280   xmlTextWriterEndDocument (w);
281   xmlFreeTextWriter (w);
282   zip_writer_add (odt->zip, file, "meta.xml");
283   close_temp_file (file);
284 }
285
286 static struct output_driver *
287 odt_create (struct file_handle *fh, enum settings_output_devices device_type,
288             struct string_map *o UNUSED)
289 {
290   struct output_driver *d;
291   struct odt_driver *odt;
292   struct zip_writer *zip;
293   const char *file_name = fh_get_file_name (fh);
294
295   zip = zip_writer_create (file_name);
296   if (zip == NULL)
297     return NULL;
298
299   odt = xzalloc (sizeof *odt);
300   d = &odt->driver;
301
302   output_driver_init (d, &odt_driver_class, file_name, device_type);
303
304   odt->zip = zip;
305   odt->file_name = xstrdup (file_name);
306
307   if (!create_mimetype (zip))
308     {
309       output_driver_destroy (d);
310       return NULL;
311     }
312
313   /* Create the manifest */
314   create_writer (&odt->manifest_file, &odt->manifest_wtr);
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   create_writer (&odt->content_file, &odt->content_wtr);
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   zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
358   close_temp_file (odt->manifest_file);
359
360   return d;
361 }
362
363 static void
364 odt_destroy (struct output_driver *driver)
365 {
366   struct odt_driver *odt = odt_driver_cast (driver);
367
368   if (odt->content_wtr != NULL)
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       zip_writer_add (odt->zip, odt->content_file, "content.xml");
377       close_temp_file (odt->content_file);
378
379       zip_writer_close (odt->zip);
380     }
381
382   free (odt->file_name);
383   free (odt);
384 }
385
386 static void
387 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
388 {
389   xmlTextWriterPtr writer = odt->content_wtr;
390
391   if (!strchr (line_, '\n'))
392     xmlTextWriterWriteString (writer, _xml(line_));
393   else
394     {
395       char *line = xstrdup (line_);
396       char *newline;
397       char *p;
398
399       for (p = line; *p; p = newline + 1)
400         {
401           newline = strchr (p, '\n');
402
403           if (!newline)
404             {
405               xmlTextWriterWriteString (writer, _xml(p));
406               free (line);
407               return;
408             }
409
410           if (newline > p && newline[-1] == '\r')
411             newline[-1] = '\0';
412           else
413             *newline = '\0';
414           xmlTextWriterWriteString (writer, _xml(p));
415           xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
416         }
417     }
418 }
419
420 static void
421 write_footnote (struct odt_driver *odt, const struct footnote *f)
422 {
423   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
424   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
425                                _xml("footnote"));
426
427   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
428   if (strlen (f->marker) > 1)
429     xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
430                                        "(%s)", f->marker);
431   else
432     xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
433                                  _xml(f->marker));
434   xmlTextWriterEndElement (odt->content_wtr);
435
436   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
437   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
438   write_xml_with_line_breaks (odt, f->content);
439   xmlTextWriterEndElement (odt->content_wtr);
440   xmlTextWriterEndElement (odt->content_wtr);
441
442   xmlTextWriterEndElement (odt->content_wtr);
443 }
444
445 static void
446 write_table_item_text (struct odt_driver *odt,
447                        const struct table_item_text *text)
448 {
449   if (!text)
450     return;
451
452   xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
453   xmlTextWriterWriteFormatAttribute (odt->content_wtr,
454                                      _xml("text:outline-level"), "%d", 2);
455   xmlTextWriterWriteString (odt->content_wtr, _xml (text->content) );
456   for (size_t i = 0; i < text->n_footnotes; i++)
457     write_footnote (odt, text->footnotes[i]);
458   xmlTextWriterEndElement (odt->content_wtr);
459 }
460
461 static void
462 write_table (struct odt_driver *odt, const struct table_item *item)
463 {
464   const struct table *tab = table_item_get_table (item);
465   int r, c;
466
467   /* Write a heading for the table */
468   write_table_item_text (odt, table_item_get_title (item));
469   write_table_item_text (odt, table_item_get_layers (item));
470
471   /* Start table */
472   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
473   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
474                                      "TABLE-%d", odt->table_num++);
475
476
477   /* Start column definitions */
478   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
479   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
480   xmlTextWriterEndElement (odt->content_wtr);
481
482
483   /* Deal with row headers */
484   if ( table_ht (tab) > 0)
485     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
486
487
488   /* Write all the rows */
489   for (r = 0 ; r < table_nr (tab); ++r)
490     {
491       /* Start row definition */
492       xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
493
494       /* Write all the columns */
495       for (c = 0 ; c < table_nc (tab) ; ++c)
496         {
497           struct table_cell cell;
498
499           table_get_cell (tab, c, r, &cell);
500
501           if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
502             {
503               int colspan = table_cell_colspan (&cell);
504               int rowspan = table_cell_rowspan (&cell);
505
506               xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
507               xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
508
509               if (colspan > 1)
510                 xmlTextWriterWriteFormatAttribute (
511                   odt->content_wtr, _xml("table:number-columns-spanned"),
512                   "%d", colspan);
513
514               if (rowspan > 1)
515                 xmlTextWriterWriteFormatAttribute (
516                   odt->content_wtr, _xml("table:number-rows-spanned"),
517                   "%d", rowspan);
518
519               xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
520
521               if ( r < table_ht (tab) || c < table_hl (tab) )
522                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
523               else
524                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
525
526               if (cell.options & TAB_MARKUP)
527                 {
528                   /* XXX */
529                   char *s = output_get_text_from_markup (cell.text);
530                   write_xml_with_line_breaks (odt, s);
531                   free (s);
532                 }
533               else
534                 write_xml_with_line_breaks (odt, cell.text);
535
536               for (int i = 0; i < cell.n_footnotes; i++)
537                 write_footnote (odt, cell.footnotes[i]);
538
539               xmlTextWriterEndElement (odt->content_wtr); /* text:p */
540               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
541             }
542           else
543             {
544               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
545               xmlTextWriterEndElement (odt->content_wtr);
546             }
547
548           table_cell_free (&cell);
549         }
550
551       xmlTextWriterEndElement (odt->content_wtr); /* row */
552
553       if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
554         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
555     }
556
557   xmlTextWriterEndElement (odt->content_wtr); /* table */
558
559   /* Write a caption for the table */
560   write_table_item_text (odt, table_item_get_caption (item));
561 }
562
563 static void
564 odt_output_text (struct odt_driver *odt, const char *text)
565 {
566   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
567   xmlTextWriterWriteString (odt->content_wtr, _xml(text));
568   xmlTextWriterEndElement (odt->content_wtr);
569 }
570
571 /* Submit a table to the ODT driver */
572 static void
573 odt_submit (struct output_driver *driver,
574             const struct output_item *output_item)
575 {
576   struct odt_driver *odt = odt_driver_cast (driver);
577
578   if (is_table_item (output_item))
579     write_table (odt, to_table_item (output_item));
580   else if (is_text_item (output_item))
581     odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
582   else if (is_message_item (output_item))
583     {
584       const struct message_item *message_item = to_message_item (output_item);
585       char *s = msg_to_string (message_item_get_msg (message_item));
586       odt_output_text (odt, s);
587       free (s);
588     }
589 }
590
591 struct output_driver_factory odt_driver_factory =
592   { "odt", "pspp.odf", odt_create };
593
594 static const struct output_driver_class odt_driver_class =
595 {
596   "odf",
597   odt_destroy,
598   odt_submit,
599   NULL,
600 };