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 "ods-reader.h"
20 #include "spreadsheet-reader.h"
25 #include <libxml/xmlreader.h>
28 #include "data/case.h"
29 #include "data/casereader-provider.h"
30 #include "data/data-in.h"
31 #include "data/dictionary.h"
32 #include "data/format.h"
33 #include "data/identifier.h"
34 #include "data/value.h"
35 #include "data/variable.h"
36 #include "libpspp/assertion.h"
37 #include "libpspp/i18n.h"
38 #include "libpspp/message.h"
39 #include "libpspp/misc.h"
40 #include "libpspp/str.h"
41 #include "libpspp/zip-reader.h"
43 #include "gl/c-strtod.h"
44 #include "gl/minmax.h"
45 #include "gl/xalloc.h"
48 #define _(msgid) gettext (msgid)
49 #define N_(msgid) (msgid)
51 static void ods_file_casereader_destroy (struct casereader *, void *);
52 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
55 static const struct casereader_class ods_file_casereader_class =
57 ods_file_casereader_read,
58 ods_file_casereader_destroy,
65 /* The name of the sheet (utf8 encoding) */
77 STATE_INIT = 0, /* Initial state */
78 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
79 STATE_TABLE, /* Found the sheet that we actually want */
80 STATE_ROW, /* Found the start of the cell array */
81 STATE_CELL, /* Found a cell */
82 STATE_CELL_CONTENT /* Found a the text within a cell */
89 enum reader_state state;
93 xmlChar *current_sheet_name;
99 state_data_destroy (struct state_data *sd)
101 xmlFree (sd->current_sheet_name);
102 sd->current_sheet_name = NULL;
104 xmlFreeTextReader (sd->xtr);
110 struct spreadsheet spreadsheet;
111 struct zip_reader *zreader;
113 int target_sheet_index;
114 xmlChar *target_sheet_name;
116 /* State data for the meta data */
117 struct state_data msd;
119 /* State data for the reader */
120 struct state_data rsd;
127 struct sheet_detail *sheets;
128 int n_allocated_sheets;
130 struct caseproto *proto;
131 struct dictionary *dict;
132 struct ccase *first_case;
133 bool used_first_case;
136 struct string ods_errs;
138 struct string zip_errs;
142 ods_unref (struct spreadsheet *s)
144 struct ods_reader *r = (struct ods_reader *) s;
146 if (--s->ref_cnt == 0)
150 state_data_destroy (&r->msd);
151 for (i = 0; i < r->n_allocated_sheets; ++i)
153 xmlFree (r->sheets[i].name);
156 dict_unref (r->dict);
158 zip_reader_destroy (r->zreader);
168 reading_target_sheet (const struct ods_reader *r, const struct state_data *msd)
170 if (r->target_sheet_name != NULL)
172 if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name))
176 if (r->target_sheet_index == msd->current_sheet + 1)
183 static void process_node (struct ods_reader *or, struct state_data *r);
187 ods_get_sheet_name (struct spreadsheet *s, int n)
189 struct ods_reader *r = (struct ods_reader *) s;
190 struct state_data *or = &r->msd;
192 assert (n < s->n_sheets);
195 (r->n_allocated_sheets <= n)
196 || or->state != STATE_SPREADSHEET
199 int ret = xmlTextReaderRead (or->xtr);
203 process_node (r, or);
206 return r->sheets[n].name;
210 ods_get_sheet_range (struct spreadsheet *s, int n)
212 struct ods_reader *r = (struct ods_reader *) s;
213 struct state_data *or = &r->msd;
215 assert (n < s->n_sheets);
218 (r->n_allocated_sheets <= n)
219 || (r->sheets[n].stop_row == -1)
220 || or->state != STATE_SPREADSHEET
223 int ret = xmlTextReaderRead (or->xtr);
227 process_node (r, or);
230 return create_cell_range (
231 r->sheets[n].start_col,
232 r->sheets[n].start_row,
233 r->sheets[n].stop_col,
234 r->sheets[n].stop_row);
239 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
241 struct ods_reader *r = r_;
245 state_data_destroy (&r->rsd);
247 if ( ! ds_is_empty (&r->ods_errs))
248 msg (ME, "%s", ds_cstr (&r->ods_errs));
250 ds_destroy (&r->ods_errs);
252 if ( r->first_case && ! r->used_first_case )
253 case_unref (r->first_case);
256 caseproto_unref (r->proto);
259 xmlFree (r->target_sheet_name);
260 r->target_sheet_name = NULL;
263 ods_unref (&r->spreadsheet);
271 process_node (struct ods_reader *or, struct state_data *r)
273 xmlChar *name = xmlTextReaderName (r->xtr);
275 name = xmlStrdup (_xml ("--"));
278 r->node_type = xmlTextReaderNodeType (r->xtr);
283 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
284 XML_READER_TYPE_ELEMENT == r->node_type)
286 r->state = STATE_SPREADSHEET;
287 r->current_sheet = -1;
288 r->current_sheet_name = NULL;
291 case STATE_SPREADSHEET:
292 if (0 == xmlStrcasecmp (name, _xml("table:table"))
294 (XML_READER_TYPE_ELEMENT == r->node_type))
296 xmlFree (r->current_sheet_name);
297 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
301 if (r->current_sheet >= or->n_allocated_sheets)
303 assert (r->current_sheet == or->n_allocated_sheets);
304 or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
305 or->sheets[or->n_allocated_sheets - 1].start_col = -1;
306 or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
307 or->sheets[or->n_allocated_sheets - 1].start_row = -1;
308 or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
309 or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
315 r->state = STATE_TABLE;
317 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
318 XML_READER_TYPE_ELEMENT == r->node_type)
320 r->state = STATE_INIT;
324 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
325 (XML_READER_TYPE_ELEMENT == r->node_type))
328 xmlTextReaderGetAttribute (r->xtr,
329 _xml ("table:number-rows-repeated"));
331 int row_span = value ? _xmlchar_to_int (value) : 1;
336 if (! xmlTextReaderIsEmptyElement (r->xtr))
337 r->state = STATE_ROW;
341 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
342 (XML_READER_TYPE_END_ELEMENT == r->node_type))
344 r->state = STATE_SPREADSHEET;
348 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
350 (XML_READER_TYPE_ELEMENT == r->node_type))
353 xmlTextReaderGetAttribute (r->xtr,
354 _xml ("table:number-columns-repeated"));
356 r->col_span = value ? _xmlchar_to_int (value) : 1;
357 r->col += r->col_span;
359 if (! xmlTextReaderIsEmptyElement (r->xtr))
360 r->state = STATE_CELL;
364 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
366 (XML_READER_TYPE_END_ELEMENT == r->node_type))
368 r->state = STATE_TABLE;
372 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
374 ( XML_READER_TYPE_ELEMENT == r->node_type))
376 if (! xmlTextReaderIsEmptyElement (r->xtr))
377 r->state = STATE_CELL_CONTENT;
380 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
382 (XML_READER_TYPE_END_ELEMENT == r->node_type)
385 r->state = STATE_ROW;
388 case STATE_CELL_CONTENT:
389 assert (r->current_sheet >= 0);
390 assert (r->current_sheet < or->n_allocated_sheets);
392 if (or->sheets[r->current_sheet].start_row == -1)
393 or->sheets[r->current_sheet].start_row = r->row - 1;
396 (or->sheets[r->current_sheet].start_col == -1)
398 (or->sheets[r->current_sheet].start_col >= r->col - 1)
400 or->sheets[r->current_sheet].start_col = r->col - 1;
402 or->sheets[r->current_sheet].stop_row = r->row - 1;
404 if ( or->sheets[r->current_sheet].stop_col < r->col - 1)
405 or->sheets[r->current_sheet].stop_col = r->col - 1;
407 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
408 r->state = STATE_CELL;
419 A struct containing the parameters of a cell's value
432 struct xml_value firstval;
436 /* Determine the width that a xmv should probably have */
438 xmv_to_width (const struct xml_value *xmv, int fallback)
440 int width = SPREADSHEET_DEFAULT_WIDTH;
442 /* Non-strings always have zero width */
443 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
450 width = ROUND_UP (xmlStrlen (xmv->value),
451 SPREADSHEET_DEFAULT_WIDTH);
453 width = ROUND_UP (xmlStrlen (xmv->text),
454 SPREADSHEET_DEFAULT_WIDTH);
460 Sets the VAR of case C, to the value corresponding to the xml data
463 convert_xml_to_value (struct ccase *c, const struct variable *var,
464 const struct xml_value *xmv, int col, int row)
466 union value *v = case_data_rw (c, var);
468 if (xmv->value == NULL && xmv->text == NULL)
469 value_set_missing (v, var_get_width (var));
470 else if ( var_is_alpha (var))
471 /* Use the text field, because it seems that there is no
472 value field for strings */
473 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
476 const struct fmt_spec *fmt = var_get_write_format (var);
477 enum fmt_category fc = fmt_get_category (fmt->type);
479 assert ( fc != FMT_CAT_STRING);
481 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
483 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
487 const char *text = xmv->value ?
488 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
490 char *m = data_in (ss_cstr (text), "UTF-8",
498 char buf [FMT_STRING_LEN_MAX + 1];
499 char *cell = create_cell_ref (col, row);
501 msg (MW, _("Cannot convert the value in the spreadsheet cell %s to format (%s): %s"),
502 cell, fmt_to_string (fmt, buf), m);
511 /* Try to find out how many sheets there are in the "workbook" */
513 get_sheet_count (struct zip_reader *zreader)
515 xmlTextReaderPtr mxtr;
516 struct zip_member *meta = NULL;
517 meta = zip_member_open (zreader, "meta.xml");
522 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
523 (xmlInputCloseCallback) NULL,
524 meta, NULL, NULL, 0);
526 while (1 == xmlTextReaderRead (mxtr))
528 xmlChar *name = xmlTextReaderName (mxtr);
529 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
531 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
535 int s = _xmlchar_to_int (attr);
536 xmlFreeTextReader (mxtr);
546 xmlFreeTextReader (mxtr);
551 ods_error_handler (void *ctx, const char *mesg,
552 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
554 struct ods_reader *r = ctx;
556 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
558 r->spreadsheet.file_name,
559 xmlTextReaderLocatorLineNumber (loc),
564 static xmlTextReaderPtr
565 init_reader (struct ods_reader *r, bool report_errors)
567 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
568 xmlTextReaderPtr xtr;
570 if ( content == NULL)
573 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
574 (xmlInputCloseCallback) NULL,
576 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
582 r->spreadsheet.type = SPREADSHEET_ODS;
585 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
593 ods_probe (const char *filename, bool report_errors)
596 struct ods_reader *r = xzalloc (sizeof *r);
597 xmlTextReaderPtr xtr;
598 struct zip_reader *zr;
600 ds_init_empty (&r->zip_errs);
602 zr = zip_reader_create (filename, &r->zip_errs);
608 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
609 filename, ds_cstr (&r->zip_errs));
611 ds_destroy (&r->zip_errs);
616 sheet_count = get_sheet_count (zr);
619 r->spreadsheet.ref_cnt = 1;
621 xtr = init_reader (r, report_errors);
629 r->msd.current_sheet = 0;
630 r->msd.state = STATE_INIT;
633 r->spreadsheet.n_sheets = sheet_count;
634 r->n_allocated_sheets = 0;
637 r->spreadsheet.file_name = strdup (filename);
638 return &r->spreadsheet;
641 ds_destroy (&r->zip_errs);
642 zip_reader_destroy (r->zreader);
648 ods_make_reader (struct spreadsheet *spreadsheet,
649 const struct spreadsheet_read_options *opts)
652 xmlChar *type = NULL;
653 unsigned long int vstart = 0;
654 casenumber n_cases = CASENUMBER_MAX;
656 struct var_spec *var_spec = NULL;
658 xmlTextReaderPtr xtr;
660 struct ods_reader *r = (struct ods_reader *) spreadsheet;
661 xmlChar *val_string = NULL;
664 r->read_names = opts->read_names;
665 ds_init_empty (&r->ods_errs);
666 ++r->spreadsheet.ref_cnt;
668 xtr = init_reader (r, true);
675 r->rsd.current_sheet = 0;
676 r->rsd.state = STATE_INIT;
678 r->used_first_case = false;
679 r->first_case = NULL;
681 if (opts->cell_range)
683 if ( ! convert_cell_ref (opts->cell_range,
684 &r->start_col, &r->start_row,
685 &r->stop_col, &r->stop_row))
687 msg (SE, _("Invalid cell range `%s'"),
700 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
701 r->target_sheet_index = opts->sheet_index;
703 /* Advance to the start of the cells for the target sheet */
704 while ( ! reading_target_sheet (r, &r->rsd)
705 || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row )
707 if (1 != (ret = xmlTextReaderRead (r->rsd.xtr)))
710 process_node (r, &r->rsd);
715 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
716 spreadsheet->file_name);
720 if ( opts->read_names)
722 while (1 == xmlTextReaderRead (r->rsd.xtr))
724 process_node (r, &r->rsd);
726 /* If the row is finished then stop for now */
727 if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row)
730 int idx = r->rsd.col - r->start_col - 1;
735 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
738 if (r->rsd.state == STATE_CELL_CONTENT
740 XML_READER_TYPE_TEXT == r->rsd.node_type)
742 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
743 if (idx >= n_var_specs)
745 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
747 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
748 memset (var_spec + n_var_specs,
750 (idx - n_var_specs + 1) * sizeof (*var_spec));
751 n_var_specs = idx + 1;
753 for (int i = 0; i < r->rsd.col_span; ++i)
755 var_spec[idx - i].firstval.text = 0;
756 var_spec[idx - i].firstval.value = 0;
757 var_spec[idx - i].firstval.type = 0;
758 var_spec[idx - i].name =
759 strdup (CHAR_CAST (const char *, value));
767 /* Read in the first row of data */
768 while (1 == xmlTextReaderRead (r->rsd.xtr))
771 process_node (r, &r->rsd);
773 if ( ! reading_target_sheet (r, &r->rsd) )
776 /* If the row is finished then stop for now */
777 if (r->rsd.state == STATE_TABLE &&
778 r->rsd.row > r->start_row + (opts->read_names ? 1 : 0))
781 idx = r->rsd.col - r->start_col - 1;
785 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
788 if ( r->rsd.state == STATE_CELL &&
789 XML_READER_TYPE_ELEMENT == r->rsd.node_type)
791 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
792 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
795 if ( r->rsd.state == STATE_CELL_CONTENT &&
796 XML_READER_TYPE_TEXT == r->rsd.node_type)
798 if (idx >= n_var_specs)
800 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
801 memset (var_spec + n_var_specs,
803 (idx - n_var_specs + 1) * sizeof (*var_spec));
805 var_spec [idx].name = NULL;
806 n_var_specs = idx + 1;
809 var_spec [idx].firstval.type = type;
810 var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr);
811 var_spec [idx].firstval.value = val_string;
819 /* Create the dictionary and populate it */
820 r->spreadsheet.dict = r->dict = dict_create (
821 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr)));
823 for (i = 0; i < n_var_specs ; ++i )
826 struct variable *var = NULL;
827 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
828 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
829 dict_create_var (r->dict, name, width);
832 var = dict_get_var (r->dict, i);
834 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
841 fmt = fmt_default_for_width (width);
843 var_set_both_formats (var, &fmt);
846 if ( n_var_specs == 0 )
848 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
849 spreadsheet->file_name);
853 /* Create the first case, and cache it */
854 r->proto = caseproto_ref (dict_get_proto (r->dict));
855 r->first_case = case_create (r->proto);
856 case_set_missing (r->first_case);
858 for (i = 0 ; i < n_var_specs; ++i)
860 const struct variable *var = dict_get_var (r->dict, i);
862 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval,
863 r->rsd.col - n_var_specs + i,
867 /* Read in the first row of data */
868 while (1 == xmlTextReaderRead (r->rsd.xtr))
870 process_node (r, &r->rsd);
872 if (r->rsd.state == STATE_ROW)
877 for ( i = 0 ; i < n_var_specs ; ++i )
879 free (var_spec[i].firstval.type);
880 free (var_spec[i].firstval.value);
881 free (var_spec[i].firstval.text);
882 free (var_spec[i].name);
888 return casereader_create_sequential
892 &ods_file_casereader_class, r);
896 for ( i = 0 ; i < n_var_specs ; ++i )
898 free (var_spec[i].firstval.type);
899 free (var_spec[i].firstval.value);
900 free (var_spec[i].firstval.text);
901 free (var_spec[i].name);
906 ods_file_casereader_destroy (NULL, r);
912 /* Reads and returns one case from READER's file. Returns a null
913 pointer on failure. */
914 static struct ccase *
915 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
917 struct ccase *c = NULL;
918 struct ods_reader *r = r_;
920 xmlChar *val_string = NULL;
921 xmlChar *type = NULL;
923 if (!r->used_first_case)
925 r->used_first_case = true;
926 return r->first_case;
930 /* Advance to the start of a row. (If there is one) */
931 while (r->rsd.state != STATE_ROW
932 && 1 == xmlTextReaderRead (r->rsd.xtr)
935 process_node (r, &r->rsd);
939 if ( ! reading_target_sheet (r, &r->rsd)
940 || r->rsd.state < STATE_TABLE
941 || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
947 c = case_create (r->proto);
948 case_set_missing (c);
950 while (1 == xmlTextReaderRead (r->rsd.xtr))
952 process_node (r, &r->rsd);
954 if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
957 if (r->rsd.state == STATE_CELL &&
958 r->rsd.node_type == XML_READER_TYPE_ELEMENT)
960 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
961 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
964 if (r->rsd.state == STATE_CELL_CONTENT &&
965 r->rsd.node_type == XML_READER_TYPE_TEXT)
968 struct xml_value *xmv = xzalloc (sizeof *xmv);
969 xmv->text = xmlTextReaderValue (r->rsd.xtr);
970 xmv->value = val_string;
975 for (col = 0; col < r->rsd.col_span; ++col)
977 const struct variable *var;
978 const int idx = r->rsd.col - col - r->start_col - 1;
981 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
983 if (idx >= dict_get_var_cnt (r->dict))
986 var = dict_get_var (r->dict, idx);
987 convert_xml_to_value (c, var, xmv, idx + r->start_col, r->rsd.row - 1);
991 xmlFree (xmv->value);
995 if ( r->rsd.state <= STATE_TABLE)
1000 xmlFree (val_string);