0d4625c8d4d39ba61046c838aa632fa6d748e811
[pspp] / src / output / odt.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009-2014 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 write_xml_with_line_breaks (xmlTextWriterPtr writer, char *line)
390 {
391   char *newline;
392   char *p;
393
394   for (p = line; *p; p = newline + 1)
395     {
396       newline = strchr (p, '\n');
397
398       if (!newline)
399         {
400           xmlTextWriterWriteString (writer, _xml(p));
401           return;
402         }
403
404       if (newline > p && newline[-1] == '\r')
405         newline[-1] = '\0';
406       else
407         *newline = '\0';
408       xmlTextWriterWriteString (writer, _xml(p));
409       xmlTextWriterWriteElement (writer, _xml("text:line-break"), _xml(""));
410     }
411 }
412
413 static void
414 write_table (struct odt_driver *odt, const struct table_item *item)
415 {
416   const struct table *tab = table_item_get_table (item);
417   const char *caption = table_item_get_caption (item);
418   int r, c;
419
420   /* Write a heading for the table */
421   if (caption != NULL)
422     {
423       xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
424       xmlTextWriterWriteFormatAttribute (odt->content_wtr,
425                                          _xml("text:outline-level"), "%d", 2);
426       xmlTextWriterWriteString (odt->content_wtr,
427                                 _xml (table_item_get_caption (item)) );
428       xmlTextWriterEndElement (odt->content_wtr);
429     }
430
431   /* Start table */
432   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
433   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"), 
434                                      "TABLE-%d", odt->table_num++);
435
436
437   /* Start column definitions */
438   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
439   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
440   xmlTextWriterEndElement (odt->content_wtr);
441
442
443   /* Deal with row headers */
444   if ( table_ht (tab) > 0)
445     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
446     
447
448   /* Write all the rows */
449   for (r = 0 ; r < table_nr (tab); ++r)
450     {
451       /* Start row definition */
452       xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
453
454       /* Write all the columns */
455       for (c = 0 ; c < table_nc (tab) ; ++c)
456         {
457           struct table_cell cell;
458           size_t i;
459
460           table_get_cell (tab, c, r, &cell);
461
462           if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
463             {
464               int colspan = table_cell_colspan (&cell);
465               int rowspan = table_cell_rowspan (&cell);
466
467               xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
468               xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
469
470               if (colspan > 1)
471                 xmlTextWriterWriteFormatAttribute (
472                   odt->content_wtr, _xml("table:number-columns-spanned"),
473                   "%d", colspan);
474
475               if (rowspan > 1)
476                 xmlTextWriterWriteFormatAttribute (
477                   odt->content_wtr, _xml("table:number-rows-spanned"),
478                   "%d", rowspan);
479
480               for (i = 0; i < cell.n_contents; i++)
481                 {
482                   const struct cell_contents *contents = &cell.contents[i];
483
484                   if (contents->text)
485                     {
486                       xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
487
488                       if ( r < table_ht (tab) || c < table_hl (tab) )
489                         xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
490                       else
491                         xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
492
493                       if (strchr (contents->text, '\n'))
494                         {
495                           char *line = xstrdup (contents->text);
496                           write_xml_with_line_breaks (odt->content_wtr, line);
497                           free (line);
498                         }
499                       else
500                         xmlTextWriterWriteString (odt->content_wtr, _xml(contents->text));
501
502                       xmlTextWriterEndElement (odt->content_wtr); /* text:p */
503                     }
504                   else if (contents->table)
505                     {
506                       write_table (odt, contents->table);
507                       continue;
508                     }
509                 }
510               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
511             }
512           else
513             {
514               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
515               xmlTextWriterEndElement (odt->content_wtr);
516             }
517
518           table_cell_free (&cell);
519         }
520   
521       xmlTextWriterEndElement (odt->content_wtr); /* row */
522
523       if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
524         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
525     }
526
527   xmlTextWriterEndElement (odt->content_wtr); /* table */
528 }
529
530 static void
531 odt_output_text (struct odt_driver *odt, const char *text)
532 {
533   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
534   xmlTextWriterWriteString (odt->content_wtr, _xml(text));
535   xmlTextWriterEndElement (odt->content_wtr);
536 }
537
538 /* Submit a table to the ODT driver */
539 static void
540 odt_submit (struct output_driver *driver,
541             const struct output_item *output_item)
542 {
543   struct odt_driver *odt = odt_driver_cast (driver);
544
545   output_driver_track_current_command (output_item, &odt->command_name);
546
547   if (is_table_item (output_item))
548     write_table (odt, to_table_item (output_item));
549   else if (is_text_item (output_item))
550     {
551       struct text_item *text_item = to_text_item (output_item);
552
553       if (text_item_get_type (text_item) != TEXT_ITEM_COMMAND_CLOSE)
554         odt_output_text (odt, text_item_get_text (text_item));
555     }
556   else if (is_message_item (output_item))
557     {
558       const struct message_item *message_item = to_message_item (output_item);
559       const struct msg *msg = message_item_get_msg (message_item);
560       char *s = msg_to_string (msg, odt->command_name);
561       odt_output_text (odt, s);
562       free (s);
563     }
564 }
565
566 struct output_driver_factory odt_driver_factory =
567   { "odt", "pspp.odf", odt_create };
568
569 static const struct output_driver_class odt_driver_class =
570 {
571   "odf",
572   odt_destroy,
573   odt_submit,
574   NULL,
575 };