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 *);
71 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 struct spreadsheet spreadsheet;
106 struct zip_reader *zreader;
107 xmlTextReaderPtr xtr;
109 enum reader_state state;
114 xmlChar *current_sheet_name;
116 const xmlChar *target_sheet_name;
117 int target_sheet_index;
120 int wanted_row_start;
121 int wanted_col_start;
132 struct sheet_detail *sheets;
133 int n_allocated_sheets;
135 struct caseproto *proto;
136 struct dictionary *dict;
137 struct ccase *first_case;
138 bool used_first_case;
141 struct string ods_errs;
146 reading_target_sheet (const struct ods_reader *r)
148 if (r->target_sheet_name != NULL)
150 if ( 0 == xmlStrcmp (r->target_sheet_name, r->current_sheet_name))
154 if (r->target_sheet_index == r->current_sheet + 1)
161 static void process_node (struct ods_reader *r);
165 ods_get_sheet_name (struct spreadsheet *s, int n)
168 struct ods_reader *or = (struct ods_reader *) s;
170 assert (n < s->n_sheets);
173 (or->n_allocated_sheets <= n)
175 (1 == (ret = xmlTextReaderRead (or->xtr)))
181 return or->sheets[n].name;
185 ods_get_sheet_range (struct spreadsheet *s, int n)
188 struct ods_reader *or = (struct ods_reader *) s;
190 assert (n < s->n_sheets);
194 (or->n_allocated_sheets <= n)
195 || (or->sheets[n].stop_row == -1) )
197 (1 == (ret = xmlTextReaderRead (or->xtr)))
204 return create_cell_ref (
205 or->sheets[n].start_col,
206 or->sheets[n].start_row,
207 or->sheets[n].stop_col,
208 or->sheets[n].stop_row);
213 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
215 struct ods_reader *r = r_;
220 xmlFreeTextReader (r->xtr);
223 if ( ! ds_is_empty (&r->ods_errs))
224 msg (ME, "%s", ds_cstr (&r->ods_errs));
226 ds_destroy (&r->ods_errs);
228 if ( ! r->used_first_case )
229 case_unref (r->first_case);
231 caseproto_unref (r->proto);
237 process_node (struct ods_reader *r)
239 xmlChar *name = xmlTextReaderName (r->xtr);
241 name = xmlStrdup (_xml ("--"));
244 r->node_type = xmlTextReaderNodeType (r->xtr);
249 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
250 XML_READER_TYPE_ELEMENT == r->node_type)
252 r->state = STATE_SPREADSHEET;
253 r->current_sheet = -1;
254 r->current_sheet_name = NULL;
257 case STATE_SPREADSHEET:
258 if (0 == xmlStrcasecmp (name, _xml("table:table"))
260 (XML_READER_TYPE_ELEMENT == r->node_type))
262 xmlFree (r->current_sheet_name);
263 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
267 if (r->current_sheet >= r->n_allocated_sheets)
269 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
270 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
271 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
272 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
273 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
274 r->sheets[r->n_allocated_sheets - 1].name = xmlStrdup (r->current_sheet_name);
280 r->state = STATE_TABLE;
282 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
283 XML_READER_TYPE_ELEMENT == r->node_type)
285 r->state = STATE_INIT;
289 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
290 (XML_READER_TYPE_ELEMENT == r->node_type))
293 xmlTextReaderGetAttribute (r->xtr,
294 _xml ("table:number-rows-repeated"));
296 int row_span = value ? _xmlchar_to_int (value) : 1;
301 if (! xmlTextReaderIsEmptyElement (r->xtr))
302 r->state = STATE_ROW;
304 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
305 (XML_READER_TYPE_END_ELEMENT == r->node_type))
307 r->state = STATE_SPREADSHEET;
311 // printf ("%s:%d Name is %s\n", __FILE__, __LINE__, name);
312 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
314 (XML_READER_TYPE_ELEMENT == r->node_type))
317 xmlTextReaderGetAttribute (r->xtr,
318 _xml ("table:number-columns-repeated"));
320 r->col_span = value ? _xmlchar_to_int (value) : 1;
321 r->col += r->col_span;
323 // printf ("%s:%d %s\n", __FILE__, __LINE__, value);
325 if (! xmlTextReaderIsEmptyElement (r->xtr))
326 r->state = STATE_CELL;
328 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
330 (XML_READER_TYPE_END_ELEMENT == r->node_type))
332 r->state = STATE_TABLE;
336 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
338 ( XML_READER_TYPE_ELEMENT == r->node_type))
340 if (! xmlTextReaderIsEmptyElement (r->xtr))
341 r->state = STATE_CELL_CONTENT;
344 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
346 (XML_READER_TYPE_END_ELEMENT == r->node_type)
349 r->state = STATE_ROW;
352 case STATE_CELL_CONTENT:
353 assert (r->current_sheet >= 0);
354 assert (r->current_sheet < r->n_allocated_sheets);
356 if (r->sheets[r->current_sheet].start_row == -1)
357 r->sheets[r->current_sheet].start_row = r->row - 1;
360 (r->sheets[r->current_sheet].start_col == -1)
362 (r->sheets[r->current_sheet].start_col >= r->col - 1)
364 r->sheets[r->current_sheet].start_col = r->col - 1;
366 r->sheets[r->current_sheet].stop_row = r->row - 1;
368 if ( r->sheets[r->current_sheet].stop_col < r->col - 1)
369 r->sheets[r->current_sheet].stop_col = r->col - 1;
371 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
372 r->state = STATE_CELL;
383 A struct containing the parameters of a cell's value
396 struct xml_value firstval;
400 /* Determine the width that a xmv should probably have */
402 xmv_to_width (const struct xml_value *xmv, int fallback)
404 int width = SPREADSHEET_DEFAULT_WIDTH;
406 /* Non-strings always have zero width */
407 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
414 width = ROUND_UP (xmlStrlen (xmv->value),
415 SPREADSHEET_DEFAULT_WIDTH);
417 width = ROUND_UP (xmlStrlen (xmv->text),
418 SPREADSHEET_DEFAULT_WIDTH);
424 Sets the VAR of case C, to the value corresponding to the xml data
427 convert_xml_to_value (struct ccase *c, const struct variable *var,
428 const struct xml_value *xmv)
430 union value *v = case_data_rw (c, var);
432 if (xmv->value == NULL && xmv->text == NULL)
433 value_set_missing (v, var_get_width (var));
434 else if ( var_is_alpha (var))
435 /* Use the text field, because it seems that there is no
436 value field for strings */
437 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
440 const struct fmt_spec *fmt = var_get_write_format (var);
441 enum fmt_category fc = fmt_get_category (fmt->type);
443 assert ( fc != FMT_CAT_STRING);
445 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
447 v->f = c_strtod (xmv->value, NULL);
451 const char *text = xmv->value ?
452 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
455 free (data_in (ss_cstr (text), "UTF-8",
465 /* Try to find out how many sheets there are in the "workbook" */
467 get_sheet_count (struct zip_reader *zreader)
469 xmlTextReaderPtr mxtr;
470 struct zip_member *meta = NULL;
471 meta = zip_member_open (zreader, "meta.xml");
476 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
477 (xmlInputCloseCallback) zip_member_finish,
478 meta, NULL, NULL, 0);
480 while (1 == xmlTextReaderRead (mxtr))
482 xmlChar *name = xmlTextReaderName (mxtr);
483 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
485 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
489 int s = _xmlchar_to_int (attr);
498 ods_error_handler (void *ctx, const char *mesg,
499 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
501 struct ods_reader *r = ctx;
503 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
505 r->spreadsheet.file_name,
506 xmlTextReaderLocatorLineNumber (loc),
512 init_reader (struct ods_reader *r, bool report_errors)
514 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
515 xmlTextReaderPtr xtr;
517 if ( content == NULL)
520 zip_member_ref (content);
523 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
524 (xmlInputCloseCallback) zip_member_finish,
526 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
532 r->spreadsheet.type = SPREADSHEET_ODS;
535 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
542 ods_probe (const char *filename, bool report_errors)
544 struct ods_reader *r;
545 struct string errs = DS_EMPTY_INITIALIZER;
547 struct zip_reader *zr = zip_reader_create (filename, &errs);
553 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
554 filename, ds_cstr (&errs));
559 sheet_count = get_sheet_count (zr);
561 r = xzalloc (sizeof *r);
564 if (! init_reader (r, report_errors))
569 r->spreadsheet.n_sheets = sheet_count;
570 r->n_allocated_sheets = 0;
575 r->spreadsheet.file_name = filename;
576 return &r->spreadsheet;
579 zip_reader_destroy (r->zreader);
586 ods_make_reader (struct spreadsheet *spreadsheet,
587 const struct spreadsheet_read_options *opts)
590 xmlChar *type = NULL;
591 unsigned long int vstart = 0;
592 casenumber n_cases = CASENUMBER_MAX;
594 struct var_spec *var_spec = NULL;
597 struct ods_reader *r = (struct ods_reader *) spreadsheet;
598 xmlChar *val_string = NULL;
601 r->read_names = opts->read_names;
602 ds_init_empty (&r->ods_errs);
605 if ( !init_reader (r, true))
609 if ( opts->cell_range )
611 if ( ! convert_cell_ref (opts->cell_range,
612 &r->start_col, &r->start_row,
613 &r->stop_col, &r->stop_row))
615 msg (SE, _("Invalid cell range `%s'"),
629 r->state = STATE_INIT;
630 r->target_sheet_name = BAD_CAST opts->sheet_name;
631 r->target_sheet_index = opts->sheet_index;
635 /* If CELLRANGE was given, then we know how many variables should be read */
636 if ( r->stop_col != -1 )
638 assert (var_spec == NULL);
639 n_var_specs = r->stop_col - r->start_col + 1;
640 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
641 memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
645 /* Advance to the start of the cells for the target sheet */
646 while ( ! reading_target_sheet (r) || r->state != STATE_ROW )
648 if (1 != (ret = xmlTextReaderRead (r->xtr)))
657 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
658 spreadsheet->file_name);
662 if ( opts->read_names)
664 while (1 == (ret = xmlTextReaderRead (r->xtr)))
670 /* If the row is finished then stop for now */
671 if (r->state == STATE_TABLE && r->row > r->wanted_row_start)
674 idx = r->col - r->wanted_col_start - 1;
676 if (r->state == STATE_CELL_CONTENT
678 XML_READER_TYPE_TEXT == r->node_type)
680 xmlChar *value = xmlTextReaderValue (r->xtr);
682 if ( idx >= n_var_specs)
684 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
686 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
687 memset (var_spec + n_var_specs,
689 (n_var_specs - idx + 1) * sizeof (*var_spec));
690 n_var_specs = idx + 1;
692 var_spec[idx].firstval.text = 0;
693 var_spec[idx].firstval.value = 0;
694 var_spec[idx].firstval.type = 0;
696 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
702 /* Read in the first row of data */
703 while (1 == xmlTextReaderRead (r->xtr))
708 /* If the row is finished then stop for now */
709 if (r->state == STATE_TABLE && r->row > r->wanted_row_start + (opts->read_names ? 1 : 0))
712 idx = r->col - r->wanted_col_start - 1;
714 if ( r->state == STATE_CELL &&
715 XML_READER_TYPE_ELEMENT == r->node_type)
717 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
718 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
721 if ( r->state == STATE_CELL_CONTENT &&
722 XML_READER_TYPE_TEXT == r->node_type)
724 if ( idx >= n_var_specs)
726 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
727 var_spec [idx].name = NULL;
728 n_var_specs = idx + 1;
731 var_spec [idx].firstval.type = type;
732 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
733 var_spec [idx].firstval.value = val_string;
741 /* Create the dictionary and populate it */
742 r->spreadsheet.dict = r->dict = dict_create (
743 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
745 for (i = 0; i < n_var_specs ; ++i )
748 struct variable *var = NULL;
749 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
750 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
751 dict_create_var (r->dict, name, width);
754 var = dict_get_var (r->dict, i);
756 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
763 fmt = fmt_default_for_width (width);
765 var_set_both_formats (var, &fmt);
768 /* Create the first case, and cache it */
769 r->used_first_case = false;
771 if ( n_var_specs == 0 )
773 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
774 spreadsheet->file_name);
778 r->proto = caseproto_ref (dict_get_proto (r->dict));
779 r->first_case = case_create (r->proto);
780 case_set_missing (r->first_case);
782 for (i = 0 ; i < n_var_specs; ++i)
784 const struct variable *var = dict_get_var (r->dict, i);
786 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
789 /* Read in the first row of data */
790 while (1 == xmlTextReaderRead (r->xtr))
794 if (r->state == STATE_ROW)
798 // zip_reader_destroy (zreader);
801 for ( i = 0 ; i < n_var_specs ; ++i )
803 free (var_spec[i].firstval.type);
804 free (var_spec[i].firstval.value);
805 free (var_spec[i].firstval.text);
806 free (var_spec[i].name);
812 return casereader_create_sequential
816 &ods_file_casereader_class, r);
820 // zip_reader_destroy (zreader);
822 for ( i = 0 ; i < n_var_specs ; ++i )
824 free (var_spec[i].firstval.type);
825 free (var_spec[i].firstval.value);
826 free (var_spec[i].firstval.text);
827 free (var_spec[i].name);
832 dict_destroy (r->spreadsheet.dict);
833 r->spreadsheet.dict = NULL;
834 ods_file_casereader_destroy (NULL, r);
841 /* Reads and returns one case from READER's file. Returns a null
842 pointer on failure. */
843 static struct ccase *
844 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
846 struct ccase *c = NULL;
847 xmlChar *val_string = NULL;
848 xmlChar *type = NULL;
849 struct ods_reader *r = r_;
850 int current_row = r->row;
852 if (!r->used_first_case)
854 r->used_first_case = true;
855 return r->first_case;
859 /* Advance to the start of a row. (If there is one) */
860 while (r->state != STATE_ROW && 1 == xmlTextReaderRead (r->xtr))
866 if ( ! reading_target_sheet (r) || r->state < STATE_TABLE)
871 c = case_create (r->proto);
872 case_set_missing (c);
874 while (1 == xmlTextReaderRead (r->xtr))
878 if (r->row > current_row && r->state == STATE_ROW)
881 // printf ("%s:%d\n", __FILE__, __LINE__);
882 if (r->state == STATE_CELL &&
883 r->node_type == XML_READER_TYPE_ELEMENT)
885 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
886 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
889 if (r->state == STATE_CELL_CONTENT &&
890 r->node_type == XML_READER_TYPE_TEXT)
893 struct xml_value *xmv = xzalloc (sizeof *xmv);
894 xmv->text = xmlTextReaderValue (r->xtr);
895 xmv->value = val_string;
899 for (col = 0; col < r->col_span; ++col)
901 const int idx = r->col + col - r->wanted_col_start - 1;
902 const struct variable *var = dict_get_var (r->dict, idx);
903 convert_xml_to_value (c, var, xmv);
910 if ( r->state <= STATE_TABLE)