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/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");
48 #include "libpspp/zip-reader.h"
54 #include <libxml/xmlreader.h>
57 #include "data/format.h"
58 #include "data/case.h"
59 #include "data/casereader-provider.h"
60 #include "data/dictionary.h"
61 #include "data/identifier.h"
62 #include "data/value.h"
63 #include "data/variable.h"
64 #include "libpspp/i18n.h"
65 #include "libpspp/str.h"
67 #include "gl/xalloc.h"
69 static void ods_file_casereader_destroy (struct casereader *, void *);
70 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
73 static const struct casereader_class ods_file_casereader_class =
75 ods_file_casereader_read,
76 ods_file_casereader_destroy,
83 /* The name of the sheet (utf8 encoding) */
95 STATE_INIT = 0, /* Initial state */
96 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
97 STATE_TABLE, /* Found the sheet that we actually want */
98 STATE_ROW, /* Found the start of the cell array */
99 STATE_CELL, /* Found a cell */
100 STATE_CELL_CONTENT /* Found a the text within a cell */
105 xmlTextReaderPtr xtr;
107 enum reader_state state;
111 xmlChar *current_sheet_name;
117 state_data_destroy (struct state_data *sd)
119 xmlFree (sd->current_sheet_name);
120 sd->current_sheet_name = NULL;
122 xmlFreeTextReader (sd->xtr);
128 struct spreadsheet spreadsheet;
129 struct zip_reader *zreader;
131 int target_sheet_index;
132 xmlChar *target_sheet_name;
134 /* State data for the meta data */
135 struct state_data msd;
137 /* State data for the reader */
138 struct state_data rsd;
145 struct sheet_detail *sheets;
146 int n_allocated_sheets;
148 struct caseproto *proto;
149 struct dictionary *dict;
150 struct ccase *first_case;
151 bool used_first_case;
154 struct string ods_errs;
156 struct string zip_errs;
160 ods_destroy (struct spreadsheet *s)
162 struct ods_reader *r = (struct ods_reader *) s;
164 if (--r->ref_cnt == 0)
168 state_data_destroy (&r->msd);
169 for (i = 0; i < r->n_allocated_sheets; ++i)
171 xmlFree (r->sheets[i].name);
174 zip_reader_destroy (r->zreader);
184 reading_target_sheet (const struct ods_reader *r, const struct state_data *msd)
186 if (r->target_sheet_name != NULL)
188 if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name))
192 if (r->target_sheet_index == msd->current_sheet + 1)
199 static void process_node (struct ods_reader *or, struct state_data *r);
203 ods_get_sheet_name (struct spreadsheet *s, int n)
205 struct ods_reader *r = (struct ods_reader *) s;
206 struct state_data *or = &r->msd;
208 assert (n < s->n_sheets);
211 (r->n_allocated_sheets <= n)
212 || or->state != STATE_SPREADSHEET
215 int ret = xmlTextReaderRead (or->xtr);
219 process_node (r, or);
222 return r->sheets[n].name;
226 ods_get_sheet_range (struct spreadsheet *s, int n)
228 struct ods_reader *r = (struct ods_reader *) s;
229 struct state_data *or = &r->msd;
231 assert (n < s->n_sheets);
234 (r->n_allocated_sheets <= n)
235 || (r->sheets[n].stop_row == -1)
236 || or->state != STATE_SPREADSHEET
239 int ret = xmlTextReaderRead (or->xtr);
243 process_node (r, or);
246 return create_cell_ref (
247 r->sheets[n].start_col,
248 r->sheets[n].start_row,
249 r->sheets[n].stop_col,
250 r->sheets[n].stop_row);
255 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
257 struct ods_reader *r = r_;
261 state_data_destroy (&r->rsd);
263 if ( ! ds_is_empty (&r->ods_errs))
264 msg (ME, "%s", ds_cstr (&r->ods_errs));
266 ds_destroy (&r->ods_errs);
268 if ( r->first_case && ! r->used_first_case )
269 case_unref (r->first_case);
272 caseproto_unref (r->proto);
275 xmlFree (r->target_sheet_name);
276 r->target_sheet_name = NULL;
279 ods_destroy (&r->spreadsheet);
287 process_node (struct ods_reader *or, struct state_data *r)
289 xmlChar *name = xmlTextReaderName (r->xtr);
291 name = xmlStrdup (_xml ("--"));
294 r->node_type = xmlTextReaderNodeType (r->xtr);
299 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
300 XML_READER_TYPE_ELEMENT == r->node_type)
302 r->state = STATE_SPREADSHEET;
303 r->current_sheet = -1;
304 r->current_sheet_name = NULL;
307 case STATE_SPREADSHEET:
308 if (0 == xmlStrcasecmp (name, _xml("table:table"))
310 (XML_READER_TYPE_ELEMENT == r->node_type))
312 xmlFree (r->current_sheet_name);
313 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
317 if (r->current_sheet >= or->n_allocated_sheets)
319 assert (r->current_sheet == or->n_allocated_sheets);
320 or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
321 or->sheets[or->n_allocated_sheets - 1].start_col = -1;
322 or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
323 or->sheets[or->n_allocated_sheets - 1].start_row = -1;
324 or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
325 or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
331 r->state = STATE_TABLE;
333 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
334 XML_READER_TYPE_ELEMENT == r->node_type)
336 r->state = STATE_INIT;
340 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
341 (XML_READER_TYPE_ELEMENT == r->node_type))
344 xmlTextReaderGetAttribute (r->xtr,
345 _xml ("table:number-rows-repeated"));
347 int row_span = value ? _xmlchar_to_int (value) : 1;
352 if (! xmlTextReaderIsEmptyElement (r->xtr))
353 r->state = STATE_ROW;
357 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
358 (XML_READER_TYPE_END_ELEMENT == r->node_type))
360 r->state = STATE_SPREADSHEET;
364 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
366 (XML_READER_TYPE_ELEMENT == r->node_type))
369 xmlTextReaderGetAttribute (r->xtr,
370 _xml ("table:number-columns-repeated"));
372 r->col_span = value ? _xmlchar_to_int (value) : 1;
373 r->col += r->col_span;
375 if (! xmlTextReaderIsEmptyElement (r->xtr))
376 r->state = STATE_CELL;
380 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
382 (XML_READER_TYPE_END_ELEMENT == r->node_type))
384 r->state = STATE_TABLE;
388 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
390 ( XML_READER_TYPE_ELEMENT == r->node_type))
392 if (! xmlTextReaderIsEmptyElement (r->xtr))
393 r->state = STATE_CELL_CONTENT;
396 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
398 (XML_READER_TYPE_END_ELEMENT == r->node_type)
401 r->state = STATE_ROW;
404 case STATE_CELL_CONTENT:
405 assert (r->current_sheet >= 0);
406 assert (r->current_sheet < or->n_allocated_sheets);
408 if (or->sheets[r->current_sheet].start_row == -1)
409 or->sheets[r->current_sheet].start_row = r->row - 1;
412 (or->sheets[r->current_sheet].start_col == -1)
414 (or->sheets[r->current_sheet].start_col >= r->col - 1)
416 or->sheets[r->current_sheet].start_col = r->col - 1;
418 or->sheets[r->current_sheet].stop_row = r->row - 1;
420 if ( or->sheets[r->current_sheet].stop_col < r->col - 1)
421 or->sheets[r->current_sheet].stop_col = r->col - 1;
423 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
424 r->state = STATE_CELL;
435 A struct containing the parameters of a cell's value
448 struct xml_value firstval;
452 /* Determine the width that a xmv should probably have */
454 xmv_to_width (const struct xml_value *xmv, int fallback)
456 int width = SPREADSHEET_DEFAULT_WIDTH;
458 /* Non-strings always have zero width */
459 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
466 width = ROUND_UP (xmlStrlen (xmv->value),
467 SPREADSHEET_DEFAULT_WIDTH);
469 width = ROUND_UP (xmlStrlen (xmv->text),
470 SPREADSHEET_DEFAULT_WIDTH);
476 Sets the VAR of case C, to the value corresponding to the xml data
479 convert_xml_to_value (struct ccase *c, const struct variable *var,
480 const struct xml_value *xmv)
482 union value *v = case_data_rw (c, var);
484 if (xmv->value == NULL && xmv->text == NULL)
485 value_set_missing (v, var_get_width (var));
486 else if ( var_is_alpha (var))
487 /* Use the text field, because it seems that there is no
488 value field for strings */
489 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
492 const struct fmt_spec *fmt = var_get_write_format (var);
493 enum fmt_category fc = fmt_get_category (fmt->type);
495 assert ( fc != FMT_CAT_STRING);
497 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
499 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
503 const char *text = xmv->value ?
504 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
507 free (data_in (ss_cstr (text), "UTF-8",
517 /* Try to find out how many sheets there are in the "workbook" */
519 get_sheet_count (struct zip_reader *zreader)
521 xmlTextReaderPtr mxtr;
522 struct zip_member *meta = NULL;
523 meta = zip_member_open (zreader, "meta.xml");
528 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
529 (xmlInputCloseCallback) NULL,
530 meta, NULL, NULL, 0);
532 while (1 == xmlTextReaderRead (mxtr))
534 xmlChar *name = xmlTextReaderName (mxtr);
535 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
537 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
541 int s = _xmlchar_to_int (attr);
542 xmlFreeTextReader (mxtr);
552 xmlFreeTextReader (mxtr);
557 ods_error_handler (void *ctx, const char *mesg,
558 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
560 struct ods_reader *r = ctx;
562 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
564 r->spreadsheet.file_name,
565 xmlTextReaderLocatorLineNumber (loc),
570 static xmlTextReaderPtr
571 init_reader (struct ods_reader *r, bool report_errors)
573 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
574 xmlTextReaderPtr xtr;
576 if ( content == NULL)
579 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
580 (xmlInputCloseCallback) NULL,
582 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
588 r->spreadsheet.type = SPREADSHEET_ODS;
591 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
599 ods_probe (const char *filename, bool report_errors)
602 struct ods_reader *r = xzalloc (sizeof *r);
603 xmlTextReaderPtr xtr;
604 struct zip_reader *zr;
606 ds_init_empty (&r->zip_errs);
608 zr = zip_reader_create (filename, &r->zip_errs);
614 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
615 filename, ds_cstr (&r->zip_errs));
617 ds_destroy (&r->zip_errs);
622 sheet_count = get_sheet_count (zr);
627 xtr = init_reader (r, report_errors);
635 r->msd.current_sheet = 0;
636 r->msd.state = STATE_INIT;
639 r->spreadsheet.n_sheets = sheet_count;
640 r->n_allocated_sheets = 0;
643 r->spreadsheet.file_name = filename;
644 return &r->spreadsheet;
647 ds_destroy (&r->zip_errs);
648 zip_reader_destroy (r->zreader);
654 ods_make_reader (struct spreadsheet *spreadsheet,
655 const struct spreadsheet_read_options *opts)
658 xmlChar *type = NULL;
659 unsigned long int vstart = 0;
660 casenumber n_cases = CASENUMBER_MAX;
662 struct var_spec *var_spec = NULL;
664 xmlTextReaderPtr xtr;
666 struct ods_reader *r = (struct ods_reader *) spreadsheet;
667 xmlChar *val_string = NULL;
670 r->read_names = opts->read_names;
671 ds_init_empty (&r->ods_errs);
674 xtr = init_reader (r, true);
681 r->rsd.current_sheet = 0;
682 r->rsd.state = STATE_INIT;
684 r->used_first_case = false;
685 r->first_case = NULL;
687 if (opts->cell_range)
689 if ( ! convert_cell_ref (opts->cell_range,
690 &r->start_col, &r->start_row,
691 &r->stop_col, &r->stop_row))
693 msg (SE, _("Invalid cell range `%s'"),
706 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
707 r->target_sheet_index = opts->sheet_index;
709 /* Advance to the start of the cells for the target sheet */
710 while ( ! reading_target_sheet (r, &r->rsd)
711 || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row )
713 if (1 != (ret = xmlTextReaderRead (r->rsd.xtr)))
716 process_node (r, &r->rsd);
721 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
722 spreadsheet->file_name);
726 if ( opts->read_names)
728 while (1 == xmlTextReaderRead (r->rsd.xtr))
732 process_node (r, &r->rsd);
734 /* If the row is finished then stop for now */
735 if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row)
738 idx = r->rsd.col - r->start_col -1 ;
743 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
746 if (r->rsd.state == STATE_CELL_CONTENT
748 XML_READER_TYPE_TEXT == r->rsd.node_type)
750 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
752 if ( idx >= n_var_specs)
754 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
756 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
757 memset (var_spec + n_var_specs,
759 (idx - n_var_specs + 1) * sizeof (*var_spec));
760 n_var_specs = idx + 1;
762 var_spec[idx].firstval.text = 0;
763 var_spec[idx].firstval.value = 0;
764 var_spec[idx].firstval.type = 0;
766 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
773 /* Read in the first row of data */
774 while (1 == xmlTextReaderRead (r->rsd.xtr))
777 process_node (r, &r->rsd);
779 if ( ! reading_target_sheet (r, &r->rsd) )
782 /* If the row is finished then stop for now */
783 if (r->rsd.state == STATE_TABLE &&
784 r->rsd.row > r->start_row + (opts->read_names ? 1 : 0))
787 idx = r->rsd.col - r->start_col - 1;
791 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
794 if ( r->rsd.state == STATE_CELL &&
795 XML_READER_TYPE_ELEMENT == r->rsd.node_type)
797 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
798 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
801 if ( r->rsd.state == STATE_CELL_CONTENT &&
802 XML_READER_TYPE_TEXT == r->rsd.node_type)
804 if (idx >= n_var_specs)
806 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
807 memset (var_spec + n_var_specs,
809 (idx - n_var_specs + 1) * sizeof (*var_spec));
811 var_spec [idx].name = NULL;
812 n_var_specs = idx + 1;
815 var_spec [idx].firstval.type = type;
816 var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr);
817 var_spec [idx].firstval.value = val_string;
825 /* Create the dictionary and populate it */
826 r->spreadsheet.dict = r->dict = dict_create (
827 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr)));
829 for (i = 0; i < n_var_specs ; ++i )
832 struct variable *var = NULL;
833 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
834 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
835 dict_create_var (r->dict, name, width);
838 var = dict_get_var (r->dict, i);
840 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
847 fmt = fmt_default_for_width (width);
849 var_set_both_formats (var, &fmt);
852 if ( n_var_specs == 0 )
854 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
855 spreadsheet->file_name);
859 /* Create the first case, and cache it */
860 r->proto = caseproto_ref (dict_get_proto (r->dict));
861 r->first_case = case_create (r->proto);
862 case_set_missing (r->first_case);
864 for (i = 0 ; i < n_var_specs; ++i)
866 const struct variable *var = dict_get_var (r->dict, i);
868 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
871 /* Read in the first row of data */
872 while (1 == xmlTextReaderRead (r->rsd.xtr))
874 process_node (r, &r->rsd);
876 if (r->rsd.state == STATE_ROW)
881 for ( i = 0 ; i < n_var_specs ; ++i )
883 free (var_spec[i].firstval.type);
884 free (var_spec[i].firstval.value);
885 free (var_spec[i].firstval.text);
886 free (var_spec[i].name);
892 return casereader_create_sequential
896 &ods_file_casereader_class, r);
900 for ( i = 0 ; i < n_var_specs ; ++i )
902 free (var_spec[i].firstval.type);
903 free (var_spec[i].firstval.value);
904 free (var_spec[i].firstval.text);
905 free (var_spec[i].name);
910 dict_destroy (r->spreadsheet.dict);
911 r->spreadsheet.dict = NULL;
912 ods_file_casereader_destroy (NULL, r);
918 /* Reads and returns one case from READER's file. Returns a null
919 pointer on failure. */
920 static struct ccase *
921 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
923 struct ccase *c = NULL;
924 struct ods_reader *r = r_;
926 xmlChar *val_string = NULL;
927 xmlChar *type = NULL;
929 if (!r->used_first_case)
931 r->used_first_case = true;
932 return r->first_case;
936 /* Advance to the start of a row. (If there is one) */
937 while (r->rsd.state != STATE_ROW
938 && 1 == xmlTextReaderRead (r->rsd.xtr)
941 process_node (r, &r->rsd);
945 if ( ! reading_target_sheet (r, &r->rsd)
946 || r->rsd.state < STATE_TABLE
947 || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
953 c = case_create (r->proto);
954 case_set_missing (c);
956 while (1 == xmlTextReaderRead (r->rsd.xtr))
958 process_node (r, &r->rsd);
960 if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
963 if (r->rsd.state == STATE_CELL &&
964 r->rsd.node_type == XML_READER_TYPE_ELEMENT)
966 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
967 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
970 if (r->rsd.state == STATE_CELL_CONTENT &&
971 r->rsd.node_type == XML_READER_TYPE_TEXT)
974 struct xml_value *xmv = xzalloc (sizeof *xmv);
975 xmv->text = xmlTextReaderValue (r->rsd.xtr);
976 xmv->value = val_string;
980 for (col = 0; col < r->rsd.col_span; ++col)
982 const struct variable *var;
983 const int idx = r->rsd.col - col - r->start_col - 1;
986 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
988 if (idx >= dict_get_var_cnt (r->dict))
991 var = dict_get_var (r->dict, idx);
992 convert_xml_to_value (c, var, xmv);
996 xmlFree (xmv->value);
1000 if ( r->rsd.state <= STATE_TABLE)