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;
158 ods_destroy (struct spreadsheet *s)
160 struct ods_reader *r = (struct ods_reader *) s;
162 if (--r->ref_cnt == 0)
166 state_data_destroy (&r->msd);
167 for (i = 0; i < r->n_allocated_sheets; ++i)
169 xmlFree (r->sheets[i].name);
172 zip_reader_destroy (r->zreader);
182 reading_target_sheet (const struct ods_reader *r, const struct state_data *msd)
184 if (r->target_sheet_name != NULL)
186 if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name))
190 if (r->target_sheet_index == msd->current_sheet + 1)
197 static void process_node (struct ods_reader *or, struct state_data *r);
201 ods_get_sheet_name (struct spreadsheet *s, int n)
203 struct ods_reader *r = (struct ods_reader *) s;
204 struct state_data *or = &r->msd;
206 assert (n < s->n_sheets);
209 (r->n_allocated_sheets <= n)
210 || or->state != STATE_SPREADSHEET
213 int ret = xmlTextReaderRead (or->xtr);
217 process_node (r, or);
220 return r->sheets[n].name;
224 ods_get_sheet_range (struct spreadsheet *s, int n)
226 struct ods_reader *r = (struct ods_reader *) s;
227 struct state_data *or = &r->msd;
229 assert (n < s->n_sheets);
232 (r->n_allocated_sheets <= n)
233 || (r->sheets[n].stop_row == -1)
234 || or->state != STATE_SPREADSHEET
237 int ret = xmlTextReaderRead (or->xtr);
241 process_node (r, or);
244 return create_cell_ref (
245 r->sheets[n].start_col,
246 r->sheets[n].start_row,
247 r->sheets[n].stop_col,
248 r->sheets[n].stop_row);
253 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
255 struct ods_reader *r = r_;
259 state_data_destroy (&r->rsd);
261 if ( ! ds_is_empty (&r->ods_errs))
262 msg (ME, "%s", ds_cstr (&r->ods_errs));
264 ds_destroy (&r->ods_errs);
266 if ( r->first_case && ! r->used_first_case )
267 case_unref (r->first_case);
270 caseproto_unref (r->proto);
273 xmlFree (r->target_sheet_name);
274 r->target_sheet_name = NULL;
277 ods_destroy (&r->spreadsheet);
285 process_node (struct ods_reader *or, struct state_data *r)
287 xmlChar *name = xmlTextReaderName (r->xtr);
289 name = xmlStrdup (_xml ("--"));
292 r->node_type = xmlTextReaderNodeType (r->xtr);
297 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
298 XML_READER_TYPE_ELEMENT == r->node_type)
300 r->state = STATE_SPREADSHEET;
301 r->current_sheet = -1;
302 r->current_sheet_name = NULL;
305 case STATE_SPREADSHEET:
306 if (0 == xmlStrcasecmp (name, _xml("table:table"))
308 (XML_READER_TYPE_ELEMENT == r->node_type))
310 xmlFree (r->current_sheet_name);
311 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
315 if (r->current_sheet >= or->n_allocated_sheets)
317 assert (r->current_sheet == or->n_allocated_sheets);
318 or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
319 or->sheets[or->n_allocated_sheets - 1].start_col = -1;
320 or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
321 or->sheets[or->n_allocated_sheets - 1].start_row = -1;
322 or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
323 or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
329 r->state = STATE_TABLE;
331 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
332 XML_READER_TYPE_ELEMENT == r->node_type)
334 r->state = STATE_INIT;
338 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
339 (XML_READER_TYPE_ELEMENT == r->node_type))
342 xmlTextReaderGetAttribute (r->xtr,
343 _xml ("table:number-rows-repeated"));
345 int row_span = value ? _xmlchar_to_int (value) : 1;
350 if (! xmlTextReaderIsEmptyElement (r->xtr))
351 r->state = STATE_ROW;
355 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
356 (XML_READER_TYPE_END_ELEMENT == r->node_type))
358 r->state = STATE_SPREADSHEET;
362 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
364 (XML_READER_TYPE_ELEMENT == r->node_type))
367 xmlTextReaderGetAttribute (r->xtr,
368 _xml ("table:number-columns-repeated"));
370 r->col_span = value ? _xmlchar_to_int (value) : 1;
371 r->col += r->col_span;
373 if (! xmlTextReaderIsEmptyElement (r->xtr))
374 r->state = STATE_CELL;
378 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
380 (XML_READER_TYPE_END_ELEMENT == r->node_type))
382 r->state = STATE_TABLE;
386 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
388 ( XML_READER_TYPE_ELEMENT == r->node_type))
390 if (! xmlTextReaderIsEmptyElement (r->xtr))
391 r->state = STATE_CELL_CONTENT;
394 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
396 (XML_READER_TYPE_END_ELEMENT == r->node_type)
399 r->state = STATE_ROW;
402 case STATE_CELL_CONTENT:
403 assert (r->current_sheet >= 0);
404 assert (r->current_sheet < or->n_allocated_sheets);
406 if (or->sheets[r->current_sheet].start_row == -1)
407 or->sheets[r->current_sheet].start_row = r->row - 1;
410 (or->sheets[r->current_sheet].start_col == -1)
412 (or->sheets[r->current_sheet].start_col >= r->col - 1)
414 or->sheets[r->current_sheet].start_col = r->col - 1;
416 or->sheets[r->current_sheet].stop_row = r->row - 1;
418 if ( or->sheets[r->current_sheet].stop_col < r->col - 1)
419 or->sheets[r->current_sheet].stop_col = r->col - 1;
421 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
422 r->state = STATE_CELL;
433 A struct containing the parameters of a cell's value
446 struct xml_value firstval;
450 /* Determine the width that a xmv should probably have */
452 xmv_to_width (const struct xml_value *xmv, int fallback)
454 int width = SPREADSHEET_DEFAULT_WIDTH;
456 /* Non-strings always have zero width */
457 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
464 width = ROUND_UP (xmlStrlen (xmv->value),
465 SPREADSHEET_DEFAULT_WIDTH);
467 width = ROUND_UP (xmlStrlen (xmv->text),
468 SPREADSHEET_DEFAULT_WIDTH);
474 Sets the VAR of case C, to the value corresponding to the xml data
477 convert_xml_to_value (struct ccase *c, const struct variable *var,
478 const struct xml_value *xmv)
480 union value *v = case_data_rw (c, var);
482 if (xmv->value == NULL && xmv->text == NULL)
483 value_set_missing (v, var_get_width (var));
484 else if ( var_is_alpha (var))
485 /* Use the text field, because it seems that there is no
486 value field for strings */
487 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
490 const struct fmt_spec *fmt = var_get_write_format (var);
491 enum fmt_category fc = fmt_get_category (fmt->type);
493 assert ( fc != FMT_CAT_STRING);
495 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
497 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
501 const char *text = xmv->value ?
502 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
505 free (data_in (ss_cstr (text), "UTF-8",
515 /* Try to find out how many sheets there are in the "workbook" */
517 get_sheet_count (struct zip_reader *zreader)
519 xmlTextReaderPtr mxtr;
520 struct zip_member *meta = NULL;
521 meta = zip_member_open (zreader, "meta.xml");
526 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
527 (xmlInputCloseCallback) NULL,
528 meta, NULL, NULL, 0);
530 while (1 == xmlTextReaderRead (mxtr))
532 xmlChar *name = xmlTextReaderName (mxtr);
533 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
535 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
539 int s = _xmlchar_to_int (attr);
540 xmlFreeTextReader (mxtr);
550 xmlFreeTextReader (mxtr);
555 ods_error_handler (void *ctx, const char *mesg,
556 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
558 struct ods_reader *r = ctx;
560 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
562 r->spreadsheet.file_name,
563 xmlTextReaderLocatorLineNumber (loc),
568 static xmlTextReaderPtr
569 init_reader (struct ods_reader *r, bool report_errors)
571 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
572 xmlTextReaderPtr xtr;
574 if ( content == NULL)
577 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
578 (xmlInputCloseCallback) NULL,
580 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
586 r->spreadsheet.type = SPREADSHEET_ODS;
589 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
596 ods_probe (const char *filename, bool report_errors)
598 struct ods_reader *r;
599 struct string errs = DS_EMPTY_INITIALIZER;
601 struct zip_reader *zr = zip_reader_create (filename, &errs);
602 xmlTextReaderPtr xtr;
608 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
609 filename, ds_cstr (&errs));
614 sheet_count = get_sheet_count (zr);
616 r = xzalloc (sizeof *r);
620 xtr = init_reader (r, report_errors);
628 r->msd.current_sheet = 0;
629 r->msd.state = STATE_INIT;
632 r->spreadsheet.n_sheets = sheet_count;
633 r->n_allocated_sheets = 0;
638 r->spreadsheet.file_name = filename;
639 return &r->spreadsheet;
642 zip_reader_destroy (r->zreader);
649 ods_make_reader (struct spreadsheet *spreadsheet,
650 const struct spreadsheet_read_options *opts)
653 xmlChar *type = NULL;
654 unsigned long int vstart = 0;
655 casenumber n_cases = CASENUMBER_MAX;
657 struct var_spec *var_spec = NULL;
659 xmlTextReaderPtr xtr;
661 struct ods_reader *r = (struct ods_reader *) spreadsheet;
662 xmlChar *val_string = NULL;
665 r->read_names = opts->read_names;
666 ds_init_empty (&r->ods_errs);
669 xtr = init_reader (r, true);
676 r->rsd.current_sheet = 0;
677 r->rsd.state = STATE_INIT;
679 r->used_first_case = false;
680 r->first_case = NULL;
682 if (opts->cell_range)
684 if ( ! convert_cell_ref (opts->cell_range,
685 &r->start_col, &r->start_row,
686 &r->stop_col, &r->stop_row))
688 msg (SE, _("Invalid cell range `%s'"),
701 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
702 r->target_sheet_index = opts->sheet_index;
704 /* Advance to the start of the cells for the target sheet */
705 while ( ! reading_target_sheet (r, &r->rsd)
706 || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row )
708 if (1 != (ret = xmlTextReaderRead (r->rsd.xtr)))
711 process_node (r, &r->rsd);
716 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
717 spreadsheet->file_name);
721 if ( opts->read_names)
723 while (1 == xmlTextReaderRead (r->rsd.xtr))
727 process_node (r, &r->rsd);
729 /* If the row is finished then stop for now */
730 if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row)
733 idx = r->rsd.col - r->start_col -1 ;
738 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
741 if (r->rsd.state == STATE_CELL_CONTENT
743 XML_READER_TYPE_TEXT == r->rsd.node_type)
745 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
747 if ( idx >= n_var_specs)
749 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
751 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
752 memset (var_spec + n_var_specs,
754 (idx - n_var_specs + 1) * sizeof (*var_spec));
755 n_var_specs = idx + 1;
757 var_spec[idx].firstval.text = 0;
758 var_spec[idx].firstval.value = 0;
759 var_spec[idx].firstval.type = 0;
761 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
768 /* Read in the first row of data */
769 while (1 == xmlTextReaderRead (r->rsd.xtr))
772 process_node (r, &r->rsd);
774 if ( ! reading_target_sheet (r, &r->rsd) )
777 /* If the row is finished then stop for now */
778 if (r->rsd.state == STATE_TABLE &&
779 r->rsd.row > r->start_row + (opts->read_names ? 1 : 0))
782 idx = r->rsd.col - r->start_col - 1;
786 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
789 if ( r->rsd.state == STATE_CELL &&
790 XML_READER_TYPE_ELEMENT == r->rsd.node_type)
792 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
793 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
796 if ( r->rsd.state == STATE_CELL_CONTENT &&
797 XML_READER_TYPE_TEXT == r->rsd.node_type)
799 if (idx >= n_var_specs)
801 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
802 memset (var_spec + n_var_specs,
804 (idx - n_var_specs + 1) * sizeof (*var_spec));
806 var_spec [idx].name = NULL;
807 n_var_specs = idx + 1;
810 var_spec [idx].firstval.type = type;
811 var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr);
812 var_spec [idx].firstval.value = val_string;
820 /* Create the dictionary and populate it */
821 r->spreadsheet.dict = r->dict = dict_create (
822 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr)));
824 for (i = 0; i < n_var_specs ; ++i )
827 struct variable *var = NULL;
828 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
829 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
830 dict_create_var (r->dict, name, width);
833 var = dict_get_var (r->dict, i);
835 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
842 fmt = fmt_default_for_width (width);
844 var_set_both_formats (var, &fmt);
847 if ( n_var_specs == 0 )
849 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
850 spreadsheet->file_name);
854 /* Create the first case, and cache it */
855 r->proto = caseproto_ref (dict_get_proto (r->dict));
856 r->first_case = case_create (r->proto);
857 case_set_missing (r->first_case);
859 for (i = 0 ; i < n_var_specs; ++i)
861 const struct variable *var = dict_get_var (r->dict, i);
863 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
866 /* Read in the first row of data */
867 while (1 == xmlTextReaderRead (r->rsd.xtr))
869 process_node (r, &r->rsd);
871 if (r->rsd.state == STATE_ROW)
876 for ( i = 0 ; i < n_var_specs ; ++i )
878 free (var_spec[i].firstval.type);
879 free (var_spec[i].firstval.value);
880 free (var_spec[i].firstval.text);
881 free (var_spec[i].name);
887 return casereader_create_sequential
891 &ods_file_casereader_class, r);
895 for ( i = 0 ; i < n_var_specs ; ++i )
897 free (var_spec[i].firstval.type);
898 free (var_spec[i].firstval.value);
899 free (var_spec[i].firstval.text);
900 free (var_spec[i].name);
905 dict_destroy (r->spreadsheet.dict);
906 r->spreadsheet.dict = NULL;
907 ods_file_casereader_destroy (NULL, r);
913 /* Reads and returns one case from READER's file. Returns a null
914 pointer on failure. */
915 static struct ccase *
916 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
918 struct ccase *c = NULL;
919 struct ods_reader *r = r_;
921 xmlChar *val_string = NULL;
922 xmlChar *type = NULL;
924 if (!r->used_first_case)
926 r->used_first_case = true;
927 return r->first_case;
931 /* Advance to the start of a row. (If there is one) */
932 while (r->rsd.state != STATE_ROW
933 && 1 == xmlTextReaderRead (r->rsd.xtr)
936 process_node (r, &r->rsd);
940 if ( ! reading_target_sheet (r, &r->rsd)
941 || r->rsd.state < STATE_TABLE
942 || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
948 c = case_create (r->proto);
949 case_set_missing (c);
951 while (1 == xmlTextReaderRead (r->rsd.xtr))
953 process_node (r, &r->rsd);
955 if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
958 if (r->rsd.state == STATE_CELL &&
959 r->rsd.node_type == XML_READER_TYPE_ELEMENT)
961 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
962 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
965 if (r->rsd.state == STATE_CELL_CONTENT &&
966 r->rsd.node_type == XML_READER_TYPE_TEXT)
969 struct xml_value *xmv = xzalloc (sizeof *xmv);
970 xmv->text = xmlTextReaderValue (r->rsd.xtr);
971 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);
991 xmlFree (xmv->value);
995 if ( r->rsd.state <= STATE_TABLE)