1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2012, 2013, 2016 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/c-strtod.h"
26 #include "gl/minmax.h"
29 #define _(msgid) gettext (msgid)
30 #define N_(msgid) (msgid)
32 #include "ods-reader.h"
33 #include "spreadsheet-reader.h"
38 ods_probe (const char *filename, bool report_errors)
41 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
47 ods_get_sheet_name (struct spreadsheet *s, int n)
53 ods_get_sheet_range (struct spreadsheet *s, int n)
59 ods_make_reader (struct spreadsheet *spreadsheet,
60 const struct spreadsheet_read_options *opts)
67 ods_unref (struct spreadsheet *r)
73 #include "libpspp/zip-reader.h"
79 #include <libxml/xmlreader.h>
82 #include "data/format.h"
83 #include "data/case.h"
84 #include "data/casereader-provider.h"
85 #include "data/dictionary.h"
86 #include "data/identifier.h"
87 #include "data/value.h"
88 #include "data/variable.h"
89 #include "libpspp/i18n.h"
90 #include "libpspp/str.h"
92 #include "gl/xalloc.h"
94 static void ods_file_casereader_destroy (struct casereader *, void *);
95 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
98 static const struct casereader_class ods_file_casereader_class =
100 ods_file_casereader_read,
101 ods_file_casereader_destroy,
108 /* The name of the sheet (utf8 encoding) */
120 STATE_INIT = 0, /* Initial state */
121 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
122 STATE_TABLE, /* Found the sheet that we actually want */
123 STATE_ROW, /* Found the start of the cell array */
124 STATE_CELL, /* Found a cell */
125 STATE_CELL_CONTENT /* Found a the text within a cell */
130 xmlTextReaderPtr xtr;
132 enum reader_state state;
136 xmlChar *current_sheet_name;
142 state_data_destroy (struct state_data *sd)
144 xmlFree (sd->current_sheet_name);
145 sd->current_sheet_name = NULL;
147 xmlFreeTextReader (sd->xtr);
153 struct spreadsheet spreadsheet;
154 struct zip_reader *zreader;
156 int target_sheet_index;
157 xmlChar *target_sheet_name;
159 /* State data for the meta data */
160 struct state_data msd;
162 /* State data for the reader */
163 struct state_data rsd;
170 struct sheet_detail *sheets;
171 int n_allocated_sheets;
173 struct caseproto *proto;
174 struct dictionary *dict;
175 struct ccase *first_case;
176 bool used_first_case;
179 struct string ods_errs;
181 struct string zip_errs;
185 ods_unref (struct spreadsheet *s)
187 struct ods_reader *r = (struct ods_reader *) s;
189 if (--s->ref_cnt == 0)
193 state_data_destroy (&r->msd);
194 for (i = 0; i < r->n_allocated_sheets; ++i)
196 xmlFree (r->sheets[i].name);
199 dict_destroy (r->dict);
201 zip_reader_destroy (r->zreader);
211 reading_target_sheet (const struct ods_reader *r, const struct state_data *msd)
213 if (r->target_sheet_name != NULL)
215 if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name))
219 if (r->target_sheet_index == msd->current_sheet + 1)
226 static void process_node (struct ods_reader *or, struct state_data *r);
230 ods_get_sheet_name (struct spreadsheet *s, int n)
232 struct ods_reader *r = (struct ods_reader *) s;
233 struct state_data *or = &r->msd;
235 assert (n < s->n_sheets);
238 (r->n_allocated_sheets <= n)
239 || or->state != STATE_SPREADSHEET
242 int ret = xmlTextReaderRead (or->xtr);
246 process_node (r, or);
249 return r->sheets[n].name;
253 ods_get_sheet_range (struct spreadsheet *s, int n)
255 struct ods_reader *r = (struct ods_reader *) s;
256 struct state_data *or = &r->msd;
258 assert (n < s->n_sheets);
261 (r->n_allocated_sheets <= n)
262 || (r->sheets[n].stop_row == -1)
263 || or->state != STATE_SPREADSHEET
266 int ret = xmlTextReaderRead (or->xtr);
270 process_node (r, or);
273 return create_cell_range (
274 r->sheets[n].start_col,
275 r->sheets[n].start_row,
276 r->sheets[n].stop_col,
277 r->sheets[n].stop_row);
282 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
284 struct ods_reader *r = r_;
288 state_data_destroy (&r->rsd);
290 if ( ! ds_is_empty (&r->ods_errs))
291 msg (ME, "%s", ds_cstr (&r->ods_errs));
293 ds_destroy (&r->ods_errs);
295 if ( r->first_case && ! r->used_first_case )
296 case_unref (r->first_case);
299 caseproto_unref (r->proto);
302 xmlFree (r->target_sheet_name);
303 r->target_sheet_name = NULL;
306 ods_unref (&r->spreadsheet);
314 process_node (struct ods_reader *or, struct state_data *r)
316 xmlChar *name = xmlTextReaderName (r->xtr);
318 name = xmlStrdup (_xml ("--"));
321 r->node_type = xmlTextReaderNodeType (r->xtr);
326 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
327 XML_READER_TYPE_ELEMENT == r->node_type)
329 r->state = STATE_SPREADSHEET;
330 r->current_sheet = -1;
331 r->current_sheet_name = NULL;
334 case STATE_SPREADSHEET:
335 if (0 == xmlStrcasecmp (name, _xml("table:table"))
337 (XML_READER_TYPE_ELEMENT == r->node_type))
339 xmlFree (r->current_sheet_name);
340 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
344 if (r->current_sheet >= or->n_allocated_sheets)
346 assert (r->current_sheet == or->n_allocated_sheets);
347 or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
348 or->sheets[or->n_allocated_sheets - 1].start_col = -1;
349 or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
350 or->sheets[or->n_allocated_sheets - 1].start_row = -1;
351 or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
352 or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
358 r->state = STATE_TABLE;
360 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
361 XML_READER_TYPE_ELEMENT == r->node_type)
363 r->state = STATE_INIT;
367 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
368 (XML_READER_TYPE_ELEMENT == r->node_type))
371 xmlTextReaderGetAttribute (r->xtr,
372 _xml ("table:number-rows-repeated"));
374 int row_span = value ? _xmlchar_to_int (value) : 1;
379 if (! xmlTextReaderIsEmptyElement (r->xtr))
380 r->state = STATE_ROW;
384 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
385 (XML_READER_TYPE_END_ELEMENT == r->node_type))
387 r->state = STATE_SPREADSHEET;
391 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
393 (XML_READER_TYPE_ELEMENT == r->node_type))
396 xmlTextReaderGetAttribute (r->xtr,
397 _xml ("table:number-columns-repeated"));
399 r->col_span = value ? _xmlchar_to_int (value) : 1;
400 r->col += r->col_span;
402 if (! xmlTextReaderIsEmptyElement (r->xtr))
403 r->state = STATE_CELL;
407 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
409 (XML_READER_TYPE_END_ELEMENT == r->node_type))
411 r->state = STATE_TABLE;
415 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
417 ( XML_READER_TYPE_ELEMENT == r->node_type))
419 if (! xmlTextReaderIsEmptyElement (r->xtr))
420 r->state = STATE_CELL_CONTENT;
423 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
425 (XML_READER_TYPE_END_ELEMENT == r->node_type)
428 r->state = STATE_ROW;
431 case STATE_CELL_CONTENT:
432 assert (r->current_sheet >= 0);
433 assert (r->current_sheet < or->n_allocated_sheets);
435 if (or->sheets[r->current_sheet].start_row == -1)
436 or->sheets[r->current_sheet].start_row = r->row - 1;
439 (or->sheets[r->current_sheet].start_col == -1)
441 (or->sheets[r->current_sheet].start_col >= r->col - 1)
443 or->sheets[r->current_sheet].start_col = r->col - 1;
445 or->sheets[r->current_sheet].stop_row = r->row - 1;
447 if ( or->sheets[r->current_sheet].stop_col < r->col - 1)
448 or->sheets[r->current_sheet].stop_col = r->col - 1;
450 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
451 r->state = STATE_CELL;
462 A struct containing the parameters of a cell's value
475 struct xml_value firstval;
479 /* Determine the width that a xmv should probably have */
481 xmv_to_width (const struct xml_value *xmv, int fallback)
483 int width = SPREADSHEET_DEFAULT_WIDTH;
485 /* Non-strings always have zero width */
486 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
493 width = ROUND_UP (xmlStrlen (xmv->value),
494 SPREADSHEET_DEFAULT_WIDTH);
496 width = ROUND_UP (xmlStrlen (xmv->text),
497 SPREADSHEET_DEFAULT_WIDTH);
503 Sets the VAR of case C, to the value corresponding to the xml data
506 convert_xml_to_value (struct ccase *c, const struct variable *var,
507 const struct xml_value *xmv, int col, int row)
509 union value *v = case_data_rw (c, var);
511 if (xmv->value == NULL && xmv->text == NULL)
512 value_set_missing (v, var_get_width (var));
513 else if ( var_is_alpha (var))
514 /* Use the text field, because it seems that there is no
515 value field for strings */
516 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
519 const struct fmt_spec *fmt = var_get_write_format (var);
520 enum fmt_category fc = fmt_get_category (fmt->type);
522 assert ( fc != FMT_CAT_STRING);
524 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
526 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
530 const char *text = xmv->value ?
531 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
533 char *m = data_in (ss_cstr (text), "UTF-8",
541 char buf [FMT_STRING_LEN_MAX + 1];
542 char *cell = create_cell_ref (col, row);
544 msg (MW, _("Cannot convert the value in the spreadsheet cell %s to format (%s): %s"),
545 cell, fmt_to_string (fmt, buf), m);
554 /* Try to find out how many sheets there are in the "workbook" */
556 get_sheet_count (struct zip_reader *zreader)
558 xmlTextReaderPtr mxtr;
559 struct zip_member *meta = NULL;
560 meta = zip_member_open (zreader, "meta.xml");
565 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
566 (xmlInputCloseCallback) NULL,
567 meta, NULL, NULL, 0);
569 while (1 == xmlTextReaderRead (mxtr))
571 xmlChar *name = xmlTextReaderName (mxtr);
572 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
574 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
578 int s = _xmlchar_to_int (attr);
579 xmlFreeTextReader (mxtr);
589 xmlFreeTextReader (mxtr);
594 ods_error_handler (void *ctx, const char *mesg,
595 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
597 struct ods_reader *r = ctx;
599 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
601 r->spreadsheet.file_name,
602 xmlTextReaderLocatorLineNumber (loc),
607 static xmlTextReaderPtr
608 init_reader (struct ods_reader *r, bool report_errors)
610 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
611 xmlTextReaderPtr xtr;
613 if ( content == NULL)
616 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
617 (xmlInputCloseCallback) NULL,
619 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
625 r->spreadsheet.type = SPREADSHEET_ODS;
628 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
636 ods_probe (const char *filename, bool report_errors)
639 struct ods_reader *r = xzalloc (sizeof *r);
640 xmlTextReaderPtr xtr;
641 struct zip_reader *zr;
643 ds_init_empty (&r->zip_errs);
645 zr = zip_reader_create (filename, &r->zip_errs);
651 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
652 filename, ds_cstr (&r->zip_errs));
654 ds_destroy (&r->zip_errs);
659 sheet_count = get_sheet_count (zr);
662 r->spreadsheet.ref_cnt = 1;
664 xtr = init_reader (r, report_errors);
672 r->msd.current_sheet = 0;
673 r->msd.state = STATE_INIT;
676 r->spreadsheet.n_sheets = sheet_count;
677 r->n_allocated_sheets = 0;
680 r->spreadsheet.file_name = strdup (filename);
681 return &r->spreadsheet;
684 ds_destroy (&r->zip_errs);
685 zip_reader_destroy (r->zreader);
691 ods_make_reader (struct spreadsheet *spreadsheet,
692 const struct spreadsheet_read_options *opts)
695 xmlChar *type = NULL;
696 unsigned long int vstart = 0;
697 casenumber n_cases = CASENUMBER_MAX;
699 struct var_spec *var_spec = NULL;
701 xmlTextReaderPtr xtr;
703 struct ods_reader *r = (struct ods_reader *) spreadsheet;
704 xmlChar *val_string = NULL;
707 r->read_names = opts->read_names;
708 ds_init_empty (&r->ods_errs);
709 ++r->spreadsheet.ref_cnt;
711 xtr = init_reader (r, true);
718 r->rsd.current_sheet = 0;
719 r->rsd.state = STATE_INIT;
721 r->used_first_case = false;
722 r->first_case = NULL;
724 if (opts->cell_range)
726 if ( ! convert_cell_ref (opts->cell_range,
727 &r->start_col, &r->start_row,
728 &r->stop_col, &r->stop_row))
730 msg (SE, _("Invalid cell range `%s'"),
743 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
744 r->target_sheet_index = opts->sheet_index;
746 /* Advance to the start of the cells for the target sheet */
747 while ( ! reading_target_sheet (r, &r->rsd)
748 || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row )
750 if (1 != (ret = xmlTextReaderRead (r->rsd.xtr)))
753 process_node (r, &r->rsd);
758 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
759 spreadsheet->file_name);
763 if ( opts->read_names)
765 while (1 == xmlTextReaderRead (r->rsd.xtr))
769 process_node (r, &r->rsd);
771 /* If the row is finished then stop for now */
772 if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row)
775 idx = r->rsd.col - r->start_col -1 ;
780 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
783 if (r->rsd.state == STATE_CELL_CONTENT
785 XML_READER_TYPE_TEXT == r->rsd.node_type)
787 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
789 if ( idx >= n_var_specs)
791 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
793 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
794 memset (var_spec + n_var_specs,
796 (idx - n_var_specs + 1) * sizeof (*var_spec));
797 n_var_specs = idx + 1;
799 var_spec[idx].firstval.text = 0;
800 var_spec[idx].firstval.value = 0;
801 var_spec[idx].firstval.type = 0;
803 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
810 /* Read in the first row of data */
811 while (1 == xmlTextReaderRead (r->rsd.xtr))
814 process_node (r, &r->rsd);
816 if ( ! reading_target_sheet (r, &r->rsd) )
819 /* If the row is finished then stop for now */
820 if (r->rsd.state == STATE_TABLE &&
821 r->rsd.row > r->start_row + (opts->read_names ? 1 : 0))
824 idx = r->rsd.col - r->start_col - 1;
828 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
831 if ( r->rsd.state == STATE_CELL &&
832 XML_READER_TYPE_ELEMENT == r->rsd.node_type)
834 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
835 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
838 if ( r->rsd.state == STATE_CELL_CONTENT &&
839 XML_READER_TYPE_TEXT == r->rsd.node_type)
841 if (idx >= n_var_specs)
843 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
844 memset (var_spec + n_var_specs,
846 (idx - n_var_specs + 1) * sizeof (*var_spec));
848 var_spec [idx].name = NULL;
849 n_var_specs = idx + 1;
852 var_spec [idx].firstval.type = type;
853 var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr);
854 var_spec [idx].firstval.value = val_string;
862 /* Create the dictionary and populate it */
863 r->spreadsheet.dict = r->dict = dict_create (
864 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr)));
866 for (i = 0; i < n_var_specs ; ++i )
869 struct variable *var = NULL;
870 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
871 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
872 dict_create_var (r->dict, name, width);
875 var = dict_get_var (r->dict, i);
877 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
884 fmt = fmt_default_for_width (width);
886 var_set_both_formats (var, &fmt);
889 if ( n_var_specs == 0 )
891 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
892 spreadsheet->file_name);
896 /* Create the first case, and cache it */
897 r->proto = caseproto_ref (dict_get_proto (r->dict));
898 r->first_case = case_create (r->proto);
899 case_set_missing (r->first_case);
901 for (i = 0 ; i < n_var_specs; ++i)
903 const struct variable *var = dict_get_var (r->dict, i);
905 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval,
906 r->rsd.col - n_var_specs + i,
910 /* Read in the first row of data */
911 while (1 == xmlTextReaderRead (r->rsd.xtr))
913 process_node (r, &r->rsd);
915 if (r->rsd.state == STATE_ROW)
920 for ( i = 0 ; i < n_var_specs ; ++i )
922 free (var_spec[i].firstval.type);
923 free (var_spec[i].firstval.value);
924 free (var_spec[i].firstval.text);
925 free (var_spec[i].name);
931 return casereader_create_sequential
935 &ods_file_casereader_class, r);
939 for ( i = 0 ; i < n_var_specs ; ++i )
941 free (var_spec[i].firstval.type);
942 free (var_spec[i].firstval.value);
943 free (var_spec[i].firstval.text);
944 free (var_spec[i].name);
949 ods_file_casereader_destroy (NULL, r);
955 /* Reads and returns one case from READER's file. Returns a null
956 pointer on failure. */
957 static struct ccase *
958 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
960 struct ccase *c = NULL;
961 struct ods_reader *r = r_;
963 xmlChar *val_string = NULL;
964 xmlChar *type = NULL;
966 if (!r->used_first_case)
968 r->used_first_case = true;
969 return r->first_case;
973 /* Advance to the start of a row. (If there is one) */
974 while (r->rsd.state != STATE_ROW
975 && 1 == xmlTextReaderRead (r->rsd.xtr)
978 process_node (r, &r->rsd);
982 if ( ! reading_target_sheet (r, &r->rsd)
983 || r->rsd.state < STATE_TABLE
984 || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
990 c = case_create (r->proto);
991 case_set_missing (c);
993 while (1 == xmlTextReaderRead (r->rsd.xtr))
995 process_node (r, &r->rsd);
997 if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
1000 if (r->rsd.state == STATE_CELL &&
1001 r->rsd.node_type == XML_READER_TYPE_ELEMENT)
1003 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
1004 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
1007 if (r->rsd.state == STATE_CELL_CONTENT &&
1008 r->rsd.node_type == XML_READER_TYPE_TEXT)
1011 struct xml_value *xmv = xzalloc (sizeof *xmv);
1012 xmv->text = xmlTextReaderValue (r->rsd.xtr);
1013 xmv->value = val_string;
1018 for (col = 0; col < r->rsd.col_span; ++col)
1020 const struct variable *var;
1021 const int idx = r->rsd.col - col - r->start_col - 1;
1024 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
1026 if (idx >= dict_get_var_cnt (r->dict))
1029 var = dict_get_var (r->dict, idx);
1030 convert_xml_to_value (c, var, xmv, idx + r->start_col, r->rsd.row - 1);
1033 xmlFree (xmv->text);
1034 xmlFree (xmv->value);
1035 xmlFree (xmv->type);
1038 if ( r->rsd.state <= STATE_TABLE)
1043 xmlFree (val_string);