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/tab.h"
44 #include "output/table-item.h"
45 #include "output/table-provider.h"
46 #include "output/text-item.h"
48 #include "gl/xalloc.h"
51 #define _(msgid) gettext (msgid)
53 #define _xml(X) (CHAR_CAST (const xmlChar *, X))
57 struct output_driver driver;
59 struct zip_writer *zip; /* ZIP file writer. */
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. */
73 /* Name of current command. */
76 /* Number of footnotes 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 /* Create the "mimetype" file needed by ODF */
91 create_mimetype (struct zip_writer *zip)
95 fp = create_temp_file ();
98 msg_error (errno, _("error creating temporary file"));
102 fprintf (fp, "application/vnd.oasis.opendocument.text");
103 zip_writer_add (zip, fp, "mimetype");
104 close_temp_file (fp);
109 /* Creates a new temporary file and stores it in *FILE, then creates an XML
110 writer for it and stores it in *W. */
112 create_writer (FILE **file, xmlTextWriterPtr *w)
114 /* XXX this can fail */
115 *file = create_temp_file ();
116 *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
118 xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
123 register_file (struct odt_driver *odt, const char *filename)
125 assert (odt->manifest_wtr);
126 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
127 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
128 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
129 xmlTextWriterEndElement (odt->manifest_wtr);
133 write_style_data (struct odt_driver *odt)
138 create_writer (&file, &w);
139 register_file (odt, "styles.xml");
141 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
142 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
143 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
145 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
146 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
148 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
149 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
151 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
155 xmlTextWriterStartElement (w, _xml ("office:styles"));
159 xmlTextWriterStartElement (w, _xml ("style:style"));
160 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
163 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
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_Contents"));
177 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
178 _xml ("Table Contents"));
180 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
183 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
186 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
189 xmlTextWriterEndElement (w); /* style:style */
193 xmlTextWriterStartElement (w, _xml ("style:style"));
194 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
195 _xml ("Table_20_Heading"));
197 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
198 _xml ("Table Heading"));
200 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
203 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
204 _xml ("Table_20_Contents"));
206 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
210 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
211 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
212 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
213 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
214 xmlTextWriterEndElement (w); /* style:text-properties */
216 xmlTextWriterEndElement (w); /* style:style */
220 xmlTextWriterEndElement (w); /* office:styles */
221 xmlTextWriterEndElement (w); /* office:document-styles */
223 xmlTextWriterEndDocument (w);
224 xmlFreeTextWriter (w);
225 zip_writer_add (odt->zip, file, "styles.xml");
226 close_temp_file (file);
230 write_meta_data (struct odt_driver *odt)
235 create_writer (&file, &w);
236 register_file (odt, "meta.xml");
238 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
239 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
240 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
241 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
242 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
243 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
245 xmlTextWriterStartElement (w, _xml ("office:meta"));
247 xmlTextWriterStartElement (w, _xml ("meta:generator"));
248 xmlTextWriterWriteString (w, _xml (version));
249 xmlTextWriterEndElement (w);
255 time_t t = time (NULL);
256 struct tm *tm = localtime (&t);
258 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
260 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
261 xmlTextWriterWriteString (w, _xml (buf));
262 xmlTextWriterEndElement (w);
264 xmlTextWriterStartElement (w, _xml ("dc:date"));
265 xmlTextWriterWriteString (w, _xml (buf));
266 xmlTextWriterEndElement (w);
271 struct passwd *pw = getpwuid (getuid ());
274 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
275 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
276 xmlTextWriterEndElement (w);
278 xmlTextWriterStartElement (w, _xml ("dc:creator"));
279 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
280 xmlTextWriterEndElement (w);
285 xmlTextWriterEndElement (w);
286 xmlTextWriterEndElement (w);
287 xmlTextWriterEndDocument (w);
288 xmlFreeTextWriter (w);
289 zip_writer_add (odt->zip, file, "meta.xml");
290 close_temp_file (file);
293 static struct output_driver *
294 odt_create (struct file_handle *fh, enum settings_output_devices device_type,
295 struct string_map *o UNUSED)
297 struct output_driver *d;
298 struct odt_driver *odt;
299 struct zip_writer *zip;
300 const char *file_name = fh_get_file_name (fh);
302 zip = zip_writer_create (file_name);
306 odt = xzalloc (sizeof *odt);
309 output_driver_init (d, &odt_driver_class, file_name, device_type);
312 odt->file_name = xstrdup (file_name);
314 if (!create_mimetype (zip))
316 output_driver_destroy (d);
320 /* Create the manifest */
321 create_writer (&odt->manifest_file, &odt->manifest_wtr);
323 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
324 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
325 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
328 /* Add a manifest entry for the document as a whole */
329 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
330 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
331 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
332 xmlTextWriterEndElement (odt->manifest_wtr);
335 write_meta_data (odt);
336 write_style_data (odt);
338 create_writer (&odt->content_file, &odt->content_wtr);
339 register_file (odt, "content.xml");
342 /* Some necessary junk at the start */
343 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
344 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
345 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
347 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
348 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
350 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
351 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
353 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
355 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
356 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
360 /* Close the manifest */
361 xmlTextWriterEndElement (odt->manifest_wtr);
362 xmlTextWriterEndDocument (odt->manifest_wtr);
363 xmlFreeTextWriter (odt->manifest_wtr);
364 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
365 close_temp_file (odt->manifest_file);
371 odt_destroy (struct output_driver *driver)
373 struct odt_driver *odt = odt_driver_cast (driver);
375 if (odt->content_wtr != NULL)
377 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
378 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
379 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
381 xmlTextWriterEndDocument (odt->content_wtr);
382 xmlFreeTextWriter (odt->content_wtr);
383 zip_writer_add (odt->zip, odt->content_file, "content.xml");
384 close_temp_file (odt->content_file);
386 zip_writer_close (odt->zip);
389 free (odt->file_name);
390 free (odt->command_name);
395 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
397 xmlTextWriterPtr writer = odt->content_wtr;
399 if (!strchr (line_, '\n'))
400 xmlTextWriterWriteString (writer, _xml(line_));
403 char *line = xstrdup (line_);
407 for (p = line; *p; p = newline + 1)
409 newline = strchr (p, '\n');
413 xmlTextWriterWriteString (writer, _xml(p));
418 if (newline > p && newline[-1] == '\r')
422 xmlTextWriterWriteString (writer, _xml(p));
423 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
429 write_footnote (struct odt_driver *odt, const char *footnote)
433 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
434 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
437 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
438 str_format_26adic (++odt->n_footnotes, false, marker, sizeof marker);
439 if (strlen (marker) > 1)
440 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
443 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
445 xmlTextWriterEndElement (odt->content_wtr);
447 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
448 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
449 write_xml_with_line_breaks (odt, footnote);
450 xmlTextWriterEndElement (odt->content_wtr);
451 xmlTextWriterEndElement (odt->content_wtr);
453 xmlTextWriterEndElement (odt->content_wtr);
457 write_table (struct odt_driver *odt, const struct table_item *item)
459 const struct table *tab = table_item_get_table (item);
460 const char *caption = table_item_get_caption (item);
461 const char *title = table_item_get_title (item);
464 /* Write a heading for the table */
467 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
468 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
469 _xml("text:outline-level"), "%d", 2);
470 xmlTextWriterWriteString (odt->content_wtr,
471 _xml (table_item_get_title (item)) );
472 xmlTextWriterEndElement (odt->content_wtr);
476 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
477 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
478 "TABLE-%d", odt->table_num++);
481 /* Start column definitions */
482 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
483 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
484 xmlTextWriterEndElement (odt->content_wtr);
487 /* Deal with row headers */
488 if ( table_ht (tab) > 0)
489 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
492 /* Write all the rows */
493 for (r = 0 ; r < table_nr (tab); ++r)
495 /* Start row definition */
496 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
498 /* Write all the columns */
499 for (c = 0 ; c < table_nc (tab) ; ++c)
501 struct table_cell cell;
504 table_get_cell (tab, c, r, &cell);
506 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
508 int colspan = table_cell_colspan (&cell);
509 int rowspan = table_cell_rowspan (&cell);
511 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
512 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
515 xmlTextWriterWriteFormatAttribute (
516 odt->content_wtr, _xml("table:number-columns-spanned"),
520 xmlTextWriterWriteFormatAttribute (
521 odt->content_wtr, _xml("table:number-rows-spanned"),
524 for (i = 0; i < cell.n_contents; i++)
526 const struct cell_contents *contents = &cell.contents[i];
531 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
533 if ( r < table_ht (tab) || c < table_hl (tab) )
534 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
536 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
538 write_xml_with_line_breaks (odt, contents->text);
540 for (j = 0; j < contents->n_footnotes; j++)
541 write_footnote (odt, contents->footnotes[j]);
543 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
545 else if (contents->table)
546 write_table (odt, contents->table);
549 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
553 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
554 xmlTextWriterEndElement (odt->content_wtr);
557 table_cell_free (&cell);
560 xmlTextWriterEndElement (odt->content_wtr); /* row */
562 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
563 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
566 xmlTextWriterEndElement (odt->content_wtr); /* table */
568 /* Write a caption for the table */
571 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
572 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
573 _xml("text:outline-level"), "%d", 2);
574 xmlTextWriterWriteString (odt->content_wtr,
575 _xml (table_item_get_caption (item)) );
576 xmlTextWriterEndElement (odt->content_wtr);
582 odt_output_text (struct odt_driver *odt, const char *text)
584 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
585 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
586 xmlTextWriterEndElement (odt->content_wtr);
589 /* Submit a table to the ODT driver */
591 odt_submit (struct output_driver *driver,
592 const struct output_item *output_item)
594 struct odt_driver *odt = odt_driver_cast (driver);
596 output_driver_track_current_command (output_item, &odt->command_name);
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 struct text_item *text_item = to_text_item (output_item);
604 if (text_item_get_type (text_item) != TEXT_ITEM_COMMAND_CLOSE)
605 odt_output_text (odt, text_item_get_text (text_item));
607 else if (is_message_item (output_item))
609 const struct message_item *message_item = to_message_item (output_item);
610 const struct msg *msg = message_item_get_msg (message_item);
611 char *s = msg_to_string (msg, odt->command_name);
612 odt_output_text (odt, s);
617 struct output_driver_factory odt_driver_factory =
618 { "odt", "pspp.odf", odt_create };
620 static const struct output_driver_class odt_driver_class =