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