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 "libpspp/zip-writer.h"
37 #include "output/driver-provider.h"
38 #include "output/message-item.h"
39 #include "output/options.h"
40 #include "output/tab.h"
41 #include "output/table-item.h"
42 #include "output/table-provider.h"
43 #include "output/text-item.h"
45 #include "gl/xalloc.h"
49 #define _(msgid) gettext (msgid)
51 #define _xml(X) (const xmlChar *)(X)
55 struct output_driver driver;
57 struct zip_writer *zip; /* ZIP file writer. */
58 char *file_name; /* Output file name. */
61 xmlTextWriterPtr content_wtr; /* XML writer. */
62 FILE *content_file; /* Temporary file. */
65 xmlTextWriterPtr manifest_wtr; /* XML writer. */
66 FILE *manifest_file; /* Temporary file. */
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 (struct zip_writer *zip)
93 error (0, errno, _("error creating temporary file"));
97 fprintf (fp, "application/vnd.oasis.opendocument.text");
98 zip_writer_add (zip, fp, "mimetype");
104 /* Creates a new temporary file and stores it in *FILE, then creates an XML
105 writer for it and stores it in *W. */
107 create_writer (FILE **file, xmlTextWriterPtr *w)
109 /* XXX this can fail */
111 *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
113 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)
133 create_writer (&file, &w);
134 register_file (odt, "styles.xml");
136 xmlTextWriterStartElement (w, _xml ("office:document-styles"));
137 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
138 _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
140 xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
141 _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
143 xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
144 _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
146 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml ("1.1"));
150 xmlTextWriterStartElement (w, _xml ("office:styles"));
154 xmlTextWriterStartElement (w, _xml ("style:style"));
155 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
158 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
161 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
164 xmlTextWriterEndElement (w); /* style:style */
168 xmlTextWriterStartElement (w, _xml ("style:style"));
169 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
170 _xml ("Table_20_Contents"));
172 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
173 _xml ("Table Contents"));
175 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
178 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
181 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
184 xmlTextWriterEndElement (w); /* style:style */
188 xmlTextWriterStartElement (w, _xml ("style:style"));
189 xmlTextWriterWriteAttribute (w, _xml ("style:name"),
190 _xml ("Table_20_Heading"));
192 xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
193 _xml ("Table Heading"));
195 xmlTextWriterWriteAttribute (w, _xml ("style:family"),
198 xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
199 _xml ("Table_20_Contents"));
201 xmlTextWriterWriteAttribute (w, _xml ("style:class"),
205 xmlTextWriterStartElement (w, _xml ("style:text-properties"));
206 xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
207 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
208 xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
209 xmlTextWriterEndElement (w); /* style:text-properties */
211 xmlTextWriterEndElement (w); /* style:style */
215 xmlTextWriterEndElement (w); /* office:styles */
216 xmlTextWriterEndElement (w); /* office:document-styles */
218 xmlTextWriterEndDocument (w);
219 xmlFreeTextWriter (w);
220 zip_writer_add (odt->zip, file, "styles.xml");
225 write_meta_data (struct odt_driver *odt)
230 create_writer (&file, &w);
231 register_file (odt, "meta.xml");
233 xmlTextWriterStartElement (w, _xml ("office:document-meta"));
234 xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
235 xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"), _xml ("http://purl.org/dc/elements/1.1/"));
236 xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
237 xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
238 xmlTextWriterWriteAttribute (w, _xml ("office:version"), _xml("1.1"));
240 xmlTextWriterStartElement (w, _xml ("office:meta"));
242 xmlTextWriterStartElement (w, _xml ("meta:generator"));
243 xmlTextWriterWriteString (w, _xml (stat_version));
244 xmlTextWriterEndElement (w);
250 time_t t = time (NULL);
251 struct tm *tm = localtime (&t);
253 strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
255 xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
256 xmlTextWriterWriteString (w, _xml (buf));
257 xmlTextWriterEndElement (w);
259 xmlTextWriterStartElement (w, _xml ("dc:date"));
260 xmlTextWriterWriteString (w, _xml (buf));
261 xmlTextWriterEndElement (w);
266 struct passwd *pw = getpwuid (getuid ());
269 xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
270 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
271 xmlTextWriterEndElement (w);
273 xmlTextWriterStartElement (w, _xml ("dc:creator"));
274 xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
275 xmlTextWriterEndElement (w);
280 xmlTextWriterEndElement (w);
281 xmlTextWriterEndElement (w);
282 xmlTextWriterEndDocument (w);
283 xmlFreeTextWriter (w);
284 zip_writer_add (odt->zip, file, "meta.xml");
288 static struct output_driver *
289 odt_create (const char *file_name, enum settings_output_devices device_type,
290 struct string_map *o UNUSED)
292 struct output_driver *d;
293 struct odt_driver *odt;
294 struct zip_writer *zip;
296 zip = zip_writer_create (file_name);
300 odt = xzalloc (sizeof *odt);
302 output_driver_init (d, &odt_driver_class, file_name, device_type);
305 odt->file_name = xstrdup (file_name);
307 if (!create_mimetype (zip))
309 output_driver_destroy (d);
313 /* Create the manifest */
314 create_writer (&odt->manifest_file, &odt->manifest_wtr);
316 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
317 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
318 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
321 /* Add a manifest entry for the document as a whole */
322 xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
323 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
324 xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
325 xmlTextWriterEndElement (odt->manifest_wtr);
328 write_meta_data (odt);
329 write_style_data (odt);
331 create_writer (&odt->content_file, &odt->content_wtr);
332 register_file (odt, "content.xml");
335 /* Some necessary junk at the start */
336 xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
337 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
338 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
340 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
341 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
343 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
344 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
346 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
348 xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
349 xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
353 /* Close the manifest */
354 xmlTextWriterEndElement (odt->manifest_wtr);
355 xmlTextWriterEndDocument (odt->manifest_wtr);
356 xmlFreeTextWriter (odt->manifest_wtr);
357 zip_writer_add (odt->zip, odt->manifest_file, "META-INF/manifest.xml");
358 fclose (odt->manifest_file);
364 odt_destroy (struct output_driver *driver)
366 struct odt_driver *odt = odt_driver_cast (driver);
368 if (odt->content_wtr != NULL)
370 xmlTextWriterEndElement (odt->content_wtr); /* office:text */
371 xmlTextWriterEndElement (odt->content_wtr); /* office:body */
372 xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
374 xmlTextWriterEndDocument (odt->content_wtr);
375 xmlFreeTextWriter (odt->content_wtr);
376 zip_writer_add (odt->zip, odt->content_file, "content.xml");
377 fclose (odt->content_file);
379 zip_writer_close (odt->zip);
382 free (odt->command_name);
387 odt_submit_table (struct odt_driver *odt, struct table_item *item)
389 const struct table *tab = table_item_get_table (item);
390 const char *caption = table_item_get_caption (item);
393 /* Write a heading for the table */
396 xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
397 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
399 xmlTextWriterWriteString (odt->content_wtr,
400 _xml (table_item_get_caption (item)) );
401 xmlTextWriterEndElement (odt->content_wtr);
405 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
406 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"),
407 "TABLE-%d", odt->table_num++);
410 /* Start column definitions */
411 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
412 xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
413 xmlTextWriterEndElement (odt->content_wtr);
416 /* Deal with row headers */
417 if ( table_ht (tab) > 0)
418 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
421 /* Write all the rows */
422 for (r = 0 ; r < table_nr (tab); ++r)
424 /* Start row definition */
425 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
427 /* Write all the columns */
428 for (c = 0 ; c < table_nc (tab) ; ++c)
430 struct table_cell cell;
432 table_get_cell (tab, c, r, &cell);
434 if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
436 int colspan = table_cell_colspan (&cell);
437 int rowspan = table_cell_rowspan (&cell);
439 xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
440 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
443 xmlTextWriterWriteFormatAttribute (
444 odt->content_wtr, _xml("table:number-columns-spanned"),
448 xmlTextWriterWriteFormatAttribute (
449 odt->content_wtr, _xml("table:number-rows-spanned"),
452 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
454 if ( r < table_ht (tab) || c < table_hl (tab) )
455 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
457 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
459 xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
461 xmlTextWriterEndElement (odt->content_wtr); /* text:p */
462 xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
466 xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
467 xmlTextWriterEndElement (odt->content_wtr);
470 table_cell_free (&cell);
473 xmlTextWriterEndElement (odt->content_wtr); /* row */
475 if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
476 xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
479 xmlTextWriterEndElement (odt->content_wtr); /* table */
483 odt_output_text (struct odt_driver *odt, const char *text)
485 xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
486 xmlTextWriterWriteString (odt->content_wtr, _xml(text));
487 xmlTextWriterEndElement (odt->content_wtr);
490 /* Submit a table to the ODT driver */
492 odt_submit (struct output_driver *driver,
493 const struct output_item *output_item)
495 struct odt_driver *odt = odt_driver_cast (driver);
497 output_driver_track_current_command (output_item, &odt->command_name);
499 if (is_table_item (output_item))
500 odt_submit_table (odt, to_table_item (output_item));
501 else if (is_text_item (output_item))
503 /* XXX apply different styles based on text_item's type. */
504 odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
506 else if (is_message_item (output_item))
508 const struct message_item *message_item = to_message_item (output_item);
509 const struct msg *msg = message_item_get_msg (message_item);
510 char *s = msg_to_string (msg, odt->command_name);
511 odt_output_text (odt, s);
516 struct output_driver_factory odt_driver_factory = { "odt", odt_create };
518 static const struct output_driver_class odt_driver_class =