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