d0860585cd40c35219bfad9533803770e74613bd
[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 "output/driver-provider.h"
40 #include "output/message-item.h"
41 #include "output/options.h"
42 #include "output/tab.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   char *file_name;            /* Output file name. */
60
61   /* content.xml */
62   xmlTextWriterPtr content_wtr; /* XML writer. */
63   FILE *content_file;           /* Temporary file. */
64
65   /* manifest.xml */
66   xmlTextWriterPtr manifest_wtr; /* XML writer. */
67   FILE *manifest_file;           /* Temporary file. */
68
69   /* Number of tables so far. */
70   int table_num;
71
72   /* Name of current command. */
73   char *command_name;
74
75   /* Number of footnotes so far. */
76   int n_footnotes;
77 };
78
79 static const struct output_driver_class odt_driver_class;
80
81 static struct odt_driver *
82 odt_driver_cast (struct output_driver *driver)
83 {
84   assert (driver->class == &odt_driver_class);
85   return UP_CAST (driver, struct odt_driver, driver);
86 }
87
88 /* Create the "mimetype" file needed by ODF */
89 static bool
90 create_mimetype (struct zip_writer *zip)
91 {
92   FILE *fp;
93
94   fp = create_temp_file ();
95   if (fp == NULL)
96     {
97       msg_error (errno, _("error creating temporary file"));
98       return false;
99     }
100
101   fprintf (fp, "application/vnd.oasis.opendocument.text");
102   zip_writer_add (zip, fp, "mimetype");
103   close_temp_file (fp);
104
105   return true;
106 }
107
108 /* Creates a new temporary file and stores it in *FILE, then creates an XML
109    writer for it and stores it in *W. */
110 static void
111 create_writer (FILE **file, xmlTextWriterPtr *w)
112 {
113   /* XXX this can fail */
114   *file = create_temp_file ();
115   *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
116
117   xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
118 }
119
120
121 static void
122 register_file (struct odt_driver *odt, const char *filename)
123 {
124   assert (odt->manifest_wtr);
125   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
126   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("text/xml"));
127   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml (filename));
128   xmlTextWriterEndElement (odt->manifest_wtr);
129 }
130
131 static void
132 write_style_data (struct odt_driver *odt)
133 {
134   xmlTextWriterPtr w;
135   FILE *file;
136
137   create_writer (&file, &w);
138   register_file (odt, "styles.xml");
139
140   xmlTextWriterStartElement (w, _xml ("office:document-styles"));
141   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
142                                _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
143
144   xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
145                                _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
146
147   xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
148                                _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
149
150   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml ("1.1"));
151                                
152
153
154   xmlTextWriterStartElement (w, _xml ("office:styles"));
155
156
157   {
158     xmlTextWriterStartElement (w, _xml ("style:style"));
159     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
160                                  _xml ("Standard"));
161
162     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
163                                  _xml ("paragraph"));
164
165     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
166                                  _xml ("text"));
167
168     xmlTextWriterEndElement (w); /* style:style */
169   }
170
171   {
172     xmlTextWriterStartElement (w, _xml ("style:style"));
173     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
174                                  _xml ("Table_20_Contents"));
175
176     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
177                                  _xml ("Table Contents"));
178
179     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
180                                  _xml ("paragraph"));
181
182     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
183                                  _xml ("Standard"));
184
185     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
186                                  _xml ("extra"));
187
188     xmlTextWriterEndElement (w); /* style:style */
189   }
190
191   {
192     xmlTextWriterStartElement (w, _xml ("style:style"));
193     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
194                                  _xml ("Table_20_Heading"));
195
196     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
197                                  _xml ("Table Heading"));
198
199     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
200                                  _xml ("paragraph"));
201
202     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
203                                  _xml ("Table_20_Contents"));
204
205     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
206                                  _xml ("extra"));
207
208
209     xmlTextWriterStartElement (w, _xml ("style:text-properties"));
210     xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
211     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
212     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
213     xmlTextWriterEndElement (w); /* style:text-properties */
214
215     xmlTextWriterEndElement (w); /* style:style */
216   }
217
218
219   xmlTextWriterEndElement (w); /* office:styles */
220   xmlTextWriterEndElement (w); /* office:document-styles */
221
222   xmlTextWriterEndDocument (w);
223   xmlFreeTextWriter (w);
224   zip_writer_add (odt->zip, file, "styles.xml");
225   close_temp_file (file);
226 }
227
228 static void
229 write_meta_data (struct odt_driver *odt)
230 {
231   xmlTextWriterPtr w;
232   FILE *file;
233
234   create_writer (&file, &w);
235   register_file (odt, "meta.xml");
236
237   xmlTextWriterStartElement (w, _xml ("office:document-meta"));
238   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
239   xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"),  _xml ("http://purl.org/dc/elements/1.1/"));
240   xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
241   xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
242   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml("1.1"));
243
244   xmlTextWriterStartElement (w, _xml ("office:meta"));
245   {
246     xmlTextWriterStartElement (w, _xml ("meta:generator"));
247     xmlTextWriterWriteString (w, _xml (stat_version));
248     xmlTextWriterEndElement (w);
249   }
250
251
252   {
253     char buf[30];
254     time_t t = time (NULL);
255     struct tm *tm =  localtime (&t);
256
257     strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
258
259     xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
260     xmlTextWriterWriteString (w, _xml (buf));
261     xmlTextWriterEndElement (w);
262
263     xmlTextWriterStartElement (w, _xml ("dc:date"));
264     xmlTextWriterWriteString (w, _xml (buf));
265     xmlTextWriterEndElement (w);
266   }
267
268 #ifdef HAVE_PWD_H
269   {
270     struct passwd *pw = getpwuid (getuid ());
271     if (pw != NULL)
272       {
273         xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
274         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
275         xmlTextWriterEndElement (w);
276
277         xmlTextWriterStartElement (w, _xml ("dc:creator"));
278         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
279         xmlTextWriterEndElement (w);
280       }
281   }
282 #endif
283
284   xmlTextWriterEndElement (w);
285   xmlTextWriterEndElement (w);
286   xmlTextWriterEndDocument (w);
287   xmlFreeTextWriter (w);
288   zip_writer_add (odt->zip, file, "meta.xml");
289   close_temp_file (file);
290 }
291
292 static struct output_driver *
293 odt_create (const char *file_name, enum settings_output_devices device_type,
294             struct string_map *o UNUSED)
295 {
296   struct output_driver *d;
297   struct odt_driver *odt;
298   struct zip_writer *zip;
299
300   zip = zip_writer_create (file_name);
301   if (zip == NULL)
302     return NULL;
303
304   odt = xzalloc (sizeof *odt);
305   d = &odt->driver;
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->command_name);
388   free (odt);
389 }
390
391 static void
392 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
393 {
394   xmlTextWriterPtr writer = odt->content_wtr;
395
396   if (!strchr (line_, '\n'))
397     xmlTextWriterWriteString (writer, _xml(line_));
398   else
399     {
400       char *line = xstrdup (line_);
401       char *newline;
402       char *p;
403
404       for (p = line; *p; p = newline + 1)
405         {
406           newline = strchr (p, '\n');
407
408           if (!newline)
409             {
410               xmlTextWriterWriteString (writer, _xml(p));
411               free (line);
412               return;
413             }
414
415           if (newline > p && newline[-1] == '\r')
416             newline[-1] = '\0';
417           else
418             *newline = '\0';
419           xmlTextWriterWriteString (writer, _xml(p));
420           xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
421         }
422     }
423 }
424
425 static void
426 write_footnote (struct odt_driver *odt, const char *footnote)
427 {
428   char marker[16];
429
430   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
431   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
432                                _xml("footnote"));
433
434   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
435   str_format_26adic (++odt->n_footnotes, false, marker, sizeof marker);
436   if (strlen (marker) > 1)
437     xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
438                                        "(%s)", marker);
439   else
440     xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
441                                  _xml(marker));
442   xmlTextWriterEndElement (odt->content_wtr);
443
444   xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
445   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
446   write_xml_with_line_breaks (odt, footnote);
447   xmlTextWriterEndElement (odt->content_wtr);
448   xmlTextWriterEndElement (odt->content_wtr);
449
450   xmlTextWriterEndElement (odt->content_wtr);
451 }
452
453 static void
454 write_table (struct odt_driver *odt, const struct table_item *item)
455 {
456   const struct table *tab = table_item_get_table (item);
457   const char *caption = table_item_get_caption (item);
458   int r, c;
459
460   /* Write a heading for the table */
461   if (caption != 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_caption (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                   if (contents->text)
526                     {
527                       xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
528
529                       if ( r < table_ht (tab) || c < table_hl (tab) )
530                         xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
531                       else
532                         xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
533
534                       write_xml_with_line_breaks (odt, contents->text);
535
536                       for (j = 0; j < contents->n_footnotes; j++)
537                         write_footnote (odt, contents->footnotes[j]);
538
539                       xmlTextWriterEndElement (odt->content_wtr); /* text:p */
540                     }
541                   else if (contents->table)
542                     write_table (odt, contents->table);
543
544                 }
545               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
546             }
547           else
548             {
549               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
550               xmlTextWriterEndElement (odt->content_wtr);
551             }
552
553           table_cell_free (&cell);
554         }
555   
556       xmlTextWriterEndElement (odt->content_wtr); /* row */
557
558       if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
559         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
560     }
561
562   xmlTextWriterEndElement (odt->content_wtr); /* table */
563 }
564
565 static void
566 odt_output_text (struct odt_driver *odt, const char *text)
567 {
568   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
569   xmlTextWriterWriteString (odt->content_wtr, _xml(text));
570   xmlTextWriterEndElement (odt->content_wtr);
571 }
572
573 /* Submit a table to the ODT driver */
574 static void
575 odt_submit (struct output_driver *driver,
576             const struct output_item *output_item)
577 {
578   struct odt_driver *odt = odt_driver_cast (driver);
579
580   output_driver_track_current_command (output_item, &odt->command_name);
581
582   if (is_table_item (output_item))
583     write_table (odt, to_table_item (output_item));
584   else if (is_text_item (output_item))
585     {
586       struct text_item *text_item = to_text_item (output_item);
587
588       if (text_item_get_type (text_item) != TEXT_ITEM_COMMAND_CLOSE)
589         odt_output_text (odt, text_item_get_text (text_item));
590     }
591   else if (is_message_item (output_item))
592     {
593       const struct message_item *message_item = to_message_item (output_item);
594       const struct msg *msg = message_item_get_msg (message_item);
595       char *s = msg_to_string (msg, odt->command_name);
596       odt_output_text (odt, s);
597       free (s);
598     }
599 }
600
601 struct output_driver_factory odt_driver_factory =
602   { "odt", "pspp.odf", odt_create };
603
604 static const struct output_driver_class odt_driver_class =
605 {
606   "odf",
607   odt_destroy,
608   odt_submit,
609   NULL,
610 };