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/message-item.h"
42 #include "output/options.h"
43 #include "output/pivot-table.h"
44 #include "output/pivot-output.h"
45 #include "output/table-item.h"
46 #include "output/table-provider.h"
47 #include "output/text-item.h"
49 #include "gl/xalloc.h"
52 #define _(msgid) gettext (msgid)
54 #define _xml(X) (CHAR_CAST (const xmlChar *, X))
56 /* This file uses TABLE_HORZ and TABLE_VERT enough to warrant abbreviating. */
62 struct output_driver driver;
64 struct zip_writer *zip; /* ZIP file writer. */
65 struct file_handle *handle; /* Handle for 'file_name'. */
66 char *file_name; /* Output file name. */
69 xmlTextWriterPtr content_wtr; /* XML writer. */
70 FILE *content_file; /* Temporary file. */
73 xmlTextWriterPtr manifest_wtr; /* XML writer. */
74 FILE *manifest_file; /* Temporary file. */
76 /* Number of tables so far. */
80 static const struct output_driver_class odt_driver_class;
82 static struct odt_driver *
83 odt_driver_cast (struct output_driver *driver)
85 assert (driver->class == &odt_driver_class);
86 return UP_CAST (driver, struct odt_driver, driver);
89 /* Creates a new temporary file and stores it in *FILE, then creates an XML
90 writer for it and stores it in *W. */
92 create_writer (FILE **file, xmlTextWriterPtr *w)
94 /* XXX this can fail */
95 *file = create_temp_file ();
96 *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
98 xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
103 register_file (struct odt_driver *odt, const char *filename)
105 assert (odt->manifest_wtr);
106 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
107 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
108 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
109 xmlTextWriterEndElement (odt->manifest_wtr);
113 write_style_data (struct odt_driver *odt)
118 create_writer (&file, &w);
119 register_file (odt, "styles.xml");
121 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
122 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
123 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
125 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
126 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
128 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
129 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"));
131 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
135 xmlTextWriterStartElement (w, _xml ("office:styles"));
139 xmlTextWriterStartElement (w, _xml ("style:style"));
140 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
143 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
146 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
149 xmlTextWriterEndElement (w); /* style:style */
153 xmlTextWriterStartElement (w, _xml ("style:style"));
154 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
155 _xml ("Table_20_Contents"));
157 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
158 _xml ("Table Contents"));
160 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
163 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
166 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
169 xmlTextWriterEndElement (w); /* style:style */
173 xmlTextWriterStartElement (w, _xml ("style:style"));
174 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
175 _xml ("Table_20_Heading"));
177 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
178 _xml ("Table Heading"));
180 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
183 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
184 _xml ("Table_20_Contents"));
186 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
190 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
191 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
192 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
193 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
194 xmlTextWriterEndElement (w); /* style:text-properties */
196 xmlTextWriterEndElement (w); /* style:style */
200 xmlTextWriterStartElement (w, _xml ("style:style"));
201 xmlTextWriterWriteAttribute (w, _xml ("style:name"), _xml ("superscript"));
202 xmlTextWriterWriteAttribute (w, _xml ("style:family"), _xml ("text"));
204 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
205 xmlTextWriterWriteAttribute (w, _xml ("style:text-position"),
207 xmlTextWriterEndElement (w); /* style:text-properties */
209 xmlTextWriterEndElement (w); /* style:style */
212 xmlTextWriterEndElement (w); /* office:styles */
213 xmlTextWriterEndElement (w); /* office:document-styles */
215 xmlTextWriterEndDocument (w);
216 xmlFreeTextWriter (w);
217 zip_writer_add (odt->zip, file, "styles.xml");
218 close_temp_file (file);
222 write_meta_data (struct odt_driver *odt)
227 create_writer (&file, &w);
228 register_file (odt, "meta.xml");
230 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
231 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
232 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
233 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
234 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
235 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
237 xmlTextWriterStartElement (w, _xml ("office:meta"));
239 xmlTextWriterStartElement (w, _xml ("meta:generator"));
240 xmlTextWriterWriteString (w, _xml (version));
241 xmlTextWriterEndElement (w);
247 time_t t = time (NULL);
248 struct tm *tm = localtime (&t);
250 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
252 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
253 xmlTextWriterWriteString (w, _xml (buf));
254 xmlTextWriterEndElement (w);
256 xmlTextWriterStartElement (w, _xml ("dc:date"));
257 xmlTextWriterWriteString (w, _xml (buf));
258 xmlTextWriterEndElement (w);
263 struct passwd *pw = getpwuid (getuid ());
266 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
267 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
268 xmlTextWriterEndElement (w);
270 xmlTextWriterStartElement (w, _xml ("dc:creator"));
271 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
272 xmlTextWriterEndElement (w);
277 xmlTextWriterEndElement (w);
278 xmlTextWriterEndElement (w);
279 xmlTextWriterEndDocument (w);
280 xmlFreeTextWriter (w);
281 zip_writer_add (odt->zip, file, "meta.xml");
282 close_temp_file (file);
285 static struct output_driver *
286 odt_create (struct file_handle *fh, enum settings_output_devices device_type,
287 struct string_map *o UNUSED)
289 struct output_driver *d;
290 struct odt_driver *odt;
291 struct zip_writer *zip;
292 const char *file_name = fh_get_file_name (fh);
294 zip = zip_writer_create (file_name);
298 odt = xzalloc (sizeof *odt);
301 output_driver_init (d, &odt_driver_class, file_name, device_type);
305 odt->file_name = xstrdup (file_name);
307 zip_writer_add_string (zip, "mimetype",
308 "application/vnd.oasis.opendocument.text");
310 /* Create the manifest */
311 create_writer (&odt->manifest_file, &odt->manifest_wtr);
313 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
314 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
315 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
318 /* Add a manifest entry for the document as a whole */
319 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
320 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
321 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
322 xmlTextWriterEndElement (odt->manifest_wtr);
325 write_meta_data (odt);
326 write_style_data (odt);
328 create_writer (&odt->content_file, &odt->content_wtr);
329 register_file (odt, "content.xml");
332 /* Some necessary junk at the start */
333 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
334 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
335 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
337 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
338 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
340 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
341 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
343 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
345 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
346 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
350 /* Close the manifest */
351 xmlTextWriterEndElement (odt->manifest_wtr);
352 xmlTextWriterEndDocument (odt->manifest_wtr);
353 xmlFreeTextWriter (odt->manifest_wtr);
354 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
355 close_temp_file (odt->manifest_file);
361 odt_destroy (struct output_driver *driver)
363 struct odt_driver *odt = odt_driver_cast (driver);
365 if (odt->content_wtr != NULL)
367 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
368 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
369 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
371 xmlTextWriterEndDocument (odt->content_wtr);
372 xmlFreeTextWriter (odt->content_wtr);
373 zip_writer_add (odt->zip, odt->content_file, "content.xml");
374 close_temp_file (odt->content_file);
376 zip_writer_close (odt->zip);
379 fh_unref (odt->handle);
380 free (odt->file_name);
385 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
387 xmlTextWriterPtr writer = odt->content_wtr;
389 if (!strchr (line_, '\n'))
390 xmlTextWriterWriteString (writer, _xml(line_));
393 char *line = xstrdup (line_);
397 for (p = line; *p; p = newline + 1)
399 newline = strchr (p, '\n');
403 xmlTextWriterWriteString (writer, _xml(p));
408 if (newline > p && newline[-1] == '\r')
412 xmlTextWriterWriteString (writer, _xml(p));
413 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
419 write_footnotes (struct odt_driver *odt,
420 const struct pivot_table *pt,
421 const size_t *footnote_indexes,
424 for (size_t i = 0; i < n_footnotes; i++)
426 const struct pivot_footnote *f = pt->footnotes[footnote_indexes[i]];
429 xmlTextWriterStartElement (odt->content_wtr, _xml("text:span"));
430 xmlTextWriterWriteAttribute (odt->content_wtr,
431 _xml("text:style-name"),
432 _xml("superscript"));
433 char *s = pivot_footnote_marker_string (f, pt);
434 write_xml_with_line_breaks (odt, s);
436 xmlTextWriterEndElement (odt->content_wtr);
442 write_table_item_cell (struct odt_driver *odt,
443 const struct pivot_table *pt,
444 const struct table_cell *cell)
446 struct string body = DS_EMPTY_INITIALIZER;
447 pivot_value_format_body (cell->value, pt, &body);
448 xmlTextWriterWriteString (odt->content_wtr, _xml (ds_cstr (&body)));
451 write_footnotes (odt, pt, cell->value->footnote_indexes,
452 cell->value->n_footnotes);
456 write_table__ (struct odt_driver *odt, const struct pivot_table *pt,
457 const struct table *t)
461 for (size_t y = 0; y < t->n[V]; y++)
463 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
464 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
465 _xml("text:outline-level"), "%d", 2);
467 struct table_cell cell;
468 table_get_cell (t, 0, y, &cell);
469 write_table_item_cell (odt, pt, &cell);
471 xmlTextWriterEndElement (odt->content_wtr);
477 write_table_layer (struct odt_driver *odt, const struct pivot_table *pt,
478 const size_t *layer_indexes)
480 struct table *title, *layers, *body, *caption, *footnotes;
481 pivot_output (pt, layer_indexes, true, &title, &layers, &body,
482 &caption, &footnotes, NULL, NULL);
484 /* Write a heading for the table */
485 write_table__ (odt, pt, title);
486 write_table__ (odt, pt, layers);
489 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
490 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
491 "TABLE-%d", odt->table_num++);
494 /* Start column definitions */
495 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
496 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", body->n[H]);
497 xmlTextWriterEndElement (odt->content_wtr);
500 /* Deal with row headers */
501 if (body->h[V][0] > 0)
502 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
505 /* Write all the rows */
506 for (int r = 0 ; r < body->n[V]; ++r)
508 /* Start row definition */
509 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
511 /* Write all the columns */
512 for (int c = 0 ; c < body->n[H] ; ++c)
514 struct table_cell cell;
516 table_get_cell (body, c, r, &cell);
518 if (c == cell.d[H][0] && r == cell.d[V][0])
520 int colspan = table_cell_colspan (&cell);
521 int rowspan = table_cell_rowspan (&cell);
523 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
524 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
527 xmlTextWriterWriteFormatAttribute (
528 odt->content_wtr, _xml("table:number-columns-spanned"),
532 xmlTextWriterWriteFormatAttribute (
533 odt->content_wtr, _xml("table:number-rows-spanned"),
536 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
538 if (r < body->h[V][0] || c < body->h[H][0])
539 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
541 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
543 write_table_item_cell (odt, pt, &cell);
545 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
546 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
550 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
551 xmlTextWriterEndElement (odt->content_wtr);
555 xmlTextWriterEndElement (odt->content_wtr); /* row */
557 int ht = body->h[V][0];
558 if (ht > 0 && r == ht - 1)
559 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
562 xmlTextWriterEndElement (odt->content_wtr); /* table */
564 /* Write a caption for the table */
565 write_table__ (odt, pt, caption);
566 write_table__ (odt, pt, footnotes);
569 table_unref (layers);
571 table_unref (caption);
572 table_unref (footnotes);
576 write_table (struct odt_driver *odt, const struct table_item *item)
578 size_t *layer_indexes;
579 PIVOT_OUTPUT_FOR_EACH_LAYER (layer_indexes, item->pt, true)
580 write_table_layer (odt, item->pt, layer_indexes);
584 odt_output_text (struct odt_driver *odt, const char *text)
586 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
587 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
588 xmlTextWriterEndElement (odt->content_wtr);
591 /* Submit a table to the ODT driver */
593 odt_submit (struct output_driver *driver,
594 const struct output_item *output_item)
596 struct odt_driver *odt = odt_driver_cast (driver);
598 if (is_table_item (output_item))
599 write_table (odt, to_table_item (output_item));
600 else if (is_text_item (output_item))
602 char *text = text_item_get_plain_text (to_text_item (output_item));
603 odt_output_text (odt, text);
606 else if (is_message_item (output_item))
608 const struct message_item *message_item = to_message_item (output_item);
609 char *s = msg_to_string (message_item_get_msg (message_item));
610 odt_output_text (odt, s);
615 struct output_driver_factory odt_driver_factory =
616 { "odt", "pspp.odf", odt_create };
618 static const struct output_driver_class odt_driver_class =