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>
26 #include <sys/types.h>
30 #include "libpspp/assertion.h"
31 #include "libpspp/cast.h"
32 #include "libpspp/str.h"
33 #include "libpspp/version.h"
34 #include "output/driver-provider.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. */
69 static struct odt_driver *
70 odt_driver_cast (struct output_driver *driver)
72 assert (driver->class == &odt_class);
73 return UP_CAST (driver, struct odt_driver, driver);
76 /* Create the "mimetype" file needed by ODF */
78 create_mimetype (const char *dirname)
81 struct string filename;
82 ds_init_cstr (&filename, dirname);
83 ds_put_cstr (&filename, "/mimetype");
84 fp = fopen (ds_cstr (&filename), "w");
88 error (0, errno, _("failed to create output file %s"),
90 ds_destroy (&filename);
93 ds_destroy (&filename);
95 fprintf (fp, "application/vnd.oasis.opendocument.text");
101 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
102 static xmlTextWriterPtr
103 create_writer (const struct odt_driver *driver, const char *filename)
108 ds_init_cstr (&str, driver->dirname);
109 ds_put_cstr (&str, "/");
110 ds_put_cstr (&str, filename);
112 /* dirname modifies its argument, so we must copy it */
113 copy = xstrdup (ds_cstr (&str));
114 mkdir (dirname (copy), 0700);
117 w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
121 xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
128 register_file (struct odt_driver *odt, const char *filename)
130 assert (odt->manifest_wtr);
131 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
132 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
133 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
134 xmlTextWriterEndElement (odt->manifest_wtr);
138 write_style_data (struct odt_driver *odt)
140 xmlTextWriterPtr w = create_writer (odt, "styles.xml");
141 register_file (odt, "styles.xml");
143 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
144 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
145 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
147 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
148 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
150 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
151 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
153 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
157 xmlTextWriterStartElement (w, _xml ("office:styles"));
161 xmlTextWriterStartElement (w, _xml ("style:style"));
162 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
165 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
168 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
171 xmlTextWriterEndElement (w); /* style:style */
175 xmlTextWriterStartElement (w, _xml ("style:style"));
176 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
177 _xml ("Table_20_Contents"));
179 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
180 _xml ("Table Contents"));
182 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
185 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
188 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
191 xmlTextWriterEndElement (w); /* style:style */
195 xmlTextWriterStartElement (w, _xml ("style:style"));
196 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
197 _xml ("Table_20_Heading"));
199 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
200 _xml ("Table Heading"));
202 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
205 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
206 _xml ("Table_20_Contents"));
208 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
212 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
213 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
214 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
215 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
216 xmlTextWriterEndElement (w); /* style:text-properties */
218 xmlTextWriterEndElement (w); /* style:style */
222 xmlTextWriterEndElement (w); /* office:styles */
223 xmlTextWriterEndElement (w); /* office:document-styles */
225 xmlTextWriterEndDocument (w);
226 xmlFreeTextWriter (w);
230 write_meta_data (struct odt_driver *odt)
232 xmlTextWriterPtr w = create_writer (odt, "meta.xml");
233 register_file (odt, "meta.xml");
235 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
236 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
237 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
238 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
239 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
240 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
242 xmlTextWriterStartElement (w, _xml ("office:meta"));
244 xmlTextWriterStartElement (w, _xml ("meta:generator"));
245 xmlTextWriterWriteString (w, _xml (stat_version));
246 xmlTextWriterEndElement (w);
252 struct passwd *pw = getpwuid (getuid ());
253 time_t t = time (NULL);
254 struct tm *tm = localtime (&t);
256 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
258 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
259 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
260 xmlTextWriterEndElement (w);
262 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
263 xmlTextWriterWriteString (w, _xml (buf));
264 xmlTextWriterEndElement (w);
266 xmlTextWriterStartElement (w, _xml ("dc:creator"));
267 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
269 xmlTextWriterEndElement (w);
271 xmlTextWriterStartElement (w, _xml ("dc:date"));
272 xmlTextWriterWriteString (w, _xml (buf));
273 xmlTextWriterEndElement (w);
276 xmlTextWriterEndElement (w);
277 xmlTextWriterEndElement (w);
278 xmlTextWriterEndDocument (w);
279 xmlFreeTextWriter (w);
288 static struct driver_option *
289 opt (struct output_driver *d, struct string_map *options, const char *key,
290 const char *default_value)
292 return driver_option_get (d, options, key, default_value);
295 static struct output_driver *
296 odt_create (const char *name, enum output_device_type device_type,
297 struct string_map *o)
299 struct output_driver *d;
300 struct odt_driver *odt;
302 odt = xzalloc (sizeof *odt);
304 output_driver_init (d, &odt_class, name, device_type);
306 odt->file_name = parse_string (opt (d, o, "output-file", "pspp.odt"));
307 odt->debug = parse_boolean (opt (d, o, "debug", "false"));
309 odt->dirname = xstrdup ("odt-XXXXXX");
310 mkdtemp (odt->dirname);
312 if (!create_mimetype (odt->dirname))
314 output_driver_destroy (d);
318 /* Create the manifest */
319 odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml");
321 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
322 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
323 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
326 /* Add a manifest entry for the document as a whole */
327 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
328 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
329 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
330 xmlTextWriterEndElement (odt->manifest_wtr);
333 write_meta_data (odt);
334 write_style_data (odt);
336 odt->content_wtr = create_writer (odt, "content.xml");
337 register_file (odt, "content.xml");
340 /* Some necessary junk at the start */
341 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
342 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
343 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
345 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
346 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
348 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
349 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
351 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
353 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
354 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
358 /* Close the manifest */
359 xmlTextWriterEndElement (odt->manifest_wtr);
360 xmlTextWriterEndDocument (odt->manifest_wtr);
361 xmlFreeTextWriter (odt->manifest_wtr);
367 odt_destroy (struct output_driver *driver)
369 struct odt_driver *odt = odt_driver_cast (driver);
371 if (odt->content_wtr != NULL)
373 struct string zip_cmd;
375 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
376 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
377 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
379 xmlTextWriterEndDocument (odt->content_wtr);
380 xmlFreeTextWriter (odt->content_wtr);
382 /* Zip up the directory */
383 ds_init_empty (&zip_cmd);
384 ds_put_format (&zip_cmd,
385 "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../%s .",
386 odt->dirname, odt->file_name, odt->file_name, odt->file_name);
387 system (ds_cstr (&zip_cmd));
388 ds_destroy (&zip_cmd);
393 /* Remove the temp dir */
394 struct string rm_cmd;
396 ds_init_empty (&rm_cmd);
397 ds_put_format (&rm_cmd, "rm -r %s", odt->dirname);
398 system (ds_cstr (&rm_cmd));
399 ds_destroy (&rm_cmd);
402 fprintf (stderr, "Not removing directory %s\n", odt->dirname);
409 odt_submit_table (struct odt_driver *odt, struct table_item *item)
411 const struct table *tab = table_item_get_table (item);
412 const char *caption = table_item_get_caption (item);
415 /* Write a heading for the table */
418 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
419 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
421 xmlTextWriterWriteString (odt->content_wtr,
422 _xml (table_item_get_caption (item)) );
423 xmlTextWriterEndElement (odt->content_wtr);
427 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
428 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
429 "TABLE-%d", odt->table_num++);
432 /* Start column definitions */
433 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
434 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
435 xmlTextWriterEndElement (odt->content_wtr);
438 /* Deal with row headers */
439 if ( table_ht (tab) > 0)
440 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
443 /* Write all the rows */
444 for (r = 0 ; r < table_nr (tab); ++r)
446 /* Start row definition */
447 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
449 /* Write all the columns */
450 for (c = 0 ; c < table_nc (tab) ; ++c)
452 struct table_cell cell;
454 table_get_cell (tab, c, r, &cell);
456 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
458 int colspan = table_cell_colspan (&cell);
459 int rowspan = table_cell_rowspan (&cell);
461 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
462 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
465 xmlTextWriterWriteFormatAttribute (
466 odt->content_wtr, _xml("table:number-columns-spanned"),
470 xmlTextWriterWriteFormatAttribute (
471 odt->content_wtr, _xml("table:number-rows-spanned"),
474 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
476 if ( r < table_ht (tab) || c < table_hl (tab) )
477 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
479 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
481 xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
483 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
484 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
488 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
489 xmlTextWriterEndElement (odt->content_wtr);
492 table_cell_free (&cell);
495 xmlTextWriterEndElement (odt->content_wtr); /* row */
497 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
498 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
501 xmlTextWriterEndElement (odt->content_wtr); /* table */
504 /* Submit a table to the ODT driver */
506 odt_submit (struct output_driver *driver,
507 const struct output_item *output_item)
509 struct odt_driver *odt = odt_driver_cast (driver);
510 if (is_table_item (output_item))
511 odt_submit_table (odt, to_table_item (output_item));
512 else if (is_text_item (output_item))
514 const struct text_item *text_item = to_text_item (output_item);
515 const char *text = text_item_get_text (text_item);
517 /* XXX apply different styles based on text_item's type. */
518 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
519 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
520 xmlTextWriterEndElement (odt->content_wtr);
524 /* ODT driver class. */
525 const struct output_driver_class odt_class =