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/>. */
20 #define _(msgid) gettext (msgid)
22 /* A driver for creating OpenDocument Format text files from PSPP's output */
24 #include <libpspp/assertion.h>
25 #include <libpspp/version.h>
27 #include <output/manager.h>
28 #include <output/output.h>
29 #include <output/table.h>
34 #include <sys/types.h>
38 #include <libxml/xmlwriter.h>
44 #define _xml(X) (const xmlChar *)(X)
47 struct odf_driver_options
49 struct outp_driver *driver;
51 char *file_name; /* Output file name. */
58 /* The name of the temporary directory used to construct the ODF */
61 /* Writer for the content.xml file */
62 xmlTextWriterPtr content_wtr;
64 /* Writer fot the manifest.xml file */
65 xmlTextWriterPtr manifest_wtr;
67 struct odf_driver_options opts;
72 /* Create the "mimetype" file needed by ODF */
74 create_mimetype (const char *dirname)
77 struct string filename;
78 ds_init_cstr (&filename, dirname);
79 ds_put_cstr (&filename, "/mimetype");
80 fp = fopen (ds_cstr (&filename), "w");
81 ds_destroy (&filename);
84 fprintf (fp, "application/vnd.oasis.opendocument.text");
88 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
89 static xmlTextWriterPtr
90 create_writer (const struct odt_driver_ext *driver, const char *filename)
95 ds_init_cstr (&str, driver->dirname);
96 ds_put_cstr (&str, "/");
97 ds_put_cstr (&str, filename);
99 /* dirname modifies its argument, so we must copy it */
100 copy = xstrdup (ds_cstr (&str));
101 mkdir (dirname (copy), 0700);
104 w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
108 xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
115 register_file (struct odt_driver_ext *x, const char *filename)
117 assert (x->manifest_wtr);
118 xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:file-entry"));
119 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
120 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
121 xmlTextWriterEndElement (x->manifest_wtr);
125 write_style_data (struct odt_driver_ext *x)
127 xmlTextWriterPtr w = create_writer (x, "styles.xml");
128 register_file (x, "styles.xml");
130 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
131 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
132 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
134 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
135 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
137 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
138 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
140 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
144 xmlTextWriterStartElement (w, _xml ("office:styles"));
148 xmlTextWriterStartElement (w, _xml ("style:style"));
149 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
152 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
155 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
158 xmlTextWriterEndElement (w); /* style:style */
162 xmlTextWriterStartElement (w, _xml ("style:style"));
163 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
164 _xml ("Table_20_Contents"));
166 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
167 _xml ("Table Contents"));
169 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
172 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
175 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
178 xmlTextWriterEndElement (w); /* style:style */
182 xmlTextWriterStartElement (w, _xml ("style:style"));
183 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
184 _xml ("Table_20_Heading"));
186 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
187 _xml ("Table Heading"));
189 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
192 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
193 _xml ("Table_20_Contents"));
195 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
199 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
200 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
201 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
202 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
203 xmlTextWriterEndElement (w); /* style:text-properties */
205 xmlTextWriterEndElement (w); /* style:style */
209 xmlTextWriterEndElement (w); /* office:styles */
210 xmlTextWriterEndElement (w); /* office:document-styles */
212 xmlTextWriterEndDocument (w);
213 xmlFreeTextWriter (w);
217 write_meta_data (struct odt_driver_ext *x)
219 xmlTextWriterPtr w = create_writer (x, "meta.xml");
220 register_file (x, "meta.xml");
222 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
223 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
224 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
225 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
226 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
227 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
229 xmlTextWriterStartElement (w, _xml ("office:meta"));
231 xmlTextWriterStartElement (w, _xml ("meta:generator"));
232 xmlTextWriterWriteString (w, _xml (stat_version));
233 xmlTextWriterEndElement (w);
239 struct passwd *pw = getpwuid (getuid ());
240 time_t t = time (NULL);
241 struct tm *tm = localtime (&t);
243 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
245 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
246 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
247 xmlTextWriterEndElement (w);
249 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
250 xmlTextWriterWriteString (w, _xml (buf));
251 xmlTextWriterEndElement (w);
253 xmlTextWriterStartElement (w, _xml ("dc:creator"));
254 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
256 xmlTextWriterEndElement (w);
258 xmlTextWriterStartElement (w, _xml ("dc:date"));
259 xmlTextWriterWriteString (w, _xml (buf));
260 xmlTextWriterEndElement (w);
263 xmlTextWriterEndElement (w);
264 xmlTextWriterEndElement (w);
265 xmlTextWriterEndDocument (w);
266 xmlFreeTextWriter (w);
275 static const struct outp_option option_tab[] =
277 {"output-file", output_file_arg,0},
279 {"debug", boolean_arg, 1},
285 handle_option (void *options_, const char *key, const struct string *val)
287 struct odf_driver_options *options = options_;
288 struct outp_driver *this = options->driver;
290 char *value = ds_cstr (val);
292 switch (outp_match_keyword (key, option_tab, &subcat))
296 _("unknown configuration parameter `%s' for %s device "
297 "driver"), key, this->class->name);
299 case output_file_arg:
300 free (options->file_name);
301 options->file_name = xstrdup (value);
304 if (!strcmp (value, "on") || !strcmp (value, "true")
305 || !strcmp (value, "yes") || atoi (value))
306 options->debug = true;
307 else if (!strcmp (value, "off") || !strcmp (value, "false")
308 || !strcmp (value, "no") || !strcmp (value, "0"))
309 options->debug = false;
312 error (0, 0, _("boolean value expected for %s"), key);
326 odt_open_driver (const char *name, int types, struct substring option_string)
328 struct odt_driver_ext *x;
329 struct outp_driver *this = outp_allocate_driver (&odt_class, name, types);
331 this->ext = x = xmalloc (sizeof *x);
333 x->opts.driver = this;
334 x->opts.file_name = xstrdup ("pspp.pdt");
335 x->opts.debug = false;
337 outp_parse_options (this->name, option_string, handle_option, &x->opts);
339 outp_register_driver (this);
341 x->dirname = xstrdup ("odt-XXXXXX");
342 mkdtemp (x->dirname);
344 create_mimetype (x->dirname);
346 /* Create the manifest */
347 x->manifest_wtr = create_writer (x, "META-INF/manifest.xml");
349 xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:manifest"));
350 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("xmlns:manifest"),
351 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
354 /* Add a manifest entry for the document as a whole */
355 xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:file-entry"));
356 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
357 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
358 xmlTextWriterEndElement (x->manifest_wtr);
362 write_style_data (x);
364 x->content_wtr = create_writer (x, "content.xml");
365 register_file (x, "content.xml");
368 /* Some necessary junk at the start */
369 xmlTextWriterStartElement (x->content_wtr, _xml("office:document-content"));
370 xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:office"),
371 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
373 xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:text"),
374 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
376 xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:table"),
377 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
379 xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:version"), _xml("1.1"));
381 xmlTextWriterStartElement (x->content_wtr, _xml("office:body"));
382 xmlTextWriterStartElement (x->content_wtr, _xml("office:text"));
386 /* Close the manifest */
387 xmlTextWriterEndElement (x->manifest_wtr);
388 xmlTextWriterEndDocument (x->manifest_wtr);
389 xmlFreeTextWriter (x->manifest_wtr);
395 odt_close_driver (struct outp_driver *this)
397 struct string zip_cmd;
398 struct string rm_cmd;
399 struct odt_driver_ext *x = this->ext;
401 xmlTextWriterEndElement (x->content_wtr); /* office:text */
402 xmlTextWriterEndElement (x->content_wtr); /* office:body */
403 xmlTextWriterEndElement (x->content_wtr); /* office:document-content */
405 xmlTextWriterEndDocument (x->content_wtr);
406 xmlFreeTextWriter (x->content_wtr);
408 /* Zip up the directory */
409 ds_init_empty (&zip_cmd);
410 ds_put_format (&zip_cmd,
411 "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../pspp.odt .",
412 x->dirname, x->opts.file_name, x->opts.file_name);
413 system (ds_cstr (&zip_cmd));
414 ds_destroy (&zip_cmd);
417 if ( !x->opts.debug )
419 /* Remove the temp dir */
420 ds_init_empty (&rm_cmd);
421 ds_put_format (&rm_cmd, "rm -r %s", x->dirname);
422 system (ds_cstr (&rm_cmd));
423 ds_destroy (&rm_cmd);
426 fprintf (stderr, "Not removing directory %s\n", x->dirname);
435 odt_open_page (struct outp_driver *this UNUSED)
440 odt_close_page (struct outp_driver *this UNUSED)
445 odt_output_chart (struct outp_driver *this UNUSED, const struct chart *chart UNUSED)
447 printf ("%s\n", __FUNCTION__);
451 /* Submit a table to the ODT driver */
453 odt_submit (struct outp_driver *this, struct som_entity *e)
457 struct odt_driver_ext *x = this->ext;
458 struct tab_table *tab = e->ext;
461 /* Write a heading for the table */
462 xmlTextWriterStartElement (x->content_wtr, _xml("text:h"));
463 xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("text:level"), "%d", e->subtable_num == 1 ? 2 : 3);
464 xmlTextWriterWriteString (x->content_wtr, _xml (tab->title) );
465 xmlTextWriterEndElement (x->content_wtr);
468 xmlTextWriterStartElement (x->content_wtr, _xml("table:table"));
469 xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:name"),
470 "TABLE-%d.%d", e->table_num, e->subtable_num);
473 /* Start column definitions */
474 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-column"));
475 xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:number-columns-repeated"), "%d", tab_nc (tab));
476 xmlTextWriterEndElement (x->content_wtr);
479 /* Deal with row headers */
480 if ( tab_t (tab) > 0)
481 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-header-rows"));
484 /* Write all the rows */
485 for (r = 0 ; r < tab_nr (tab); ++r)
487 int spanned_columns = 0;
488 /* Start row definition */
489 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-row"));
491 /* Write all the columns */
492 for (c = 0 ; c < tab_nc (tab) ; ++c)
495 unsigned int opts = tab->ct[tab_nc (tab) * r + c];
496 struct substring ss = tab->cc[tab_nc (tab) * r + c];
498 if (opts & TAB_EMPTY)
500 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-cell"));
501 xmlTextWriterEndElement (x->content_wtr);
505 if ( opts & TAB_JOIN)
507 if ( spanned_columns == 0)
509 struct tab_joined_cell *j = (struct tab_joined_cell*) ss_data (ss);
510 s = ss_xstrdup (j->contents);
516 if ( spanned_columns == 0 )
518 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-cell"));
519 xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:value-type"), _xml("string"));
521 if ( opts & TAB_JOIN )
523 struct tab_joined_cell *j = (struct tab_joined_cell*) ss_data (ss);
524 spanned_columns = j->x2 - j->x1;
526 xmlTextWriterWriteFormatAttribute (x->content_wtr,
527 _xml("table:number-columns-spanned"),
528 "%d", spanned_columns);
531 xmlTextWriterStartElement (x->content_wtr, _xml("text:p"));
533 if ( r < tab_t (tab) || c < tab_l (tab) )
534 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
536 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
538 xmlTextWriterWriteString (x->content_wtr, _xml (s));
540 xmlTextWriterEndElement (x->content_wtr); /* text:p */
541 xmlTextWriterEndElement (x->content_wtr); /* table:table-cell */
545 xmlTextWriterStartElement (x->content_wtr, _xml("table:covered-table-cell"));
546 xmlTextWriterEndElement (x->content_wtr);
548 if ( opts & TAB_JOIN )
554 xmlTextWriterEndElement (x->content_wtr); /* row */
556 if ( tab_t (tab) > 0 && r == tab_t (tab) - 1)
557 xmlTextWriterEndElement (x->content_wtr); /* table-header-rows */
560 xmlTextWriterEndElement (x->content_wtr); /* table */
564 /* ODT driver class. */
565 const struct outp_class odt_class =