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