6511aa3f10022bf23e675b32564c9fe1a4fec32b
[pspp] / src / output / odt.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009 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
20 #include <libpspp/assertion.h>
21 #include <libpspp/start-date.h>
22 #include <libpspp/version.h>
23 #include <output/afm.h>
24 #include <output/chart-provider.h>
25 #include <output/manager.h>
26 #include <output/output.h>
27 #include <output/table.h>
28
29 #include <sys/stat.h>
30 #include <sys/types.h>
31
32 #include <libgen.h>
33
34 #include <libxml/xmlwriter.h>
35
36 #include <stdlib.h>
37
38 #include "error.h"
39 #include "intprops.h"
40 #include "minmax.h"
41 #include "xalloc.h"
42
43 #define _xml(X) (const xmlChar *)(X)
44
45 struct odt_driver_ext 
46 {
47   /* The name of the temporary directory used to construct the ODF */
48   char *dirname;
49
50   /* Writer for the content.xml file */
51   xmlTextWriterPtr content_wtr;
52
53   /* Writer fot the manifest.xml file */
54   xmlTextWriterPtr manifest_wtr;
55 };
56
57
58
59 /* Create the "mimetype" file needed by ODF */
60 static void
61 create_mimetype (const char *dirname)
62 {
63   FILE *fp;
64   struct string filename;
65   ds_init_cstr (&filename, dirname);
66   ds_put_cstr (&filename, "/mimetype");
67   fp = fopen (ds_cstr (&filename), "w");
68   ds_destroy (&filename);
69
70   fprintf (fp, "application/vnd.oasis.opendocument.text");
71   fclose (fp);
72 }
73
74 static xmlTextWriterPtr
75 create_writer (const struct odt_driver_ext *driver, const char *filename)
76 {
77   char *copy = NULL;
78   xmlTextWriterPtr w;
79   struct string str;
80   ds_init_cstr (&str, driver->dirname);
81   ds_put_cstr (&str, "/");
82   ds_put_cstr (&str, filename);
83
84   /* dirname modifies its argument, so we must copy it */
85   copy = xstrdup (ds_cstr (&str));
86   mkdir (dirname (copy), 0700);
87   free (copy);
88
89   w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
90
91   ds_destroy (&str);
92
93   xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
94
95   return w;
96 }
97
98 static bool
99 odt_open_driver (const char *name, int types, struct substring option_string)
100 {
101   struct odt_driver_ext *x;
102   struct outp_driver *this = outp_allocate_driver (&odt_class, name, types);
103
104   this->ext = x = xmalloc (sizeof *x);
105
106   outp_register_driver (this);
107
108   x->dirname = xstrdup ("odt-XXXXXX");
109   mkdtemp (x->dirname);
110
111   printf ("Dir name is \"%s\"\n", x->dirname);
112
113   create_mimetype (x->dirname);
114
115   x->manifest_wtr = create_writer (x, "META-INF/manifest.xml");
116
117   xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:manifest"));
118   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("xmlns:manifest"),
119                                _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
120
121
122   xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:file-entry"));
123   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:media-type"),  _xml("application/vnd.oasis.opendocument.text"));
124   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:full-path"),  _xml("/"));
125   xmlTextWriterEndElement (x->manifest_wtr);
126
127
128   x->content_wtr = create_writer (x, "content.xml");
129
130   xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:file-entry"));
131   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:media-type"),  _xml("text/xml"));
132   xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:full-path"),  _xml("content.xml"));
133   xmlTextWriterEndElement (x->manifest_wtr);
134
135
136   xmlTextWriterStartElement (x->content_wtr, _xml("office:document-content"));
137   xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:office"),
138                                _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
139
140   xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:text"),
141                                _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
142
143   xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:table"),
144                                _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
145
146   xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:version"), _xml("1.1"));
147
148   xmlTextWriterStartElement (x->content_wtr, _xml("office:body"));
149   xmlTextWriterStartElement (x->content_wtr, _xml("office:text"));
150
151
152   xmlTextWriterEndElement (x->manifest_wtr);
153   xmlTextWriterEndDocument (x->manifest_wtr);
154   xmlFreeTextWriter (x->manifest_wtr);
155
156   return true;
157 }
158
159 static bool
160 odt_close_driver (struct outp_driver *this)
161 {
162  struct odt_driver_ext *x = this->ext;
163
164  xmlTextWriterEndElement (x->content_wtr); /* office:text */
165  xmlTextWriterEndElement (x->content_wtr); /* office:body */
166  xmlTextWriterEndElement (x->content_wtr); /* office:document-content */
167
168  xmlTextWriterEndDocument (x->content_wtr);
169  xmlFreeTextWriter (x->content_wtr);
170
171  return true;
172 }
173
174 static void
175 odt_open_page (struct outp_driver *this)
176 {
177  printf ("%s\n", __FUNCTION__);
178 }
179
180 static void
181 odt_close_page (struct outp_driver *this)
182 {
183  printf ("%s\n", __FUNCTION__);
184 }
185
186 static void
187 odt_output_chart (struct outp_driver *this, const struct chart *chart)
188 {
189  printf ("%s\n", __FUNCTION__);
190 }
191
192 static void
193 odt_submit (struct outp_driver *this, struct som_entity *e)
194 {
195   int r, c;
196   
197   struct odt_driver_ext *x = this->ext;
198   struct tab_table *tab = e->ext;
199
200   xmlTextWriterStartElement (x->content_wtr, _xml("text:h"));
201   xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("text:level"), "%d", e->subtable_num == 1 ? 2 : 3);
202   xmlTextWriterWriteString (x->content_wtr, _xml (tab->title) );
203   xmlTextWriterEndElement (x->content_wtr);
204
205   xmlTextWriterStartElement (x->content_wtr, _xml("table:table"));
206   xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:name"), 
207                                      "TABLE-%d.%d", e->table_num, e->subtable_num);
208
209
210   /* Start column definitions */
211   xmlTextWriterStartElement (x->content_wtr, _xml("table:table-column"));
212   xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:number-columns-repeated"), "%d", tab->nc);
213   xmlTextWriterEndElement (x->content_wtr);
214
215
216   /* Deal with row headers */
217   if ( tab->t > 0)
218     xmlTextWriterStartElement (x->content_wtr, _xml("table:table-header-rows"));
219     
220   for (r = 0 ; r < tab->nr; ++r)
221     {
222       /* Start row definition */
223       xmlTextWriterStartElement (x->content_wtr, _xml("table:table-row"));
224
225       for (c = 0 ; c < tab->nc ; ++c)
226         {
227           int opts = tab->ct[tab->nc * r + c];
228           xmlTextWriterStartElement (x->content_wtr, _xml("table:table-cell"));
229           xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:value-type"), _xml("string"));
230
231           if (! (opts & TAB_EMPTY) ) 
232             {
233               char *s = ss_xstrdup (tab->cc[tab->nc * r + c]);
234               xmlTextWriterStartElement (x->content_wtr, _xml("text:p"));
235               if ( r < tab->t || c < tab->l )
236                 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
237               else
238                 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
239
240               xmlTextWriterWriteString (x->content_wtr, _xml (s));
241           
242               xmlTextWriterEndElement (x->content_wtr);
243               free (s);
244             }
245           xmlTextWriterEndElement (x->content_wtr);
246         }
247   
248       xmlTextWriterEndElement (x->content_wtr); /* row */
249
250       if ( tab->t > 0 && r == tab->t - 1)
251         xmlTextWriterEndElement (x->content_wtr);
252     }
253
254   xmlTextWriterEndElement (x->content_wtr); /* table */
255 }
256
257
258 /* ODT driver class. */
259 const struct outp_class odt_class =
260 {
261   "odt",
262   1,
263
264   odt_open_driver,
265   odt_close_driver,
266
267   odt_open_page,
268   odt_close_page,
269   NULL,
270
271   odt_output_chart,
272   odt_submit,
273
274   NULL,
275   NULL,
276   NULL,
277 };