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 char *file_name; /* Output file name. */
62 xmlTextWriterPtr content_wtr; /* XML writer. */
63 FILE *content_file; /* Temporary file. */
66 xmlTextWriterPtr manifest_wtr; /* XML writer. */
67 FILE *manifest_file; /* Temporary file. */
69 /* Number of tables so far. */
73 static const struct output_driver_class odt_driver_class;
75 static struct odt_driver *
76 odt_driver_cast (struct output_driver *driver)
78 assert (driver->class == &odt_driver_class);
79 return UP_CAST (driver, struct odt_driver, driver);
82 /* Create the "mimetype" file needed by ODF */
84 create_mimetype (struct zip_writer *zip)
88 fp = create_temp_file ();
91 msg_error (errno, _("error creating temporary file"));
95 fprintf (fp, "application/vnd.oasis.opendocument.text");
96 zip_writer_add (zip, fp, "mimetype");
102 /* Creates a new temporary file and stores it in *FILE, then creates an XML
103 writer for it and stores it in *W. */
105 create_writer (FILE **file, xmlTextWriterPtr *w)
107 /* XXX this can fail */
108 *file = create_temp_file ();
109 *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
111 xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
116 register_file (struct odt_driver *odt, const char *filename)
118 assert (odt->manifest_wtr);
119 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
120 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
121 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
122 xmlTextWriterEndElement (odt->manifest_wtr);
126 write_style_data (struct odt_driver *odt)
131 create_writer (&file, &w);
132 register_file (odt, "styles.xml");
134 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
135 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
136 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
138 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
139 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
141 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
142 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
144 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
148 xmlTextWriterStartElement (w, _xml ("office:styles"));
152 xmlTextWriterStartElement (w, _xml ("style:style"));
153 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
156 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
159 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
162 xmlTextWriterEndElement (w); /* style:style */
166 xmlTextWriterStartElement (w, _xml ("style:style"));
167 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
168 _xml ("Table_20_Contents"));
170 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
171 _xml ("Table Contents"));
173 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
176 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
179 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
182 xmlTextWriterEndElement (w); /* style:style */
186 xmlTextWriterStartElement (w, _xml ("style:style"));
187 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
188 _xml ("Table_20_Heading"));
190 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
191 _xml ("Table Heading"));
193 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
196 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
197 _xml ("Table_20_Contents"));
199 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
203 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
204 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
205 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
206 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
207 xmlTextWriterEndElement (w); /* style:text-properties */
209 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);
305 odt->file_name = xstrdup (file_name);
307 if (!create_mimetype (zip))
309 output_driver_destroy (d);
313 /* Create the manifest */
314 create_writer (&odt->manifest_file, &odt->manifest_wtr);
316 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
317 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
318 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
321 /* Add a manifest entry for the document as a whole */
322 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
323 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
324 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
325 xmlTextWriterEndElement (odt->manifest_wtr);
328 write_meta_data (odt);
329 write_style_data (odt);
331 create_writer (&odt->content_file, &odt->content_wtr);
332 register_file (odt, "content.xml");
335 /* Some necessary junk at the start */
336 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
337 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
338 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
340 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
341 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
343 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
344 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
346 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
348 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
349 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
353 /* Close the manifest */
354 xmlTextWriterEndElement (odt->manifest_wtr);
355 xmlTextWriterEndDocument (odt->manifest_wtr);
356 xmlFreeTextWriter (odt->manifest_wtr);
357 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
358 close_temp_file (odt->manifest_file);
364 odt_destroy (struct output_driver *driver)
366 struct odt_driver *odt = odt_driver_cast (driver);
368 if (odt->content_wtr != NULL)
370 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
371 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
372 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
374 xmlTextWriterEndDocument (odt->content_wtr);
375 xmlFreeTextWriter (odt->content_wtr);
376 zip_writer_add (odt->zip, odt->content_file, "content.xml");
377 close_temp_file (odt->content_file);
379 zip_writer_close (odt->zip);
382 free (odt->file_name);
387 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
389 xmlTextWriterPtr writer = odt->content_wtr;
391 if (!strchr (line_, '\n'))
392 xmlTextWriterWriteString (writer, _xml(line_));
395 char *line = xstrdup (line_);
399 for (p = line; *p; p = newline + 1)
401 newline = strchr (p, '\n');
405 xmlTextWriterWriteString (writer, _xml(p));
410 if (newline > p && newline[-1] == '\r')
414 xmlTextWriterWriteString (writer, _xml(p));
415 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
421 write_footnote (struct odt_driver *odt, const struct footnote *f)
423 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
424 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
427 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
428 if (strlen (f->marker) > 1)
429 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
432 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
434 xmlTextWriterEndElement (odt->content_wtr);
436 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
437 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
438 write_xml_with_line_breaks (odt, f->content);
439 xmlTextWriterEndElement (odt->content_wtr);
440 xmlTextWriterEndElement (odt->content_wtr);
442 xmlTextWriterEndElement (odt->content_wtr);
446 write_table_item_text (struct odt_driver *odt,
447 const struct table_item_text *text)
452 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
453 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
454 _xml("text:outline-level"), "%d", 2);
455 xmlTextWriterWriteString (odt->content_wtr, _xml (text->content) );
456 for (size_t i = 0; i < text->n_footnotes; i++)
457 write_footnote (odt, text->footnotes[i]);
458 xmlTextWriterEndElement (odt->content_wtr);
462 write_table (struct odt_driver *odt, const struct table_item *item)
464 const struct table *tab = table_item_get_table (item);
467 /* Write a heading for the table */
468 write_table_item_text (odt, table_item_get_title (item));
469 write_table_item_text (odt, table_item_get_layers (item));
472 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
473 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
474 "TABLE-%d", odt->table_num++);
477 /* Start column definitions */
478 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
479 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
480 xmlTextWriterEndElement (odt->content_wtr);
483 /* Deal with row headers */
484 if ( table_ht (tab) > 0)
485 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
488 /* Write all the rows */
489 for (r = 0 ; r < table_nr (tab); ++r)
491 /* Start row definition */
492 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
494 /* Write all the columns */
495 for (c = 0 ; c < table_nc (tab) ; ++c)
497 struct table_cell cell;
499 table_get_cell (tab, c, r, &cell);
501 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
503 int colspan = table_cell_colspan (&cell);
504 int rowspan = table_cell_rowspan (&cell);
506 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
507 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
510 xmlTextWriterWriteFormatAttribute (
511 odt->content_wtr, _xml("table:number-columns-spanned"),
515 xmlTextWriterWriteFormatAttribute (
516 odt->content_wtr, _xml("table:number-rows-spanned"),
519 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
521 if ( r < table_ht (tab) || c < table_hl (tab) )
522 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
524 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
526 if (cell.options & TAB_MARKUP)
529 char *s = output_get_text_from_markup (cell.text);
530 write_xml_with_line_breaks (odt, s);
534 write_xml_with_line_breaks (odt, cell.text);
536 for (int i = 0; i < cell.n_footnotes; i++)
537 write_footnote (odt, cell.footnotes[i]);
539 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
540 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
544 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
545 xmlTextWriterEndElement (odt->content_wtr);
548 table_cell_free (&cell);
551 xmlTextWriterEndElement (odt->content_wtr); /* row */
553 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
554 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
557 xmlTextWriterEndElement (odt->content_wtr); /* table */
559 /* Write a caption for the table */
560 write_table_item_text (odt, table_item_get_caption (item));
564 odt_output_text (struct odt_driver *odt, const char *text)
566 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
567 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
568 xmlTextWriterEndElement (odt->content_wtr);
571 /* Submit a table to the ODT driver */
573 odt_submit (struct output_driver *driver,
574 const struct output_item *output_item)
576 struct odt_driver *odt = odt_driver_cast (driver);
578 if (is_table_item (output_item))
579 write_table (odt, to_table_item (output_item));
580 else if (is_text_item (output_item))
581 odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
582 else if (is_message_item (output_item))
584 const struct message_item *message_item = to_message_item (output_item);
585 char *s = msg_to_string (message_item_get_msg (message_item));
586 odt_output_text (odt, s);
591 struct output_driver_factory odt_driver_factory =
592 { "odt", "pspp.odf", odt_create };
594 static const struct output_driver_class odt_driver_class =