1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2009 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/>. */
20 /* A driver for creating OpenDocument Format text files from PSPP's output */
22 #include <libpspp/assertion.h>
23 #include <libpspp/start-date.h>
24 #include <libpspp/version.h>
25 #include <output/afm.h>
26 #include <output/chart-provider.h>
27 #include <output/manager.h>
28 #include <output/output.h>
29 #include <output/table.h>
32 #include <sys/types.h>
36 #include <libxml/xmlwriter.h>
45 #define _xml(X) (const xmlChar *)(X)
49 /* The name of the temporary directory used to construct the ODF */
52 /* Writer for the content.xml file */
53 xmlTextWriterPtr content_wtr;
55 /* Writer fot the manifest.xml file */
56 xmlTextWriterPtr manifest_wtr;
61 /* Create the "mimetype" file needed by ODF */
63 create_mimetype (const char *dirname)
66 struct string filename;
67 ds_init_cstr (&filename, dirname);
68 ds_put_cstr (&filename, "/mimetype");
69 fp = fopen (ds_cstr (&filename), "w");
70 ds_destroy (&filename);
73 fprintf (fp, "application/vnd.oasis.opendocument.text");
77 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
78 static xmlTextWriterPtr
79 create_writer (const struct odt_driver_ext *driver, const char *filename)
84 ds_init_cstr (&str, driver->dirname);
85 ds_put_cstr (&str, "/");
86 ds_put_cstr (&str, filename);
88 /* dirname modifies its argument, so we must copy it */
89 copy = xstrdup (ds_cstr (&str));
90 mkdir (dirname (copy), 0700);
93 w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
97 xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
103 odt_open_driver (const char *name, int types, struct substring option_string)
105 struct odt_driver_ext *x;
106 struct outp_driver *this = outp_allocate_driver (&odt_class, name, types);
108 this->ext = x = xmalloc (sizeof *x);
110 outp_register_driver (this);
112 x->dirname = xstrdup ("odt-XXXXXX");
113 mkdtemp (x->dirname);
115 create_mimetype (x->dirname);
117 /* Create the manifest */
118 x->manifest_wtr = create_writer (x, "META-INF/manifest.xml");
120 xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:manifest"));
121 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("xmlns:manifest"),
122 _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
125 /* Add a manifest entry for the document as a whole */
126 xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:file-entry"));
127 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:media-type"), _xml("application/vnd.oasis.opendocument.text"));
128 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:full-path"), _xml("/"));
129 xmlTextWriterEndElement (x->manifest_wtr);
132 x->content_wtr = create_writer (x, "content.xml");
134 /* Add a manifest entry for content.xml */
135 xmlTextWriterStartElement (x->manifest_wtr, _xml("manifest:file-entry"));
136 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:media-type"), _xml("text/xml"));
137 xmlTextWriterWriteAttribute (x->manifest_wtr, _xml("manifest:full-path"), _xml("content.xml"));
138 xmlTextWriterEndElement (x->manifest_wtr);
141 /* Some necessary junk at the start */
142 xmlTextWriterStartElement (x->content_wtr, _xml("office:document-content"));
143 xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:office"),
144 _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
146 xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:text"),
147 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
149 xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:table"),
150 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
152 xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:version"), _xml("1.1"));
154 xmlTextWriterStartElement (x->content_wtr, _xml("office:body"));
155 xmlTextWriterStartElement (x->content_wtr, _xml("office:text"));
159 /* Close the manifest */
160 xmlTextWriterEndElement (x->manifest_wtr);
161 xmlTextWriterEndDocument (x->manifest_wtr);
162 xmlFreeTextWriter (x->manifest_wtr);
168 odt_close_driver (struct outp_driver *this)
170 struct string zip_cmd;
171 struct string rm_cmd;
172 struct odt_driver_ext *x = this->ext;
174 xmlTextWriterEndElement (x->content_wtr); /* office:text */
175 xmlTextWriterEndElement (x->content_wtr); /* office:body */
176 xmlTextWriterEndElement (x->content_wtr); /* office:document-content */
178 xmlTextWriterEndDocument (x->content_wtr);
179 xmlFreeTextWriter (x->content_wtr);
181 /* Zip up the directory */
182 ds_init_empty (&zip_cmd);
183 ds_put_format (&zip_cmd, "cd %s ; zip -r ../pspp.odt . > /dev/null", x->dirname);
184 system (ds_cstr (&zip_cmd));
185 ds_destroy (&zip_cmd);
187 /* Remove the temp dir */
188 ds_init_empty (&rm_cmd);
189 ds_put_format (&rm_cmd, "rm -r %s", x->dirname);
190 system (ds_cstr (&rm_cmd));
191 ds_destroy (&rm_cmd);
200 odt_open_page (struct outp_driver *this)
205 odt_close_page (struct outp_driver *this)
210 odt_output_chart (struct outp_driver *this, const struct chart *chart)
212 printf ("%s\n", __FUNCTION__);
216 /* Submit a table to the ODT driver */
218 odt_submit (struct outp_driver *this, struct som_entity *e)
222 struct odt_driver_ext *x = this->ext;
223 struct tab_table *tab = e->ext;
226 /* Write a heading for the table */
227 xmlTextWriterStartElement (x->content_wtr, _xml("text:h"));
228 xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("text:level"), "%d", e->subtable_num == 1 ? 2 : 3);
229 xmlTextWriterWriteString (x->content_wtr, _xml (tab->title) );
230 xmlTextWriterEndElement (x->content_wtr);
233 xmlTextWriterStartElement (x->content_wtr, _xml("table:table"));
234 xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:name"),
235 "TABLE-%d.%d", e->table_num, e->subtable_num);
238 /* Start column definitions */
239 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-column"));
240 xmlTextWriterWriteFormatAttribute (x->content_wtr, _xml("table:number-columns-repeated"), "%d", tab->nc);
241 xmlTextWriterEndElement (x->content_wtr);
244 /* Deal with row headers */
246 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-header-rows"));
249 /* Write all the rows */
250 for (r = 0 ; r < tab->nr; ++r)
252 /* Start row definition */
253 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-row"));
255 /* Write all the columns */
256 for (c = 0 ; c < tab->nc ; ++c)
258 int opts = tab->ct[tab->nc * r + c];
259 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-cell"));
260 xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:value-type"), _xml("string"));
262 if (! (opts & TAB_EMPTY) )
264 char *s = ss_xstrdup (tab->cc[tab->nc * r + c]);
265 xmlTextWriterStartElement (x->content_wtr, _xml("text:p"));
266 if ( r < tab->t || c < tab->l )
267 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
269 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
271 xmlTextWriterWriteString (x->content_wtr, _xml (s));
273 xmlTextWriterEndElement (x->content_wtr);
276 xmlTextWriterEndElement (x->content_wtr);
279 xmlTextWriterEndElement (x->content_wtr); /* row */
281 if ( tab->t > 0 && r == tab->t - 1)
282 xmlTextWriterEndElement (x->content_wtr); /* table-header-rows */
285 xmlTextWriterEndElement (x->content_wtr); /* table */
289 /* ODT driver class. */
290 const struct outp_class odt_class =