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>
28 #include <sys/types.h>
32 #include "libpspp/assertion.h"
33 #include "libpspp/cast.h"
34 #include "libpspp/str.h"
35 #include "libpspp/version.h"
36 #include "output/driver-provider.h"
37 #include "output/message-item.h"
38 #include "output/options.h"
39 #include "output/tab.h"
40 #include "output/table-item.h"
41 #include "output/table-provider.h"
42 #include "output/text-item.h"
44 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
50 #define _xml(X) (const xmlChar *)(X)
54 struct output_driver driver;
56 char *file_name; /* Output file name. */
59 /* The name of the temporary directory used to construct the ODF */
62 /* Writer for the content.xml file */
63 xmlTextWriterPtr content_wtr;
65 /* Writer fot the manifest.xml file */
66 xmlTextWriterPtr manifest_wtr;
68 /* Number of tables so far. */
71 /* Name of current command. */
75 static const struct output_driver_class odt_driver_class;
77 static struct odt_driver *
78 odt_driver_cast (struct output_driver *driver)
80 assert (driver->class == &odt_driver_class);
81 return UP_CAST (driver, struct odt_driver, driver);
84 /* Create the "mimetype" file needed by ODF */
86 create_mimetype (const char *dirname)
89 struct string filename;
90 ds_init_cstr (&filename, dirname);
91 ds_put_cstr (&filename, "/mimetype");
92 fp = fopen (ds_cstr (&filename), "w");
96 error (0, errno, _("error opening output file \"%s\""),
98 ds_destroy (&filename);
101 ds_destroy (&filename);
103 fprintf (fp, "application/vnd.oasis.opendocument.text");
109 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
110 static xmlTextWriterPtr
111 create_writer (const struct odt_driver *driver, const char *filename)
116 ds_init_cstr (&str, driver->dirname);
117 ds_put_cstr (&str, "/");
118 ds_put_cstr (&str, filename);
120 /* dirname modifies its argument, so we must copy it */
121 copy = xstrdup (ds_cstr (&str));
122 mkdir (dirname (copy), 0700);
125 w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
129 xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
136 register_file (struct odt_driver *odt, const char *filename)
138 assert (odt->manifest_wtr);
139 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
140 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
141 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
142 xmlTextWriterEndElement (odt->manifest_wtr);
146 write_style_data (struct odt_driver *odt)
148 xmlTextWriterPtr w = create_writer (odt, "styles.xml");
149 register_file (odt, "styles.xml");
151 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
152 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
153 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
155 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
156 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
158 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
159 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
161 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
165 xmlTextWriterStartElement (w, _xml ("office:styles"));
169 xmlTextWriterStartElement (w, _xml ("style:style"));
170 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
173 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
176 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
179 xmlTextWriterEndElement (w); /* style:style */
183 xmlTextWriterStartElement (w, _xml ("style:style"));
184 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
185 _xml ("Table_20_Contents"));
187 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
188 _xml ("Table Contents"));
190 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
193 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
196 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
199 xmlTextWriterEndElement (w); /* style:style */
203 xmlTextWriterStartElement (w, _xml ("style:style"));
204 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
205 _xml ("Table_20_Heading"));
207 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
208 _xml ("Table Heading"));
210 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
213 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
214 _xml ("Table_20_Contents"));
216 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
220 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
221 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
222 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
223 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
224 xmlTextWriterEndElement (w); /* style:text-properties */
226 xmlTextWriterEndElement (w); /* style:style */
230 xmlTextWriterEndElement (w); /* office:styles */
231 xmlTextWriterEndElement (w); /* office:document-styles */
233 xmlTextWriterEndDocument (w);
234 xmlFreeTextWriter (w);
238 write_meta_data (struct odt_driver *odt)
240 xmlTextWriterPtr w = create_writer (odt, "meta.xml");
241 register_file (odt, "meta.xml");
243 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
244 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
245 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
246 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
247 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
248 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
250 xmlTextWriterStartElement (w, _xml ("office:meta"));
252 xmlTextWriterStartElement (w, _xml ("meta:generator"));
253 xmlTextWriterWriteString (w, _xml (stat_version));
254 xmlTextWriterEndElement (w);
260 time_t t = time (NULL);
261 struct tm *tm = localtime (&t);
263 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
265 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
266 xmlTextWriterWriteString (w, _xml (buf));
267 xmlTextWriterEndElement (w);
269 xmlTextWriterStartElement (w, _xml ("dc:date"));
270 xmlTextWriterWriteString (w, _xml (buf));
271 xmlTextWriterEndElement (w);
276 struct passwd *pw = getpwuid (getuid ());
279 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
280 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
281 xmlTextWriterEndElement (w);
283 xmlTextWriterStartElement (w, _xml ("dc:creator"));
284 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
285 xmlTextWriterEndElement (w);
290 xmlTextWriterEndElement (w);
291 xmlTextWriterEndElement (w);
292 xmlTextWriterEndDocument (w);
293 xmlFreeTextWriter (w);
302 static struct driver_option *
303 opt (struct output_driver *d, struct string_map *options, const char *key,
304 const char *default_value)
306 return driver_option_get (d, options, key, default_value);
309 static struct output_driver *
310 odt_create (const char *file_name, enum settings_output_devices device_type,
311 struct string_map *o)
313 struct output_driver *d;
314 struct odt_driver *odt;
316 odt = xzalloc (sizeof *odt);
318 output_driver_init (d, &odt_driver_class, file_name, device_type);
320 odt->file_name = xstrdup (file_name);
321 odt->debug = parse_boolean (opt (d, o, "debug", "false"));
323 odt->dirname = xstrdup ("odt-XXXXXX");
324 mkdtemp (odt->dirname);
326 if (!create_mimetype (odt->dirname))
328 output_driver_destroy (d);
332 /* Create the manifest */
333 odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml");
335 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
336 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
337 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
340 /* Add a manifest entry for the document as a whole */
341 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
342 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
343 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
344 xmlTextWriterEndElement (odt->manifest_wtr);
347 write_meta_data (odt);
348 write_style_data (odt);
350 odt->content_wtr = create_writer (odt, "content.xml");
351 register_file (odt, "content.xml");
354 /* Some necessary junk at the start */
355 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
356 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
357 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
359 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
360 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
362 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
363 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
365 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
367 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
368 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
372 /* Close the manifest */
373 xmlTextWriterEndElement (odt->manifest_wtr);
374 xmlTextWriterEndDocument (odt->manifest_wtr);
375 xmlFreeTextWriter (odt->manifest_wtr);
381 odt_destroy (struct output_driver *driver)
383 struct odt_driver *odt = odt_driver_cast (driver);
385 if (odt->content_wtr != NULL)
387 struct string zip_cmd;
389 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
390 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
391 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
393 xmlTextWriterEndDocument (odt->content_wtr);
394 xmlFreeTextWriter (odt->content_wtr);
396 /* Zip up the directory */
397 ds_init_empty (&zip_cmd);
398 ds_put_format (&zip_cmd,
399 "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../%s .",
400 odt->dirname, odt->file_name, odt->file_name, odt->file_name);
401 system (ds_cstr (&zip_cmd));
402 ds_destroy (&zip_cmd);
407 /* Remove the temp dir */
408 struct string rm_cmd;
410 ds_init_empty (&rm_cmd);
411 ds_put_format (&rm_cmd, "rm -r %s", odt->dirname);
412 system (ds_cstr (&rm_cmd));
413 ds_destroy (&rm_cmd);
416 fprintf (stderr, "Not removing directory %s\n", odt->dirname);
418 free (odt->command_name);
424 odt_submit_table (struct odt_driver *odt, struct table_item *item)
426 const struct table *tab = table_item_get_table (item);
427 const char *caption = table_item_get_caption (item);
430 /* Write a heading for the table */
433 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
434 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
436 xmlTextWriterWriteString (odt->content_wtr,
437 _xml (table_item_get_caption (item)) );
438 xmlTextWriterEndElement (odt->content_wtr);
442 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
443 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
444 "TABLE-%d", odt->table_num++);
447 /* Start column definitions */
448 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
449 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
450 xmlTextWriterEndElement (odt->content_wtr);
453 /* Deal with row headers */
454 if ( table_ht (tab) > 0)
455 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
458 /* Write all the rows */
459 for (r = 0 ; r < table_nr (tab); ++r)
461 /* Start row definition */
462 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
464 /* Write all the columns */
465 for (c = 0 ; c < table_nc (tab) ; ++c)
467 struct table_cell cell;
469 table_get_cell (tab, c, r, &cell);
471 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
473 int colspan = table_cell_colspan (&cell);
474 int rowspan = table_cell_rowspan (&cell);
476 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
477 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
480 xmlTextWriterWriteFormatAttribute (
481 odt->content_wtr, _xml("table:number-columns-spanned"),
485 xmlTextWriterWriteFormatAttribute (
486 odt->content_wtr, _xml("table:number-rows-spanned"),
489 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
491 if ( r < table_ht (tab) || c < table_hl (tab) )
492 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
494 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
496 xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
498 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
499 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
503 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
504 xmlTextWriterEndElement (odt->content_wtr);
507 table_cell_free (&cell);
510 xmlTextWriterEndElement (odt->content_wtr); /* row */
512 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
513 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
516 xmlTextWriterEndElement (odt->content_wtr); /* table */
520 odt_output_text (struct odt_driver *odt, const char *text)
522 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
523 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
524 xmlTextWriterEndElement (odt->content_wtr);
527 /* Submit a table to the ODT driver */
529 odt_submit (struct output_driver *driver,
530 const struct output_item *output_item)
532 struct odt_driver *odt = odt_driver_cast (driver);
534 output_driver_track_current_command (output_item, &odt->command_name);
536 if (is_table_item (output_item))
537 odt_submit_table (odt, to_table_item (output_item));
538 else if (is_text_item (output_item))
540 /* XXX apply different styles based on text_item's type. */
541 odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
543 else if (is_message_item (output_item))
545 const struct message_item *message_item = to_message_item (output_item);
546 const struct msg *msg = message_item_get_msg (message_item);
547 char *s = msg_to_string (msg, odt->command_name);
548 odt_output_text (odt, s);
553 struct output_driver_factory odt_driver_factory = { "odt", odt_create };
555 static const struct output_driver_class odt_driver_class =