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