f0143515e0aac73f6a18aff018901ecc2e416a9a
[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
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->file_name = xstrdup (file_name);
307
308   if (!create_mimetype (zip))
309     {
310       output_driver_destroy (d);
311       return NULL;
312     }
313
314   /* Create the manifest */
315   create_writer (&odt->manifest_file, &odt->manifest_wtr);
316
317   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
318   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
319                                _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
320
321
322   /* Add a manifest entry for the document as a whole */
323   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
324   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("application/vnd.oasis.opendocument.text"));
325   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml("/"));
326   xmlTextWriterEndElement (odt->manifest_wtr);
327
328
329   write_meta_data (odt);
330   write_style_data (odt);
331
332   create_writer (&odt->content_file, &odt->content_wtr);
333   register_file (odt, "content.xml");
334
335
336   /* Some necessary junk at the start */
337   xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
338   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
339                                _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
340
341   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
342                                _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
343
344   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
345                                _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
346
347   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
348
349   xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
350   xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
351
352
353
354   /* Close the manifest */
355   xmlTextWriterEndElement (odt->manifest_wtr);
356   xmlTextWriterEndDocument (odt->manifest_wtr);
357   xmlFreeTextWriter (odt->manifest_wtr);
358   zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
359   close_temp_file (odt->manifest_file);
360
361   return d;
362 }
363
364 static void
365 odt_destroy (struct output_driver *driver)
366 {
367   struct odt_driver *odt = odt_driver_cast (driver);
368
369   if (odt->content_wtr != NULL)
370     {
371       xmlTextWriterEndElement (odt->content_wtr); /* office:text */
372       xmlTextWriterEndElement (odt->content_wtr); /* office:body */
373       xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
374
375       xmlTextWriterEndDocument (odt->content_wtr);
376       xmlFreeTextWriter (odt->content_wtr);
377       zip_writer_add (odt->zip, odt->content_file, "content.xml");
378       close_temp_file (odt->content_file);
379
380       zip_writer_close (odt->zip);
381     }
382
383   free (odt->file_name);
384   free (odt);
385 }
386
387 static void
388 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
389 {
390   xmlTextWriterPtr writer = odt->content_wtr;
391
392   if (!strchr (line_, '\n'))
393     xmlTextWriterWriteString (writer, _xml(line_));
394   else
395     {
396       char *line = xstrdup (line_);
397       char *newline;
398       char *p;
399
400       for (p = line; *p; p = newline + 1)
401         {
402           newline = strchr (p, '\n');
403
404           if (!newline)
405             {
406               xmlTextWriterWriteString (writer, _xml(p));
407               free (line);
408               return;
409             }
410
411           if (newline > p && newline[-1] == '\r')
412             newline[-1] = '\0';
413           else
414             *newline = '\0';
415           xmlTextWriterWriteString (writer, _xml(p));
416           xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
417         }
418     }
419 }
420
421 static void
422 write_footnote (struct odt_driver *odt, const struct footnote *f)
423 {
424   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
425   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
426                                _xml("footnote"));
427
428   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
429   if (strlen (f->marker) > 1)
430     xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
431                                        "(%s)", f->marker);
432   else
433     xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
434                                  _xml(f->marker));
435   xmlTextWriterEndElement (odt->content_wtr);
436
437   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
438   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
439   write_xml_with_line_breaks (odt, f->content);
440   xmlTextWriterEndElement (odt->content_wtr);
441   xmlTextWriterEndElement (odt->content_wtr);
442
443   xmlTextWriterEndElement (odt->content_wtr);
444 }
445
446 static void
447 write_table_item_text (struct odt_driver *odt,
448                        const struct table_item_text *text)
449 {
450   if (!text)
451     return;
452
453   xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
454   xmlTextWriterWriteFormatAttribute (odt->content_wtr,
455                                      _xml("text:outline-level"), "%d", 2);
456   xmlTextWriterWriteString (odt->content_wtr, _xml (text->content) );
457   for (size_t i = 0; i < text->n_footnotes; i++)
458     write_footnote (odt, text->footnotes[i]);
459   xmlTextWriterEndElement (odt->content_wtr);
460 }
461
462 static void
463 write_table (struct odt_driver *odt, const struct table_item *item)
464 {
465   const struct table *tab = table_item_get_table (item);
466   int r, c;
467
468   /* Write a heading for the table */
469   write_table_item_text (odt, table_item_get_title (item));
470   write_table_item_text (odt, table_item_get_layers (item));
471
472   /* Start table */
473   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
474   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
475                                      "TABLE-%d", odt->table_num++);
476
477
478   /* Start column definitions */
479   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
480   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
481   xmlTextWriterEndElement (odt->content_wtr);
482
483
484   /* Deal with row headers */
485   if ( table_ht (tab) > 0)
486     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
487
488
489   /* Write all the rows */
490   for (r = 0 ; r < table_nr (tab); ++r)
491     {
492       /* Start row definition */
493       xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
494
495       /* Write all the columns */
496       for (c = 0 ; c < table_nc (tab) ; ++c)
497         {
498           struct table_cell cell;
499           size_t i;
500
501           table_get_cell (tab, c, r, &cell);
502
503           if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
504             {
505               int colspan = table_cell_colspan (&cell);
506               int rowspan = table_cell_rowspan (&cell);
507
508               xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
509               xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
510
511               if (colspan > 1)
512                 xmlTextWriterWriteFormatAttribute (
513                   odt->content_wtr, _xml("table:number-columns-spanned"),
514                   "%d", colspan);
515
516               if (rowspan > 1)
517                 xmlTextWriterWriteFormatAttribute (
518                   odt->content_wtr, _xml("table:number-rows-spanned"),
519                   "%d", rowspan);
520
521               for (i = 0; i < cell.n_contents; i++)
522                 {
523                   const struct cell_contents *contents = &cell.contents[i];
524                   int j;
525
526                   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
527
528                   if ( r < table_ht (tab) || c < table_hl (tab) )
529                     xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
530                   else
531                     xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
532
533                   if (contents->options & TAB_MARKUP)
534                     {
535                       /* XXX */
536                       char *s = output_get_text_from_markup (
537                         contents->text);
538                       write_xml_with_line_breaks (odt, s);
539                       free (s);
540                     }
541                   else
542                     write_xml_with_line_breaks (odt, contents->text);
543
544                   for (j = 0; j < contents->n_footnotes; j++)
545                     write_footnote (odt, contents->footnotes[j]);
546
547                   xmlTextWriterEndElement (odt->content_wtr); /* text:p */
548                 }
549               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
550             }
551           else
552             {
553               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
554               xmlTextWriterEndElement (odt->content_wtr);
555             }
556
557           table_cell_free (&cell);
558         }
559
560       xmlTextWriterEndElement (odt->content_wtr); /* row */
561
562       if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
563         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
564     }
565
566   xmlTextWriterEndElement (odt->content_wtr); /* table */
567
568   /* Write a caption for the table */
569   write_table_item_text (odt, table_item_get_caption (item));
570 }
571
572 static void
573 odt_output_text (struct odt_driver *odt, const char *text)
574 {
575   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
576   xmlTextWriterWriteString (odt->content_wtr, _xml(text));
577   xmlTextWriterEndElement (odt->content_wtr);
578 }
579
580 /* Submit a table to the ODT driver */
581 static void
582 odt_submit (struct output_driver *driver,
583             const struct output_item *output_item)
584 {
585   struct odt_driver *odt = odt_driver_cast (driver);
586
587   if (is_table_item (output_item))
588     write_table (odt, to_table_item (output_item));
589   else if (is_text_item (output_item))
590     odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
591   else if (is_message_item (output_item))
592     {
593       const struct message_item *message_item = to_message_item (output_item);
594       char *s = msg_to_string (message_item_get_msg (message_item));
595       odt_output_text (odt, s);
596       free (s);
597     }
598 }
599
600 struct output_driver_factory odt_driver_factory =
601   { "odt", "pspp.odf", odt_create };
602
603 static const struct output_driver_class odt_driver_class =
604 {
605   "odf",
606   odt_destroy,
607   odt_submit,
608   NULL,
609 };