1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009 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 */
21 #include <libpspp/assertion.h>
22 #include <libpspp/cast.h>
23 #include <libpspp/str.h>
24 #include <libpspp/version.h>
25 #include <output/driver-provider.h>
26 #include <output/options.h>
27 #include <output/tab.h>
28 #include <output/table-item.h>
29 #include <output/table-provider.h>
30 #include <output/text-item.h>
33 #include <libxml/xmlwriter.h>
36 #include <sys/types.h>
44 #define _(msgid) gettext (msgid)
46 #define _xml(X) (const xmlChar *)(X)
50 struct output_driver driver;
52 char *file_name; /* Output file name. */
55 /* The name of the temporary directory used to construct the ODF */
58 /* Writer for the content.xml file */
59 xmlTextWriterPtr content_wtr;
61 /* Writer fot the manifest.xml file */
62 xmlTextWriterPtr manifest_wtr;
64 /* Number of tables so far. */
68 static struct odt_driver *
69 odt_driver_cast (struct output_driver *driver)
71 assert (driver->class == &odt_class);
72 return UP_CAST (driver, struct odt_driver, driver);
75 /* Create the "mimetype" file needed by ODF */
77 create_mimetype (const char *dirname)
80 struct string filename;
81 ds_init_cstr (&filename, dirname);
82 ds_put_cstr (&filename, "/mimetype");
83 fp = fopen (ds_cstr (&filename), "w");
84 ds_destroy (&filename);
87 fprintf (fp, "application/vnd.oasis.opendocument.text");
91 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
92 static xmlTextWriterPtr
93 create_writer (const struct odt_driver *driver, const char *filename)
98 ds_init_cstr (&str, driver->dirname);
99 ds_put_cstr (&str, "/");
100 ds_put_cstr (&str, filename);
102 /* dirname modifies its argument, so we must copy it */
103 copy = xstrdup (ds_cstr (&str));
104 mkdir (dirname (copy), 0700);
107 w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
111 xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
118 register_file (struct odt_driver *odt, const char *filename)
120 assert (odt->manifest_wtr);
121 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
122 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
123 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
124 xmlTextWriterEndElement (odt->manifest_wtr);
128 write_style_data (struct odt_driver *odt)
130 xmlTextWriterPtr w = create_writer (odt, "styles.xml");
131 register_file (odt, "styles.xml");
133 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
134 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
135 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
137 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
138 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
140 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
141 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
143 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
147 xmlTextWriterStartElement (w, _xml ("office:styles"));
151 xmlTextWriterStartElement (w, _xml ("style:style"));
152 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
155 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
158 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
161 xmlTextWriterEndElement (w); /* style:style */
165 xmlTextWriterStartElement (w, _xml ("style:style"));
166 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
167 _xml ("Table_20_Contents"));
169 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
170 _xml ("Table Contents"));
172 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
175 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
178 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
181 xmlTextWriterEndElement (w); /* style:style */
185 xmlTextWriterStartElement (w, _xml ("style:style"));
186 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
187 _xml ("Table_20_Heading"));
189 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
190 _xml ("Table Heading"));
192 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
195 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
196 _xml ("Table_20_Contents"));
198 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
202 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
203 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
204 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
205 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
206 xmlTextWriterEndElement (w); /* style:text-properties */
208 xmlTextWriterEndElement (w); /* style:style */
212 xmlTextWriterEndElement (w); /* office:styles */
213 xmlTextWriterEndElement (w); /* office:document-styles */
215 xmlTextWriterEndDocument (w);
216 xmlFreeTextWriter (w);
220 write_meta_data (struct odt_driver *odt)
222 xmlTextWriterPtr w = create_writer (odt, "meta.xml");
223 register_file (odt, "meta.xml");
225 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
226 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
227 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
228 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
229 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
230 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
232 xmlTextWriterStartElement (w, _xml ("office:meta"));
234 xmlTextWriterStartElement (w, _xml ("meta:generator"));
235 xmlTextWriterWriteString (w, _xml (stat_version));
236 xmlTextWriterEndElement (w);
242 struct passwd *pw = getpwuid (getuid ());
243 time_t t = time (NULL);
244 struct tm *tm = localtime (&t);
246 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
248 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
249 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
250 xmlTextWriterEndElement (w);
252 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
253 xmlTextWriterWriteString (w, _xml (buf));
254 xmlTextWriterEndElement (w);
256 xmlTextWriterStartElement (w, _xml ("dc:creator"));
257 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
259 xmlTextWriterEndElement (w);
261 xmlTextWriterStartElement (w, _xml ("dc:date"));
262 xmlTextWriterWriteString (w, _xml (buf));
263 xmlTextWriterEndElement (w);
266 xmlTextWriterEndElement (w);
267 xmlTextWriterEndElement (w);
268 xmlTextWriterEndDocument (w);
269 xmlFreeTextWriter (w);
278 static struct driver_option *
279 opt (struct output_driver *d, struct string_map *options, const char *key,
280 const char *default_value)
282 return driver_option_get (d, options, key, default_value);
285 static struct output_driver *
286 odt_create (const char *name, enum output_device_type device_type,
287 struct string_map *o)
289 struct output_driver *d;
290 struct odt_driver *odt;
292 odt = xzalloc (sizeof *odt);
294 output_driver_init (d, &odt_class, name, device_type);
296 odt->file_name = parse_string (opt (d, o, "output-file", "pspp.odt"));
297 odt->debug = parse_boolean (opt (d, o, "debug", "false"));
299 odt->dirname = xstrdup ("odt-XXXXXX");
300 mkdtemp (odt->dirname);
302 create_mimetype (odt->dirname);
304 /* Create the manifest */
305 odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml");
307 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
308 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
309 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
312 /* Add a manifest entry for the document as a whole */
313 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
314 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
315 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
316 xmlTextWriterEndElement (odt->manifest_wtr);
319 write_meta_data (odt);
320 write_style_data (odt);
322 odt->content_wtr = create_writer (odt, "content.xml");
323 register_file (odt, "content.xml");
326 /* Some necessary junk at the start */
327 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
328 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
329 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
331 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
332 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
334 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
335 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
337 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
339 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
340 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
344 /* Close the manifest */
345 xmlTextWriterEndElement (odt->manifest_wtr);
346 xmlTextWriterEndDocument (odt->manifest_wtr);
347 xmlFreeTextWriter (odt->manifest_wtr);
353 odt_destroy (struct output_driver *driver)
355 struct odt_driver *odt = odt_driver_cast (driver);
357 struct string zip_cmd;
358 struct string rm_cmd;
360 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
361 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
362 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
364 xmlTextWriterEndDocument (odt->content_wtr);
365 xmlFreeTextWriter (odt->content_wtr);
367 /* Zip up the directory */
368 ds_init_empty (&zip_cmd);
369 ds_put_format (&zip_cmd,
370 "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../pspp.odt .",
371 odt->dirname, odt->file_name, odt->file_name);
372 system (ds_cstr (&zip_cmd));
373 ds_destroy (&zip_cmd);
378 /* Remove the temp dir */
379 ds_init_empty (&rm_cmd);
380 ds_put_format (&rm_cmd, "rm -r %s", odt->dirname);
381 system (ds_cstr (&rm_cmd));
382 ds_destroy (&rm_cmd);
385 fprintf (stderr, "Not removing directory %s\n", odt->dirname);
392 odt_submit_table (struct odt_driver *odt, struct table_item *item)
394 const struct table *tab = table_item_get_table (item);
395 const char *caption = table_item_get_caption (item);
398 /* Write a heading for the table */
401 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
402 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
404 xmlTextWriterWriteString (odt->content_wtr,
405 _xml (table_item_get_caption (item)) );
406 xmlTextWriterEndElement (odt->content_wtr);
410 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
411 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
412 "TABLE-%d", odt->table_num++);
415 /* Start column definitions */
416 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
417 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
418 xmlTextWriterEndElement (odt->content_wtr);
421 /* Deal with row headers */
422 if ( table_ht (tab) > 0)
423 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
426 /* Write all the rows */
427 for (r = 0 ; r < table_nr (tab); ++r)
429 /* Start row definition */
430 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
432 /* Write all the columns */
433 for (c = 0 ; c < table_nc (tab) ; ++c)
435 struct table_cell cell;
437 table_get_cell (tab, c, r, &cell);
439 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
441 int colspan = table_cell_colspan (&cell);
442 int rowspan = table_cell_rowspan (&cell);
444 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
445 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
448 xmlTextWriterWriteFormatAttribute (
449 odt->content_wtr, _xml("table:number-columns-spanned"),
453 xmlTextWriterWriteFormatAttribute (
454 odt->content_wtr, _xml("table:number-rows-spanned"),
457 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
459 if ( r < table_ht (tab) || c < table_hl (tab) )
460 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
462 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
464 xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
466 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
467 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
471 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
472 xmlTextWriterEndElement (odt->content_wtr);
475 table_cell_free (&cell);
478 xmlTextWriterEndElement (odt->content_wtr); /* row */
480 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
481 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
484 xmlTextWriterEndElement (odt->content_wtr); /* table */
487 /* Submit a table to the ODT driver */
489 odt_submit (struct output_driver *driver,
490 const struct output_item *output_item)
492 struct odt_driver *odt = odt_driver_cast (driver);
493 if (is_table_item (output_item))
494 odt_submit_table (odt, to_table_item (output_item));
495 else if (is_text_item (output_item))
497 const struct text_item *text_item = to_text_item (output_item);
498 const char *text = text_item_get_text (text_item);
500 /* XXX apply different styles based on text_item's type. */
501 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
502 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
503 xmlTextWriterEndElement (odt->content_wtr);
507 /* ODT driver class. */
508 const struct output_driver_class odt_class =