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 xmlFreeTextReader (sd->xtr);
125 struct spreadsheet spreadsheet;
126 struct zip_reader *zreader;
128 int target_sheet_index;
129 xmlChar *target_sheet_name;
131 /* State data for the meta data */
132 struct state_data msd;
134 /* State data for the reader */
135 struct state_data rsd;
142 struct sheet_detail *sheets;
143 int n_allocated_sheets;
145 struct caseproto *proto;
146 struct dictionary *dict;
147 struct ccase *first_case;
148 bool used_first_case;
151 struct string ods_errs;
155 ods_destroy (struct spreadsheet *s)
157 struct ods_reader *r = (struct ods_reader *) s;
159 if (--r->ref_cnt == 0)
163 state_data_destroy (&r->msd);
164 for (i = 0; i < r->n_allocated_sheets; ++i)
166 xmlFree (r->sheets[i].name);
169 zip_reader_destroy (r->zreader);
179 reading_target_sheet (const struct ods_reader *r, const struct state_data *msd)
181 if (r->target_sheet_name != NULL)
183 if ( 0 == xmlStrcmp (r->target_sheet_name, msd->current_sheet_name))
187 if (r->target_sheet_index == msd->current_sheet + 1)
194 static void process_node (struct ods_reader *or, struct state_data *r);
198 ods_get_sheet_name (struct spreadsheet *s, int n)
200 struct ods_reader *r = (struct ods_reader *) s;
201 struct state_data *or = &r->msd;
203 assert (n < s->n_sheets);
206 (r->n_allocated_sheets <= n)
207 || or->state != STATE_SPREADSHEET
210 int ret = xmlTextReaderRead (or->xtr);
214 process_node (r, or);
217 return r->sheets[n].name;
221 ods_get_sheet_range (struct spreadsheet *s, int n)
223 struct ods_reader *r = (struct ods_reader *) s;
224 struct state_data *or = &r->msd;
226 assert (n < s->n_sheets);
229 (r->n_allocated_sheets <= n)
230 || (r->sheets[n].stop_row == -1)
231 || or->state != STATE_SPREADSHEET
234 int ret = xmlTextReaderRead (or->xtr);
238 process_node (r, or);
241 return create_cell_ref (
242 r->sheets[n].start_col,
243 r->sheets[n].start_row,
244 r->sheets[n].stop_col,
245 r->sheets[n].stop_row);
250 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
252 struct ods_reader *r = r_;
256 state_data_destroy (&r->rsd);
258 if ( ! ds_is_empty (&r->ods_errs))
259 msg (ME, "%s", ds_cstr (&r->ods_errs));
261 ds_destroy (&r->ods_errs);
263 if ( ! r->used_first_case )
264 case_unref (r->first_case);
266 caseproto_unref (r->proto);
268 xmlFree (r->target_sheet_name);
270 ods_destroy (&r->spreadsheet);
278 process_node (struct ods_reader *or, struct state_data *r)
280 xmlChar *name = xmlTextReaderName (r->xtr);
282 name = xmlStrdup (_xml ("--"));
285 r->node_type = xmlTextReaderNodeType (r->xtr);
290 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
291 XML_READER_TYPE_ELEMENT == r->node_type)
293 r->state = STATE_SPREADSHEET;
294 r->current_sheet = -1;
295 r->current_sheet_name = NULL;
298 case STATE_SPREADSHEET:
299 if (0 == xmlStrcasecmp (name, _xml("table:table"))
301 (XML_READER_TYPE_ELEMENT == r->node_type))
303 xmlFree (r->current_sheet_name);
304 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
308 if (r->current_sheet >= or->n_allocated_sheets)
310 assert (r->current_sheet == or->n_allocated_sheets);
311 or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
312 or->sheets[or->n_allocated_sheets - 1].start_col = -1;
313 or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
314 or->sheets[or->n_allocated_sheets - 1].start_row = -1;
315 or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
316 or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
322 r->state = STATE_TABLE;
324 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
325 XML_READER_TYPE_ELEMENT == r->node_type)
327 r->state = STATE_INIT;
331 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
332 (XML_READER_TYPE_ELEMENT == r->node_type))
335 xmlTextReaderGetAttribute (r->xtr,
336 _xml ("table:number-rows-repeated"));
338 int row_span = value ? _xmlchar_to_int (value) : 1;
343 if (! xmlTextReaderIsEmptyElement (r->xtr))
344 r->state = STATE_ROW;
348 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
349 (XML_READER_TYPE_END_ELEMENT == r->node_type))
351 r->state = STATE_SPREADSHEET;
355 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
357 (XML_READER_TYPE_ELEMENT == r->node_type))
360 xmlTextReaderGetAttribute (r->xtr,
361 _xml ("table:number-columns-repeated"));
363 r->col_span = value ? _xmlchar_to_int (value) : 1;
364 r->col += r->col_span;
366 if (! xmlTextReaderIsEmptyElement (r->xtr))
367 r->state = STATE_CELL;
371 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
373 (XML_READER_TYPE_END_ELEMENT == r->node_type))
375 r->state = STATE_TABLE;
379 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
381 ( XML_READER_TYPE_ELEMENT == r->node_type))
383 if (! xmlTextReaderIsEmptyElement (r->xtr))
384 r->state = STATE_CELL_CONTENT;
387 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
389 (XML_READER_TYPE_END_ELEMENT == r->node_type)
392 r->state = STATE_ROW;
395 case STATE_CELL_CONTENT:
396 assert (r->current_sheet >= 0);
397 assert (r->current_sheet < or->n_allocated_sheets);
399 if (or->sheets[r->current_sheet].start_row == -1)
400 or->sheets[r->current_sheet].start_row = r->row - 1;
403 (or->sheets[r->current_sheet].start_col == -1)
405 (or->sheets[r->current_sheet].start_col >= r->col - 1)
407 or->sheets[r->current_sheet].start_col = r->col - 1;
409 or->sheets[r->current_sheet].stop_row = r->row - 1;
411 if ( or->sheets[r->current_sheet].stop_col < r->col - 1)
412 or->sheets[r->current_sheet].stop_col = r->col - 1;
414 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
415 r->state = STATE_CELL;
426 A struct containing the parameters of a cell's value
439 struct xml_value firstval;
443 /* Determine the width that a xmv should probably have */
445 xmv_to_width (const struct xml_value *xmv, int fallback)
447 int width = SPREADSHEET_DEFAULT_WIDTH;
449 /* Non-strings always have zero width */
450 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
457 width = ROUND_UP (xmlStrlen (xmv->value),
458 SPREADSHEET_DEFAULT_WIDTH);
460 width = ROUND_UP (xmlStrlen (xmv->text),
461 SPREADSHEET_DEFAULT_WIDTH);
467 Sets the VAR of case C, to the value corresponding to the xml data
470 convert_xml_to_value (struct ccase *c, const struct variable *var,
471 const struct xml_value *xmv)
473 union value *v = case_data_rw (c, var);
475 if (xmv->value == NULL && xmv->text == NULL)
476 value_set_missing (v, var_get_width (var));
477 else if ( var_is_alpha (var))
478 /* Use the text field, because it seems that there is no
479 value field for strings */
480 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
483 const struct fmt_spec *fmt = var_get_write_format (var);
484 enum fmt_category fc = fmt_get_category (fmt->type);
486 assert ( fc != FMT_CAT_STRING);
488 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
490 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
494 const char *text = xmv->value ?
495 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
498 free (data_in (ss_cstr (text), "UTF-8",
508 /* Try to find out how many sheets there are in the "workbook" */
510 get_sheet_count (struct zip_reader *zreader)
512 xmlTextReaderPtr mxtr;
513 struct zip_member *meta = NULL;
514 meta = zip_member_open (zreader, "meta.xml");
519 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
520 (xmlInputCloseCallback) NULL,
521 meta, NULL, NULL, 0);
523 while (1 == xmlTextReaderRead (mxtr))
525 xmlChar *name = xmlTextReaderName (mxtr);
526 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
528 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
532 int s = _xmlchar_to_int (attr);
533 xmlFreeTextReader (mxtr);
543 xmlFreeTextReader (mxtr);
548 ods_error_handler (void *ctx, const char *mesg,
549 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
551 struct ods_reader *r = ctx;
553 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
555 r->spreadsheet.file_name,
556 xmlTextReaderLocatorLineNumber (loc),
561 static xmlTextReaderPtr
562 init_reader (struct ods_reader *r, bool report_errors)
564 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
565 xmlTextReaderPtr xtr;
567 if ( content == NULL)
570 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
571 (xmlInputCloseCallback) NULL,
573 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
579 r->spreadsheet.type = SPREADSHEET_ODS;
582 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
589 ods_probe (const char *filename, bool report_errors)
591 struct ods_reader *r;
592 struct string errs = DS_EMPTY_INITIALIZER;
594 struct zip_reader *zr = zip_reader_create (filename, &errs);
595 xmlTextReaderPtr xtr;
601 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
602 filename, ds_cstr (&errs));
607 sheet_count = get_sheet_count (zr);
609 r = xzalloc (sizeof *r);
613 xtr = init_reader (r, report_errors);
621 r->msd.current_sheet = 0;
622 r->msd.state = STATE_INIT;
625 r->spreadsheet.n_sheets = sheet_count;
626 r->n_allocated_sheets = 0;
631 r->spreadsheet.file_name = filename;
632 return &r->spreadsheet;
635 zip_reader_destroy (r->zreader);
642 ods_make_reader (struct spreadsheet *spreadsheet,
643 const struct spreadsheet_read_options *opts)
646 xmlChar *type = NULL;
647 unsigned long int vstart = 0;
648 casenumber n_cases = CASENUMBER_MAX;
650 struct var_spec *var_spec = NULL;
652 xmlTextReaderPtr xtr;
654 struct ods_reader *r = (struct ods_reader *) spreadsheet;
655 xmlChar *val_string = NULL;
658 r->read_names = opts->read_names;
659 ds_init_empty (&r->ods_errs);
662 xtr = init_reader (r, true);
669 r->rsd.current_sheet = 0;
670 r->rsd.state = STATE_INIT;
673 if (opts->cell_range)
675 if ( ! convert_cell_ref (opts->cell_range,
676 &r->start_col, &r->start_row,
677 &r->stop_col, &r->stop_row))
679 msg (SE, _("Invalid cell range `%s'"),
692 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
693 r->target_sheet_index = opts->sheet_index;
695 /* Advance to the start of the cells for the target sheet */
696 while ( ! reading_target_sheet (r, &r->rsd)
697 || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row )
699 if (1 != (ret = xmlTextReaderRead (r->rsd.xtr)))
702 process_node (r, &r->rsd);
707 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
708 spreadsheet->file_name);
712 if ( opts->read_names)
714 while (1 == (ret = xmlTextReaderRead (r->rsd.xtr)))
718 process_node (r, &r->rsd);
720 /* If the row is finished then stop for now */
721 if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row)
724 idx = r->rsd.col - r->start_col -1 ;
729 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
732 if (r->rsd.state == STATE_CELL_CONTENT
734 XML_READER_TYPE_TEXT == r->rsd.node_type)
736 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
738 if ( idx >= n_var_specs)
740 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
742 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
743 memset (var_spec + n_var_specs,
745 (idx - n_var_specs + 1) * sizeof (*var_spec));
746 n_var_specs = idx + 1;
748 var_spec[idx].firstval.text = 0;
749 var_spec[idx].firstval.value = 0;
750 var_spec[idx].firstval.type = 0;
752 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
759 /* Read in the first row of data */
760 while (1 == xmlTextReaderRead (r->rsd.xtr))
763 process_node (r, &r->rsd);
765 if ( ! reading_target_sheet (r, &r->rsd) )
768 /* If the row is finished then stop for now */
769 if (r->rsd.state == STATE_TABLE &&
770 r->rsd.row > r->start_row + (opts->read_names ? 1 : 0))
773 idx = r->rsd.col - r->start_col - 1;
777 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
780 if ( r->rsd.state == STATE_CELL &&
781 XML_READER_TYPE_ELEMENT == r->rsd.node_type)
783 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
784 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
787 if ( r->rsd.state == STATE_CELL_CONTENT &&
788 XML_READER_TYPE_TEXT == r->rsd.node_type)
790 if (idx >= n_var_specs)
792 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
793 memset (var_spec + n_var_specs,
795 (idx - n_var_specs + 1) * sizeof (*var_spec));
797 var_spec [idx].name = NULL;
798 n_var_specs = idx + 1;
801 var_spec [idx].firstval.type = type;
802 var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr);
803 var_spec [idx].firstval.value = val_string;
811 /* Create the dictionary and populate it */
812 r->spreadsheet.dict = r->dict = dict_create (
813 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr)));
815 for (i = 0; i < n_var_specs ; ++i )
818 struct variable *var = NULL;
819 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
820 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
821 dict_create_var (r->dict, name, width);
824 var = dict_get_var (r->dict, i);
826 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
833 fmt = fmt_default_for_width (width);
835 var_set_both_formats (var, &fmt);
838 /* Create the first case, and cache it */
839 r->used_first_case = false;
841 if ( n_var_specs == 0 )
843 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
844 spreadsheet->file_name);
848 r->proto = caseproto_ref (dict_get_proto (r->dict));
849 r->first_case = case_create (r->proto);
850 case_set_missing (r->first_case);
852 for (i = 0 ; i < n_var_specs; ++i)
854 const struct variable *var = dict_get_var (r->dict, i);
856 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
859 /* Read in the first row of data */
860 while (1 == xmlTextReaderRead (r->rsd.xtr))
862 process_node (r, &r->rsd);
864 if (r->rsd.state == STATE_ROW)
869 for ( i = 0 ; i < n_var_specs ; ++i )
871 free (var_spec[i].firstval.type);
872 free (var_spec[i].firstval.value);
873 free (var_spec[i].firstval.text);
874 free (var_spec[i].name);
880 return casereader_create_sequential
884 &ods_file_casereader_class, r);
888 for ( i = 0 ; i < n_var_specs ; ++i )
890 free (var_spec[i].firstval.type);
891 free (var_spec[i].firstval.value);
892 free (var_spec[i].firstval.text);
893 free (var_spec[i].name);
898 dict_destroy (r->spreadsheet.dict);
899 r->spreadsheet.dict = NULL;
900 ods_file_casereader_destroy (NULL, r);
906 /* Reads and returns one case from READER's file. Returns a null
907 pointer on failure. */
908 static struct ccase *
909 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
911 struct ccase *c = NULL;
912 struct ods_reader *r = r_;
914 xmlChar *val_string = NULL;
915 xmlChar *type = NULL;
917 if (!r->used_first_case)
919 r->used_first_case = true;
920 return r->first_case;
924 /* Advance to the start of a row. (If there is one) */
925 while (r->rsd.state != STATE_ROW
926 && 1 == xmlTextReaderRead (r->rsd.xtr)
929 process_node (r, &r->rsd);
933 if ( ! reading_target_sheet (r, &r->rsd)
934 || r->rsd.state < STATE_TABLE
935 || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
941 c = case_create (r->proto);
942 case_set_missing (c);
944 while (1 == xmlTextReaderRead (r->rsd.xtr))
946 process_node (r, &r->rsd);
948 if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
951 if (r->rsd.state == STATE_CELL &&
952 r->rsd.node_type == XML_READER_TYPE_ELEMENT)
954 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
955 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
958 if (r->rsd.state == STATE_CELL_CONTENT &&
959 r->rsd.node_type == XML_READER_TYPE_TEXT)
962 struct xml_value *xmv = xzalloc (sizeof *xmv);
963 xmv->text = xmlTextReaderValue (r->rsd.xtr);
964 xmv->value = val_string;
968 for (col = 0; col < r->rsd.col_span; ++col)
970 const struct variable *var;
971 const int idx = r->rsd.col - col - r->start_col - 1;
974 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
976 if (idx >= dict_get_var_cnt (r->dict))
979 var = dict_get_var (r->dict, idx);
980 convert_xml_to_value (c, var, xmv);
984 xmlFree (xmv->value);
988 if ( r->rsd.state <= STATE_TABLE)