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 driver_options *o UNUSED)
287 const char *file_name = fh_get_file_name (fh);
288 struct zip_writer *zip = zip_writer_create (file_name);
292 struct odt_driver *odt = xmalloc (sizeof *odt);
293 *odt = (struct odt_driver) {
295 .class = &odt_driver_class,
296 .name = xstrdup (file_name),
297 .device_type = device_type,
301 .file_name = xstrdup (file_name),
304 zip_writer_add_string (zip, "mimetype",
305 "application/vnd.oasis.opendocument.text");
307 /* Create the manifest */
308 create_writer (&odt->manifest_file, &odt->manifest_wtr);
310 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
311 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
312 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
315 /* Add a manifest entry for the document as a whole */
316 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
317 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
318 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
319 xmlTextWriterEndElement (odt->manifest_wtr);
322 write_meta_data (odt);
323 write_style_data (odt);
325 create_writer (&odt->content_file, &odt->content_wtr);
326 register_file (odt, "content.xml");
329 /* Some necessary junk at the start */
330 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
331 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
332 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
334 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
335 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
337 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
338 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
340 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
342 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
343 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
347 /* Close the manifest */
348 xmlTextWriterEndElement (odt->manifest_wtr);
349 xmlTextWriterEndDocument (odt->manifest_wtr);
350 xmlFreeTextWriter (odt->manifest_wtr);
351 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
352 close_temp_file (odt->manifest_file);
358 odt_destroy (struct output_driver *driver)
360 struct odt_driver *odt = odt_driver_cast (driver);
362 if (odt->content_wtr != NULL)
364 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
365 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
366 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
368 xmlTextWriterEndDocument (odt->content_wtr);
369 xmlFreeTextWriter (odt->content_wtr);
370 zip_writer_add (odt->zip, odt->content_file, "content.xml");
371 close_temp_file (odt->content_file);
373 zip_writer_close (odt->zip);
376 fh_unref (odt->handle);
377 free (odt->file_name);
382 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
384 xmlTextWriterPtr writer = odt->content_wtr;
386 if (!strchr (line_, '\n'))
387 xmlTextWriterWriteString (writer, _xml(line_));
390 char *line = xstrdup (line_);
394 for (p = line; *p; p = newline + 1)
396 newline = strchr (p, '\n');
400 xmlTextWriterWriteString (writer, _xml(p));
405 if (newline > p && newline[-1] == '\r')
409 xmlTextWriterWriteString (writer, _xml(p));
410 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
416 write_footnotes (struct odt_driver *odt,
417 const struct pivot_table *pt,
418 const size_t *footnote_indexes,
421 for (size_t i = 0; i < n_footnotes; i++)
423 const struct pivot_footnote *f = pt->footnotes[footnote_indexes[i]];
426 xmlTextWriterStartElement (odt->content_wtr, _xml("text:span"));
427 xmlTextWriterWriteAttribute (odt->content_wtr,
428 _xml("text:style-name"),
429 _xml("superscript"));
430 char *s = pivot_footnote_marker_string (f, pt);
431 write_xml_with_line_breaks (odt, s);
433 xmlTextWriterEndElement (odt->content_wtr);
439 write_table_item_cell (struct odt_driver *odt,
440 const struct pivot_table *pt,
441 const struct table_cell *cell)
443 struct string body = DS_EMPTY_INITIALIZER;
444 pivot_value_format_body (cell->value, pt, &body);
445 xmlTextWriterWriteString (odt->content_wtr, _xml (ds_cstr (&body)));
448 const struct pivot_value_ex *ex = pivot_value_ex (cell->value);
449 write_footnotes (odt, pt, ex->footnote_indexes, ex->n_footnotes);
453 write_table__ (struct odt_driver *odt, const struct pivot_table *pt,
454 const struct table *t)
458 for (size_t y = 0; y < t->n[V]; y++)
460 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
461 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
462 _xml("text:outline-level"), "%d", 2);
464 struct table_cell cell;
465 table_get_cell (t, 0, y, &cell);
466 write_table_item_cell (odt, pt, &cell);
468 xmlTextWriterEndElement (odt->content_wtr);
474 write_table_layer (struct odt_driver *odt, const struct pivot_table *pt,
475 const size_t *layer_indexes)
477 struct table *title, *layers, *body, *caption, *footnotes;
478 pivot_output (pt, layer_indexes, true, &title, &layers, &body,
479 &caption, &footnotes, NULL, NULL);
481 /* Write a heading for the table */
482 write_table__ (odt, pt, title);
483 write_table__ (odt, pt, layers);
486 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
487 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
488 "TABLE-%d", odt->table_num++);
491 /* Start column definitions */
492 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
493 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", body->n[H]);
494 xmlTextWriterEndElement (odt->content_wtr);
497 /* Deal with row headers */
498 if (body->h[V][0] > 0)
499 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
502 /* Write all the rows */
503 for (int r = 0 ; r < body->n[V]; ++r)
505 /* Start row definition */
506 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
508 /* Write all the columns */
509 for (int c = 0 ; c < body->n[H] ; ++c)
511 struct table_cell cell;
513 table_get_cell (body, c, r, &cell);
515 if (c == cell.d[H][0] && r == cell.d[V][0])
517 int colspan = table_cell_colspan (&cell);
518 int rowspan = table_cell_rowspan (&cell);
520 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
521 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
524 xmlTextWriterWriteFormatAttribute (
525 odt->content_wtr, _xml("table:number-columns-spanned"),
529 xmlTextWriterWriteFormatAttribute (
530 odt->content_wtr, _xml("table:number-rows-spanned"),
533 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
535 if (r < body->h[V][0] || c < body->h[H][0])
536 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
538 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
540 write_table_item_cell (odt, pt, &cell);
542 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
543 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
547 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
548 xmlTextWriterEndElement (odt->content_wtr);
552 xmlTextWriterEndElement (odt->content_wtr); /* row */
554 int ht = body->h[V][0];
555 if (ht > 0 && r == ht - 1)
556 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
559 xmlTextWriterEndElement (odt->content_wtr); /* table */
561 /* Write a caption for the table */
562 write_table__ (odt, pt, caption);
563 write_table__ (odt, pt, footnotes);
566 table_unref (layers);
568 table_unref (caption);
569 table_unref (footnotes);
573 write_table (struct odt_driver *odt, const struct pivot_table *pt)
575 size_t *layer_indexes;
576 PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, pt, true)
577 write_table_layer (odt, pt, layer_indexes);
581 odt_output_text (struct odt_driver *odt, const char *text)
583 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
584 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
585 xmlTextWriterEndElement (odt->content_wtr);
588 /* Submit a table to the ODT driver */
590 odt_submit (struct output_driver *driver, const struct output_item *item)
592 struct odt_driver *odt = odt_driver_cast (driver);
596 case OUTPUT_ITEM_CHART:
599 case OUTPUT_ITEM_GROUP:
602 case OUTPUT_ITEM_IMAGE:
605 case OUTPUT_ITEM_MESSAGE:
607 char *s = msg_to_string (item->message);
608 odt_output_text (odt, s);
613 case OUTPUT_ITEM_PAGE_BREAK:
616 case OUTPUT_ITEM_TABLE:
617 write_table (odt, item->table);
620 case OUTPUT_ITEM_TEXT:
622 char *text = text_item_get_plain_text (item);
623 odt_output_text (odt, text);
630 struct output_driver_factory odt_driver_factory =
631 { "odt", "pspp.odf", odt_create };
633 static const struct output_driver_class odt_driver_class =
636 .destroy = odt_destroy,
637 .submit = odt_submit,