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;
115 struct sheet_detail *sheets;
116 int n_allocated_sheets;
121 struct spreadsheet spreadsheet;
122 struct zip_reader *zreader;
124 int target_sheet_index;
125 xmlChar *target_sheet_name;
135 struct caseproto *proto;
136 struct dictionary *dict;
137 struct ccase *first_case;
138 bool used_first_case;
141 struct string ods_errs;
145 ods_destroy (struct spreadsheet *s)
148 struct ods_reader *r = (struct ods_reader *) s;
150 if (--r->ref_cnt == 0)
154 for (i = 0; i < r->n_allocated_sheets; ++i)
156 xmlFree (r->sheets[i].name);
159 zip_reader_destroy (r->zreader);
169 reading_target_sheet (const struct ods_reader *r)
171 if (r->target_sheet_name != NULL)
173 if ( 0 == xmlStrcmp (r->target_sheet_name, r->foo.current_sheet_name))
177 if (r->target_sheet_index == r->foo.current_sheet + 1)
184 static void process_node (struct foo *r);
188 ods_get_sheet_name (struct spreadsheet *s, int n)
190 struct foo *or = &((struct ods_reader *) s)->foo;
192 assert (n < s->n_sheets);
195 (or->n_allocated_sheets <= n)
196 || or->state != STATE_SPREADSHEET
199 int ret = xmlTextReaderRead (or->xtr);
206 return or->sheets[n].name;
210 ods_get_sheet_range (struct spreadsheet *s, int n)
212 struct foo *or = &((struct ods_reader *) s)->foo;
214 assert (n < s->n_sheets);
217 (or->n_allocated_sheets <= n)
218 || (or->sheets[n].stop_row == -1)
219 || or->state != STATE_SPREADSHEET
222 int ret = xmlTextReaderRead (or->xtr);
229 return create_cell_ref (
230 or->sheets[n].start_col,
231 or->sheets[n].start_row,
232 or->sheets[n].stop_col,
233 or->sheets[n].stop_row);
238 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
241 struct ods_reader *r = r_;
246 xmlFreeTextReader (r->xtr);
249 if ( ! ds_is_empty (&r->ods_errs))
250 msg (ME, "%s", ds_cstr (&r->ods_errs));
252 ds_destroy (&r->ods_errs);
254 if ( ! r->used_first_case )
255 case_unref (r->first_case);
257 caseproto_unref (r->proto);
259 xmlFree (r->current_sheet_name);
260 xmlFree (r->target_sheet_name);
262 ods_destroy (&r->spreadsheet);
271 process_node (struct foo *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 >= r->n_allocated_sheets)
303 assert (r->current_sheet == r->n_allocated_sheets);
304 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
305 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
306 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
307 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
308 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
309 r->sheets[r->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 < r->n_allocated_sheets);
392 if (r->sheets[r->current_sheet].start_row == -1)
393 r->sheets[r->current_sheet].start_row = r->row - 1;
396 (r->sheets[r->current_sheet].start_col == -1)
398 (r->sheets[r->current_sheet].start_col >= r->col - 1)
400 r->sheets[r->current_sheet].start_col = r->col - 1;
402 r->sheets[r->current_sheet].stop_row = r->row - 1;
404 if ( r->sheets[r->current_sheet].stop_col < r->col - 1)
405 r->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)
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);
491 free (data_in (ss_cstr (text), "UTF-8",
501 /* Try to find out how many sheets there are in the "workbook" */
503 get_sheet_count (struct zip_reader *zreader)
505 xmlTextReaderPtr mxtr;
506 struct zip_member *meta = NULL;
507 meta = zip_member_open (zreader, "meta.xml");
512 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
513 (xmlInputCloseCallback) NULL,
514 meta, NULL, NULL, 0);
516 while (1 == xmlTextReaderRead (mxtr))
518 xmlChar *name = xmlTextReaderName (mxtr);
519 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
521 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
525 int s = _xmlchar_to_int (attr);
526 xmlFreeTextReader (mxtr);
536 xmlFreeTextReader (mxtr);
541 ods_error_handler (void *ctx, const char *mesg,
542 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
544 struct ods_reader *r = ctx;
546 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
548 r->spreadsheet.file_name,
549 xmlTextReaderLocatorLineNumber (loc),
555 init_reader (struct ods_reader *r, bool report_errors)
557 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
558 xmlTextReaderPtr xtr;
560 if ( content == NULL)
564 xmlFreeTextReader (r->foo.xtr);
566 zip_member_ref (content);
567 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
568 (xmlInputCloseCallback) zip_member_finish,
570 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
576 r->spreadsheet.type = SPREADSHEET_ODS;
579 r->foo.current_sheet = 0;
580 r->foo.state = STATE_INIT;
583 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
590 ods_probe (const char *filename, bool report_errors)
592 struct ods_reader *r;
593 struct string errs = DS_EMPTY_INITIALIZER;
595 struct zip_reader *zr = zip_reader_create (filename, &errs);
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 if (! init_reader (r, report_errors))
618 r->spreadsheet.n_sheets = sheet_count;
619 r->foo.n_allocated_sheets = 0;
620 r->foo.sheets = NULL;
624 r->spreadsheet.file_name = filename;
625 return &r->spreadsheet;
628 zip_reader_destroy (r->zreader);
635 ods_make_reader (struct spreadsheet *spreadsheet,
636 const struct spreadsheet_read_options *opts)
639 xmlChar *type = NULL;
640 unsigned long int vstart = 0;
641 casenumber n_cases = CASENUMBER_MAX;
643 struct var_spec *var_spec = NULL;
646 struct ods_reader *r = (struct ods_reader *) spreadsheet;
647 xmlChar *val_string = NULL;
650 r->read_names = opts->read_names;
651 ds_init_empty (&r->ods_errs);
654 if ( !init_reader (r, true))
657 if (opts->cell_range)
659 if ( ! convert_cell_ref (opts->cell_range,
660 &r->start_col, &r->start_row,
661 &r->stop_col, &r->stop_row))
663 msg (SE, _("Invalid cell range `%s'"),
676 r->foo.state = STATE_INIT;
677 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
678 r->target_sheet_index = opts->sheet_index;
679 r->foo.row = r->foo.col = 0;
682 /* Advance to the start of the cells for the target sheet */
683 while ( ! reading_target_sheet (r)
684 || r->foo.state != STATE_ROW || r->foo.row <= r->start_row )
686 if (1 != (ret = xmlTextReaderRead (r->foo.xtr)))
689 process_node (&r->foo);
694 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
695 spreadsheet->file_name);
699 if ( opts->read_names)
701 while (1 == (ret = xmlTextReaderRead (r->foo.xtr)))
705 process_node (&r->foo);
707 /* If the row is finished then stop for now */
708 if (r->foo.state == STATE_TABLE && r->foo.row > r->start_row)
711 idx = r->foo.col - r->start_col -1 ;
716 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
719 if (r->foo.state == STATE_CELL_CONTENT
721 XML_READER_TYPE_TEXT == r->foo.node_type)
723 xmlChar *value = xmlTextReaderValue (r->foo.xtr);
725 if ( idx >= n_var_specs)
727 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
729 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
730 memset (var_spec + n_var_specs,
732 (idx - n_var_specs + 1) * sizeof (*var_spec));
733 n_var_specs = idx + 1;
735 var_spec[idx].firstval.text = 0;
736 var_spec[idx].firstval.value = 0;
737 var_spec[idx].firstval.type = 0;
739 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
746 /* Read in the first row of data */
747 while (1 == xmlTextReaderRead (r->foo.xtr))
750 process_node (&r->foo);
752 if ( ! reading_target_sheet (r) )
755 /* If the row is finished then stop for now */
756 if (r->foo.state == STATE_TABLE &&
757 r->foo.row > r->start_row + (opts->read_names ? 1 : 0))
760 idx = r->foo.col - r->start_col - 1;
764 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
767 if ( r->foo.state == STATE_CELL &&
768 XML_READER_TYPE_ELEMENT == r->foo.node_type)
770 type = xmlTextReaderGetAttribute (r->foo.xtr, _xml ("office:value-type"));
771 val_string = xmlTextReaderGetAttribute (r->foo.xtr, _xml ("office:value"));
774 if ( r->foo.state == STATE_CELL_CONTENT &&
775 XML_READER_TYPE_TEXT == r->foo.node_type)
777 if (idx >= n_var_specs)
779 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
780 memset (var_spec + n_var_specs,
782 (idx - n_var_specs + 1) * sizeof (*var_spec));
784 var_spec [idx].name = NULL;
785 n_var_specs = idx + 1;
788 var_spec [idx].firstval.type = type;
789 var_spec [idx].firstval.text = xmlTextReaderValue (r->foo.xtr);
790 var_spec [idx].firstval.value = val_string;
798 /* Create the dictionary and populate it */
799 r->spreadsheet.dict = r->dict = dict_create (
800 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->foo.xtr)));
802 for (i = 0; i < n_var_specs ; ++i )
805 struct variable *var = NULL;
806 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
807 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
808 dict_create_var (r->dict, name, width);
811 var = dict_get_var (r->dict, i);
813 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
820 fmt = fmt_default_for_width (width);
822 var_set_both_formats (var, &fmt);
825 /* Create the first case, and cache it */
826 r->used_first_case = false;
828 if ( n_var_specs == 0 )
830 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
831 spreadsheet->file_name);
835 r->proto = caseproto_ref (dict_get_proto (r->dict));
836 r->first_case = case_create (r->proto);
837 case_set_missing (r->first_case);
839 for (i = 0 ; i < n_var_specs; ++i)
841 const struct variable *var = dict_get_var (r->dict, i);
843 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
846 /* Read in the first row of data */
847 while (1 == xmlTextReaderRead (r->foo.xtr))
849 process_node (&r->foo);
851 if (r->foo.state == STATE_ROW)
856 for ( i = 0 ; i < n_var_specs ; ++i )
858 free (var_spec[i].firstval.type);
859 free (var_spec[i].firstval.value);
860 free (var_spec[i].firstval.text);
861 free (var_spec[i].name);
867 return casereader_create_sequential
871 &ods_file_casereader_class, r);
875 for ( i = 0 ; i < n_var_specs ; ++i )
877 free (var_spec[i].firstval.type);
878 free (var_spec[i].firstval.value);
879 free (var_spec[i].firstval.text);
880 free (var_spec[i].name);
885 dict_destroy (r->spreadsheet.dict);
886 r->spreadsheet.dict = NULL;
887 ods_file_casereader_destroy (NULL, r);
893 /* Reads and returns one case from READER's file. Returns a null
894 pointer on failure. */
895 static struct ccase *
896 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
898 struct ccase *c = NULL;
899 xmlChar *val_string = NULL;
900 xmlChar *type = NULL;
901 struct ods_reader *r = r_;
903 if (!r->used_first_case)
905 r->used_first_case = true;
906 return r->first_case;
910 /* Advance to the start of a row. (If there is one) */
911 while (r->foo.state != STATE_ROW
912 && 1 == xmlTextReaderRead (r->foo.xtr)
915 process_node (&r->foo);
919 if ( ! reading_target_sheet (r)
920 || r->foo.state < STATE_TABLE
921 || (r->stop_row != -1 && r->foo.row > r->stop_row + 1)
927 c = case_create (r->proto);
928 case_set_missing (c);
930 while (1 == xmlTextReaderRead (r->foo.xtr))
932 process_node (&r->foo);
934 if ( r->stop_row != -1 && r->foo.row > r->stop_row + 1)
937 if (r->foo.state == STATE_CELL &&
938 r->foo.node_type == XML_READER_TYPE_ELEMENT)
940 type = xmlTextReaderGetAttribute (r->foo.xtr, _xml ("office:value-type"));
941 val_string = xmlTextReaderGetAttribute (r->foo.xtr, _xml ("office:value"));
944 if (r->foo.state == STATE_CELL_CONTENT &&
945 r->foo.node_type == XML_READER_TYPE_TEXT)
948 struct xml_value *xmv = xzalloc (sizeof *xmv);
949 xmv->text = xmlTextReaderValue (r->foo.xtr);
950 xmv->value = val_string;
954 for (col = 0; col < r->foo.col_span; ++col)
956 const struct variable *var;
957 const int idx = r->foo.col - col - r->start_col - 1;
960 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
962 if (idx >= dict_get_var_cnt (r->dict))
965 var = dict_get_var (r->dict, idx);
966 convert_xml_to_value (c, var, xmv);
970 xmlFree (xmv->value);
974 if ( r->foo.state <= STATE_TABLE)