49a55b4ccef3cbf9d3827607f673397d2f3cb670
[pspp] / src / output / odt.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 /* A driver for creating OpenDocument Format text files from PSPP's output */
20
21 #include <errno.h>
22 #include <libgen.h>
23 #include <libxml/xmlwriter.h>
24 #ifdef HAVE_PWD_H
25 #include <pwd.h>
26 #endif
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <time.h>
30 #include <unistd.h>
31
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"
44
45 #include "gl/xalloc.h"
46 #include "gl/error.h"
47
48 #include "gettext.h"
49 #define _(msgid) gettext (msgid)
50
51 #define _xml(X) (const xmlChar *)(X)
52
53 struct odt_driver
54 {
55   struct output_driver driver;
56
57   struct zip_writer *zip;     /* ZIP file writer. */
58   char *file_name;            /* Output file name. */
59
60   /* content.xml */
61   xmlTextWriterPtr content_wtr; /* XML writer. */
62   FILE *content_file;           /* Temporary file. */
63
64   /* manifest.xml */
65   xmlTextWriterPtr manifest_wtr; /* XML writer. */
66   FILE *manifest_file;           /* Temporary file. */
67
68   /* Number of tables so far. */
69   int table_num;
70
71   /* Name of current command. */
72   char *command_name;
73 };
74
75 static const struct output_driver_class odt_driver_class;
76
77 static struct odt_driver *
78 odt_driver_cast (struct output_driver *driver)
79 {
80   assert (driver->class == &odt_driver_class);
81   return UP_CAST (driver, struct odt_driver, driver);
82 }
83
84 /* Create the "mimetype" file needed by ODF */
85 static bool
86 create_mimetype (struct zip_writer *zip)
87 {
88   FILE *fp;
89
90   fp = tmpfile ();
91   if (fp == NULL)
92     {
93       error (0, errno, _("error creating temporary file"));
94       return false;
95     }
96
97   fprintf (fp, "application/vnd.oasis.opendocument.text");
98   zip_writer_add (zip, fp, "mimetype");
99   fclose (fp);
100
101   return true;
102 }
103
104 /* Creates a new temporary file and stores it in *FILE, then creates an XML
105    writer for it and stores it in *W. */
106 static void
107 create_writer (FILE **file, xmlTextWriterPtr *w)
108 {
109   /* XXX this can fail */
110   *file = tmpfile ();
111   *w = xmlNewTextWriter (xmlOutputBufferCreateFile (*file, NULL));
112
113   xmlTextWriterStartDocument (*w, NULL, "UTF-8", NULL);
114 }
115
116
117 static void
118 register_file (struct odt_driver *odt, const char *filename)
119 {
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);
125 }
126
127 static void
128 write_style_data (struct odt_driver *odt)
129 {
130   xmlTextWriterPtr w;
131   FILE *file;
132
133   create_writer (&file, &w);
134   register_file (odt, "styles.xml");
135
136   xmlTextWriterStartElement (w, _xml ("office:document-styles"));
137   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
138                                _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
139
140   xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
141                                _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
142
143   xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
144                                _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
145
146   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml ("1.1"));
147                                
148
149
150   xmlTextWriterStartElement (w, _xml ("office:styles"));
151
152
153   {
154     xmlTextWriterStartElement (w, _xml ("style:style"));
155     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
156                                  _xml ("Standard"));
157
158     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
159                                  _xml ("paragraph"));
160
161     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
162                                  _xml ("text"));
163
164     xmlTextWriterEndElement (w); /* style:style */
165   }
166
167   {
168     xmlTextWriterStartElement (w, _xml ("style:style"));
169     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
170                                  _xml ("Table_20_Contents"));
171
172     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
173                                  _xml ("Table Contents"));
174
175     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
176                                  _xml ("paragraph"));
177
178     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
179                                  _xml ("Standard"));
180
181     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
182                                  _xml ("extra"));
183
184     xmlTextWriterEndElement (w); /* style:style */
185   }
186
187   {
188     xmlTextWriterStartElement (w, _xml ("style:style"));
189     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
190                                  _xml ("Table_20_Heading"));
191
192     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
193                                  _xml ("Table Heading"));
194
195     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
196                                  _xml ("paragraph"));
197
198     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
199                                  _xml ("Table_20_Contents"));
200
201     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
202                                  _xml ("extra"));
203
204
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 */
210
211     xmlTextWriterEndElement (w); /* style:style */
212   }
213
214
215   xmlTextWriterEndElement (w); /* office:styles */
216   xmlTextWriterEndElement (w); /* office:document-styles */
217
218   xmlTextWriterEndDocument (w);
219   xmlFreeTextWriter (w);
220   zip_writer_add (odt->zip, file, "styles.xml");
221   fclose (file);
222 }
223
224 static void
225 write_meta_data (struct odt_driver *odt)
226 {
227   xmlTextWriterPtr w;
228   FILE *file;
229
230   create_writer (&file, &w);
231   register_file (odt, "meta.xml");
232
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"));
239
240   xmlTextWriterStartElement (w, _xml ("office:meta"));
241   {
242     xmlTextWriterStartElement (w, _xml ("meta:generator"));
243     xmlTextWriterWriteString (w, _xml (stat_version));
244     xmlTextWriterEndElement (w);
245   }
246
247
248   {
249     char buf[30];
250     time_t t = time (NULL);
251     struct tm *tm =  localtime (&t);
252
253     strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
254
255     xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
256     xmlTextWriterWriteString (w, _xml (buf));
257     xmlTextWriterEndElement (w);
258
259     xmlTextWriterStartElement (w, _xml ("dc:date"));
260     xmlTextWriterWriteString (w, _xml (buf));
261     xmlTextWriterEndElement (w);
262   }
263
264 #ifdef HAVE_PWD_H
265   {
266     struct passwd *pw = getpwuid (getuid ());
267     if (pw != NULL)
268       {
269         xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
270         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
271         xmlTextWriterEndElement (w);
272
273         xmlTextWriterStartElement (w, _xml ("dc:creator"));
274         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
275         xmlTextWriterEndElement (w);
276       }
277   }
278 #endif
279
280   xmlTextWriterEndElement (w);
281   xmlTextWriterEndElement (w);
282   xmlTextWriterEndDocument (w);
283   xmlFreeTextWriter (w);
284   zip_writer_add (odt->zip, file, "meta.xml");
285   fclose (file);
286 }
287
288 static struct output_driver *
289 odt_create (const char *file_name, enum settings_output_devices device_type,
290             struct string_map *o UNUSED)
291 {
292   struct output_driver *d;
293   struct odt_driver *odt;
294   struct zip_writer *zip;
295
296   zip = zip_writer_create (file_name);
297   if (zip == NULL)
298     return NULL;
299
300   odt = xzalloc (sizeof *odt);
301   d = &odt->driver;
302   output_driver_init (d, &odt_driver_class, file_name, device_type);
303
304   odt->zip = zip;
305   odt->file_name = xstrdup (file_name);
306
307   if (!create_mimetype (zip))
308     {
309       output_driver_destroy (d);
310       return NULL;
311     }
312
313   /* Create the manifest */
314   create_writer (&odt->manifest_file, &odt->manifest_wtr);
315
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"));
319
320
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);
326
327
328   write_meta_data (odt);
329   write_style_data (odt);
330
331   create_writer (&odt->content_file, &odt->content_wtr);
332   register_file (odt, "content.xml");
333
334
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"));
339
340   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
341                                _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
342
343   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
344                                _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
345
346   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
347
348   xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
349   xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
350
351
352
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);
359
360   return d;
361 }
362
363 static void
364 odt_destroy (struct output_driver *driver)
365 {
366   struct odt_driver *odt = odt_driver_cast (driver);
367
368   if (odt->content_wtr != NULL)
369     {
370       xmlTextWriterEndElement (odt->content_wtr); /* office:text */
371       xmlTextWriterEndElement (odt->content_wtr); /* office:body */
372       xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
373
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);
378
379       zip_writer_close (odt->zip);
380     }
381   
382   free (odt->command_name);
383   free (odt);
384 }
385
386 static void
387 odt_submit_table (struct odt_driver *odt, struct table_item *item)
388 {
389   const struct table *tab = table_item_get_table (item);
390   const char *caption = table_item_get_caption (item);
391   int r, c;
392
393   /* Write a heading for the table */
394   if (caption != NULL)
395     {
396       xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
397       xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
398                                          "%d", 2);
399       xmlTextWriterWriteString (odt->content_wtr,
400                                 _xml (table_item_get_caption (item)) );
401       xmlTextWriterEndElement (odt->content_wtr);
402     }
403
404   /* Start table */
405   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
406   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"), 
407                                      "TABLE-%d", odt->table_num++);
408
409
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);
414
415
416   /* Deal with row headers */
417   if ( table_ht (tab) > 0)
418     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
419     
420
421   /* Write all the rows */
422   for (r = 0 ; r < table_nr (tab); ++r)
423     {
424       /* Start row definition */
425       xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
426
427       /* Write all the columns */
428       for (c = 0 ; c < table_nc (tab) ; ++c)
429         {
430           struct table_cell cell;
431
432           table_get_cell (tab, c, r, &cell);
433
434           if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
435             {
436               int colspan = table_cell_colspan (&cell);
437               int rowspan = table_cell_rowspan (&cell);
438
439               xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
440               xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
441
442               if (colspan > 1)
443                 xmlTextWriterWriteFormatAttribute (
444                   odt->content_wtr, _xml("table:number-columns-spanned"),
445                   "%d", colspan);
446
447               if (rowspan > 1)
448                 xmlTextWriterWriteFormatAttribute (
449                   odt->content_wtr, _xml("table:number-rows-spanned"),
450                   "%d", rowspan);
451
452               xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
453
454               if ( r < table_ht (tab) || c < table_hl (tab) )
455                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
456               else
457                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
458
459               xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
460
461               xmlTextWriterEndElement (odt->content_wtr); /* text:p */
462               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
463             }
464           else
465             {
466               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
467               xmlTextWriterEndElement (odt->content_wtr);
468             }
469
470           table_cell_free (&cell);
471         }
472   
473       xmlTextWriterEndElement (odt->content_wtr); /* row */
474
475       if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
476         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
477     }
478
479   xmlTextWriterEndElement (odt->content_wtr); /* table */
480 }
481
482 static void
483 odt_output_text (struct odt_driver *odt, const char *text)
484 {
485   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
486   xmlTextWriterWriteString (odt->content_wtr, _xml(text));
487   xmlTextWriterEndElement (odt->content_wtr);
488 }
489
490 /* Submit a table to the ODT driver */
491 static void
492 odt_submit (struct output_driver *driver,
493             const struct output_item *output_item)
494 {
495   struct odt_driver *odt = odt_driver_cast (driver);
496
497   output_driver_track_current_command (output_item, &odt->command_name);
498
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))
502     {
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)));
505     }
506   else if (is_message_item (output_item))
507     {
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);
512       free (s);
513     }
514 }
515
516 struct output_driver_factory odt_driver_factory = { "odt", odt_create };
517
518 static const struct output_driver_class odt_driver_class =
519 {
520   "odf",
521   odt_destroy,
522   odt_submit,
523   NULL,
524 };