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/table-item.h"
44 #include "output/table-provider.h"
45 #include "output/text-item.h"
47 #include "gl/xalloc.h"
50 #define _(msgid) gettext (msgid)
52 #define _xml(X) (CHAR_CAST (const xmlChar *, X))
56 struct output_driver driver;
58 struct zip_writer *zip; /* ZIP file writer. */
59 struct file_handle *handle; /* Handle for 'file_name'. */
60 char *file_name; /* Output file name. */
63 xmlTextWriterPtr content_wtr; /* XML writer. */
64 FILE *content_file; /* Temporary file. */
67 xmlTextWriterPtr manifest_wtr; /* XML writer. */
68 FILE *manifest_file; /* Temporary file. */
70 /* Number of tables so far. */
74 static const struct output_driver_class odt_driver_class;
76 static struct odt_driver *
77 odt_driver_cast (struct output_driver *driver)
79 assert (driver->class == &odt_driver_class);
80 return UP_CAST (driver, struct odt_driver, driver);
83 /* Create the "mimetype" file needed by ODF */
85 create_mimetype (struct zip_writer *zip)
89 fp = create_temp_file ();
92 msg_error (errno, _("error creating temporary file"));
96 fprintf (fp, "application/vnd.oasis.opendocument.text");
97 zip_writer_add (zip, fp, "mimetype");
103 /* Creates a new temporary file and stores it in *FILE, then creates an XML
104 writer for it and stores it in *W. */
106 create_writer (FILE **file, xmlTextWriterPtr *w)
108 /* XXX this can fail */
109 *file = create_temp_file ();
110 *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
112 xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
117 register_file (struct odt_driver *odt, const char *filename)
119 assert (odt->manifest_wtr);
120 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
121 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
122 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
123 xmlTextWriterEndElement (odt->manifest_wtr);
127 write_style_data (struct odt_driver *odt)
132 create_writer (&file, &w);
133 register_file (odt, "styles.xml");
135 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
136 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
137 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
139 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
140 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
142 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
143 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
145 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
149 xmlTextWriterStartElement (w, _xml ("office:styles"));
153 xmlTextWriterStartElement (w, _xml ("style:style"));
154 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
157 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
160 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
163 xmlTextWriterEndElement (w); /* style:style */
167 xmlTextWriterStartElement (w, _xml ("style:style"));
168 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
169 _xml ("Table_20_Contents"));
171 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
172 _xml ("Table Contents"));
174 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
177 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
180 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
183 xmlTextWriterEndElement (w); /* style:style */
187 xmlTextWriterStartElement (w, _xml ("style:style"));
188 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
189 _xml ("Table_20_Heading"));
191 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
192 _xml ("Table Heading"));
194 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
197 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
198 _xml ("Table_20_Contents"));
200 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
204 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
205 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
206 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
207 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
208 xmlTextWriterEndElement (w); /* style:text-properties */
210 xmlTextWriterEndElement (w); /* style:style */
214 xmlTextWriterEndElement (w); /* office:styles */
215 xmlTextWriterEndElement (w); /* office:document-styles */
217 xmlTextWriterEndDocument (w);
218 xmlFreeTextWriter (w);
219 zip_writer_add (odt->zip, file, "styles.xml");
220 close_temp_file (file);
224 write_meta_data (struct odt_driver *odt)
229 create_writer (&file, &w);
230 register_file (odt, "meta.xml");
232 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
233 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
234 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
235 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
236 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
237 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
239 xmlTextWriterStartElement (w, _xml ("office:meta"));
241 xmlTextWriterStartElement (w, _xml ("meta:generator"));
242 xmlTextWriterWriteString (w, _xml (version));
243 xmlTextWriterEndElement (w);
249 time_t t = time (NULL);
250 struct tm *tm = localtime (&t);
252 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
254 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
255 xmlTextWriterWriteString (w, _xml (buf));
256 xmlTextWriterEndElement (w);
258 xmlTextWriterStartElement (w, _xml ("dc:date"));
259 xmlTextWriterWriteString (w, _xml (buf));
260 xmlTextWriterEndElement (w);
265 struct passwd *pw = getpwuid (getuid ());
268 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
269 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
270 xmlTextWriterEndElement (w);
272 xmlTextWriterStartElement (w, _xml ("dc:creator"));
273 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
274 xmlTextWriterEndElement (w);
279 xmlTextWriterEndElement (w);
280 xmlTextWriterEndElement (w);
281 xmlTextWriterEndDocument (w);
282 xmlFreeTextWriter (w);
283 zip_writer_add (odt->zip, file, "meta.xml");
284 close_temp_file (file);
287 static struct output_driver *
288 odt_create (struct file_handle *fh, enum settings_output_devices device_type,
289 struct string_map *o UNUSED)
291 struct output_driver *d;
292 struct odt_driver *odt;
293 struct zip_writer *zip;
294 const char *file_name = fh_get_file_name (fh);
296 zip = zip_writer_create (file_name);
300 odt = xzalloc (sizeof *odt);
303 output_driver_init (d, &odt_driver_class, file_name, device_type);
307 odt->file_name = xstrdup (file_name);
309 if (!create_mimetype (zip))
311 output_driver_destroy (d);
315 /* Create the manifest */
316 create_writer (&odt->manifest_file, &odt->manifest_wtr);
318 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
319 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
320 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
323 /* Add a manifest entry for the document as a whole */
324 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
325 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
326 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
327 xmlTextWriterEndElement (odt->manifest_wtr);
330 write_meta_data (odt);
331 write_style_data (odt);
333 create_writer (&odt->content_file, &odt->content_wtr);
334 register_file (odt, "content.xml");
337 /* Some necessary junk at the start */
338 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
339 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
340 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
342 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
343 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
345 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
346 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
348 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
350 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
351 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
355 /* Close the manifest */
356 xmlTextWriterEndElement (odt->manifest_wtr);
357 xmlTextWriterEndDocument (odt->manifest_wtr);
358 xmlFreeTextWriter (odt->manifest_wtr);
359 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
360 close_temp_file (odt->manifest_file);
366 odt_destroy (struct output_driver *driver)
368 struct odt_driver *odt = odt_driver_cast (driver);
370 if (odt->content_wtr != NULL)
372 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
373 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
374 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
376 xmlTextWriterEndDocument (odt->content_wtr);
377 xmlFreeTextWriter (odt->content_wtr);
378 zip_writer_add (odt->zip, odt->content_file, "content.xml");
379 close_temp_file (odt->content_file);
381 zip_writer_close (odt->zip);
384 fh_unref (odt->handle);
385 free (odt->file_name);
390 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
392 xmlTextWriterPtr writer = odt->content_wtr;
394 if (!strchr (line_, '\n'))
395 xmlTextWriterWriteString (writer, _xml(line_));
398 char *line = xstrdup (line_);
402 for (p = line; *p; p = newline + 1)
404 newline = strchr (p, '\n');
408 xmlTextWriterWriteString (writer, _xml(p));
413 if (newline > p && newline[-1] == '\r')
417 xmlTextWriterWriteString (writer, _xml(p));
418 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
424 write_footnote (struct odt_driver *odt, const struct footnote *f)
426 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
427 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
430 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
431 if (strlen (f->marker) > 1)
432 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
435 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
437 xmlTextWriterEndElement (odt->content_wtr);
439 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
440 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
441 write_xml_with_line_breaks (odt, f->content);
442 xmlTextWriterEndElement (odt->content_wtr);
443 xmlTextWriterEndElement (odt->content_wtr);
445 xmlTextWriterEndElement (odt->content_wtr);
449 write_table_item_text (struct odt_driver *odt,
450 const struct table_item_text *text)
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 (text->content) );
459 for (size_t i = 0; i < text->n_footnotes; i++)
460 write_footnote (odt, text->footnotes[i]);
461 xmlTextWriterEndElement (odt->content_wtr);
465 write_table (struct odt_driver *odt, const struct table_item *item)
467 const struct table *tab = table_item_get_table (item);
470 /* Write a heading for the table */
471 write_table_item_text (odt, table_item_get_title (item));
472 write_table_item_text (odt, table_item_get_layers (item));
475 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
476 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
477 "TABLE-%d", odt->table_num++);
480 /* Start column definitions */
481 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
482 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
483 xmlTextWriterEndElement (odt->content_wtr);
486 /* Deal with row headers */
487 if ( table_ht (tab) > 0)
488 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
491 /* Write all the rows */
492 for (r = 0 ; r < table_nr (tab); ++r)
494 /* Start row definition */
495 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
497 /* Write all the columns */
498 for (c = 0 ; c < table_nc (tab) ; ++c)
500 struct table_cell cell;
502 table_get_cell (tab, c, r, &cell);
504 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
506 int colspan = table_cell_colspan (&cell);
507 int rowspan = table_cell_rowspan (&cell);
509 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
510 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
513 xmlTextWriterWriteFormatAttribute (
514 odt->content_wtr, _xml("table:number-columns-spanned"),
518 xmlTextWriterWriteFormatAttribute (
519 odt->content_wtr, _xml("table:number-rows-spanned"),
522 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
524 if ( r < table_ht (tab) || c < table_hl (tab) )
525 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
527 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
529 if (cell.options & TAB_MARKUP)
532 char *s = output_get_text_from_markup (cell.text);
533 write_xml_with_line_breaks (odt, s);
537 write_xml_with_line_breaks (odt, cell.text);
539 for (int i = 0; i < cell.n_footnotes; i++)
540 write_footnote (odt, cell.footnotes[i]);
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);
551 table_cell_free (&cell);
554 xmlTextWriterEndElement (odt->content_wtr); /* row */
556 if ( table_ht (tab) > 0 && r == table_ht (tab) - 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_item_text (odt, table_item_get_caption (item));
567 odt_output_text (struct odt_driver *odt, const char *text)
569 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
570 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
571 xmlTextWriterEndElement (odt->content_wtr);
574 /* Submit a table to the ODT driver */
576 odt_submit (struct output_driver *driver,
577 const struct output_item *output_item)
579 struct odt_driver *odt = odt_driver_cast (driver);
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))
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);
594 struct output_driver_factory odt_driver_factory =
595 { "odt", "pspp.odf", odt_create };
597 static const struct output_driver_class odt_driver_class =