1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011 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/>. */
19 #include "libpspp/message.h"
20 #include "libpspp/misc.h"
22 #include "data/data-in.h"
24 #include "gl/minmax.h"
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) (msgid)
30 #include "ods-reader.h"
31 #include "spreadsheet-reader.h"
36 ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
38 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
45 #include "libpspp/zip-reader.h"
51 #include <libxml/xmlreader.h>
54 #include "data/format.h"
55 #include "data/case.h"
56 #include "data/casereader-provider.h"
57 #include "data/dictionary.h"
58 #include "data/identifier.h"
59 #include "data/value.h"
60 #include "data/variable.h"
61 #include "libpspp/i18n.h"
62 #include "libpspp/str.h"
64 #include "gl/xalloc.h"
66 static void ods_file_casereader_destroy (struct casereader *, void *);
68 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
70 static const struct casereader_class ods_file_casereader_class =
72 ods_file_casereader_read,
73 ods_file_casereader_destroy,
80 STATE_INIT = 0, /* Initial state */
81 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
82 STATE_TABLE, /* Found the sheet that we actually want */
83 STATE_ROW, /* Found the start of the cell array */
84 STATE_CELL, /* Found a cell */
85 STATE_CELL_CONTENT /* Found a the text within a cell */
92 enum reader_state state;
99 const xmlChar *target_sheet;
100 int target_sheet_index;
107 struct caseproto *proto;
108 struct dictionary *dict;
109 struct ccase *first_case;
110 bool used_first_case;
113 struct string ods_errs;
117 static void process_node (struct ods_reader *r);
120 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
122 struct ods_reader *r = r_;
127 xmlFreeTextReader (r->xtr);
129 if ( ! ds_is_empty (&r->ods_errs))
130 msg (ME, "%s", ds_cstr (&r->ods_errs));
132 ds_destroy (&r->ods_errs);
134 if ( ! r->used_first_case )
135 case_unref (r->first_case);
137 caseproto_unref (r->proto);
143 process_node (struct ods_reader *r)
145 xmlChar *name = xmlTextReaderName (r->xtr);
147 name = xmlStrdup (_xml ("--"));
149 r->node_type = xmlTextReaderNodeType (r->xtr);
154 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
155 XML_READER_TYPE_ELEMENT == r->node_type)
157 r->state = STATE_SPREADSHEET;
160 case STATE_SPREADSHEET:
161 if (0 == xmlStrcasecmp (name, _xml("table:table")))
163 if (XML_READER_TYPE_ELEMENT == r->node_type)
168 if ( r->target_sheet != NULL)
170 xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
171 if ( 0 == xmlStrcmp (value, r->target_sheet))
173 r->sheet_found = true;
174 r->state = STATE_TABLE;
178 else if (r->target_sheet_index == r->sheet_index)
180 r->sheet_found = true;
181 r->state = STATE_TABLE;
183 else if ( r->target_sheet_index == -1)
184 r->state = STATE_TABLE;
187 else if (XML_READER_TYPE_END_ELEMENT == r->node_type
190 r->state = STATE_INIT;
194 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) )
196 if ( XML_READER_TYPE_ELEMENT == r->node_type)
198 if (! xmlTextReaderIsEmptyElement (r->xtr))
200 r->state = STATE_ROW;
206 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
208 r->state = STATE_SPREADSHEET;
212 if (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
214 if ( XML_READER_TYPE_ELEMENT == r->node_type)
217 xmlTextReaderGetAttribute (r->xtr,
218 _xml ("table:number-columns-repeated"));
220 r->span = value ? _xmlchar_to_int (value) : 1;
222 if (! xmlTextReaderIsEmptyElement (r->xtr))
224 r->state = STATE_CELL;
228 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
230 r->state = STATE_TABLE;
232 /* Set the span back to the default */
237 if (0 == xmlStrcasecmp (name, _xml("text:p")))
239 if ( XML_READER_TYPE_ELEMENT == r->node_type)
241 r->state = STATE_CELL_CONTENT;
244 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
246 r->state = STATE_ROW;
249 case STATE_CELL_CONTENT:
250 if (XML_READER_TYPE_TEXT != r->node_type)
251 r->state = STATE_CELL;
261 A struct containing the parameters of a cell's value
274 struct xml_value firstval;
278 /* Determine the width that a xmv should probably have */
280 xmv_to_width (const struct xml_value *xmv, int fallback)
282 int width = SPREADSHEET_DEFAULT_WIDTH;
284 /* Non-strings always have zero width */
285 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
292 width = ROUND_UP (xmlStrlen (xmv->value),
293 SPREADSHEET_DEFAULT_WIDTH);
295 width = ROUND_UP (xmlStrlen (xmv->text),
296 SPREADSHEET_DEFAULT_WIDTH);
302 Sets the VAR of case C, to the value corresponding to the xml data
305 convert_xml_to_value (struct ccase *c, const struct variable *var,
306 const struct xml_value *xmv)
308 union value *v = case_data_rw (c, var);
310 if (xmv->value == NULL && xmv->text == NULL)
311 value_set_missing (v, var_get_width (var));
312 else if ( var_is_alpha (var))
313 /* Use the text field, because it seems that there is no
314 value field for strings */
315 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
319 const struct fmt_spec *fmt = var_get_write_format (var);
320 enum fmt_category fc = fmt_get_category (fmt->type);
322 assert ( fc != FMT_CAT_STRING);
325 xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
327 data_in (ss_cstr (text), "UTF-8",
337 ods_open_reader (struct spreadsheet_read_info *gri, struct dictionary **dict)
340 xmlChar *type = NULL;
341 unsigned long int vstart = 0;
342 casenumber n_cases = CASENUMBER_MAX;
344 struct var_spec *var_spec = NULL;
347 struct ods_reader *r = xzalloc (sizeof *r);
348 struct zip_member *content = NULL;
349 struct zip_reader *zreader ;
350 xmlChar *val_string = NULL;
352 r->read_names = gri->read_names;
353 ds_init_empty (&r->ods_errs);
355 zreader = zip_reader_create (gri->file_name, &r->ods_errs);
357 if ( NULL == zreader)
359 msg (ME, _("Error opening `%s' for reading as a OpenDocument spreadsheet file: %s."),
360 gri->file_name, ds_cstr (&r->ods_errs));
365 content = zip_member_open (zreader, "content.xml");
366 if ( NULL == content)
368 msg (ME, _("Could not extract OpenDocument spreadsheet from file `%s': %s."),
369 gri->file_name, ds_cstr (&r->ods_errs));
374 zip_member_ref (content);
376 r->xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
377 (xmlInputCloseCallback) zip_member_finish,
378 content, NULL, NULL, XML_PARSE_RECOVER);
385 if ( gri->cell_range )
387 if ( ! convert_cell_ref (gri->cell_range,
388 &r->start_col, &r->start_row,
389 &r->stop_col, &r->stop_row))
391 msg (SE, _("Invalid cell range `%s'"),
404 r->state = STATE_INIT;
405 r->target_sheet = BAD_CAST gri->sheet_name;
406 r->target_sheet_index = gri->sheet_index;
407 r->row = r->col = -1;
411 /* If CELLRANGE was given, then we know how many variables should be read */
412 if ( r->stop_col != -1 )
414 n_var_specs = r->stop_col - r->start_col + 1;
415 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
419 /* Advance to the start of the cells for the target sheet */
420 while ( (r->row < r->start_row ))
422 if (1 != (ret = xmlTextReaderRead (r->xtr)))
430 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
435 if ( gri->read_names)
437 while (1 == (ret = xmlTextReaderRead (r->xtr)))
441 if ( r->row > r->start_row)
444 if (r->col == -1 && r->row == r->start_row)
447 if ( r->col < r->start_col)
450 idx = r->col - r->start_col;
452 if (r->state == STATE_CELL_CONTENT
454 XML_READER_TYPE_TEXT == r->node_type)
456 xmlChar *value = xmlTextReaderValue (r->xtr);
457 if ( idx >= n_var_specs)
460 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
462 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
463 memset (var_spec + n_var_specs * sizeof (*var_spec),
465 (n_var_specs - idx + 1) * sizeof (*var_spec));
466 n_var_specs = idx + 1;
468 var_spec[idx].firstval.text = 0;
469 var_spec[idx].firstval.value = 0;
470 var_spec[idx].firstval.type = 0;
472 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
479 /* Read in the first row of data */
480 while (1 == xmlTextReaderRead (r->xtr))
484 if ( r->row >= r->start_row + 1 + gri->read_names)
487 if ( r->col < r->start_col)
490 if ( r->col - r->start_col + 1 > n_var_specs)
493 idx = r->col - r->start_col;
495 if ( r->state == STATE_CELL &&
496 XML_READER_TYPE_ELEMENT == r->node_type)
498 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
499 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
502 if ( r->state == STATE_CELL_CONTENT &&
503 XML_READER_TYPE_TEXT == r->node_type)
505 var_spec [idx].firstval.type = type;
506 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
507 var_spec [idx].firstval.value = val_string;
513 /* Create the dictionary and populate it */
514 *dict = r->dict = dict_create (
515 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
517 for (i = 0 ; i < n_var_specs ; ++i )
520 struct variable *var = NULL;
521 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
522 int width = xmv_to_width (&var_spec[i].firstval, gri->asw);
523 dict_create_var (r->dict, name, width);
526 var = dict_get_var (r->dict, i);
528 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
535 fmt = fmt_default_for_width (width);
537 var_set_both_formats (var, &fmt);
540 /* Create the first case, and cache it */
541 r->used_first_case = false;
543 if ( n_var_specs == 0 )
545 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
550 r->proto = caseproto_ref (dict_get_proto (r->dict));
551 r->first_case = case_create (r->proto);
552 case_set_missing (r->first_case);
554 for ( i = 0 ; i < n_var_specs ; ++i )
556 const struct variable *var = dict_get_var (r->dict, i);
558 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
561 zip_reader_destroy (zreader);
563 for ( i = 0 ; i < n_var_specs ; ++i )
565 free (var_spec[i].firstval.type);
566 free (var_spec[i].firstval.value);
567 free (var_spec[i].firstval.text);
568 free (var_spec[i].name);
573 return casereader_create_sequential
577 &ods_file_casereader_class, r);
581 zip_reader_destroy (zreader);
583 for ( i = 0 ; i < n_var_specs ; ++i )
585 free (var_spec[i].firstval.type);
586 free (var_spec[i].firstval.value);
587 free (var_spec[i].firstval.text);
588 free (var_spec[i].name);
597 /* Reads and returns one case from READER's file. Returns a null
598 pointer on failure. */
599 static struct ccase *
600 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
602 struct ccase *c = NULL;
603 xmlChar *val_string = NULL;
604 struct ods_reader *r = r_;
605 int current_row = r->row;
610 if ( !r->used_first_case )
612 r->used_first_case = true;
613 return r->first_case;
617 if ( r->state > STATE_INIT)
619 c = case_create (r->proto);
620 case_set_missing (c);
623 while (1 == xmlTextReaderRead (r->xtr))
626 if ( r->row > current_row)
630 if ( r->col < r->start_col || (r->stop_col != -1 && r->col > r->stop_col))
634 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
638 if ( r->stop_row != -1 && r->row > r->stop_row)
642 if ( r->state == STATE_CELL &&
643 r->node_type == XML_READER_TYPE_ELEMENT )
645 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
648 if ( r->state == STATE_CELL_CONTENT && r->node_type == XML_READER_TYPE_TEXT )
651 struct xml_value *xmv = xzalloc (sizeof *xmv);
652 xmv->text = xmlTextReaderValue (r->xtr);
653 xmv->value = val_string;
656 for (col = 0; col < r->span ; ++col)
658 const int idx = r->col + col - r->start_col;
660 const struct variable *var = dict_get_var (r->dict, idx);
662 convert_xml_to_value (c, var, xmv);
669 if ( r->state < STATE_TABLE)
673 if (NULL == c || (r->stop_row != -1 && r->row > r->stop_row + 1))