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. */
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);
306 odt->file_name = xstrdup (file_name);
308 if (!create_mimetype (zip))
310 output_driver_destroy (d);
314 /* Create the manifest */
315 create_writer (&odt->manifest_file, &odt->manifest_wtr);
317 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
318 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
319 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
322 /* Add a manifest entry for the document as a whole */
323 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
324 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
325 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
326 xmlTextWriterEndElement (odt->manifest_wtr);
329 write_meta_data (odt);
330 write_style_data (odt);
332 create_writer (&odt->content_file, &odt->content_wtr);
333 register_file (odt, "content.xml");
336 /* Some necessary junk at the start */
337 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
338 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
339 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
341 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
342 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
344 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
345 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
347 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
349 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
350 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
354 /* Close the manifest */
355 xmlTextWriterEndElement (odt->manifest_wtr);
356 xmlTextWriterEndDocument (odt->manifest_wtr);
357 xmlFreeTextWriter (odt->manifest_wtr);
358 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
359 close_temp_file (odt->manifest_file);
365 odt_destroy (struct output_driver *driver)
367 struct odt_driver *odt = odt_driver_cast (driver);
369 if (odt->content_wtr != NULL)
371 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
372 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
373 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
375 xmlTextWriterEndDocument (odt->content_wtr);
376 xmlFreeTextWriter (odt->content_wtr);
377 zip_writer_add (odt->zip, odt->content_file, "content.xml");
378 close_temp_file (odt->content_file);
380 zip_writer_close (odt->zip);
383 free (odt->file_name);
388 write_xml_with_line_breaks (struct odt_driver *odt, const char *line_)
390 xmlTextWriterPtr writer = odt->content_wtr;
392 if (!strchr (line_, '\n'))
393 xmlTextWriterWriteString (writer, _xml(line_));
396 char *line = xstrdup (line_);
400 for (p = line; *p; p = newline + 1)
402 newline = strchr (p, '\n');
406 xmlTextWriterWriteString (writer, _xml(p));
411 if (newline > p && newline[-1] == '\r')
415 xmlTextWriterWriteString (writer, _xml(p));
416 xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
422 write_footnote (struct odt_driver *odt, const struct footnote *f)
424 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note"));
425 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:note-class"),
428 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-citation"));
429 if (strlen (f->marker) > 1)
430 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:label"),
433 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:label"),
435 xmlTextWriterEndElement (odt->content_wtr);
437 xmlTextWriterStartElement (odt->content_wtr, _xml("text:note-body"));
438 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
439 write_xml_with_line_breaks (odt, f->content);
440 xmlTextWriterEndElement (odt->content_wtr);
441 xmlTextWriterEndElement (odt->content_wtr);
443 xmlTextWriterEndElement (odt->content_wtr);
447 write_table_item_text (struct odt_driver *odt,
448 const struct table_item_text *text)
453 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
454 xmlTextWriterWriteFormatAttribute (odt->content_wtr,
455 _xml("text:outline-level"), "%d", 2);
456 xmlTextWriterWriteString (odt->content_wtr, _xml (text->content) );
457 for (size_t i = 0; i < text->n_footnotes; i++)
458 write_footnote (odt, text->footnotes[i]);
459 xmlTextWriterEndElement (odt->content_wtr);
463 write_table (struct odt_driver *odt, const struct table_item *item)
465 const struct table *tab = table_item_get_table (item);
468 /* Write a heading for the table */
469 write_table_item_text (odt, table_item_get_title (item));
470 write_table_item_text (odt, table_item_get_layers (item));
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];
526 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
528 if ( r < table_ht (tab) || c < table_hl (tab) )
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 write_xml_with_line_breaks (odt, contents->text);
535 for (j = 0; j < contents->n_footnotes; j++)
536 write_footnote (odt, contents->footnotes[j]);
538 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 =