1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009, 2010 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>
25 #include <sys/types.h>
29 #include "libpspp/assertion.h"
30 #include "libpspp/cast.h"
31 #include "libpspp/str.h"
32 #include "libpspp/version.h"
33 #include "output/driver-provider.h"
34 #include "output/message-item.h"
35 #include "output/options.h"
36 #include "output/tab.h"
37 #include "output/table-item.h"
38 #include "output/table-provider.h"
39 #include "output/text-item.h"
41 #include "gl/xalloc.h"
45 #define _(msgid) gettext (msgid)
47 #define _xml(X) (const xmlChar *)(X)
51 struct output_driver driver;
53 char *file_name; /* Output file name. */
56 /* The name of the temporary directory used to construct the ODF */
59 /* Writer for the content.xml file */
60 xmlTextWriterPtr content_wtr;
62 /* Writer fot the manifest.xml file */
63 xmlTextWriterPtr manifest_wtr;
65 /* Number of tables so far. */
68 /* Name of current command. */
72 static const struct output_driver_class odt_driver_class;
74 static struct odt_driver *
75 odt_driver_cast (struct output_driver *driver)
77 assert (driver->class == &odt_driver_class);
78 return UP_CAST (driver, struct odt_driver, driver);
81 /* Create the "mimetype" file needed by ODF */
83 create_mimetype (const char *dirname)
86 struct string filename;
87 ds_init_cstr (&filename, dirname);
88 ds_put_cstr (&filename, "/mimetype");
89 fp = fopen (ds_cstr (&filename), "w");
93 error (0, errno, _("error opening output file \"%s\""),
95 ds_destroy (&filename);
98 ds_destroy (&filename);
100 fprintf (fp, "application/vnd.oasis.opendocument.text");
106 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
107 static xmlTextWriterPtr
108 create_writer (const struct odt_driver *driver, const char *filename)
113 ds_init_cstr (&str, driver->dirname);
114 ds_put_cstr (&str, "/");
115 ds_put_cstr (&str, filename);
117 /* dirname modifies its argument, so we must copy it */
118 copy = xstrdup (ds_cstr (&str));
119 mkdir (dirname (copy), 0700);
122 w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
126 xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
133 register_file (struct odt_driver *odt, const char *filename)
135 assert (odt->manifest_wtr);
136 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
137 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
138 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
139 xmlTextWriterEndElement (odt->manifest_wtr);
143 write_style_data (struct odt_driver *odt)
145 xmlTextWriterPtr w = create_writer (odt, "styles.xml");
146 register_file (odt, "styles.xml");
148 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
149 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
150 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
152 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
153 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
155 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
156 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
158 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
162 xmlTextWriterStartElement (w, _xml ("office:styles"));
166 xmlTextWriterStartElement (w, _xml ("style:style"));
167 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
170 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
173 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
176 xmlTextWriterEndElement (w); /* style:style */
180 xmlTextWriterStartElement (w, _xml ("style:style"));
181 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
182 _xml ("Table_20_Contents"));
184 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
185 _xml ("Table Contents"));
187 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
190 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
193 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
196 xmlTextWriterEndElement (w); /* style:style */
200 xmlTextWriterStartElement (w, _xml ("style:style"));
201 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
202 _xml ("Table_20_Heading"));
204 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
205 _xml ("Table Heading"));
207 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
210 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
211 _xml ("Table_20_Contents"));
213 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
217 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
218 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
219 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
220 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
221 xmlTextWriterEndElement (w); /* style:text-properties */
223 xmlTextWriterEndElement (w); /* style:style */
227 xmlTextWriterEndElement (w); /* office:styles */
228 xmlTextWriterEndElement (w); /* office:document-styles */
230 xmlTextWriterEndDocument (w);
231 xmlFreeTextWriter (w);
235 write_meta_data (struct odt_driver *odt)
237 xmlTextWriterPtr w = create_writer (odt, "meta.xml");
238 register_file (odt, "meta.xml");
240 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
241 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
242 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
243 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
244 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
245 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
247 xmlTextWriterStartElement (w, _xml ("office:meta"));
249 xmlTextWriterStartElement (w, _xml ("meta:generator"));
250 xmlTextWriterWriteString (w, _xml (stat_version));
251 xmlTextWriterEndElement (w);
257 time_t t = time (NULL);
258 struct tm *tm = localtime (&t);
260 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
262 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
263 xmlTextWriterWriteString (w, _xml (buf));
264 xmlTextWriterEndElement (w);
266 xmlTextWriterStartElement (w, _xml ("dc:date"));
267 xmlTextWriterWriteString (w, _xml (buf));
268 xmlTextWriterEndElement (w);
271 xmlTextWriterEndElement (w);
272 xmlTextWriterEndElement (w);
273 xmlTextWriterEndDocument (w);
274 xmlFreeTextWriter (w);
283 static struct driver_option *
284 opt (struct output_driver *d, struct string_map *options, const char *key,
285 const char *default_value)
287 return driver_option_get (d, options, key, default_value);
290 static struct output_driver *
291 odt_create (const char *file_name, enum settings_output_devices device_type,
292 struct string_map *o)
294 struct output_driver *d;
295 struct odt_driver *odt;
297 odt = xzalloc (sizeof *odt);
299 output_driver_init (d, &odt_driver_class, file_name, device_type);
301 odt->file_name = xstrdup (file_name);
302 odt->debug = parse_boolean (opt (d, o, "debug", "false"));
304 odt->dirname = xstrdup ("odt-XXXXXX");
305 mkdtemp (odt->dirname);
307 if (!create_mimetype (odt->dirname))
309 output_driver_destroy (d);
313 /* Create the manifest */
314 odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml");
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 odt->content_wtr = create_writer (odt, "content.xml");
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);
362 odt_destroy (struct output_driver *driver)
364 struct odt_driver *odt = odt_driver_cast (driver);
366 if (odt->content_wtr != NULL)
368 struct string zip_cmd;
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);
377 /* Zip up the directory */
378 ds_init_empty (&zip_cmd);
379 ds_put_format (&zip_cmd,
380 "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../%s .",
381 odt->dirname, odt->file_name, odt->file_name, odt->file_name);
382 system (ds_cstr (&zip_cmd));
383 ds_destroy (&zip_cmd);
388 /* Remove the temp dir */
389 struct string rm_cmd;
391 ds_init_empty (&rm_cmd);
392 ds_put_format (&rm_cmd, "rm -r %s", odt->dirname);
393 system (ds_cstr (&rm_cmd));
394 ds_destroy (&rm_cmd);
397 fprintf (stderr, "Not removing directory %s\n", odt->dirname);
399 free (odt->command_name);
405 odt_submit_table (struct odt_driver *odt, struct table_item *item)
407 const struct table *tab = table_item_get_table (item);
408 const char *caption = table_item_get_caption (item);
411 /* Write a heading for the table */
414 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
415 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
417 xmlTextWriterWriteString (odt->content_wtr,
418 _xml (table_item_get_caption (item)) );
419 xmlTextWriterEndElement (odt->content_wtr);
423 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
424 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
425 "TABLE-%d", odt->table_num++);
428 /* Start column definitions */
429 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
430 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
431 xmlTextWriterEndElement (odt->content_wtr);
434 /* Deal with row headers */
435 if ( table_ht (tab) > 0)
436 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
439 /* Write all the rows */
440 for (r = 0 ; r < table_nr (tab); ++r)
442 /* Start row definition */
443 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
445 /* Write all the columns */
446 for (c = 0 ; c < table_nc (tab) ; ++c)
448 struct table_cell cell;
450 table_get_cell (tab, c, r, &cell);
452 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
454 int colspan = table_cell_colspan (&cell);
455 int rowspan = table_cell_rowspan (&cell);
457 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
458 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
461 xmlTextWriterWriteFormatAttribute (
462 odt->content_wtr, _xml("table:number-columns-spanned"),
466 xmlTextWriterWriteFormatAttribute (
467 odt->content_wtr, _xml("table:number-rows-spanned"),
470 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
472 if ( r < table_ht (tab) || c < table_hl (tab) )
473 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
475 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
477 xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
479 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
480 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
484 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
485 xmlTextWriterEndElement (odt->content_wtr);
488 table_cell_free (&cell);
491 xmlTextWriterEndElement (odt->content_wtr); /* row */
493 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
494 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
497 xmlTextWriterEndElement (odt->content_wtr); /* table */
501 odt_output_text (struct odt_driver *odt, const char *text)
503 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
504 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
505 xmlTextWriterEndElement (odt->content_wtr);
508 /* Submit a table to the ODT driver */
510 odt_submit (struct output_driver *driver,
511 const struct output_item *output_item)
513 struct odt_driver *odt = odt_driver_cast (driver);
515 output_driver_track_current_command (output_item, &odt->command_name);
517 if (is_table_item (output_item))
518 odt_submit_table (odt, to_table_item (output_item));
519 else if (is_text_item (output_item))
521 /* XXX apply different styles based on text_item's type. */
522 odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
524 else if (is_message_item (output_item))
526 const struct message_item *message_item = to_message_item (output_item);
527 const struct msg *msg = message_item_get_msg (message_item);
528 char *s = msg_to_string (msg, odt->command_name);
529 odt_output_text (odt, s);
534 struct output_driver_factory odt_driver_factory = { "odt", odt_create };
536 static const struct output_driver_class odt_driver_class =