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_open_reader (const struct spreadsheet_read_options *opts,
39 struct dictionary **dict)
41 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
47 ods_make_reader (struct spreadsheet *spreadsheet,
48 const struct spreadsheet_read_options *opts)
55 ods_unref (struct spreadsheet *r)
61 #include "libpspp/zip-reader.h"
67 #include <libxml/xmlreader.h>
70 #include "data/format.h"
71 #include "data/case.h"
72 #include "data/casereader-provider.h"
73 #include "data/dictionary.h"
74 #include "data/identifier.h"
75 #include "data/value.h"
76 #include "data/variable.h"
77 #include "libpspp/i18n.h"
78 #include "libpspp/str.h"
80 #include "gl/xalloc.h"
82 static void ods_file_casereader_destroy (struct casereader *, void *);
83 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
86 static const struct casereader_class ods_file_casereader_class =
88 ods_file_casereader_read,
89 ods_file_casereader_destroy,
96 /* The name of the sheet (utf8 encoding) */
108 STATE_INIT = 0, /* Initial state */
109 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
110 STATE_TABLE, /* Found the sheet that we actually want */
111 STATE_ROW, /* Found the start of the cell array */
112 STATE_CELL, /* Found a cell */
113 STATE_CELL_CONTENT /* Found a the text within a cell */
118 xmlTextReaderPtr xtr;
120 enum reader_state state;
124 xmlChar *current_sheet_name;
130 state_data_destroy (struct state_data *sd)
132 xmlFree (sd->current_sheet_name);
133 sd->current_sheet_name = NULL;
135 xmlFreeTextReader (sd->xtr);
141 struct spreadsheet spreadsheet;
142 struct zip_reader *zreader;
144 int target_sheet_index;
145 xmlChar *target_sheet_name;
147 /* State data for the meta data */
148 struct state_data msd;
150 /* State data for the reader */
151 struct state_data rsd;
158 struct sheet_detail *sheets;
159 int n_allocated_sheets;
161 struct caseproto *proto;
162 struct dictionary *dict;
163 struct ccase *first_case;
164 bool used_first_case;
167 struct string ods_errs;
169 struct string zip_errs;
173 ods_unref (struct spreadsheet *s)
175 struct ods_reader *r = (struct ods_reader *) s;
177 if (--s->ref_cnt == 0)
181 state_data_destroy (&r->msd);
182 for (i = 0; i < r->n_allocated_sheets; ++i)
184 xmlFree (r->sheets[i].name);
187 dict_destroy (r->dict);
189 zip_reader_destroy (r->zreader);
199 reading_target_sheet (const struct ods_reader *r, const struct state_data *msd)
201 if (r->target_sheet_name != NULL)
203 if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name))
207 if (r->target_sheet_index == msd->current_sheet + 1)
214 static void process_node (struct ods_reader *or, struct state_data *r);
218 ods_get_sheet_name (struct spreadsheet *s, int n)
220 struct ods_reader *r = (struct ods_reader *) s;
221 struct state_data *or = &r->msd;
223 assert (n < s->n_sheets);
226 (r->n_allocated_sheets <= n)
227 || or->state != STATE_SPREADSHEET
230 int ret = xmlTextReaderRead (or->xtr);
234 process_node (r, or);
237 return r->sheets[n].name;
241 ods_get_sheet_range (struct spreadsheet *s, int n)
243 struct ods_reader *r = (struct ods_reader *) s;
244 struct state_data *or = &r->msd;
246 assert (n < s->n_sheets);
249 (r->n_allocated_sheets <= n)
250 || (r->sheets[n].stop_row == -1)
251 || or->state != STATE_SPREADSHEET
254 int ret = xmlTextReaderRead (or->xtr);
258 process_node (r, or);
261 return create_cell_range (
262 r->sheets[n].start_col,
263 r->sheets[n].start_row,
264 r->sheets[n].stop_col,
265 r->sheets[n].stop_row);
270 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
272 struct ods_reader *r = r_;
276 state_data_destroy (&r->rsd);
278 if ( ! ds_is_empty (&r->ods_errs))
279 msg (ME, "%s", ds_cstr (&r->ods_errs));
281 ds_destroy (&r->ods_errs);
283 if ( r->first_case && ! r->used_first_case )
284 case_unref (r->first_case);
287 caseproto_unref (r->proto);
290 xmlFree (r->target_sheet_name);
291 r->target_sheet_name = NULL;
294 ods_unref (&r->spreadsheet);
302 process_node (struct ods_reader *or, struct state_data *r)
304 xmlChar *name = xmlTextReaderName (r->xtr);
306 name = xmlStrdup (_xml ("--"));
309 r->node_type = xmlTextReaderNodeType (r->xtr);
314 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
315 XML_READER_TYPE_ELEMENT == r->node_type)
317 r->state = STATE_SPREADSHEET;
318 r->current_sheet = -1;
319 r->current_sheet_name = NULL;
322 case STATE_SPREADSHEET:
323 if (0 == xmlStrcasecmp (name, _xml("table:table"))
325 (XML_READER_TYPE_ELEMENT == r->node_type))
327 xmlFree (r->current_sheet_name);
328 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
332 if (r->current_sheet >= or->n_allocated_sheets)
334 assert (r->current_sheet == or->n_allocated_sheets);
335 or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
336 or->sheets[or->n_allocated_sheets - 1].start_col = -1;
337 or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
338 or->sheets[or->n_allocated_sheets - 1].start_row = -1;
339 or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
340 or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
346 r->state = STATE_TABLE;
348 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
349 XML_READER_TYPE_ELEMENT == r->node_type)
351 r->state = STATE_INIT;
355 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
356 (XML_READER_TYPE_ELEMENT == r->node_type))
359 xmlTextReaderGetAttribute (r->xtr,
360 _xml ("table:number-rows-repeated"));
362 int row_span = value ? _xmlchar_to_int (value) : 1;
367 if (! xmlTextReaderIsEmptyElement (r->xtr))
368 r->state = STATE_ROW;
372 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
373 (XML_READER_TYPE_END_ELEMENT == r->node_type))
375 r->state = STATE_SPREADSHEET;
379 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
381 (XML_READER_TYPE_ELEMENT == r->node_type))
384 xmlTextReaderGetAttribute (r->xtr,
385 _xml ("table:number-columns-repeated"));
387 r->col_span = value ? _xmlchar_to_int (value) : 1;
388 r->col += r->col_span;
390 if (! xmlTextReaderIsEmptyElement (r->xtr))
391 r->state = STATE_CELL;
395 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
397 (XML_READER_TYPE_END_ELEMENT == r->node_type))
399 r->state = STATE_TABLE;
403 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
405 ( XML_READER_TYPE_ELEMENT == r->node_type))
407 if (! xmlTextReaderIsEmptyElement (r->xtr))
408 r->state = STATE_CELL_CONTENT;
411 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
413 (XML_READER_TYPE_END_ELEMENT == r->node_type)
416 r->state = STATE_ROW;
419 case STATE_CELL_CONTENT:
420 assert (r->current_sheet >= 0);
421 assert (r->current_sheet < or->n_allocated_sheets);
423 if (or->sheets[r->current_sheet].start_row == -1)
424 or->sheets[r->current_sheet].start_row = r->row - 1;
427 (or->sheets[r->current_sheet].start_col == -1)
429 (or->sheets[r->current_sheet].start_col >= r->col - 1)
431 or->sheets[r->current_sheet].start_col = r->col - 1;
433 or->sheets[r->current_sheet].stop_row = r->row - 1;
435 if ( or->sheets[r->current_sheet].stop_col < r->col - 1)
436 or->sheets[r->current_sheet].stop_col = r->col - 1;
438 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
439 r->state = STATE_CELL;
450 A struct containing the parameters of a cell's value
463 struct xml_value firstval;
467 /* Determine the width that a xmv should probably have */
469 xmv_to_width (const struct xml_value *xmv, int fallback)
471 int width = SPREADSHEET_DEFAULT_WIDTH;
473 /* Non-strings always have zero width */
474 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
481 width = ROUND_UP (xmlStrlen (xmv->value),
482 SPREADSHEET_DEFAULT_WIDTH);
484 width = ROUND_UP (xmlStrlen (xmv->text),
485 SPREADSHEET_DEFAULT_WIDTH);
491 Sets the VAR of case C, to the value corresponding to the xml data
494 convert_xml_to_value (struct ccase *c, const struct variable *var,
495 const struct xml_value *xmv, int col, int row)
497 union value *v = case_data_rw (c, var);
499 if (xmv->value == NULL && xmv->text == NULL)
500 value_set_missing (v, var_get_width (var));
501 else if ( var_is_alpha (var))
502 /* Use the text field, because it seems that there is no
503 value field for strings */
504 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
507 const struct fmt_spec *fmt = var_get_write_format (var);
508 enum fmt_category fc = fmt_get_category (fmt->type);
510 assert ( fc != FMT_CAT_STRING);
512 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
514 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
518 const char *text = xmv->value ?
519 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
521 char *m = data_in (ss_cstr (text), "UTF-8",
529 char buf [FMT_STRING_LEN_MAX + 1];
530 char *cell = create_cell_ref (col, row);
532 msg (MW, _("Cannot convert the value in the spreadsheet cell %s to format (%s): %s"),
533 cell, fmt_to_string (fmt, buf), m);
542 /* Try to find out how many sheets there are in the "workbook" */
544 get_sheet_count (struct zip_reader *zreader)
546 xmlTextReaderPtr mxtr;
547 struct zip_member *meta = NULL;
548 meta = zip_member_open (zreader, "meta.xml");
553 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
554 (xmlInputCloseCallback) NULL,
555 meta, NULL, NULL, 0);
557 while (1 == xmlTextReaderRead (mxtr))
559 xmlChar *name = xmlTextReaderName (mxtr);
560 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
562 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
566 int s = _xmlchar_to_int (attr);
567 xmlFreeTextReader (mxtr);
577 xmlFreeTextReader (mxtr);
582 ods_error_handler (void *ctx, const char *mesg,
583 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
585 struct ods_reader *r = ctx;
587 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
589 r->spreadsheet.file_name,
590 xmlTextReaderLocatorLineNumber (loc),
595 static xmlTextReaderPtr
596 init_reader (struct ods_reader *r, bool report_errors)
598 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
599 xmlTextReaderPtr xtr;
601 if ( content == NULL)
604 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
605 (xmlInputCloseCallback) NULL,
607 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
613 r->spreadsheet.type = SPREADSHEET_ODS;
616 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
624 ods_probe (const char *filename, bool report_errors)
627 struct ods_reader *r = xzalloc (sizeof *r);
628 xmlTextReaderPtr xtr;
629 struct zip_reader *zr;
631 ds_init_empty (&r->zip_errs);
633 zr = zip_reader_create (filename, &r->zip_errs);
639 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
640 filename, ds_cstr (&r->zip_errs));
642 ds_destroy (&r->zip_errs);
647 sheet_count = get_sheet_count (zr);
650 r->spreadsheet.ref_cnt = 1;
652 xtr = init_reader (r, report_errors);
660 r->msd.current_sheet = 0;
661 r->msd.state = STATE_INIT;
664 r->spreadsheet.n_sheets = sheet_count;
665 r->n_allocated_sheets = 0;
668 r->spreadsheet.file_name = strdup (filename);
669 return &r->spreadsheet;
672 ds_destroy (&r->zip_errs);
673 zip_reader_destroy (r->zreader);
679 ods_make_reader (struct spreadsheet *spreadsheet,
680 const struct spreadsheet_read_options *opts)
683 xmlChar *type = NULL;
684 unsigned long int vstart = 0;
685 casenumber n_cases = CASENUMBER_MAX;
687 struct var_spec *var_spec = NULL;
689 xmlTextReaderPtr xtr;
691 struct ods_reader *r = (struct ods_reader *) spreadsheet;
692 xmlChar *val_string = NULL;
695 r->read_names = opts->read_names;
696 ds_init_empty (&r->ods_errs);
697 ++r->spreadsheet.ref_cnt;
699 xtr = init_reader (r, true);
706 r->rsd.current_sheet = 0;
707 r->rsd.state = STATE_INIT;
709 r->used_first_case = false;
710 r->first_case = NULL;
712 if (opts->cell_range)
714 if ( ! convert_cell_ref (opts->cell_range,
715 &r->start_col, &r->start_row,
716 &r->stop_col, &r->stop_row))
718 msg (SE, _("Invalid cell range `%s'"),
731 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
732 r->target_sheet_index = opts->sheet_index;
734 /* Advance to the start of the cells for the target sheet */
735 while ( ! reading_target_sheet (r, &r->rsd)
736 || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row )
738 if (1 != (ret = xmlTextReaderRead (r->rsd.xtr)))
741 process_node (r, &r->rsd);
746 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
747 spreadsheet->file_name);
751 if ( opts->read_names)
753 while (1 == xmlTextReaderRead (r->rsd.xtr))
757 process_node (r, &r->rsd);
759 /* If the row is finished then stop for now */
760 if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row)
763 idx = r->rsd.col - r->start_col -1 ;
768 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
771 if (r->rsd.state == STATE_CELL_CONTENT
773 XML_READER_TYPE_TEXT == r->rsd.node_type)
775 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
777 if ( idx >= n_var_specs)
779 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
781 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
782 memset (var_spec + n_var_specs,
784 (idx - n_var_specs + 1) * sizeof (*var_spec));
785 n_var_specs = idx + 1;
787 var_spec[idx].firstval.text = 0;
788 var_spec[idx].firstval.value = 0;
789 var_spec[idx].firstval.type = 0;
791 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
798 /* Read in the first row of data */
799 while (1 == xmlTextReaderRead (r->rsd.xtr))
802 process_node (r, &r->rsd);
804 if ( ! reading_target_sheet (r, &r->rsd) )
807 /* If the row is finished then stop for now */
808 if (r->rsd.state == STATE_TABLE &&
809 r->rsd.row > r->start_row + (opts->read_names ? 1 : 0))
812 idx = r->rsd.col - r->start_col - 1;
816 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
819 if ( r->rsd.state == STATE_CELL &&
820 XML_READER_TYPE_ELEMENT == r->rsd.node_type)
822 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
823 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
826 if ( r->rsd.state == STATE_CELL_CONTENT &&
827 XML_READER_TYPE_TEXT == r->rsd.node_type)
829 if (idx >= n_var_specs)
831 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
832 memset (var_spec + n_var_specs,
834 (idx - n_var_specs + 1) * sizeof (*var_spec));
836 var_spec [idx].name = NULL;
837 n_var_specs = idx + 1;
840 var_spec [idx].firstval.type = type;
841 var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr);
842 var_spec [idx].firstval.value = val_string;
850 /* Create the dictionary and populate it */
851 r->spreadsheet.dict = r->dict = dict_create (
852 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr)));
854 for (i = 0; i < n_var_specs ; ++i )
857 struct variable *var = NULL;
858 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
859 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
860 dict_create_var (r->dict, name, width);
863 var = dict_get_var (r->dict, i);
865 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
872 fmt = fmt_default_for_width (width);
874 var_set_both_formats (var, &fmt);
877 if ( n_var_specs == 0 )
879 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
880 spreadsheet->file_name);
884 /* Create the first case, and cache it */
885 r->proto = caseproto_ref (dict_get_proto (r->dict));
886 r->first_case = case_create (r->proto);
887 case_set_missing (r->first_case);
889 for (i = 0 ; i < n_var_specs; ++i)
891 const struct variable *var = dict_get_var (r->dict, i);
893 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval,
894 r->rsd.col - n_var_specs + i,
898 /* Read in the first row of data */
899 while (1 == xmlTextReaderRead (r->rsd.xtr))
901 process_node (r, &r->rsd);
903 if (r->rsd.state == STATE_ROW)
908 for ( i = 0 ; i < n_var_specs ; ++i )
910 free (var_spec[i].firstval.type);
911 free (var_spec[i].firstval.value);
912 free (var_spec[i].firstval.text);
913 free (var_spec[i].name);
919 return casereader_create_sequential
923 &ods_file_casereader_class, r);
927 for ( i = 0 ; i < n_var_specs ; ++i )
929 free (var_spec[i].firstval.type);
930 free (var_spec[i].firstval.value);
931 free (var_spec[i].firstval.text);
932 free (var_spec[i].name);
937 ods_file_casereader_destroy (NULL, r);
943 /* Reads and returns one case from READER's file. Returns a null
944 pointer on failure. */
945 static struct ccase *
946 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
948 struct ccase *c = NULL;
949 struct ods_reader *r = r_;
951 xmlChar *val_string = NULL;
952 xmlChar *type = NULL;
954 if (!r->used_first_case)
956 r->used_first_case = true;
957 return r->first_case;
961 /* Advance to the start of a row. (If there is one) */
962 while (r->rsd.state != STATE_ROW
963 && 1 == xmlTextReaderRead (r->rsd.xtr)
966 process_node (r, &r->rsd);
970 if ( ! reading_target_sheet (r, &r->rsd)
971 || r->rsd.state < STATE_TABLE
972 || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
978 c = case_create (r->proto);
979 case_set_missing (c);
981 while (1 == xmlTextReaderRead (r->rsd.xtr))
983 process_node (r, &r->rsd);
985 if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
988 if (r->rsd.state == STATE_CELL &&
989 r->rsd.node_type == XML_READER_TYPE_ELEMENT)
991 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
992 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
995 if (r->rsd.state == STATE_CELL_CONTENT &&
996 r->rsd.node_type == XML_READER_TYPE_TEXT)
999 struct xml_value *xmv = xzalloc (sizeof *xmv);
1000 xmv->text = xmlTextReaderValue (r->rsd.xtr);
1001 xmv->value = val_string;
1006 for (col = 0; col < r->rsd.col_span; ++col)
1008 const struct variable *var;
1009 const int idx = r->rsd.col - col - r->start_col - 1;
1012 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
1014 if (idx >= dict_get_var_cnt (r->dict))
1017 var = dict_get_var (r->dict, idx);
1018 convert_xml_to_value (c, var, xmv, idx + r->start_col, r->rsd.row - 1);
1021 xmlFree (xmv->text);
1022 xmlFree (xmv->value);
1023 xmlFree (xmv->type);
1026 if ( r->rsd.state <= STATE_TABLE)
1031 xmlFree (val_string);