1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
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.
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.
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/>. */
19 /* A driver for creating OpenDocument Format text files from PSPP's output */
23 #include <libxml/xmlwriter.h>
28 #include <sys/types.h>
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/options.h"
42 #include "output/output-item.h"
43 #include "output/pivot-table.h"
44 #include "output/pivot-output.h"
45 #include "output/table-provider.h"
47 #include "gl/xalloc.h"
50 #define _(msgid) gettext (msgid)
52 #define _xml(X) (CHAR_CAST (const xmlChar *, X))
54 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
60 struct output_driver driver;
62 struct zip_writer *zip; /* ZIP file writer. */
63 struct file_handle *handle; /* Handle for 'file_name'. */
64 char *file_name; /* Output file name. */
67 xmlTextWriterPtr content_wtr; /* XML writer. */
68 FILE *content_file; /* Temporary file. */
71 xmlTextWriterPtr manifest_wtr; /* XML writer. */
72 FILE *manifest_file; /* Temporary file. */
74 /* Number of tables so far. */
78 static const struct output_driver_class odt_driver_class;
80 static struct odt_driver *
81 odt_driver_cast (struct output_driver *driver)
83 assert (driver->class == &odt_driver_class);
84 return UP_CAST (driver, struct odt_driver, driver);
87 /* Creates a new temporary file and stores it in *FILE, then creates an XML
88 writer for it and stores it in *W. */
90 create_writer (FILE **file, xmlTextWriterPtr *w)
92 /* XXX this can fail */
93 *file = create_temp_file ();
94 *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
96 xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
101 register_file (struct odt_driver *odt, const char *filename)
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);
111 write_style_data (struct odt_driver *odt)
116 create_writer (&file, &w);
117 register_file (odt, "styles.xml");
119 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
120 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
121 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
123 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
124 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
126 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
127 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"));
129 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
133 xmlTextWriterStartElement (w, _xml ("office:styles"));
137 xmlTextWriterStartElement (w, _xml ("style:style"));
138 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
141 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
144 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
147 xmlTextWriterEndElement (w); /* style:style */
151 xmlTextWriterStartElement (w, _xml ("style:style"));
152 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
153 _xml ("Table_20_Contents"));
155 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
156 _xml ("Table Contents"));
158 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
161 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
164 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
167 xmlTextWriterEndElement (w); /* style:style */
171 xmlTextWriterStartElement (w, _xml ("style:style"));
172 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
173 _xml ("Table_20_Heading"));
175 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
176 _xml ("Table Heading"));
178 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
181 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
182 _xml ("Table_20_Contents"));
184 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
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 */
194 xmlTextWriterEndElement (w); /* style:style */
198 xmlTextWriterStartElement (w, _xml ("style:style"));
199 xmlTextWriterWriteAttribute (w, _xml ("style:name"), _xml ("superscript"));
200 xmlTextWriterWriteAttribute (w, _xml ("style:family"), _xml ("text"));
202 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
203 xmlTextWriterWriteAttribute (w, _xml ("style:text-position"),
205 xmlTextWriterEndElement (w); /* style:text-properties */
207 xmlTextWriterEndElement (w); /* style:style */
210 xmlTextWriterEndElement (w); /* office:styles */
211 xmlTextWriterEndElement (w); /* office:document-styles */
213 xmlTextWriterEndDocument (w);
214 xmlFreeTextWriter (w);
215 zip_writer_add (odt->zip, file, "styles.xml");
216 close_temp_file (file);
220 write_meta_data (struct odt_driver *odt)
225 create_writer (&file, &w);
226 register_file (odt, "meta.xml");
228 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
229 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
230 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
231 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
232 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
233 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
235 xmlTextWriterStartElement (w, _xml ("office:meta"));
237 xmlTextWriterStartElement (w, _xml ("meta:generator"));
238 xmlTextWriterWriteString (w, _xml (version));
239 xmlTextWriterEndElement (w);
245 time_t t = time (NULL);
246 struct tm *tm = localtime (&t);
248 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
250 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
251 xmlTextWriterWriteString (w, _xml (buf));
252 xmlTextWriterEndElement (w);
254 xmlTextWriterStartElement (w, _xml ("dc:date"));
255 xmlTextWriterWriteString (w, _xml (buf));
256 xmlTextWriterEndElement (w);
261 struct passwd *pw = getpwuid (getuid ());
264 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
265 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
266 xmlTextWriterEndElement (w);
268 xmlTextWriterStartElement (w, _xml ("dc:creator"));
269 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
270 xmlTextWriterEndElement (w);
275 xmlTextWriterEndElement (w);
276 xmlTextWriterEndElement (w);
277 xmlTextWriterEndDocument (w);
278 xmlFreeTextWriter (w);
279 zip_writer_add (odt->zip, file, "meta.xml");
280 close_temp_file (file);
283 static struct output_driver *
284 odt_create (struct file_handle *fh, enum settings_output_devices device_type,
285 struct string_map *o UNUSED)
287 struct output_driver *d;
288 struct odt_driver *odt;
289 struct zip_writer *zip;
290 const char *file_name = fh_get_file_name (fh);
292 zip = zip_writer_create (file_name);
296 odt = xzalloc (sizeof *odt);
299 output_driver_init (d, &odt_driver_class, file_name, device_type);
303 odt->file_name = xstrdup (file_name);
305 zip_writer_add_string (zip, "mimetype",
306 "application/vnd.oasis.opendocument.text");
308 /* Create the manifest */
309 create_writer (&odt->manifest_file, &odt->manifest_wtr);
311 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
312 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
313 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
316 /* Add a manifest entry for the document as a whole */
317 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
318 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
319 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
320 xmlTextWriterEndElement (odt->manifest_wtr);
323 write_meta_data (odt);
324 write_style_data (odt);
326 create_writer (&odt->content_file, &odt->content_wtr);
327 register_file (odt, "content.xml");
330 /* Some necessary junk at the start */
331 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
332 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
333 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
335 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
336 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
338 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
339 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
341 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
343 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
344 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
348 /* Close the manifest */
349 xmlTextWriterEndElement (odt->manifest_wtr);
350 xmlTextWriterEndDocument (odt->manifest_wtr);
351 xmlFreeTextWriter (odt->manifest_wtr);
352 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
353 close_temp_file (odt->manifest_file);
359 odt_destroy (struct output_driver *driver)
361 struct odt_driver *odt = odt_driver_cast (driver);
363 if (odt->content_wtr != NULL)
365 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
366 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
367 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
369 xmlTextWriterEndDocument (odt->content_wtr);
370 xmlFreeTextWriter (odt->content_wtr);
371 zip_writer_add (odt->zip, odt->content_file, "content.xml");
372 close_temp_file (odt->content_file);
374 zip_writer_close (odt->zip);
377 fh_unref (odt->handle);
378 free (odt->file_name);
383 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
385 xmlTextWriterPtr writer = odt->content_wtr;
387 if (!strchr (line_, '\n'))
388 xmlTextWriterWriteString (writer, _xml(line_));
391 char *line = xstrdup (line_);
395 for (p = line; *p; p = newline + 1)
397 newline = strchr (p, '\n');
401 xmlTextWriterWriteString (writer, _xml(p));
406 if (newline > p && newline[-1] == '\r')
410 xmlTextWriterWriteString (writer, _xml(p));
411 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
417 write_footnotes (struct odt_driver *odt,
418 const struct pivot_table *pt,
419 const size_t *footnote_indexes,
422 for (size_t i = 0; i < n_footnotes; i++)
424 const struct pivot_footnote *f = pt->footnotes[footnote_indexes[i]];
427 xmlTextWriterStartElement (odt->content_wtr, _xml("text:span"));
428 xmlTextWriterWriteAttribute (odt->content_wtr,
429 _xml("text:style-name"),
430 _xml("superscript"));
431 char *s = pivot_footnote_marker_string (f, pt);
432 write_xml_with_line_breaks (odt, s);
434 xmlTextWriterEndElement (odt->content_wtr);
440 write_table_item_cell (struct odt_driver *odt,
441 const struct pivot_table *pt,
442 const struct table_cell *cell)
444 struct string body = DS_EMPTY_INITIALIZER;
445 pivot_value_format_body (cell->value, pt, &body);
446 xmlTextWriterWriteString (odt->content_wtr, _xml (ds_cstr (&body)));
449 const struct pivot_value_ex *ex = pivot_value_ex (cell->value);
450 write_footnotes (odt, pt, ex->footnote_indexes, ex->n_footnotes);
454 write_table__ (struct odt_driver *odt, const struct pivot_table *pt,
455 const struct table *t)
459 for (size_t y = 0; y < t->n[V]; y++)
461 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
462 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
463 _xml("text:outline-level"), "%d", 2);
465 struct table_cell cell;
466 table_get_cell (t, 0, y, &cell);
467 write_table_item_cell (odt, pt, &cell);
469 xmlTextWriterEndElement (odt->content_wtr);
475 write_table_layer (struct odt_driver *odt, const struct pivot_table *pt,
476 const size_t *layer_indexes)
478 struct table *title, *layers, *body, *caption, *footnotes;
479 pivot_output (pt, layer_indexes, true, &title, &layers, &body,
480 &caption, &footnotes, NULL, NULL);
482 /* Write a heading for the table */
483 write_table__ (odt, pt, title);
484 write_table__ (odt, pt, layers);
487 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
488 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
489 "TABLE-%d", odt->table_num++);
492 /* Start column definitions */
493 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
494 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", body->n[H]);
495 xmlTextWriterEndElement (odt->content_wtr);
498 /* Deal with row headers */
499 if (body->h[V][0] > 0)
500 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
503 /* Write all the rows */
504 for (int r = 0 ; r < body->n[V]; ++r)
506 /* Start row definition */
507 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
509 /* Write all the columns */
510 for (int c = 0 ; c < body->n[H] ; ++c)
512 struct table_cell cell;
514 table_get_cell (body, c, r, &cell);
516 if (c == cell.d[H][0] && r == cell.d[V][0])
518 int colspan = table_cell_colspan (&cell);
519 int rowspan = table_cell_rowspan (&cell);
521 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
522 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
525 xmlTextWriterWriteFormatAttribute (
526 odt->content_wtr, _xml("table:number-columns-spanned"),
530 xmlTextWriterWriteFormatAttribute (
531 odt->content_wtr, _xml("table:number-rows-spanned"),
534 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
536 if (r < body->h[V][0] || c < body->h[H][0])
537 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
539 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
541 write_table_item_cell (odt, pt, &cell);
543 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
544 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
548 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
549 xmlTextWriterEndElement (odt->content_wtr);
553 xmlTextWriterEndElement (odt->content_wtr); /* row */
555 int ht = body->h[V][0];
556 if (ht > 0 && r == ht - 1)
557 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
560 xmlTextWriterEndElement (odt->content_wtr); /* table */
562 /* Write a caption for the table */
563 write_table__ (odt, pt, caption);
564 write_table__ (odt, pt, footnotes);
567 table_unref (layers);
569 table_unref (caption);
570 table_unref (footnotes);
574 write_table (struct odt_driver *odt, const struct pivot_table *pt)
576 size_t *layer_indexes;
577 PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, pt, true)
578 write_table_layer (odt, pt, layer_indexes);
582 odt_output_text (struct odt_driver *odt, const char *text)
584 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
585 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
586 xmlTextWriterEndElement (odt->content_wtr);
589 /* Submit a table to the ODT driver */
591 odt_submit (struct output_driver *driver, const struct output_item *item)
593 struct odt_driver *odt = odt_driver_cast (driver);
597 case OUTPUT_ITEM_CHART:
600 case OUTPUT_ITEM_GROUP:
603 case OUTPUT_ITEM_IMAGE:
606 case OUTPUT_ITEM_MESSAGE:
608 char *s = msg_to_string (item->message);
609 odt_output_text (odt, s);
614 case OUTPUT_ITEM_PAGE_BREAK:
617 case OUTPUT_ITEM_TABLE:
618 write_table (odt, item->table);
621 case OUTPUT_ITEM_TEXT:
623 char *text = text_item_get_plain_text (item);
624 odt_output_text (odt, text);
631 struct output_driver_factory odt_driver_factory =
632 { "odt", "pspp.odf", odt_create };
634 static const struct output_driver_class odt_driver_class =
637 .destroy = odt_destroy,
638 .submit = odt_submit,