1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2012, 2013 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"
21 #include "libpspp/assertion.h"
23 #include "data/data-in.h"
25 #include "gl/minmax.h"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) (msgid)
31 #include "ods-reader.h"
32 #include "spreadsheet-reader.h"
37 ods_open_reader (const struct spreadsheet_read_options *opts,
38 struct dictionary **dict)
40 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
47 #include "libpspp/zip-reader.h"
53 #include <libxml/xmlreader.h>
56 #include "data/format.h"
57 #include "data/case.h"
58 #include "data/casereader-provider.h"
59 #include "data/dictionary.h"
60 #include "data/identifier.h"
61 #include "data/value.h"
62 #include "data/variable.h"
63 #include "libpspp/i18n.h"
64 #include "libpspp/str.h"
66 #include "gl/xalloc.h"
68 static void ods_file_casereader_destroy (struct casereader *, void *);
70 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
72 static const struct casereader_class ods_file_casereader_class =
74 ods_file_casereader_read,
75 ods_file_casereader_destroy,
82 /* The name of the sheet (utf8 encoding) */
94 STATE_INIT = 0, /* Initial state */
95 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
96 STATE_TABLE, /* Found the sheet that we actually want */
97 STATE_ROW, /* Found the start of the cell array */
98 STATE_CELL, /* Found a cell */
99 STATE_CELL_CONTENT /* Found a the text within a cell */
104 struct spreadsheet spreadsheet;
105 struct zip_reader *zreader;
106 xmlTextReaderPtr xtr;
108 enum reader_state state;
115 const xmlChar *target_sheet_name;
116 int target_sheet_index;
119 int wanted_row_start;
120 int wanted_col_start;
131 struct sheet_detail *sheets;
132 int n_allocated_sheets;
134 struct caseproto *proto;
135 struct dictionary *dict;
136 struct ccase *first_case;
137 bool used_first_case;
140 struct string ods_errs;
144 static void process_node (struct ods_reader *r);
148 ods_get_sheet_name (struct spreadsheet *s, int n)
151 struct ods_reader *or = (struct ods_reader *) s;
153 assert (n < s->n_sheets);
156 (or->n_allocated_sheets <= n)
158 (1 == (ret = xmlTextReaderRead (or->xtr)))
164 return or->sheets[n].name;
168 ods_get_sheet_range (struct spreadsheet *s, int n)
171 struct ods_reader *or = (struct ods_reader *) s;
173 assert (n < s->n_sheets);
177 (or->n_allocated_sheets <= n)
178 || (or->sheets[n].stop_row == -1) )
180 (1 == (ret = xmlTextReaderRead (or->xtr)))
187 return create_cell_ref (
188 or->sheets[n].start_col,
189 or->sheets[n].start_row,
190 or->sheets[n].stop_col,
191 or->sheets[n].stop_row);
196 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
198 struct ods_reader *r = r_;
203 xmlFreeTextReader (r->xtr);
206 if ( ! ds_is_empty (&r->ods_errs))
207 msg (ME, "%s", ds_cstr (&r->ods_errs));
209 ds_destroy (&r->ods_errs);
211 if ( ! r->used_first_case )
212 case_unref (r->first_case);
214 caseproto_unref (r->proto);
220 process_node (struct ods_reader *r)
222 xmlChar *name = xmlTextReaderName (r->xtr);
224 name = xmlStrdup (_xml ("--"));
227 r->node_type = xmlTextReaderNodeType (r->xtr);
232 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
233 XML_READER_TYPE_ELEMENT == r->node_type)
235 r->state = STATE_SPREADSHEET;
236 r->current_sheet = -1;
239 case STATE_SPREADSHEET:
240 if (0 == xmlStrcasecmp (name, _xml("table:table"))
242 (XML_READER_TYPE_ELEMENT == r->node_type))
244 xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
248 if (r->current_sheet >= r->n_allocated_sheets)
250 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
251 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
252 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
253 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
254 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
255 r->sheets[r->n_allocated_sheets - 1].name = value;
261 if ( r->target_sheet_name != NULL)
263 if ( 0 == xmlStrcmp (value, r->target_sheet_name))
265 r->sheet_found = true;
268 else if (r->target_sheet_index == r->current_sheet)
270 r->sheet_found = true;
272 r->state = STATE_TABLE;
274 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
275 XML_READER_TYPE_ELEMENT == r->node_type)
277 r->state = STATE_INIT;
278 printf ("%s:%d End of Workbook %d: Rows %d Cols %d\n", __FILE__, __LINE__,
279 r->current_sheet, r->row, r->col);
283 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
284 (XML_READER_TYPE_ELEMENT == r->node_type))
287 xmlTextReaderGetAttribute (r->xtr,
288 _xml ("table:number-rows-repeated"));
290 int row_span = value ? _xmlchar_to_int (value) : 1;
295 if (! xmlTextReaderIsEmptyElement (r->xtr))
296 r->state = STATE_ROW;
298 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
299 (XML_READER_TYPE_END_ELEMENT == r->node_type))
301 r->state = STATE_SPREADSHEET;
305 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
307 (XML_READER_TYPE_ELEMENT == r->node_type))
310 xmlTextReaderGetAttribute (r->xtr,
311 _xml ("table:number-columns-repeated"));
313 r->col_span = value ? _xmlchar_to_int (value) : 1;
314 r->col += r->col_span;
316 if (! xmlTextReaderIsEmptyElement (r->xtr))
317 r->state = STATE_CELL;
319 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
321 (XML_READER_TYPE_END_ELEMENT == r->node_type))
323 r->state = STATE_TABLE;
327 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
329 ( XML_READER_TYPE_ELEMENT == r->node_type))
331 if (! xmlTextReaderIsEmptyElement (r->xtr))
332 r->state = STATE_CELL_CONTENT;
335 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
337 (XML_READER_TYPE_END_ELEMENT == r->node_type)
340 r->state = STATE_ROW;
343 case STATE_CELL_CONTENT:
344 assert (r->current_sheet >= 0);
345 assert (r->current_sheet < r->n_allocated_sheets);
347 if (r->sheets[r->current_sheet].start_row == -1)
348 r->sheets[r->current_sheet].start_row = r->row - 1;
351 (r->sheets[r->current_sheet].start_col == -1)
353 (r->sheets[r->current_sheet].start_col >= r->col - 1)
355 r->sheets[r->current_sheet].start_col = r->col - 1;
357 r->sheets[r->current_sheet].stop_row = r->row - 1;
359 if ( r->sheets[r->current_sheet].stop_col < r->col - 1)
360 r->sheets[r->current_sheet].stop_col = r->col - 1;
362 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
363 r->state = STATE_CELL;
374 A struct containing the parameters of a cell's value
387 struct xml_value firstval;
391 /* Determine the width that a xmv should probably have */
393 xmv_to_width (const struct xml_value *xmv, int fallback)
395 int width = SPREADSHEET_DEFAULT_WIDTH;
397 /* Non-strings always have zero width */
398 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
405 width = ROUND_UP (xmlStrlen (xmv->value),
406 SPREADSHEET_DEFAULT_WIDTH);
408 width = ROUND_UP (xmlStrlen (xmv->text),
409 SPREADSHEET_DEFAULT_WIDTH);
415 Sets the VAR of case C, to the value corresponding to the xml data
418 convert_xml_to_value (struct ccase *c, const struct variable *var,
419 const struct xml_value *xmv)
421 union value *v = case_data_rw (c, var);
423 if (xmv->value == NULL && xmv->text == NULL)
424 value_set_missing (v, var_get_width (var));
425 else if ( var_is_alpha (var))
426 /* Use the text field, because it seems that there is no
427 value field for strings */
428 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
432 const struct fmt_spec *fmt = var_get_write_format (var);
433 enum fmt_category fc = fmt_get_category (fmt->type);
435 assert ( fc != FMT_CAT_STRING);
438 xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
440 free (data_in (ss_cstr (text), "UTF-8",
449 /* Try to find out how many sheets there are in the "workbook" */
451 get_sheet_count (struct zip_reader *zreader)
453 xmlTextReaderPtr mxtr;
454 struct zip_member *meta = NULL;
455 meta = zip_member_open (zreader, "meta.xml");
460 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
461 (xmlInputCloseCallback) zip_member_finish,
462 meta, NULL, NULL, 0);
464 while (1 == xmlTextReaderRead (mxtr))
466 xmlChar *name = xmlTextReaderName (mxtr);
467 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
469 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
473 int s = _xmlchar_to_int (attr);
482 ods_error_handler (void *ctx, const char *mesg,
483 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
485 struct ods_reader *r = ctx;
487 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
489 r->spreadsheet.file_name,
490 xmlTextReaderLocatorLineNumber (loc),
496 init_reader (struct ods_reader *r, bool report_errors)
498 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
499 xmlTextReaderPtr xtr;
501 if ( content == NULL)
504 zip_member_ref (content);
507 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
508 (xmlInputCloseCallback) zip_member_finish,
510 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
516 r->spreadsheet.type = SPREADSHEET_ODS;
519 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
526 ods_probe (const char *filename, bool report_errors)
528 struct ods_reader *r;
529 struct string errs = DS_EMPTY_INITIALIZER;
531 struct zip_reader *zr = zip_reader_create (filename, &errs);
536 sheet_count = get_sheet_count (zr);
538 r = xzalloc (sizeof *r);
541 if (! init_reader (r, report_errors))
546 r->spreadsheet.n_sheets = sheet_count;
547 r->n_allocated_sheets = 0;
552 r->spreadsheet.file_name = filename;
553 return &r->spreadsheet;
556 zip_reader_destroy (r->zreader);
563 ods_make_reader (struct spreadsheet *spreadsheet,
564 const struct spreadsheet_read_options *opts)
567 xmlChar *type = NULL;
568 unsigned long int vstart = 0;
569 casenumber n_cases = CASENUMBER_MAX;
571 struct var_spec *var_spec = NULL;
574 struct ods_reader *r = (struct ods_reader *) spreadsheet;
575 xmlChar *val_string = NULL;
578 r->read_names = opts->read_names;
579 ds_init_empty (&r->ods_errs);
582 if ( !init_reader (r, true))
586 if ( opts->cell_range )
588 if ( ! convert_cell_ref (opts->cell_range,
589 &r->start_col, &r->start_row,
590 &r->stop_col, &r->stop_row))
592 msg (SE, _("Invalid cell range `%s'"),
606 r->state = STATE_INIT;
607 r->target_sheet_name = BAD_CAST opts->sheet_name;
608 r->target_sheet_index = opts->sheet_index;
612 /* If CELLRANGE was given, then we know how many variables should be read */
613 if ( r->stop_col != -1 )
615 assert (var_spec == NULL);
616 n_var_specs = r->stop_col - r->start_col + 1;
617 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
618 memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
622 /* Advance to the start of the cells for the target sheet */
623 while ( r->current_sheet < r->target_sheet_index - 1 ||
624 r->state != STATE_TABLE
627 if (1 != (ret = xmlTextReaderRead (r->xtr)))
636 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
637 spreadsheet->file_name);
641 if ( opts->read_names)
643 while (1 == (ret = xmlTextReaderRead (r->xtr)))
649 /* If the row is finished then stop for now */
650 if (r->state == STATE_TABLE && r->row > r->wanted_row_start)
653 idx = r->col - r->wanted_col_start - 1;
655 if (r->state == STATE_CELL_CONTENT
657 XML_READER_TYPE_TEXT == r->node_type)
659 xmlChar *value = xmlTextReaderValue (r->xtr);
661 if ( idx >= n_var_specs)
663 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
665 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
666 memset (var_spec + n_var_specs,
668 (n_var_specs - idx + 1) * sizeof (*var_spec));
669 n_var_specs = idx + 1;
671 var_spec[idx].firstval.text = 0;
672 var_spec[idx].firstval.value = 0;
673 var_spec[idx].firstval.type = 0;
675 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
681 /* Read in the first row of data */
682 while (1 == xmlTextReaderRead (r->xtr))
687 /* If the row is finished then stop for now */
688 if (r->state == STATE_TABLE && r->row > r->wanted_row_start + (opts->read_names ? 1 : 0))
691 idx = r->col - r->wanted_col_start - 1;
693 if ( r->state == STATE_CELL &&
694 XML_READER_TYPE_ELEMENT == r->node_type)
696 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
697 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
700 if ( r->state == STATE_CELL_CONTENT &&
701 XML_READER_TYPE_TEXT == r->node_type)
703 if ( idx >= n_var_specs)
705 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
706 var_spec [idx].name = NULL;
707 n_var_specs = idx + 1;
710 var_spec [idx].firstval.type = type;
711 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
712 var_spec [idx].firstval.value = val_string;
719 /* Create the dictionary and populate it */
720 r->spreadsheet.dict = r->dict = dict_create (
721 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
723 for (i = 0; i < n_var_specs ; ++i )
726 struct variable *var = NULL;
727 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
728 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
729 dict_create_var (r->dict, name, width);
732 var = dict_get_var (r->dict, i);
734 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
741 fmt = fmt_default_for_width (width);
743 var_set_both_formats (var, &fmt);
746 /* Create the first case, and cache it */
747 r->used_first_case = false;
749 if ( n_var_specs == 0 )
751 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
752 spreadsheet->file_name);
756 r->proto = caseproto_ref (dict_get_proto (r->dict));
757 r->first_case = case_create (r->proto);
758 case_set_missing (r->first_case);
760 for (i = 0 ; i < n_var_specs; ++i)
762 const struct variable *var = dict_get_var (r->dict, i);
764 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
767 /* Read in the first row of data */
768 while (1 == xmlTextReaderRead (r->xtr))
772 if (r->state == STATE_ROW)
776 // zip_reader_destroy (zreader);
779 for ( i = 0 ; i < n_var_specs ; ++i )
781 free (var_spec[i].firstval.type);
782 free (var_spec[i].firstval.value);
783 free (var_spec[i].firstval.text);
784 free (var_spec[i].name);
790 return casereader_create_sequential
794 &ods_file_casereader_class, r);
798 // zip_reader_destroy (zreader);
800 for ( i = 0 ; i < n_var_specs ; ++i )
802 free (var_spec[i].firstval.type);
803 free (var_spec[i].firstval.value);
804 free (var_spec[i].firstval.text);
805 free (var_spec[i].name);
810 dict_destroy (r->spreadsheet.dict);
811 r->spreadsheet.dict = NULL;
812 ods_file_casereader_destroy (NULL, r);
819 /* Reads and returns one case from READER's file. Returns a null
820 pointer on failure. */
821 static struct ccase *
822 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
824 struct ccase *c = NULL;
825 xmlChar *val_string = NULL;
826 struct ods_reader *r = r_;
827 int current_row = r->row;
829 if ( !r->used_first_case )
831 r->used_first_case = true;
832 return r->first_case;
835 if ( r->state > STATE_INIT)
837 c = case_create (r->proto);
838 case_set_missing (c);
841 if (r->state == STATE_SPREADSHEET
843 r->current_sheet == r->target_sheet_index - 1
849 while (1 == xmlTextReaderRead (r->xtr))
853 if (r->row > current_row && r->state == STATE_ROW)
856 if ( r->state == STATE_CELL &&
857 r->node_type == XML_READER_TYPE_ELEMENT )
859 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
862 if ( r->state == STATE_CELL_CONTENT &&
863 r->node_type == XML_READER_TYPE_TEXT )
866 struct xml_value *xmv = xzalloc (sizeof *xmv);
867 xmv->text = xmlTextReaderValue (r->xtr);
868 xmv->value = val_string;
871 for (col = 0; col < r->col_span; ++col)
873 const int idx = r->col + col - r->wanted_col_start - 1;
874 const struct variable *var = dict_get_var (r->dict, idx);
875 convert_xml_to_value (c, var, xmv);
883 if ( r->state < STATE_TABLE)