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 #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>
30 #include <sys/types.h>
34 #include <libxml/xmlwriter.h>
43 #define _xml(X) (const xmlChar *)(X)
47 /* The name of the temporary directory used to construct the ODF */
50 /* Writer for the content.xml file */
51 xmlTextWriterPtr content_wtr;
53 /* Writer fot the manifest.xml file */
54 xmlTextWriterPtr manifest_wtr;
59 /* Create the "mimetype" file needed by ODF */
61 create_mimetype (const char *dirname)
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);
70 fprintf (fp, "application/vnd.oasis.opendocument.text");
74 static xmlTextWriterPtr
75 create_writer (const struct odt_driver_ext *driver, const char *filename)
80 ds_init_cstr (&str, driver->dirname);
81 ds_put_cstr (&str, "/");
82 ds_put_cstr (&str, filename);
84 /* dirname modifies its argument, so we must copy it */
85 copy = xstrdup (ds_cstr (&str));
86 mkdir (dirname (copy), 0700);
89 w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
93 xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
99 odt_open_driver (const char *name, int types, struct substring option_string)
101 struct odt_driver_ext *x;
102 struct outp_driver *this = outp_allocate_driver (&odt_class, name, types);
104 this->ext = x = xmalloc (sizeof *x);
106 outp_register_driver (this);
108 x->dirname = xstrdup ("odt-XXXXXX");
109 mkdtemp (x->dirname);
111 printf ("Dir name is \"%s\"\n", x->dirname);
113 create_mimetype (x->dirname);
115 x->manifest_wtr = create_writer (x, "META-INF/manifest.xml");
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"));
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);
128 x->content_wtr = create_writer (x, "content.xml");
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);
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"));
140 xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:text"),
141 _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
143 xmlTextWriterWriteAttribute (x->content_wtr, _xml("xmlns:table"),
144 _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
146 xmlTextWriterWriteAttribute (x->content_wtr, _xml("office:version"), _xml("1.1"));
148 xmlTextWriterStartElement (x->content_wtr, _xml("office:body"));
149 xmlTextWriterStartElement (x->content_wtr, _xml("office:text"));
152 xmlTextWriterEndElement (x->manifest_wtr);
153 xmlTextWriterEndDocument (x->manifest_wtr);
154 xmlFreeTextWriter (x->manifest_wtr);
160 odt_close_driver (struct outp_driver *this)
162 struct odt_driver_ext *x = this->ext;
164 xmlTextWriterEndElement (x->content_wtr); /* office:text */
165 xmlTextWriterEndElement (x->content_wtr); /* office:body */
166 xmlTextWriterEndElement (x->content_wtr); /* office:document-content */
168 xmlTextWriterEndDocument (x->content_wtr);
169 xmlFreeTextWriter (x->content_wtr);
175 odt_open_page (struct outp_driver *this)
177 printf ("%s\n", __FUNCTION__);
181 odt_close_page (struct outp_driver *this)
183 printf ("%s\n", __FUNCTION__);
187 odt_output_chart (struct outp_driver *this, const struct chart *chart)
189 printf ("%s\n", __FUNCTION__);
193 odt_submit (struct outp_driver *this, struct som_entity *e)
197 struct odt_driver_ext *x = this->ext;
198 struct tab_table *tab = e->ext;
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);
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);
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);
216 /* Deal with row headers */
218 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-header-rows"));
220 for (r = 0 ; r < tab->nr; ++r)
222 /* Start row definition */
223 xmlTextWriterStartElement (x->content_wtr, _xml("table:table-row"));
225 for (c = 0 ; c < tab->nc ; ++c)
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"));
231 if (! (opts & TAB_EMPTY) )
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"));
238 xmlTextWriterWriteAttribute (x->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
240 xmlTextWriterWriteString (x->content_wtr, _xml (s));
242 xmlTextWriterEndElement (x->content_wtr);
245 xmlTextWriterEndElement (x->content_wtr);
248 xmlTextWriterEndElement (x->content_wtr); /* row */
250 if ( tab->t > 0 && r == tab->t - 1)
251 xmlTextWriterEndElement (x->content_wtr);
254 xmlTextWriterEndElement (x->content_wtr); /* table */
258 /* ODT driver class. */
259 const struct outp_class odt_class =