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;
129 struct sheet_detail *sheets;
130 int n_allocated_sheets;
132 struct caseproto *proto;
133 struct dictionary *dict;
134 struct ccase *first_case;
135 bool used_first_case;
138 struct string ods_errs;
143 reading_target_sheet (const struct ods_reader *r)
145 if (r->target_sheet_name != NULL)
147 if ( 0 == xmlStrcmp (r->target_sheet_name, r->current_sheet_name))
151 if (r->target_sheet_index == r->current_sheet + 1)
158 static void process_node (struct ods_reader *r);
162 ods_get_sheet_name (struct spreadsheet *s, int n)
165 struct ods_reader *or = (struct ods_reader *) s;
167 assert (n < s->n_sheets);
170 (or->n_allocated_sheets <= n)
172 (1 == (ret = xmlTextReaderRead (or->xtr)))
178 return or->sheets[n].name;
182 ods_get_sheet_range (struct spreadsheet *s, int n)
185 struct ods_reader *or = (struct ods_reader *) s;
187 assert (n < s->n_sheets);
191 (or->n_allocated_sheets <= n)
192 || (or->sheets[n].stop_row == -1) )
194 (1 == (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_)
212 struct ods_reader *r = r_;
217 xmlFreeTextReader (r->xtr);
220 if ( ! ds_is_empty (&r->ods_errs))
221 msg (ME, "%s", ds_cstr (&r->ods_errs));
223 ds_destroy (&r->ods_errs);
225 if ( ! r->used_first_case )
226 case_unref (r->first_case);
228 caseproto_unref (r->proto);
234 process_node (struct ods_reader *r)
236 xmlChar *name = xmlTextReaderName (r->xtr);
238 name = xmlStrdup (_xml ("--"));
241 r->node_type = xmlTextReaderNodeType (r->xtr);
246 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
247 XML_READER_TYPE_ELEMENT == r->node_type)
249 r->state = STATE_SPREADSHEET;
250 r->current_sheet = -1;
251 r->current_sheet_name = NULL;
254 case STATE_SPREADSHEET:
255 if (0 == xmlStrcasecmp (name, _xml("table:table"))
257 (XML_READER_TYPE_ELEMENT == r->node_type))
259 xmlFree (r->current_sheet_name);
260 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
264 if (r->current_sheet >= r->n_allocated_sheets)
266 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
267 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
268 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
269 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
270 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
271 r->sheets[r->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
277 r->state = STATE_TABLE;
279 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
280 XML_READER_TYPE_ELEMENT == r->node_type)
282 r->state = STATE_INIT;
286 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
287 (XML_READER_TYPE_ELEMENT == r->node_type))
290 xmlTextReaderGetAttribute (r->xtr,
291 _xml ("table:number-rows-repeated"));
293 int row_span = value ? _xmlchar_to_int (value) : 1;
298 if (! xmlTextReaderIsEmptyElement (r->xtr))
299 r->state = STATE_ROW;
301 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
302 (XML_READER_TYPE_END_ELEMENT == r->node_type))
304 r->state = STATE_SPREADSHEET;
308 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
310 (XML_READER_TYPE_ELEMENT == r->node_type))
313 xmlTextReaderGetAttribute (r->xtr,
314 _xml ("table:number-columns-repeated"));
316 r->col_span = value ? _xmlchar_to_int (value) : 1;
317 r->col += r->col_span;
319 if (! xmlTextReaderIsEmptyElement (r->xtr))
320 r->state = STATE_CELL;
324 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
326 (XML_READER_TYPE_END_ELEMENT == r->node_type))
328 r->state = STATE_TABLE;
332 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
334 ( XML_READER_TYPE_ELEMENT == r->node_type))
336 if (! xmlTextReaderIsEmptyElement (r->xtr))
337 r->state = STATE_CELL_CONTENT;
340 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
342 (XML_READER_TYPE_END_ELEMENT == r->node_type)
345 r->state = STATE_ROW;
348 case STATE_CELL_CONTENT:
349 assert (r->current_sheet >= 0);
350 assert (r->current_sheet < r->n_allocated_sheets);
352 if (r->sheets[r->current_sheet].start_row == -1)
353 r->sheets[r->current_sheet].start_row = r->row - 1;
356 (r->sheets[r->current_sheet].start_col == -1)
358 (r->sheets[r->current_sheet].start_col >= r->col - 1)
360 r->sheets[r->current_sheet].start_col = r->col - 1;
362 r->sheets[r->current_sheet].stop_row = r->row - 1;
364 if ( r->sheets[r->current_sheet].stop_col < r->col - 1)
365 r->sheets[r->current_sheet].stop_col = r->col - 1;
367 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
368 r->state = STATE_CELL;
379 A struct containing the parameters of a cell's value
392 struct xml_value firstval;
396 /* Determine the width that a xmv should probably have */
398 xmv_to_width (const struct xml_value *xmv, int fallback)
400 int width = SPREADSHEET_DEFAULT_WIDTH;
402 /* Non-strings always have zero width */
403 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
410 width = ROUND_UP (xmlStrlen (xmv->value),
411 SPREADSHEET_DEFAULT_WIDTH);
413 width = ROUND_UP (xmlStrlen (xmv->text),
414 SPREADSHEET_DEFAULT_WIDTH);
420 Sets the VAR of case C, to the value corresponding to the xml data
423 convert_xml_to_value (struct ccase *c, const struct variable *var,
424 const struct xml_value *xmv)
426 union value *v = case_data_rw (c, var);
428 if (xmv->value == NULL && xmv->text == NULL)
429 value_set_missing (v, var_get_width (var));
430 else if ( var_is_alpha (var))
431 /* Use the text field, because it seems that there is no
432 value field for strings */
433 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
436 const struct fmt_spec *fmt = var_get_write_format (var);
437 enum fmt_category fc = fmt_get_category (fmt->type);
439 assert ( fc != FMT_CAT_STRING);
441 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
443 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
447 const char *text = xmv->value ?
448 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
451 free (data_in (ss_cstr (text), "UTF-8",
461 /* Try to find out how many sheets there are in the "workbook" */
463 get_sheet_count (struct zip_reader *zreader)
465 xmlTextReaderPtr mxtr;
466 struct zip_member *meta = NULL;
467 meta = zip_member_open (zreader, "meta.xml");
472 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
473 (xmlInputCloseCallback) zip_member_finish,
474 meta, NULL, NULL, 0);
476 while (1 == xmlTextReaderRead (mxtr))
478 xmlChar *name = xmlTextReaderName (mxtr);
479 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
481 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
485 int s = _xmlchar_to_int (attr);
486 xmlFreeTextReader (mxtr);
496 xmlFreeTextReader (mxtr);
501 ods_error_handler (void *ctx, const char *mesg,
502 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
504 struct ods_reader *r = ctx;
506 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
508 r->spreadsheet.file_name,
509 xmlTextReaderLocatorLineNumber (loc),
515 init_reader (struct ods_reader *r, bool report_errors)
517 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
518 xmlTextReaderPtr xtr;
520 if ( content == NULL)
523 zip_member_ref (content);
526 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
527 (xmlInputCloseCallback) zip_member_finish,
529 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
535 r->spreadsheet.type = SPREADSHEET_ODS;
538 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
545 ods_probe (const char *filename, bool report_errors)
547 struct ods_reader *r;
548 struct string errs = DS_EMPTY_INITIALIZER;
550 struct zip_reader *zr = zip_reader_create (filename, &errs);
556 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
557 filename, ds_cstr (&errs));
562 sheet_count = get_sheet_count (zr);
564 r = xzalloc (sizeof *r);
567 if (! init_reader (r, report_errors))
572 r->spreadsheet.n_sheets = sheet_count;
573 r->n_allocated_sheets = 0;
578 r->spreadsheet.file_name = filename;
579 return &r->spreadsheet;
582 zip_reader_destroy (r->zreader);
589 ods_make_reader (struct spreadsheet *spreadsheet,
590 const struct spreadsheet_read_options *opts)
593 xmlChar *type = NULL;
594 unsigned long int vstart = 0;
595 casenumber n_cases = CASENUMBER_MAX;
597 struct var_spec *var_spec = NULL;
600 struct ods_reader *r = (struct ods_reader *) spreadsheet;
601 xmlChar *val_string = NULL;
604 r->read_names = opts->read_names;
605 ds_init_empty (&r->ods_errs);
608 if ( !init_reader (r, true))
611 if ( opts->cell_range )
613 if ( ! convert_cell_ref (opts->cell_range,
614 &r->start_col, &r->start_row,
615 &r->stop_col, &r->stop_row))
617 msg (SE, _("Invalid cell range `%s'"),
630 r->state = STATE_INIT;
631 r->target_sheet_name = BAD_CAST opts->sheet_name;
632 r->target_sheet_index = opts->sheet_index;
637 printf ("%s:%d %d,%d %d,%d\n", __FILE__, __LINE__,
645 /* Advance to the start of the cells for the target sheet */
646 while ( ! reading_target_sheet (r)
647 || r->state != STATE_ROW || r->row <= r->start_row )
649 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->start_row)
674 idx = r->col - r->start_col -1 ;
679 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
682 if (r->state == STATE_CELL_CONTENT
684 XML_READER_TYPE_TEXT == r->node_type)
686 xmlChar *value = xmlTextReaderValue (r->xtr);
688 if ( idx >= n_var_specs)
690 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
692 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
693 memset (var_spec + n_var_specs,
695 (n_var_specs - idx + 1) * sizeof (*var_spec));
696 n_var_specs = idx + 1;
698 var_spec[idx].firstval.text = 0;
699 var_spec[idx].firstval.value = 0;
700 var_spec[idx].firstval.type = 0;
702 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
709 /* Read in the first row of data */
710 while (1 == xmlTextReaderRead (r->xtr))
715 if ( ! reading_target_sheet (r) )
718 /* If the row is finished then stop for now */
719 if (r->state == STATE_TABLE &&
720 r->row > r->start_row + (opts->read_names ? 1 : 0))
723 idx = r->col - r->start_col - 1;
727 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
730 if ( r->state == STATE_CELL &&
731 XML_READER_TYPE_ELEMENT == r->node_type)
733 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
734 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
737 if ( r->state == STATE_CELL_CONTENT &&
738 XML_READER_TYPE_TEXT == r->node_type)
741 printf ("%s:%d Idx %d n_var_specs %d\n", __FILE__, __LINE__,
744 printf ("%s:%d Idx %d r_col %d\n", __FILE__, __LINE__,
748 if (idx >= n_var_specs)
750 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
751 var_spec [idx].name = NULL;
752 n_var_specs = idx + 1;
755 var_spec [idx].firstval.type = type;
756 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
757 var_spec [idx].firstval.value = val_string;
765 /* Create the dictionary and populate it */
766 r->spreadsheet.dict = r->dict = dict_create (
767 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
769 for (i = 0; i < n_var_specs ; ++i )
772 struct variable *var = NULL;
773 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
774 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
775 dict_create_var (r->dict, name, width);
778 var = dict_get_var (r->dict, i);
780 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
787 fmt = fmt_default_for_width (width);
789 var_set_both_formats (var, &fmt);
792 /* Create the first case, and cache it */
793 r->used_first_case = false;
795 if ( n_var_specs == 0 )
797 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
798 spreadsheet->file_name);
802 r->proto = caseproto_ref (dict_get_proto (r->dict));
803 r->first_case = case_create (r->proto);
804 case_set_missing (r->first_case);
806 for (i = 0 ; i < n_var_specs; ++i)
808 const struct variable *var = dict_get_var (r->dict, i);
810 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
813 /* Read in the first row of data */
814 while (1 == xmlTextReaderRead (r->xtr))
818 if (r->state == STATE_ROW)
822 // zip_reader_destroy (zreader);
824 for ( i = 0 ; i < n_var_specs ; ++i )
826 free (var_spec[i].firstval.type);
827 free (var_spec[i].firstval.value);
828 free (var_spec[i].firstval.text);
829 free (var_spec[i].name);
835 return casereader_create_sequential
839 &ods_file_casereader_class, r);
843 //zip_reader_destroy (zreader);
845 for ( i = 0 ; i < n_var_specs ; ++i )
847 free (var_spec[i].firstval.type);
848 free (var_spec[i].firstval.value);
849 free (var_spec[i].firstval.text);
850 free (var_spec[i].name);
855 dict_destroy (r->spreadsheet.dict);
856 r->spreadsheet.dict = NULL;
857 ods_file_casereader_destroy (NULL, r);
864 /* Reads and returns one case from READER's file. Returns a null
865 pointer on failure. */
866 static struct ccase *
867 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
869 struct ccase *c = NULL;
870 xmlChar *val_string = NULL;
871 xmlChar *type = NULL;
872 struct ods_reader *r = r_;
874 if (!r->used_first_case)
876 r->used_first_case = true;
877 return r->first_case;
881 /* Advance to the start of a row. (If there is one) */
882 while (r->state != STATE_ROW
883 && 1 == xmlTextReaderRead (r->xtr)
890 if ( ! reading_target_sheet (r)
891 || r->state < STATE_TABLE
892 || (r->stop_row != -1 && r->row > r->stop_row + 1)
898 c = case_create (r->proto);
899 case_set_missing (c);
901 while (1 == xmlTextReaderRead (r->xtr))
905 if ( r->stop_row != -1 && r->row > r->stop_row + 1)
908 if (r->state == STATE_CELL &&
909 r->node_type == XML_READER_TYPE_ELEMENT)
911 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
912 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
915 if (r->state == STATE_CELL_CONTENT &&
916 r->node_type == XML_READER_TYPE_TEXT)
919 struct xml_value *xmv = xzalloc (sizeof *xmv);
920 xmv->text = xmlTextReaderValue (r->xtr);
921 xmv->value = val_string;
925 for (col = 0; col < r->col_span; ++col)
927 const struct variable *var;
928 const int idx = r->col + col - r->start_col - 1;
931 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
934 var = dict_get_var (r->dict, idx);
935 convert_xml_to_value (c, var, xmv);
939 xmlFree (xmv->value);
943 if ( r->state <= STATE_TABLE)