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 */
154 xmlTextWriterStartElement (w, _xml ("style:style"));
155 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
156 _xml ("Table_20_Contents"));
158 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
159 _xml ("Table Contents"));
161 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
164 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
167 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
170 xmlTextWriterEndElement (w); /* style:style */
174 xmlTextWriterStartElement (w, _xml ("style:style"));
175 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
176 _xml ("Table_20_Heading"));
178 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
179 _xml ("Table Heading"));
181 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
184 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
185 _xml ("Table_20_Contents"));
187 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
191 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
192 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
193 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
194 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
195 xmlTextWriterEndElement (w); /* style:text-properties */
197 xmlTextWriterEndElement (w); /* style:style */
201 xmlTextWriterStartElement (w, _xml ("style:style"));
202 xmlTextWriterWriteAttribute (w, _xml ("style:name"), _xml ("superscript"));
203 xmlTextWriterWriteAttribute (w, _xml ("style:family"), _xml ("text"));
205 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
206 xmlTextWriterWriteAttribute (w, _xml ("style:text-position"),
208 xmlTextWriterEndElement (w); /* style:text-properties */
210 xmlTextWriterEndElement (w); /* style:style */
213 xmlTextWriterEndElement (w); /* office:styles */
214 xmlTextWriterEndElement (w); /* office:document-styles */
216 xmlTextWriterEndDocument (w);
217 xmlFreeTextWriter (w);
218 zip_writer_add (odt->zip, file, "styles.xml");
219 close_temp_file (file);
223 write_meta_data (struct odt_driver *odt)
228 create_writer (&file, &w);
229 register_file (odt, "meta.xml");
231 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
232 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
233 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
234 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
235 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
236 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
238 xmlTextWriterStartElement (w, _xml ("office:meta"));
240 xmlTextWriterStartElement (w, _xml ("meta:generator"));
241 xmlTextWriterWriteString (w, _xml (version));
242 xmlTextWriterEndElement (w);
248 time_t t = time (NULL);
249 struct tm *tm = localtime (&t);
251 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
253 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
254 xmlTextWriterWriteString (w, _xml (buf));
255 xmlTextWriterEndElement (w);
257 xmlTextWriterStartElement (w, _xml ("dc:date"));
258 xmlTextWriterWriteString (w, _xml (buf));
259 xmlTextWriterEndElement (w);
264 struct passwd *pw = getpwuid (getuid ());
267 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
268 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
269 xmlTextWriterEndElement (w);
271 xmlTextWriterStartElement (w, _xml ("dc:creator"));
272 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
273 xmlTextWriterEndElement (w);
278 xmlTextWriterEndElement (w);
279 xmlTextWriterEndElement (w);
280 xmlTextWriterEndDocument (w);
281 xmlFreeTextWriter (w);
282 zip_writer_add (odt->zip, file, "meta.xml");
283 close_temp_file (file);
286 static struct output_driver *
287 odt_create (struct file_handle *fh, enum settings_output_devices device_type,
288 struct string_map *o UNUSED)
290 struct output_driver *d;
291 struct odt_driver *odt;
292 struct zip_writer *zip;
293 const char *file_name = fh_get_file_name (fh);
295 zip = zip_writer_create (file_name);
299 odt = xzalloc (sizeof *odt);
302 output_driver_init (d, &odt_driver_class, file_name, device_type);
306 odt->file_name = xstrdup (file_name);
308 zip_writer_add_string (zip, "mimetype",
309 "application/vnd.oasis.opendocument.text");
311 /* Create the manifest */
312 create_writer (&odt->manifest_file, &odt->manifest_wtr);
314 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
315 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
316 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
319 /* Add a manifest entry for the document as a whole */
320 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
321 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
322 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
323 xmlTextWriterEndElement (odt->manifest_wtr);
326 write_meta_data (odt);
327 write_style_data (odt);
329 create_writer (&odt->content_file, &odt->content_wtr);
330 register_file (odt, "content.xml");
333 /* Some necessary junk at the start */
334 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
335 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
336 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
338 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
339 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
341 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
342 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
344 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
346 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
347 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
351 /* Close the manifest */
352 xmlTextWriterEndElement (odt->manifest_wtr);
353 xmlTextWriterEndDocument (odt->manifest_wtr);
354 xmlFreeTextWriter (odt->manifest_wtr);
355 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
356 close_temp_file (odt->manifest_file);
362 odt_destroy (struct output_driver *driver)
364 struct odt_driver *odt = odt_driver_cast (driver);
366 if (odt->content_wtr != NULL)
368 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
369 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
370 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
372 xmlTextWriterEndDocument (odt->content_wtr);
373 xmlFreeTextWriter (odt->content_wtr);
374 zip_writer_add (odt->zip, odt->content_file, "content.xml");
375 close_temp_file (odt->content_file);
377 zip_writer_close (odt->zip);
380 fh_unref (odt->handle);
381 free (odt->file_name);
386 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
388 xmlTextWriterPtr writer = odt->content_wtr;
390 if (!strchr (line_, '\n'))
391 xmlTextWriterWriteString (writer, _xml(line_));
394 char *line = xstrdup (line_);
398 for (p = line; *p; p = newline + 1)
400 newline = strchr (p, '\n');
404 xmlTextWriterWriteString (writer, _xml(p));
409 if (newline > p && newline[-1] == '\r')
413 xmlTextWriterWriteString (writer, _xml(p));
414 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
420 write_footnotes (struct odt_driver *odt, struct pivot_footnote **footnotes,
423 for (size_t i = 0; i < n_footnotes; i++)
425 xmlTextWriterStartElement (odt->content_wtr, _xml("text:span"));
426 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"),
427 _xml("superscript"));
428 char *s = pivot_value_to_string (footnotes[i]->marker,
429 SETTINGS_VALUE_SHOW_DEFAULT,
430 SETTINGS_VALUE_SHOW_DEFAULT);
431 write_xml_with_line_breaks (odt, s);
433 xmlTextWriterEndElement (odt->content_wtr);
438 write_table_item_cell (struct odt_driver *odt,
439 const struct table_cell *cell)
444 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
445 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
446 _xml("text:outline-level"), "%d", 2);
447 xmlTextWriterWriteString (odt->content_wtr, _xml (cell->text));
448 write_footnotes (odt, cell->footnotes, cell->n_footnotes);
449 xmlTextWriterEndElement (odt->content_wtr);
453 write_table__ (struct odt_driver *odt, const struct table *t)
457 for (size_t y = 0; y < t->n[V]; y++)
459 struct table_cell cell;
460 table_get_cell (t, 0, y, &cell);
461 write_table_item_cell (odt, &cell);
467 write_table_layer (struct odt_driver *odt, const struct pivot_table *pt,
468 const size_t *layer_indexes)
470 struct table *title, *layers, *body, *caption, *footnotes;
471 pivot_output (pt, layer_indexes, true, &title, &layers, &body,
472 &caption, &footnotes, NULL, NULL);
474 /* Write a heading for the table */
475 write_table__ (odt, title);
476 write_table__ (odt, layers);
479 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
480 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
481 "TABLE-%d", odt->table_num++);
484 /* Start column definitions */
485 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
486 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", body->n[H]);
487 xmlTextWriterEndElement (odt->content_wtr);
490 /* Deal with row headers */
491 if (body->h[V][0] > 0)
492 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
495 /* Write all the rows */
496 for (int r = 0 ; r < body->n[V]; ++r)
498 /* Start row definition */
499 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
501 /* Write all the columns */
502 for (int c = 0 ; c < body->n[H] ; ++c)
504 struct table_cell cell;
506 table_get_cell (body, c, r, &cell);
508 if (c == cell.d[H][0] && r == cell.d[V][0])
510 int colspan = table_cell_colspan (&cell);
511 int rowspan = table_cell_rowspan (&cell);
513 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
514 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
517 xmlTextWriterWriteFormatAttribute (
518 odt->content_wtr, _xml("table:number-columns-spanned"),
522 xmlTextWriterWriteFormatAttribute (
523 odt->content_wtr, _xml("table:number-rows-spanned"),
526 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
528 if (r < body->h[V][0] || c < body->h[H][0])
529 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
531 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
533 if (cell.options & TAB_MARKUP)
536 char *s = output_get_text_from_markup (cell.text);
537 write_xml_with_line_breaks (odt, s);
541 write_xml_with_line_breaks (odt, cell.text);
543 write_footnotes (odt, cell.footnotes, cell.n_footnotes);
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, caption);
566 write_table__ (odt, 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))
601 odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
602 else if (is_message_item (output_item))
604 const struct message_item *message_item = to_message_item (output_item);
605 char *s = msg_to_string (message_item_get_msg (message_item));
606 odt_output_text (odt, s);
611 struct output_driver_factory odt_driver_factory =
612 { "odt", "pspp.odf", odt_create };
614 static const struct output_driver_class odt_driver_class =