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;
127 struct sheet_detail *sheets;
128 int n_allocated_sheets;
130 struct caseproto *proto;
131 struct dictionary *dict;
132 struct ccase *first_case;
133 bool used_first_case;
136 struct string ods_errs;
141 reading_target_sheet (const struct ods_reader *r)
143 if (r->target_sheet_name != NULL)
145 if ( 0 == xmlStrcmp (r->target_sheet_name, r->current_sheet_name))
149 if (r->target_sheet_index == r->current_sheet + 1)
156 static void process_node (struct ods_reader *r);
160 ods_get_sheet_name (struct spreadsheet *s, int n)
162 struct ods_reader *or = (struct ods_reader *) s;
164 assert (n < s->n_sheets);
167 (or->n_allocated_sheets <= n)
168 || or->state != STATE_SPREADSHEET
171 int ret = xmlTextReaderRead (or->xtr);
178 return or->sheets[n].name;
182 ods_get_sheet_range (struct spreadsheet *s, int n)
184 struct ods_reader *or = (struct ods_reader *) s;
186 assert (n < s->n_sheets);
189 (or->n_allocated_sheets <= n)
190 || (or->sheets[n].stop_row == -1)
191 || or->state != STATE_SPREADSHEET
194 int ret = xmlTextReaderRead (or->xtr);
201 return create_cell_ref (
202 or->sheets[n].start_col,
203 or->sheets[n].start_row,
204 or->sheets[n].stop_col,
205 or->sheets[n].stop_row);
210 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
213 struct ods_reader *r = r_;
218 xmlFreeTextReader (r->xtr);
221 if ( ! ds_is_empty (&r->ods_errs))
222 msg (ME, "%s", ds_cstr (&r->ods_errs));
224 ds_destroy (&r->ods_errs);
226 if ( ! r->used_first_case )
227 case_unref (r->first_case);
229 caseproto_unref (r->proto);
231 xmlFree (r->current_sheet_name);
233 for (i = 0; i < r->n_allocated_sheets; ++i)
235 xmlFree (r->sheets[i].name);
244 process_node (struct ods_reader *r)
246 xmlChar *name = xmlTextReaderName (r->xtr);
248 name = xmlStrdup (_xml ("--"));
251 r->node_type = xmlTextReaderNodeType (r->xtr);
256 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
257 XML_READER_TYPE_ELEMENT == r->node_type)
259 r->state = STATE_SPREADSHEET;
260 r->current_sheet = -1;
261 r->current_sheet_name = NULL;
264 case STATE_SPREADSHEET:
265 if (0 == xmlStrcasecmp (name, _xml("table:table"))
267 (XML_READER_TYPE_ELEMENT == r->node_type))
269 xmlFree (r->current_sheet_name);
270 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
274 if (r->current_sheet >= r->n_allocated_sheets)
276 assert (r->current_sheet == r->n_allocated_sheets);
277 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
278 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
279 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
280 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
281 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
282 r->sheets[r->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
288 r->state = STATE_TABLE;
290 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
291 XML_READER_TYPE_ELEMENT == r->node_type)
293 r->state = STATE_INIT;
297 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
298 (XML_READER_TYPE_ELEMENT == r->node_type))
301 xmlTextReaderGetAttribute (r->xtr,
302 _xml ("table:number-rows-repeated"));
304 int row_span = value ? _xmlchar_to_int (value) : 1;
309 if (! xmlTextReaderIsEmptyElement (r->xtr))
310 r->state = STATE_ROW;
314 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
315 (XML_READER_TYPE_END_ELEMENT == r->node_type))
317 r->state = STATE_SPREADSHEET;
321 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
323 (XML_READER_TYPE_ELEMENT == r->node_type))
326 xmlTextReaderGetAttribute (r->xtr,
327 _xml ("table:number-columns-repeated"));
329 r->col_span = value ? _xmlchar_to_int (value) : 1;
330 r->col += r->col_span;
332 if (! xmlTextReaderIsEmptyElement (r->xtr))
333 r->state = STATE_CELL;
337 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
339 (XML_READER_TYPE_END_ELEMENT == r->node_type))
341 r->state = STATE_TABLE;
345 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
347 ( XML_READER_TYPE_ELEMENT == r->node_type))
349 if (! xmlTextReaderIsEmptyElement (r->xtr))
350 r->state = STATE_CELL_CONTENT;
353 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
355 (XML_READER_TYPE_END_ELEMENT == r->node_type)
358 r->state = STATE_ROW;
361 case STATE_CELL_CONTENT:
362 assert (r->current_sheet >= 0);
363 assert (r->current_sheet < r->n_allocated_sheets);
365 if (r->sheets[r->current_sheet].start_row == -1)
366 r->sheets[r->current_sheet].start_row = r->row - 1;
369 (r->sheets[r->current_sheet].start_col == -1)
371 (r->sheets[r->current_sheet].start_col >= r->col - 1)
373 r->sheets[r->current_sheet].start_col = r->col - 1;
375 r->sheets[r->current_sheet].stop_row = r->row - 1;
377 if ( r->sheets[r->current_sheet].stop_col < r->col - 1)
378 r->sheets[r->current_sheet].stop_col = r->col - 1;
380 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
381 r->state = STATE_CELL;
392 A struct containing the parameters of a cell's value
405 struct xml_value firstval;
409 /* Determine the width that a xmv should probably have */
411 xmv_to_width (const struct xml_value *xmv, int fallback)
413 int width = SPREADSHEET_DEFAULT_WIDTH;
415 /* Non-strings always have zero width */
416 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
423 width = ROUND_UP (xmlStrlen (xmv->value),
424 SPREADSHEET_DEFAULT_WIDTH);
426 width = ROUND_UP (xmlStrlen (xmv->text),
427 SPREADSHEET_DEFAULT_WIDTH);
433 Sets the VAR of case C, to the value corresponding to the xml data
436 convert_xml_to_value (struct ccase *c, const struct variable *var,
437 const struct xml_value *xmv)
439 union value *v = case_data_rw (c, var);
441 if (xmv->value == NULL && xmv->text == NULL)
442 value_set_missing (v, var_get_width (var));
443 else if ( var_is_alpha (var))
444 /* Use the text field, because it seems that there is no
445 value field for strings */
446 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
449 const struct fmt_spec *fmt = var_get_write_format (var);
450 enum fmt_category fc = fmt_get_category (fmt->type);
452 assert ( fc != FMT_CAT_STRING);
454 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
456 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
460 const char *text = xmv->value ?
461 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
464 free (data_in (ss_cstr (text), "UTF-8",
474 /* Try to find out how many sheets there are in the "workbook" */
476 get_sheet_count (struct zip_reader *zreader)
478 xmlTextReaderPtr mxtr;
479 struct zip_member *meta = NULL;
480 meta = zip_member_open (zreader, "meta.xml");
485 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
486 (xmlInputCloseCallback) zip_member_finish,
487 meta, NULL, NULL, 0);
489 while (1 == xmlTextReaderRead (mxtr))
491 xmlChar *name = xmlTextReaderName (mxtr);
492 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
494 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
498 int s = _xmlchar_to_int (attr);
499 xmlFreeTextReader (mxtr);
509 xmlFreeTextReader (mxtr);
514 ods_error_handler (void *ctx, const char *mesg,
515 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
517 struct ods_reader *r = ctx;
519 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
521 r->spreadsheet.file_name,
522 xmlTextReaderLocatorLineNumber (loc),
528 init_reader (struct ods_reader *r, bool report_errors)
530 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
531 xmlTextReaderPtr xtr;
533 if ( content == NULL)
536 zip_member_ref (content);
539 xmlFreeTextReader (r->xtr);
541 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
542 (xmlInputCloseCallback) zip_member_finish,
544 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
550 r->spreadsheet.type = SPREADSHEET_ODS;
553 r->current_sheet = 0;
554 r->state = STATE_INIT;
557 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
564 ods_probe (const char *filename, bool report_errors)
566 struct ods_reader *r;
567 struct string errs = DS_EMPTY_INITIALIZER;
569 struct zip_reader *zr = zip_reader_create (filename, &errs);
575 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
576 filename, ds_cstr (&errs));
581 sheet_count = get_sheet_count (zr);
583 r = xzalloc (sizeof *r);
586 if (! init_reader (r, report_errors))
591 r->spreadsheet.n_sheets = sheet_count;
592 r->n_allocated_sheets = 0;
597 r->spreadsheet.file_name = filename;
598 return &r->spreadsheet;
601 zip_reader_destroy (r->zreader);
608 ods_make_reader (struct spreadsheet *spreadsheet,
609 const struct spreadsheet_read_options *opts)
612 xmlChar *type = NULL;
613 unsigned long int vstart = 0;
614 casenumber n_cases = CASENUMBER_MAX;
616 struct var_spec *var_spec = NULL;
619 struct ods_reader *r = (struct ods_reader *) spreadsheet;
620 xmlChar *val_string = NULL;
623 r->read_names = opts->read_names;
624 ds_init_empty (&r->ods_errs);
627 if ( !init_reader (r, true))
630 if ( opts->cell_range )
632 if ( ! convert_cell_ref (opts->cell_range,
633 &r->start_col, &r->start_row,
634 &r->stop_col, &r->stop_row))
636 msg (SE, _("Invalid cell range `%s'"),
649 r->state = STATE_INIT;
650 r->target_sheet_name = BAD_CAST opts->sheet_name;
651 r->target_sheet_index = opts->sheet_index;
655 /* Advance to the start of the cells for the target sheet */
656 while ( ! reading_target_sheet (r)
657 || r->state != STATE_ROW || r->row <= r->start_row )
659 if (1 != (ret = xmlTextReaderRead (r->xtr)))
667 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
668 spreadsheet->file_name);
672 if ( opts->read_names)
674 while (1 == (ret = xmlTextReaderRead (r->xtr)))
680 /* If the row is finished then stop for now */
681 if (r->state == STATE_TABLE && r->row > r->start_row)
684 idx = r->col - r->start_col -1 ;
689 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
692 if (r->state == STATE_CELL_CONTENT
694 XML_READER_TYPE_TEXT == r->node_type)
696 xmlChar *value = xmlTextReaderValue (r->xtr);
698 if ( idx >= n_var_specs)
700 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
702 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
703 memset (var_spec + n_var_specs,
705 (idx - n_var_specs + 1) * sizeof (*var_spec));
706 n_var_specs = idx + 1;
708 var_spec[idx].firstval.text = 0;
709 var_spec[idx].firstval.value = 0;
710 var_spec[idx].firstval.type = 0;
712 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
719 /* Read in the first row of data */
720 while (1 == xmlTextReaderRead (r->xtr))
725 if ( ! reading_target_sheet (r) )
728 /* If the row is finished then stop for now */
729 if (r->state == STATE_TABLE &&
730 r->row > r->start_row + (opts->read_names ? 1 : 0))
733 idx = r->col - r->start_col - 1;
737 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
740 if ( r->state == STATE_CELL &&
741 XML_READER_TYPE_ELEMENT == r->node_type)
743 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
744 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
747 if ( r->state == STATE_CELL_CONTENT &&
748 XML_READER_TYPE_TEXT == r->node_type)
750 if (idx >= n_var_specs)
752 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
753 memset (var_spec + n_var_specs,
755 (idx - n_var_specs + 1) * sizeof (*var_spec));
757 var_spec [idx].name = NULL;
758 n_var_specs = idx + 1;
761 var_spec [idx].firstval.type = type;
762 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
763 var_spec [idx].firstval.value = val_string;
771 /* Create the dictionary and populate it */
772 r->spreadsheet.dict = r->dict = dict_create (
773 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
775 for (i = 0; i < n_var_specs ; ++i )
778 struct variable *var = NULL;
779 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
780 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
781 dict_create_var (r->dict, name, width);
784 var = dict_get_var (r->dict, i);
786 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
793 fmt = fmt_default_for_width (width);
795 var_set_both_formats (var, &fmt);
798 /* Create the first case, and cache it */
799 r->used_first_case = false;
801 if ( n_var_specs == 0 )
803 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
804 spreadsheet->file_name);
808 r->proto = caseproto_ref (dict_get_proto (r->dict));
809 r->first_case = case_create (r->proto);
810 case_set_missing (r->first_case);
812 for (i = 0 ; i < n_var_specs; ++i)
814 const struct variable *var = dict_get_var (r->dict, i);
816 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
819 /* Read in the first row of data */
820 while (1 == xmlTextReaderRead (r->xtr))
824 if (r->state == STATE_ROW)
828 // zip_reader_destroy (zreader);
830 for ( i = 0 ; i < n_var_specs ; ++i )
832 free (var_spec[i].firstval.type);
833 free (var_spec[i].firstval.value);
834 free (var_spec[i].firstval.text);
835 free (var_spec[i].name);
841 return casereader_create_sequential
845 &ods_file_casereader_class, r);
849 //zip_reader_destroy (zreader);
851 for ( i = 0 ; i < n_var_specs ; ++i )
853 free (var_spec[i].firstval.type);
854 free (var_spec[i].firstval.value);
855 free (var_spec[i].firstval.text);
856 free (var_spec[i].name);
861 dict_destroy (r->spreadsheet.dict);
862 r->spreadsheet.dict = NULL;
863 ods_file_casereader_destroy (NULL, r);
870 /* Reads and returns one case from READER's file. Returns a null
871 pointer on failure. */
872 static struct ccase *
873 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
875 struct ccase *c = NULL;
876 xmlChar *val_string = NULL;
877 xmlChar *type = NULL;
878 struct ods_reader *r = r_;
880 if (!r->used_first_case)
882 r->used_first_case = true;
883 return r->first_case;
887 /* Advance to the start of a row. (If there is one) */
888 while (r->state != STATE_ROW
889 && 1 == xmlTextReaderRead (r->xtr)
896 if ( ! reading_target_sheet (r)
897 || r->state < STATE_TABLE
898 || (r->stop_row != -1 && r->row > r->stop_row + 1)
904 c = case_create (r->proto);
905 case_set_missing (c);
907 while (1 == xmlTextReaderRead (r->xtr))
911 if ( r->stop_row != -1 && r->row > r->stop_row + 1)
914 if (r->state == STATE_CELL &&
915 r->node_type == XML_READER_TYPE_ELEMENT)
917 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
918 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
921 if (r->state == STATE_CELL_CONTENT &&
922 r->node_type == XML_READER_TYPE_TEXT)
925 struct xml_value *xmv = xzalloc (sizeof *xmv);
926 xmv->text = xmlTextReaderValue (r->xtr);
927 xmv->value = val_string;
931 for (col = 0; col < r->col_span; ++col)
933 const struct variable *var;
934 const int idx = r->col - col - r->start_col - 1;
937 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
939 if (idx >= dict_get_var_cnt (r->dict))
942 var = dict_get_var (r->dict, idx);
943 convert_xml_to_value (c, var, xmv);
947 xmlFree (xmv->value);
951 if ( r->state <= STATE_TABLE)