Replace numerous instances of xzalloc with XZALLOC
[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 zip_writer *zip;
289   const char *file_name = fh_get_file_name (fh);
290
291   zip = zip_writer_create (file_name);
292   if (zip == NULL)
293     return NULL;
294
295   struct odt_driver *odt = XZALLOC (struct odt_driver);
296   d = &odt->driver;
297
298   output_driver_init (d, &odt_driver_class, file_name, device_type);
299
300   odt->zip = zip;
301   odt->handle = fh;
302   odt->file_name = xstrdup (file_name);
303
304   zip_writer_add_string (zip, "mimetype",
305                          "application/vnd.oasis.opendocument.text");
306
307   /* Create the manifest */
308   create_writer (&odt->manifest_file, &odt->manifest_wtr);
309
310   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
311   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
312                                _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
313
314
315   /* Add a manifest entry for the document as a whole */
316   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
317   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("application/vnd.oasis.opendocument.text"));
318   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml("/"));
319   xmlTextWriterEndElement (odt->manifest_wtr);
320
321
322   write_meta_data (odt);
323   write_style_data (odt);
324
325   create_writer (&odt->content_file, &odt->content_wtr);
326   register_file (odt, "content.xml");
327
328
329   /* Some necessary junk at the start */
330   xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
331   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
332                                _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
333
334   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
335                                _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
336
337   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
338                                _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
339
340   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
341
342   xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
343   xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
344
345
346
347   /* Close the manifest */
348   xmlTextWriterEndElement (odt->manifest_wtr);
349   xmlTextWriterEndDocument (odt->manifest_wtr);
350   xmlFreeTextWriter (odt->manifest_wtr);
351   zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
352   close_temp_file (odt->manifest_file);
353
354   return d;
355 }
356
357 static void
358 odt_destroy (struct output_driver *driver)
359 {
360   struct odt_driver *odt = odt_driver_cast (driver);
361
362   if (odt->content_wtr != NULL)
363     {
364       xmlTextWriterEndElement (odt->content_wtr); /* office:text */
365       xmlTextWriterEndElement (odt->content_wtr); /* office:body */
366       xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
367
368       xmlTextWriterEndDocument (odt->content_wtr);
369       xmlFreeTextWriter (odt->content_wtr);
370       zip_writer_add (odt->zip, odt->content_file, "content.xml");
371       close_temp_file (odt->content_file);
372
373       zip_writer_close (odt->zip);
374     }
375
376   fh_unref (odt->handle);
377   free (odt->file_name);
378   free (odt);
379 }
380
381 static void
382 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
383 {
384   xmlTextWriterPtr writer = odt->content_wtr;
385
386   if (!strchr (line_, '\n'))
387     xmlTextWriterWriteString (writer, _xml(line_));
388   else
389     {
390       char *line = xstrdup (line_);
391       char *newline;
392       char *p;
393
394       for (p = line; *p; p = newline + 1)
395         {
396           newline = strchr (p, '\n');
397
398           if (!newline)
399             {
400               xmlTextWriterWriteString (writer, _xml(p));
401               free (line);
402               return;
403             }
404
405           if (newline > p && newline[-1] == '\r')
406             newline[-1] = '\0';
407           else
408             *newline = '\0';
409           xmlTextWriterWriteString (writer, _xml(p));
410           xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
411         }
412     }
413 }
414
415 static void
416 write_footnotes (struct odt_driver *odt,
417                  const struct pivot_table *pt,
418                  const size_t *footnote_indexes,
419                  size_t n_footnotes)
420 {
421   for (size_t i = 0; i < n_footnotes; i++)
422     {
423       const struct pivot_footnote *f = pt->footnotes[footnote_indexes[i]];
424       if (f->show)
425         {
426           xmlTextWriterStartElement (odt->content_wtr, _xml("text:span"));
427           xmlTextWriterWriteAttribute (odt->content_wtr,
428                                        _xml("text:style-name"),
429                                        _xml("superscript"));
430           char *s = pivot_footnote_marker_string (f, pt);
431           write_xml_with_line_breaks (odt, s);
432           free (s);
433           xmlTextWriterEndElement (odt->content_wtr);
434         }
435     }
436 }
437
438 static void
439 write_table_item_cell (struct odt_driver *odt,
440                        const struct pivot_table *pt,
441                        const struct table_cell *cell)
442 {
443   struct string body = DS_EMPTY_INITIALIZER;
444   pivot_value_format_body (cell->value, pt, &body);
445   xmlTextWriterWriteString (odt->content_wtr, _xml (ds_cstr (&body)));
446   ds_destroy (&body);
447
448   const struct pivot_value_ex *ex = pivot_value_ex (cell->value);
449   write_footnotes (odt, pt, ex->footnote_indexes, ex->n_footnotes);
450 }
451
452 static void
453 write_table__ (struct odt_driver *odt, const struct pivot_table *pt,
454                const struct table *t)
455 {
456   if (t)
457     {
458       for (size_t y = 0; y < t->n[V]; y++)
459         {
460           xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
461           xmlTextWriterWriteFormatAttribute (odt->content_wtr,
462                                              _xml("text:outline-level"), "%d", 2);
463
464           struct table_cell cell;
465           table_get_cell (t, 0, y, &cell);
466           write_table_item_cell (odt, pt, &cell);
467
468           xmlTextWriterEndElement (odt->content_wtr);
469         }
470     }
471 }
472
473 static void
474 write_table_layer (struct odt_driver *odt, const struct pivot_table *pt,
475                    const size_t *layer_indexes)
476 {
477   struct table *title, *layers, *body, *caption, *footnotes;
478   pivot_output (pt, layer_indexes, true, &title, &layers, &body,
479                 &caption, &footnotes, NULL, NULL);
480
481   /* Write a heading for the table */
482   write_table__ (odt, pt, title);
483   write_table__ (odt, pt, layers);
484
485   /* Start table */
486   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
487   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
488                                      "TABLE-%d", odt->table_num++);
489
490
491   /* Start column definitions */
492   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
493   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", body->n[H]);
494   xmlTextWriterEndElement (odt->content_wtr);
495
496
497   /* Deal with row headers */
498   if (body->h[V][0] > 0)
499     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
500
501
502   /* Write all the rows */
503   for (int r = 0 ; r < body->n[V]; ++r)
504     {
505       /* Start row definition */
506       xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
507
508       /* Write all the columns */
509       for (int c = 0 ; c < body->n[H] ; ++c)
510         {
511           struct table_cell cell;
512
513           table_get_cell (body, c, r, &cell);
514
515           if (c == cell.d[H][0] && r == cell.d[V][0])
516             {
517               int colspan = table_cell_colspan (&cell);
518               int rowspan = table_cell_rowspan (&cell);
519
520               xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
521               xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
522
523               if (colspan > 1)
524                 xmlTextWriterWriteFormatAttribute (
525                   odt->content_wtr, _xml("table:number-columns-spanned"),
526                   "%d", colspan);
527
528               if (rowspan > 1)
529                 xmlTextWriterWriteFormatAttribute (
530                   odt->content_wtr, _xml("table:number-rows-spanned"),
531                   "%d", rowspan);
532
533               xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
534
535               if (r < body->h[V][0] || c < body->h[H][0])
536                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
537               else
538                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
539
540               write_table_item_cell (odt, pt, &cell);
541
542               xmlTextWriterEndElement (odt->content_wtr); /* text:p */
543               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
544             }
545           else
546             {
547               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
548               xmlTextWriterEndElement (odt->content_wtr);
549             }
550         }
551
552       xmlTextWriterEndElement (odt->content_wtr); /* row */
553
554       int ht = body->h[V][0];
555       if (ht > 0 && r == ht - 1)
556         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
557     }
558
559   xmlTextWriterEndElement (odt->content_wtr); /* table */
560
561   /* Write a caption for the table */
562   write_table__ (odt, pt, caption);
563   write_table__ (odt, pt, footnotes);
564
565   table_unref (title);
566   table_unref (layers);
567   table_unref (body);
568   table_unref (caption);
569   table_unref (footnotes);
570 }
571
572 static void
573 write_table (struct odt_driver *odt, const struct pivot_table *pt)
574 {
575   size_t *layer_indexes;
576   PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, pt, true)
577     write_table_layer (odt, pt, layer_indexes);
578 }
579
580 static void
581 odt_output_text (struct odt_driver *odt, const char *text)
582 {
583   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
584   xmlTextWriterWriteString (odt->content_wtr, _xml(text));
585   xmlTextWriterEndElement (odt->content_wtr);
586 }
587
588 /* Submit a table to the ODT driver */
589 static void
590 odt_submit (struct output_driver *driver, const struct output_item *item)
591 {
592   struct odt_driver *odt = odt_driver_cast (driver);
593
594   switch (item->type)
595     {
596     case OUTPUT_ITEM_CHART:
597       break;
598
599     case OUTPUT_ITEM_GROUP:
600       break;
601
602     case OUTPUT_ITEM_IMAGE:
603       break;
604
605     case OUTPUT_ITEM_MESSAGE:
606       {
607         char *s = msg_to_string (item->message);
608         odt_output_text (odt, s);
609         free (s);
610       }
611       break;
612
613     case OUTPUT_ITEM_PAGE_BREAK:
614       break;
615
616     case OUTPUT_ITEM_TABLE:
617       write_table (odt, item->table);
618       break;
619
620     case OUTPUT_ITEM_TEXT:
621       {
622         char *text = text_item_get_plain_text (item);
623         odt_output_text (odt, text);
624         free (text);
625       }
626       break;
627     }
628 }
629
630 struct output_driver_factory odt_driver_factory =
631   { "odt", "pspp.odf", odt_create };
632
633 static const struct output_driver_class odt_driver_class =
634 {
635   .name = "odf",
636   .destroy = odt_destroy,
637   .submit = odt_submit,
638 };