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 struct state_data foo;
132 struct state_data rsd;
139 struct sheet_detail *sheets;
140 int n_allocated_sheets;
142 struct caseproto *proto;
143 struct dictionary *dict;
144 struct ccase *first_case;
145 bool used_first_case;
148 struct string ods_errs;
152 ods_destroy (struct spreadsheet *s)
154 struct ods_reader *r = (struct ods_reader *) s;
156 if (--r->ref_cnt == 0)
160 state_data_destroy (&r->foo);
161 for (i = 0; i < r->n_allocated_sheets; ++i)
163 xmlFree (r->sheets[i].name);
166 zip_reader_destroy (r->zreader);
176 reading_target_sheet (const struct ods_reader *r, const struct state_data *foo)
178 if (r->target_sheet_name != NULL)
180 if ( 0 == xmlStrcmp (r->target_sheet_name, foo->current_sheet_name))
184 if (r->target_sheet_index == foo->current_sheet + 1)
191 static void process_node (struct ods_reader *or, struct state_data *r);
195 ods_get_sheet_name (struct spreadsheet *s, int n)
197 struct ods_reader *r = (struct ods_reader *) s;
198 struct state_data *or = &r->foo;
200 assert (n < s->n_sheets);
203 (r->n_allocated_sheets <= n)
204 || or->state != STATE_SPREADSHEET
207 int ret = xmlTextReaderRead (or->xtr);
211 process_node (r, or);
214 return r->sheets[n].name;
218 ods_get_sheet_range (struct spreadsheet *s, int n)
220 struct ods_reader *r = (struct ods_reader *) s;
221 struct state_data *or = &r->foo;
223 assert (n < s->n_sheets);
226 (r->n_allocated_sheets <= n)
227 || (r->sheets[n].stop_row == -1)
228 || or->state != STATE_SPREADSHEET
231 int ret = xmlTextReaderRead (or->xtr);
235 process_node (r, or);
238 return create_cell_ref (
239 r->sheets[n].start_col,
240 r->sheets[n].start_row,
241 r->sheets[n].stop_col,
242 r->sheets[n].stop_row);
247 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
249 struct ods_reader *r = r_;
253 state_data_destroy (&r->rsd);
255 if ( ! ds_is_empty (&r->ods_errs))
256 msg (ME, "%s", ds_cstr (&r->ods_errs));
258 ds_destroy (&r->ods_errs);
260 if ( ! r->used_first_case )
261 case_unref (r->first_case);
263 caseproto_unref (r->proto);
265 xmlFree (r->target_sheet_name);
267 ods_destroy (&r->spreadsheet);
275 process_node (struct ods_reader *or, struct state_data *r)
277 xmlChar *name = xmlTextReaderName (r->xtr);
279 name = xmlStrdup (_xml ("--"));
282 r->node_type = xmlTextReaderNodeType (r->xtr);
287 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
288 XML_READER_TYPE_ELEMENT == r->node_type)
290 r->state = STATE_SPREADSHEET;
291 r->current_sheet = -1;
292 r->current_sheet_name = NULL;
295 case STATE_SPREADSHEET:
296 if (0 == xmlStrcasecmp (name, _xml("table:table"))
298 (XML_READER_TYPE_ELEMENT == r->node_type))
300 xmlFree (r->current_sheet_name);
301 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
305 if (r->current_sheet >= or->n_allocated_sheets)
307 assert (r->current_sheet == or->n_allocated_sheets);
308 or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
309 or->sheets[or->n_allocated_sheets - 1].start_col = -1;
310 or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
311 or->sheets[or->n_allocated_sheets - 1].start_row = -1;
312 or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
313 or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
319 r->state = STATE_TABLE;
321 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
322 XML_READER_TYPE_ELEMENT == r->node_type)
324 r->state = STATE_INIT;
328 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
329 (XML_READER_TYPE_ELEMENT == r->node_type))
332 xmlTextReaderGetAttribute (r->xtr,
333 _xml ("table:number-rows-repeated"));
335 int row_span = value ? _xmlchar_to_int (value) : 1;
340 if (! xmlTextReaderIsEmptyElement (r->xtr))
341 r->state = STATE_ROW;
345 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
346 (XML_READER_TYPE_END_ELEMENT == r->node_type))
348 r->state = STATE_SPREADSHEET;
352 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
354 (XML_READER_TYPE_ELEMENT == r->node_type))
357 xmlTextReaderGetAttribute (r->xtr,
358 _xml ("table:number-columns-repeated"));
360 r->col_span = value ? _xmlchar_to_int (value) : 1;
361 r->col += r->col_span;
363 if (! xmlTextReaderIsEmptyElement (r->xtr))
364 r->state = STATE_CELL;
368 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
370 (XML_READER_TYPE_END_ELEMENT == r->node_type))
372 r->state = STATE_TABLE;
376 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
378 ( XML_READER_TYPE_ELEMENT == r->node_type))
380 if (! xmlTextReaderIsEmptyElement (r->xtr))
381 r->state = STATE_CELL_CONTENT;
384 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
386 (XML_READER_TYPE_END_ELEMENT == r->node_type)
389 r->state = STATE_ROW;
392 case STATE_CELL_CONTENT:
393 assert (r->current_sheet >= 0);
394 assert (r->current_sheet < or->n_allocated_sheets);
396 if (or->sheets[r->current_sheet].start_row == -1)
397 or->sheets[r->current_sheet].start_row = r->row - 1;
400 (or->sheets[r->current_sheet].start_col == -1)
402 (or->sheets[r->current_sheet].start_col >= r->col - 1)
404 or->sheets[r->current_sheet].start_col = r->col - 1;
406 or->sheets[r->current_sheet].stop_row = r->row - 1;
408 if ( or->sheets[r->current_sheet].stop_col < r->col - 1)
409 or->sheets[r->current_sheet].stop_col = r->col - 1;
411 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
412 r->state = STATE_CELL;
423 A struct containing the parameters of a cell's value
436 struct xml_value firstval;
440 /* Determine the width that a xmv should probably have */
442 xmv_to_width (const struct xml_value *xmv, int fallback)
444 int width = SPREADSHEET_DEFAULT_WIDTH;
446 /* Non-strings always have zero width */
447 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
454 width = ROUND_UP (xmlStrlen (xmv->value),
455 SPREADSHEET_DEFAULT_WIDTH);
457 width = ROUND_UP (xmlStrlen (xmv->text),
458 SPREADSHEET_DEFAULT_WIDTH);
464 Sets the VAR of case C, to the value corresponding to the xml data
467 convert_xml_to_value (struct ccase *c, const struct variable *var,
468 const struct xml_value *xmv)
470 union value *v = case_data_rw (c, var);
472 if (xmv->value == NULL && xmv->text == NULL)
473 value_set_missing (v, var_get_width (var));
474 else if ( var_is_alpha (var))
475 /* Use the text field, because it seems that there is no
476 value field for strings */
477 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
480 const struct fmt_spec *fmt = var_get_write_format (var);
481 enum fmt_category fc = fmt_get_category (fmt->type);
483 assert ( fc != FMT_CAT_STRING);
485 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
487 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
491 const char *text = xmv->value ?
492 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
495 free (data_in (ss_cstr (text), "UTF-8",
505 /* Try to find out how many sheets there are in the "workbook" */
507 get_sheet_count (struct zip_reader *zreader)
509 xmlTextReaderPtr mxtr;
510 struct zip_member *meta = NULL;
511 meta = zip_member_open (zreader, "meta.xml");
516 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
517 (xmlInputCloseCallback) NULL,
518 meta, NULL, NULL, 0);
520 while (1 == xmlTextReaderRead (mxtr))
522 xmlChar *name = xmlTextReaderName (mxtr);
523 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
525 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
529 int s = _xmlchar_to_int (attr);
530 xmlFreeTextReader (mxtr);
540 xmlFreeTextReader (mxtr);
545 ods_error_handler (void *ctx, const char *mesg,
546 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
548 struct ods_reader *r = ctx;
550 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
552 r->spreadsheet.file_name,
553 xmlTextReaderLocatorLineNumber (loc),
558 static xmlTextReaderPtr
559 init_reader (struct ods_reader *r, bool report_errors)
561 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
562 xmlTextReaderPtr xtr;
564 if ( content == NULL)
567 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
568 (xmlInputCloseCallback) NULL,
570 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
576 r->spreadsheet.type = SPREADSHEET_ODS;
579 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
586 ods_probe (const char *filename, bool report_errors)
588 struct ods_reader *r;
589 struct string errs = DS_EMPTY_INITIALIZER;
591 struct zip_reader *zr = zip_reader_create (filename, &errs);
592 xmlTextReaderPtr xtr;
598 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
599 filename, ds_cstr (&errs));
604 sheet_count = get_sheet_count (zr);
606 r = xzalloc (sizeof *r);
610 xtr = init_reader (r, report_errors);
618 r->foo.current_sheet = 0;
619 r->foo.state = STATE_INIT;
622 r->spreadsheet.n_sheets = sheet_count;
623 r->n_allocated_sheets = 0;
628 r->spreadsheet.file_name = filename;
629 return &r->spreadsheet;
632 zip_reader_destroy (r->zreader);
639 ods_make_reader (struct spreadsheet *spreadsheet,
640 const struct spreadsheet_read_options *opts)
643 xmlChar *type = NULL;
644 unsigned long int vstart = 0;
645 casenumber n_cases = CASENUMBER_MAX;
647 struct var_spec *var_spec = NULL;
649 xmlTextReaderPtr xtr;
651 struct ods_reader *r = (struct ods_reader *) spreadsheet;
652 xmlChar *val_string = NULL;
655 r->read_names = opts->read_names;
656 ds_init_empty (&r->ods_errs);
659 xtr = init_reader (r, true);
666 r->rsd.current_sheet = 0;
667 r->rsd.state = STATE_INIT;
670 if (opts->cell_range)
672 if ( ! convert_cell_ref (opts->cell_range,
673 &r->start_col, &r->start_row,
674 &r->stop_col, &r->stop_row))
676 msg (SE, _("Invalid cell range `%s'"),
689 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
690 r->target_sheet_index = opts->sheet_index;
692 /* Advance to the start of the cells for the target sheet */
693 while ( ! reading_target_sheet (r, &r->rsd)
694 || r->rsd.state != STATE_ROW || r->rsd.row <= r->start_row )
696 if (1 != (ret = xmlTextReaderRead (r->rsd.xtr)))
699 process_node (r, &r->rsd);
704 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
705 spreadsheet->file_name);
709 if ( opts->read_names)
711 while (1 == (ret = xmlTextReaderRead (r->rsd.xtr)))
715 process_node (r, &r->rsd);
717 /* If the row is finished then stop for now */
718 if (r->rsd.state == STATE_TABLE && r->rsd.row > r->start_row)
721 idx = r->rsd.col - r->start_col -1 ;
726 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
729 if (r->rsd.state == STATE_CELL_CONTENT
731 XML_READER_TYPE_TEXT == r->rsd.node_type)
733 xmlChar *value = xmlTextReaderValue (r->rsd.xtr);
735 if ( idx >= n_var_specs)
737 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
739 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
740 memset (var_spec + n_var_specs,
742 (idx - n_var_specs + 1) * sizeof (*var_spec));
743 n_var_specs = idx + 1;
745 var_spec[idx].firstval.text = 0;
746 var_spec[idx].firstval.value = 0;
747 var_spec[idx].firstval.type = 0;
749 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
756 /* Read in the first row of data */
757 while (1 == xmlTextReaderRead (r->rsd.xtr))
760 process_node (r, &r->rsd);
762 if ( ! reading_target_sheet (r, &r->rsd) )
765 /* If the row is finished then stop for now */
766 if (r->rsd.state == STATE_TABLE &&
767 r->rsd.row > r->start_row + (opts->read_names ? 1 : 0))
770 idx = r->rsd.col - r->start_col - 1;
774 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
777 if ( r->rsd.state == STATE_CELL &&
778 XML_READER_TYPE_ELEMENT == r->rsd.node_type)
780 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
781 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
784 if ( r->rsd.state == STATE_CELL_CONTENT &&
785 XML_READER_TYPE_TEXT == r->rsd.node_type)
787 if (idx >= n_var_specs)
789 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
790 memset (var_spec + n_var_specs,
792 (idx - n_var_specs + 1) * sizeof (*var_spec));
794 var_spec [idx].name = NULL;
795 n_var_specs = idx + 1;
798 var_spec [idx].firstval.type = type;
799 var_spec [idx].firstval.text = xmlTextReaderValue (r->rsd.xtr);
800 var_spec [idx].firstval.value = val_string;
808 /* Create the dictionary and populate it */
809 r->spreadsheet.dict = r->dict = dict_create (
810 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->rsd.xtr)));
812 for (i = 0; i < n_var_specs ; ++i )
815 struct variable *var = NULL;
816 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
817 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
818 dict_create_var (r->dict, name, width);
821 var = dict_get_var (r->dict, i);
823 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
830 fmt = fmt_default_for_width (width);
832 var_set_both_formats (var, &fmt);
835 /* Create the first case, and cache it */
836 r->used_first_case = false;
838 if ( n_var_specs == 0 )
840 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
841 spreadsheet->file_name);
845 r->proto = caseproto_ref (dict_get_proto (r->dict));
846 r->first_case = case_create (r->proto);
847 case_set_missing (r->first_case);
849 for (i = 0 ; i < n_var_specs; ++i)
851 const struct variable *var = dict_get_var (r->dict, i);
853 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
856 /* Read in the first row of data */
857 while (1 == xmlTextReaderRead (r->rsd.xtr))
859 process_node (r, &r->rsd);
861 if (r->rsd.state == STATE_ROW)
866 for ( i = 0 ; i < n_var_specs ; ++i )
868 free (var_spec[i].firstval.type);
869 free (var_spec[i].firstval.value);
870 free (var_spec[i].firstval.text);
871 free (var_spec[i].name);
877 return casereader_create_sequential
881 &ods_file_casereader_class, r);
885 for ( i = 0 ; i < n_var_specs ; ++i )
887 free (var_spec[i].firstval.type);
888 free (var_spec[i].firstval.value);
889 free (var_spec[i].firstval.text);
890 free (var_spec[i].name);
895 dict_destroy (r->spreadsheet.dict);
896 r->spreadsheet.dict = NULL;
897 ods_file_casereader_destroy (NULL, r);
903 /* Reads and returns one case from READER's file. Returns a null
904 pointer on failure. */
905 static struct ccase *
906 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
908 struct ccase *c = NULL;
909 struct ods_reader *r = r_;
911 if (!r->used_first_case)
913 r->used_first_case = true;
914 return r->first_case;
918 /* Advance to the start of a row. (If there is one) */
919 while (r->rsd.state != STATE_ROW
920 && 1 == xmlTextReaderRead (r->rsd.xtr)
923 process_node (r, &r->rsd);
927 if ( ! reading_target_sheet (r, &r->rsd)
928 || r->rsd.state < STATE_TABLE
929 || (r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
935 c = case_create (r->proto);
936 case_set_missing (c);
938 xmlChar *val_string = NULL;
939 xmlChar *type = NULL;
941 while (1 == xmlTextReaderRead (r->rsd.xtr))
943 process_node (r, &r->rsd);
945 if ( r->stop_row != -1 && r->rsd.row > r->stop_row + 1)
948 if (r->rsd.state == STATE_CELL &&
949 r->rsd.node_type == XML_READER_TYPE_ELEMENT)
951 type = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value-type"));
952 val_string = xmlTextReaderGetAttribute (r->rsd.xtr, _xml ("office:value"));
955 if (r->rsd.state == STATE_CELL_CONTENT &&
956 r->rsd.node_type == XML_READER_TYPE_TEXT)
959 struct xml_value *xmv = xzalloc (sizeof *xmv);
960 xmv->text = xmlTextReaderValue (r->rsd.xtr);
961 xmv->value = val_string;
965 for (col = 0; col < r->rsd.col_span; ++col)
967 const struct variable *var;
968 const int idx = r->rsd.col - col - r->start_col - 1;
971 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
973 if (idx >= dict_get_var_cnt (r->dict))
976 var = dict_get_var (r->dict, idx);
977 convert_xml_to_value (c, var, xmv);
981 xmlFree (xmv->value);
985 if ( r->rsd.state <= STATE_TABLE)