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/message-item.h"
36 #include "output/options.h"
37 #include "output/tab.h"
38 #include "output/table-item.h"
39 #include "output/table-provider.h"
40 #include "output/text-item.h"
42 #include "gl/xalloc.h"
46 #define _(msgid) gettext (msgid)
48 #define _xml(X) (const xmlChar *)(X)
52 struct output_driver driver;
54 char *file_name; /* Output file name. */
57 /* The name of the temporary directory used to construct the ODF */
60 /* Writer for the content.xml file */
61 xmlTextWriterPtr content_wtr;
63 /* Writer fot the manifest.xml file */
64 xmlTextWriterPtr manifest_wtr;
66 /* Number of tables so far. */
69 /* Name of current command. */
73 static const struct output_driver_class odt_driver_class;
75 static struct odt_driver *
76 odt_driver_cast (struct output_driver *driver)
78 assert (driver->class == &odt_driver_class);
79 return UP_CAST (driver, struct odt_driver, driver);
82 /* Create the "mimetype" file needed by ODF */
84 create_mimetype (const char *dirname)
87 struct string filename;
88 ds_init_cstr (&filename, dirname);
89 ds_put_cstr (&filename, "/mimetype");
90 fp = fopen (ds_cstr (&filename), "w");
94 error (0, errno, _("failed to create output file %s"),
96 ds_destroy (&filename);
99 ds_destroy (&filename);
101 fprintf (fp, "application/vnd.oasis.opendocument.text");
107 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
108 static xmlTextWriterPtr
109 create_writer (const struct odt_driver *driver, const char *filename)
114 ds_init_cstr (&str, driver->dirname);
115 ds_put_cstr (&str, "/");
116 ds_put_cstr (&str, filename);
118 /* dirname modifies its argument, so we must copy it */
119 copy = xstrdup (ds_cstr (&str));
120 mkdir (dirname (copy), 0700);
123 w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
127 xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
134 register_file (struct odt_driver *odt, const char *filename)
136 assert (odt->manifest_wtr);
137 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
138 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
139 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml (filename));
140 xmlTextWriterEndElement (odt->manifest_wtr);
144 write_style_data (struct odt_driver *odt)
146 xmlTextWriterPtr w = create_writer (odt, "styles.xml");
147 register_file (odt, "styles.xml");
149 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
150 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
151 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
153 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
154 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
156 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
157 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
159 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
163 xmlTextWriterStartElement (w, _xml ("office:styles"));
167 xmlTextWriterStartElement (w, _xml ("style:style"));
168 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
171 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
174 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
177 xmlTextWriterEndElement (w); /* style:style */
181 xmlTextWriterStartElement (w, _xml ("style:style"));
182 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
183 _xml ("Table_20_Contents"));
185 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
186 _xml ("Table Contents"));
188 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
191 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
194 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
197 xmlTextWriterEndElement (w); /* style:style */
201 xmlTextWriterStartElement (w, _xml ("style:style"));
202 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
203 _xml ("Table_20_Heading"));
205 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
206 _xml ("Table Heading"));
208 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
211 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
212 _xml ("Table_20_Contents"));
214 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
218 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
219 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
220 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
221 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
222 xmlTextWriterEndElement (w); /* style:text-properties */
224 xmlTextWriterEndElement (w); /* style:style */
228 xmlTextWriterEndElement (w); /* office:styles */
229 xmlTextWriterEndElement (w); /* office:document-styles */
231 xmlTextWriterEndDocument (w);
232 xmlFreeTextWriter (w);
236 write_meta_data (struct odt_driver *odt)
238 xmlTextWriterPtr w = create_writer (odt, "meta.xml");
239 register_file (odt, "meta.xml");
241 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
242 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
243 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
244 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
245 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
246 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
248 xmlTextWriterStartElement (w, _xml ("office:meta"));
250 xmlTextWriterStartElement (w, _xml ("meta:generator"));
251 xmlTextWriterWriteString (w, _xml (stat_version));
252 xmlTextWriterEndElement (w);
258 struct passwd *pw = getpwuid (getuid ());
259 time_t t = time (NULL);
260 struct tm *tm = localtime (&t);
262 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
264 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
265 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
266 xmlTextWriterEndElement (w);
268 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
269 xmlTextWriterWriteString (w, _xml (buf));
270 xmlTextWriterEndElement (w);
272 xmlTextWriterStartElement (w, _xml ("dc:creator"));
273 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
275 xmlTextWriterEndElement (w);
277 xmlTextWriterStartElement (w, _xml ("dc:date"));
278 xmlTextWriterWriteString (w, _xml (buf));
279 xmlTextWriterEndElement (w);
282 xmlTextWriterEndElement (w);
283 xmlTextWriterEndElement (w);
284 xmlTextWriterEndDocument (w);
285 xmlFreeTextWriter (w);
294 static struct driver_option *
295 opt (struct output_driver *d, struct string_map *options, const char *key,
296 const char *default_value)
298 return driver_option_get (d, options, key, default_value);
301 static struct output_driver *
302 odt_create (const char *file_name, enum settings_output_devices device_type,
303 struct string_map *o)
305 struct output_driver *d;
306 struct odt_driver *odt;
308 odt = xzalloc (sizeof *odt);
310 output_driver_init (d, &odt_driver_class, file_name, device_type);
312 odt->file_name = xstrdup (file_name);
313 odt->debug = parse_boolean (opt (d, o, "debug", "false"));
315 odt->dirname = xstrdup ("odt-XXXXXX");
316 mkdtemp (odt->dirname);
318 if (!create_mimetype (odt->dirname))
320 output_driver_destroy (d);
324 /* Create the manifest */
325 odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml");
327 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
328 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
329 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
332 /* Add a manifest entry for the document as a whole */
333 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
334 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
335 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
336 xmlTextWriterEndElement (odt->manifest_wtr);
339 write_meta_data (odt);
340 write_style_data (odt);
342 odt->content_wtr = create_writer (odt, "content.xml");
343 register_file (odt, "content.xml");
346 /* Some necessary junk at the start */
347 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
348 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
349 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
351 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
352 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
354 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
355 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
357 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
359 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
360 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
364 /* Close the manifest */
365 xmlTextWriterEndElement (odt->manifest_wtr);
366 xmlTextWriterEndDocument (odt->manifest_wtr);
367 xmlFreeTextWriter (odt->manifest_wtr);
373 odt_destroy (struct output_driver *driver)
375 struct odt_driver *odt = odt_driver_cast (driver);
377 if (odt->content_wtr != NULL)
379 struct string zip_cmd;
381 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
382 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
383 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
385 xmlTextWriterEndDocument (odt->content_wtr);
386 xmlFreeTextWriter (odt->content_wtr);
388 /* Zip up the directory */
389 ds_init_empty (&zip_cmd);
390 ds_put_format (&zip_cmd,
391 "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../%s .",
392 odt->dirname, odt->file_name, odt->file_name, odt->file_name);
393 system (ds_cstr (&zip_cmd));
394 ds_destroy (&zip_cmd);
399 /* Remove the temp dir */
400 struct string rm_cmd;
402 ds_init_empty (&rm_cmd);
403 ds_put_format (&rm_cmd, "rm -r %s", odt->dirname);
404 system (ds_cstr (&rm_cmd));
405 ds_destroy (&rm_cmd);
408 fprintf (stderr, "Not removing directory %s\n", odt->dirname);
410 free (odt->command_name);
416 odt_submit_table (struct odt_driver *odt, struct table_item *item)
418 const struct table *tab = table_item_get_table (item);
419 const char *caption = table_item_get_caption (item);
422 /* Write a heading for the table */
425 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
426 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
428 xmlTextWriterWriteString (odt->content_wtr,
429 _xml (table_item_get_caption (item)) );
430 xmlTextWriterEndElement (odt->content_wtr);
434 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
435 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
436 "TABLE-%d", odt->table_num++);
439 /* Start column definitions */
440 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
441 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
442 xmlTextWriterEndElement (odt->content_wtr);
445 /* Deal with row headers */
446 if ( table_ht (tab) > 0)
447 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
450 /* Write all the rows */
451 for (r = 0 ; r < table_nr (tab); ++r)
453 /* Start row definition */
454 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
456 /* Write all the columns */
457 for (c = 0 ; c < table_nc (tab) ; ++c)
459 struct table_cell cell;
461 table_get_cell (tab, c, r, &cell);
463 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
465 int colspan = table_cell_colspan (&cell);
466 int rowspan = table_cell_rowspan (&cell);
468 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
469 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
472 xmlTextWriterWriteFormatAttribute (
473 odt->content_wtr, _xml("table:number-columns-spanned"),
477 xmlTextWriterWriteFormatAttribute (
478 odt->content_wtr, _xml("table:number-rows-spanned"),
481 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
483 if ( r < table_ht (tab) || c < table_hl (tab) )
484 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
486 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
488 xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
490 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
491 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
495 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
496 xmlTextWriterEndElement (odt->content_wtr);
499 table_cell_free (&cell);
502 xmlTextWriterEndElement (odt->content_wtr); /* row */
504 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
505 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
508 xmlTextWriterEndElement (odt->content_wtr); /* table */
512 odt_output_text (struct odt_driver *odt, const char *text)
514 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
515 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
516 xmlTextWriterEndElement (odt->content_wtr);
519 /* Submit a table to the ODT driver */
521 odt_submit (struct output_driver *driver,
522 const struct output_item *output_item)
524 struct odt_driver *odt = odt_driver_cast (driver);
526 output_driver_track_current_command (output_item, &odt->command_name);
528 if (is_table_item (output_item))
529 odt_submit_table (odt, to_table_item (output_item));
530 else if (is_text_item (output_item))
532 /* XXX apply different styles based on text_item's type. */
533 odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
535 else if (is_message_item (output_item))
537 const struct message_item *message_item = to_message_item (output_item);
538 const struct msg *msg = message_item_get_msg (message_item);
539 char *s = msg_to_string (msg, odt->command_name);
540 odt_output_text (odt, s);
545 struct output_driver_factory odt_driver_factory = { "odt", odt_create };
547 static const struct output_driver_class odt_driver_class =