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