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 "output/driver-provider.h"
40 #include "output/message-item.h"
41 #include "output/options.h"
42 #include "output/tab.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. */
72 /* Name of current command. */
75 /* Number of footnotes so far. */
79 static const struct output_driver_class odt_driver_class;
81 static struct odt_driver *
82 odt_driver_cast (struct output_driver *driver)
84 assert (driver->class == &odt_driver_class);
85 return UP_CAST (driver, struct odt_driver, driver);
88 /* Create the "mimetype" file needed by ODF */
90 create_mimetype (struct zip_writer *zip)
94 fp = create_temp_file ();
97 msg_error (errno, _("error creating temporary file"));
101 fprintf (fp, "application/vnd.oasis.opendocument.text");
102 zip_writer_add (zip, fp, "mimetype");
103 close_temp_file (fp);
108 /* Creates a new temporary file and stores it in *FILE, then creates an XML
109 writer for it and stores it in *W. */
111 create_writer (FILE **file, xmlTextWriterPtr *w)
113 /* XXX this can fail */
114 *file = create_temp_file ();
115 *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
117 xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
122 register_file (struct odt_driver *odt, const char *filename)
124 assert (odt->manifest_wtr);
125 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
126 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
127 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
128 xmlTextWriterEndElement (odt->manifest_wtr);
132 write_style_data (struct odt_driver *odt)
137 create_writer (&file, &w);
138 register_file (odt, "styles.xml");
140 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
141 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
142 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
144 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
145 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
147 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
148 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
150 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
154 xmlTextWriterStartElement (w, _xml ("office:styles"));
158 xmlTextWriterStartElement (w, _xml ("style:style"));
159 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
162 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
165 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
168 xmlTextWriterEndElement (w); /* style:style */
172 xmlTextWriterStartElement (w, _xml ("style:style"));
173 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
174 _xml ("Table_20_Contents"));
176 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
177 _xml ("Table Contents"));
179 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
182 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
185 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
188 xmlTextWriterEndElement (w); /* style:style */
192 xmlTextWriterStartElement (w, _xml ("style:style"));
193 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
194 _xml ("Table_20_Heading"));
196 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
197 _xml ("Table Heading"));
199 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
202 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
203 _xml ("Table_20_Contents"));
205 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
209 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
210 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
211 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
212 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
213 xmlTextWriterEndElement (w); /* style:text-properties */
215 xmlTextWriterEndElement (w); /* style:style */
219 xmlTextWriterEndElement (w); /* office:styles */
220 xmlTextWriterEndElement (w); /* office:document-styles */
222 xmlTextWriterEndDocument (w);
223 xmlFreeTextWriter (w);
224 zip_writer_add (odt->zip, file, "styles.xml");
225 close_temp_file (file);
229 write_meta_data (struct odt_driver *odt)
234 create_writer (&file, &w);
235 register_file (odt, "meta.xml");
237 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
238 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
239 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
240 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
241 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
242 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
244 xmlTextWriterStartElement (w, _xml ("office:meta"));
246 xmlTextWriterStartElement (w, _xml ("meta:generator"));
247 xmlTextWriterWriteString (w, _xml (stat_version));
248 xmlTextWriterEndElement (w);
254 time_t t = time (NULL);
255 struct tm *tm = localtime (&t);
257 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
259 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
260 xmlTextWriterWriteString (w, _xml (buf));
261 xmlTextWriterEndElement (w);
263 xmlTextWriterStartElement (w, _xml ("dc:date"));
264 xmlTextWriterWriteString (w, _xml (buf));
265 xmlTextWriterEndElement (w);
270 struct passwd *pw = getpwuid (getuid ());
273 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
274 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
275 xmlTextWriterEndElement (w);
277 xmlTextWriterStartElement (w, _xml ("dc:creator"));
278 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
279 xmlTextWriterEndElement (w);
284 xmlTextWriterEndElement (w);
285 xmlTextWriterEndElement (w);
286 xmlTextWriterEndDocument (w);
287 xmlFreeTextWriter (w);
288 zip_writer_add (odt->zip, file, "meta.xml");
289 close_temp_file (file);
292 static struct output_driver *
293 odt_create (const char *file_name, enum settings_output_devices device_type,
294 struct string_map *o UNUSED)
296 struct output_driver *d;
297 struct odt_driver *odt;
298 struct zip_writer *zip;
300 zip = zip_writer_create (file_name);
304 odt = xzalloc (sizeof *odt);
306 output_driver_init (d, &odt_driver_class, file_name, device_type);
309 odt->file_name = xstrdup (file_name);
311 if (!create_mimetype (zip))
313 output_driver_destroy (d);
317 /* Create the manifest */
318 create_writer (&odt->manifest_file, &odt->manifest_wtr);
320 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
321 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
322 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
325 /* Add a manifest entry for the document as a whole */
326 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
327 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
328 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
329 xmlTextWriterEndElement (odt->manifest_wtr);
332 write_meta_data (odt);
333 write_style_data (odt);
335 create_writer (&odt->content_file, &odt->content_wtr);
336 register_file (odt, "content.xml");
339 /* Some necessary junk at the start */
340 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
341 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
342 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
344 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
345 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
347 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
348 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
350 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
352 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
353 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
357 /* Close the manifest */
358 xmlTextWriterEndElement (odt->manifest_wtr);
359 xmlTextWriterEndDocument (odt->manifest_wtr);
360 xmlFreeTextWriter (odt->manifest_wtr);
361 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
362 close_temp_file (odt->manifest_file);
368 odt_destroy (struct output_driver *driver)
370 struct odt_driver *odt = odt_driver_cast (driver);
372 if (odt->content_wtr != NULL)
374 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
375 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
376 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
378 xmlTextWriterEndDocument (odt->content_wtr);
379 xmlFreeTextWriter (odt->content_wtr);
380 zip_writer_add (odt->zip, odt->content_file, "content.xml");
381 close_temp_file (odt->content_file);
383 zip_writer_close (odt->zip);
386 free (odt->file_name);
387 free (odt->command_name);
392 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
394 xmlTextWriterPtr writer = odt->content_wtr;
396 if (!strchr (line_, '\n'))
397 xmlTextWriterWriteString (writer, _xml(line_));
400 char *line = xstrdup (line_);
404 for (p = line; *p; p = newline + 1)
406 newline = strchr (p, '\n');
410 xmlTextWriterWriteString (writer, _xml(p));
415 if (newline > p && newline[-1] == '\r')
419 xmlTextWriterWriteString (writer, _xml(p));
420 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
426 write_footnote (struct odt_driver *odt, const char *footnote)
430 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
431 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
434 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
435 str_format_26adic (++odt->n_footnotes, false, marker, sizeof marker);
436 if (strlen (marker) > 1)
437 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
440 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
442 xmlTextWriterEndElement (odt->content_wtr);
444 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
445 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
446 write_xml_with_line_breaks (odt, footnote);
447 xmlTextWriterEndElement (odt->content_wtr);
448 xmlTextWriterEndElement (odt->content_wtr);
450 xmlTextWriterEndElement (odt->content_wtr);
454 write_table (struct odt_driver *odt, const struct table_item *item)
456 const struct table *tab = table_item_get_table (item);
457 const char *caption = table_item_get_caption (item);
458 const char *title = table_item_get_title (item);
461 /* Write a heading for the table */
464 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
465 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
466 _xml("text:outline-level"), "%d", 2);
467 xmlTextWriterWriteString (odt->content_wtr,
468 _xml (table_item_get_title (item)) );
469 xmlTextWriterEndElement (odt->content_wtr);
473 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
474 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
475 "TABLE-%d", odt->table_num++);
478 /* Start column definitions */
479 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
480 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
481 xmlTextWriterEndElement (odt->content_wtr);
484 /* Deal with row headers */
485 if ( table_ht (tab) > 0)
486 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
489 /* Write all the rows */
490 for (r = 0 ; r < table_nr (tab); ++r)
492 /* Start row definition */
493 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
495 /* Write all the columns */
496 for (c = 0 ; c < table_nc (tab) ; ++c)
498 struct table_cell cell;
501 table_get_cell (tab, c, r, &cell);
503 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
505 int colspan = table_cell_colspan (&cell);
506 int rowspan = table_cell_rowspan (&cell);
508 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
509 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
512 xmlTextWriterWriteFormatAttribute (
513 odt->content_wtr, _xml("table:number-columns-spanned"),
517 xmlTextWriterWriteFormatAttribute (
518 odt->content_wtr, _xml("table:number-rows-spanned"),
521 for (i = 0; i < cell.n_contents; i++)
523 const struct cell_contents *contents = &cell.contents[i];
528 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
530 if ( r < table_ht (tab) || c < table_hl (tab) )
531 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
533 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
535 write_xml_with_line_breaks (odt, contents->text);
537 for (j = 0; j < contents->n_footnotes; j++)
538 write_footnote (odt, contents->footnotes[j]);
540 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
542 else if (contents->table)
543 write_table (odt, contents->table);
546 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
550 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
551 xmlTextWriterEndElement (odt->content_wtr);
554 table_cell_free (&cell);
557 xmlTextWriterEndElement (odt->content_wtr); /* row */
559 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
560 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
563 xmlTextWriterEndElement (odt->content_wtr); /* table */
565 /* Write a caption for the table */
568 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
569 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
570 _xml("text:outline-level"), "%d", 2);
571 xmlTextWriterWriteString (odt->content_wtr,
572 _xml (table_item_get_caption (item)) );
573 xmlTextWriterEndElement (odt->content_wtr);
579 odt_output_text (struct odt_driver *odt, const char *text)
581 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
582 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
583 xmlTextWriterEndElement (odt->content_wtr);
586 /* Submit a table to the ODT driver */
588 odt_submit (struct output_driver *driver,
589 const struct output_item *output_item)
591 struct odt_driver *odt = odt_driver_cast (driver);
593 output_driver_track_current_command (output_item, &odt->command_name);
595 if (is_table_item (output_item))
596 write_table (odt, to_table_item (output_item));
597 else if (is_text_item (output_item))
599 struct text_item *text_item = to_text_item (output_item);
601 if (text_item_get_type (text_item) != TEXT_ITEM_COMMAND_CLOSE)
602 odt_output_text (odt, text_item_get_text (text_item));
604 else if (is_message_item (output_item))
606 const struct message_item *message_item = to_message_item (output_item);
607 const struct msg *msg = message_item_get_msg (message_item);
608 char *s = msg_to_string (msg, odt->command_name);
609 odt_output_text (odt, s);
614 struct output_driver_factory odt_driver_factory =
615 { "odt", "pspp.odf", odt_create };
617 static const struct output_driver_class odt_driver_class =