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/minmax.h"
28 #define _(msgid) gettext (msgid)
29 #define N_(msgid) (msgid)
31 #include "ods-reader.h"
32 #include "spreadsheet-reader.h"
37 ods_open_reader (const struct spreadsheet_read_options *opts,
38 struct dictionary **dict)
40 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
47 #include "libpspp/zip-reader.h"
53 #include <libxml/xmlreader.h>
56 #include "data/format.h"
57 #include "data/case.h"
58 #include "data/casereader-provider.h"
59 #include "data/dictionary.h"
60 #include "data/identifier.h"
61 #include "data/value.h"
62 #include "data/variable.h"
63 #include "libpspp/i18n.h"
64 #include "libpspp/str.h"
66 #include "gl/xalloc.h"
68 static void ods_file_casereader_destroy (struct casereader *, void *);
70 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
72 static const struct casereader_class ods_file_casereader_class =
74 ods_file_casereader_read,
75 ods_file_casereader_destroy,
82 /* The name of the sheet (utf8 encoding) */
94 STATE_INIT = 0, /* Initial state */
95 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
96 STATE_TABLE, /* Found the sheet that we actually want */
97 STATE_ROW, /* Found the start of the cell array */
98 STATE_CELL, /* Found a cell */
99 STATE_CELL_CONTENT /* Found a the text within a cell */
104 struct spreadsheet spreadsheet;
105 struct zip_reader *zreader;
106 xmlTextReaderPtr xtr;
108 enum reader_state state;
115 const xmlChar *target_sheet;
116 int target_sheet_index;
123 struct sheet_detail *sheets;
124 int n_allocated_sheets;
126 struct caseproto *proto;
127 struct dictionary *dict;
128 struct ccase *first_case;
129 bool used_first_case;
132 struct string ods_errs;
136 static void process_node (struct ods_reader *r);
140 ods_get_sheet_name (struct spreadsheet *s, int n)
143 struct ods_reader *or = (struct ods_reader *) s;
145 assert (n < s->n_sheets);
148 (or->n_allocated_sheets <= n)
150 (1 == (ret = xmlTextReaderRead (or->xtr)))
156 return or->sheets[n].name;
160 ods_get_sheet_range (struct spreadsheet *s, int n)
163 struct ods_reader *or = (struct ods_reader *) s;
165 assert (n < s->n_sheets);
169 (or->n_allocated_sheets <= n)
170 || (or->sheets[n].stop_row == -1) )
172 (1 == (ret = xmlTextReaderRead (or->xtr)))
179 return create_cell_ref (
180 or->sheets[n].start_col,
181 or->sheets[n].start_row,
182 or->sheets[n].stop_col,
183 or->sheets[n].stop_row);
188 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
190 struct ods_reader *r = r_;
195 xmlFreeTextReader (r->xtr);
198 if ( ! ds_is_empty (&r->ods_errs))
199 msg (ME, "%s", ds_cstr (&r->ods_errs));
201 ds_destroy (&r->ods_errs);
203 if ( ! r->used_first_case )
204 case_unref (r->first_case);
206 caseproto_unref (r->proto);
212 process_node (struct ods_reader *r)
214 xmlChar *name = xmlTextReaderName (r->xtr);
216 name = xmlStrdup (_xml ("--"));
219 r->node_type = xmlTextReaderNodeType (r->xtr);
224 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
225 XML_READER_TYPE_ELEMENT == r->node_type)
227 printf ("%s:%d Start of Workbook %d: Rows %d\n", __FILE__, __LINE__,
228 r->sheet_index, r->row);
230 r->state = STATE_SPREADSHEET;
234 case STATE_SPREADSHEET:
235 if (0 == xmlStrcasecmp (name, _xml("table:table"))
237 (XML_READER_TYPE_ELEMENT == r->node_type))
239 xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
244 printf ("%s:%d Start of SHEET %d: Allocated Sheets %d\n", __FILE__, __LINE__,
245 r->sheet_index, r->n_allocated_sheets);
247 if (r->sheet_index >= r->n_allocated_sheets)
249 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
250 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
251 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
252 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
253 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
254 r->sheets[r->n_allocated_sheets - 1].name = value;
263 if ( r->target_sheet != NULL)
265 if ( 0 == xmlStrcmp (value, r->target_sheet))
267 r->sheet_found = true;
270 else if (r->target_sheet_index == r->sheet_index)
272 r->sheet_found = true;
274 r->state = STATE_TABLE;
276 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
277 XML_READER_TYPE_ELEMENT == r->node_type)
279 r->state = STATE_INIT;
280 printf ("%s:%d End of Workbook %d: Rows %d Cols %d\n", __FILE__, __LINE__,
281 r->sheet_index, r->row, r->col);
285 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
286 (XML_READER_TYPE_ELEMENT == r->node_type))
289 xmlTextReaderGetAttribute (r->xtr,
290 _xml ("table:number-rows-repeated"));
292 int row_span = value ? _xmlchar_to_int (value) : 1;
294 printf ("%s:%d Start of Row %d Span %d\n", __FILE__, __LINE__, r->row, row_span);
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 printf ("%s:%d End of SHEET %d %d,%d\n", __FILE__, __LINE__, r->sheet_index, r->row, r->col);
305 r->state = STATE_SPREADSHEET;
310 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
312 (XML_READER_TYPE_ELEMENT == r->node_type))
315 xmlTextReaderGetAttribute (r->xtr,
316 _xml ("table:number-columns-repeated"));
318 int col_span = value ? _xmlchar_to_int (value) : 1;
322 printf ("%s:%d (span %d) Start of Cell %d, %d\n", __FILE__, __LINE__,
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 /* Set the span back to the default */
333 printf ("%s:%d End of Cell: %d, %d\n", __FILE__, __LINE__, r->row, r->col);
334 r->state = STATE_TABLE;
338 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
340 ( XML_READER_TYPE_ELEMENT == r->node_type))
342 printf ("%s:%d Start of Cell Contents %d\n", __FILE__, __LINE__, r->row);
343 if (! xmlTextReaderIsEmptyElement (r->xtr))
344 r->state = STATE_CELL_CONTENT;
347 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
349 (XML_READER_TYPE_END_ELEMENT == r->node_type)
352 printf ("%s:%d End of Cell contents: Rows %d\n", __FILE__, __LINE__, r->row);
353 r->state = STATE_ROW;
356 case STATE_CELL_CONTENT:
357 assert (r->sheet_index >= 0);
358 assert (r->sheet_index < r->n_allocated_sheets);
360 if (r->sheets[r->sheet_index].start_row == -1)
361 r->sheets[r->sheet_index].start_row = r->row - 1;
364 (r->sheets[r->sheet_index].start_col == -1)
366 (r->sheets[r->sheet_index].start_col >= r->col - 1)
368 r->sheets[r->sheet_index].start_col = r->col - 1;
370 r->sheets[r->sheet_index].stop_row = r->row - 1;
371 printf ("Stop Row for %d is %d\n", r->sheet_index, r->sheets[r->sheet_index].stop_row);
373 if ( r->sheets[r->sheet_index].stop_col < r->col - 1)
374 r->sheets[r->sheet_index].stop_col = r->col - 1;
376 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
377 r->state = STATE_CELL;
388 A struct containing the parameters of a cell's value
401 struct xml_value firstval;
405 /* Determine the width that a xmv should probably have */
407 xmv_to_width (const struct xml_value *xmv, int fallback)
409 int width = SPREADSHEET_DEFAULT_WIDTH;
411 /* Non-strings always have zero width */
412 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
419 width = ROUND_UP (xmlStrlen (xmv->value),
420 SPREADSHEET_DEFAULT_WIDTH);
422 width = ROUND_UP (xmlStrlen (xmv->text),
423 SPREADSHEET_DEFAULT_WIDTH);
429 Sets the VAR of case C, to the value corresponding to the xml data
432 convert_xml_to_value (struct ccase *c, const struct variable *var,
433 const struct xml_value *xmv)
435 union value *v = case_data_rw (c, var);
437 if (xmv->value == NULL && xmv->text == NULL)
438 value_set_missing (v, var_get_width (var));
439 else if ( var_is_alpha (var))
440 /* Use the text field, because it seems that there is no
441 value field for strings */
442 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
446 const struct fmt_spec *fmt = var_get_write_format (var);
447 enum fmt_category fc = fmt_get_category (fmt->type);
449 assert ( fc != FMT_CAT_STRING);
452 xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
454 free (data_in (ss_cstr (text), "UTF-8",
463 /* Try to find out how many sheets there are in the "workbook" */
465 get_sheet_count (struct zip_reader *zreader)
467 xmlTextReaderPtr mxtr;
468 struct zip_member *meta = NULL;
469 meta = zip_member_open (zreader, "meta.xml");
474 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
475 (xmlInputCloseCallback) zip_member_finish,
476 meta, NULL, NULL, 0);
478 while (1 == xmlTextReaderRead (mxtr))
480 xmlChar *name = xmlTextReaderName (mxtr);
481 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
483 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
487 int s = _xmlchar_to_int (attr);
496 ods_error_handler (void *ctx, const char *mesg,
497 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
499 struct ods_reader *r = ctx;
501 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
503 r->spreadsheet.file_name,
504 xmlTextReaderLocatorLineNumber (loc),
510 init_reader (struct ods_reader *r, bool report_errors)
512 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
513 xmlTextReaderPtr xtr;
515 if ( content == NULL)
518 zip_member_ref (content);
521 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
522 (xmlInputCloseCallback) zip_member_finish,
524 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
530 r->spreadsheet.type = SPREADSHEET_ODS;
533 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
540 ods_probe (const char *filename, bool report_errors)
542 struct ods_reader *r;
543 struct string errs = DS_EMPTY_INITIALIZER;
545 struct zip_reader *zr = zip_reader_create (filename, &errs);
550 sheet_count = get_sheet_count (zr);
552 r = xzalloc (sizeof *r);
555 if (! init_reader (r, false))
560 r->spreadsheet.n_sheets = sheet_count;
561 r->n_allocated_sheets = 0;
567 printf ("%s:%d\n", __FILE__, __LINE__);
569 r->spreadsheet.file_name = filename;
570 return &r->spreadsheet;
573 zip_reader_destroy (r->zreader);
580 ods_make_reader (struct spreadsheet *spreadsheet,
581 const struct spreadsheet_read_options *opts)
584 xmlChar *type = NULL;
585 unsigned long int vstart = 0;
586 casenumber n_cases = CASENUMBER_MAX;
588 struct var_spec *var_spec = NULL;
591 struct ods_reader *r = (struct ods_reader *) spreadsheet;
592 xmlChar *val_string = NULL;
595 r->read_names = opts->read_names;
596 ds_init_empty (&r->ods_errs);
599 printf ("%s:%d\n", __FILE__, __LINE__);
601 if ( !init_reader (r, true))
604 printf ("%s:%d\n", __FILE__, __LINE__);
606 if ( opts->cell_range )
608 if ( ! convert_cell_ref (opts->cell_range,
609 &r->start_col, &r->start_row,
610 &r->stop_col, &r->stop_row))
612 msg (SE, _("Invalid cell range `%s'"),
625 r->state = STATE_INIT;
626 r->target_sheet = BAD_CAST opts->sheet_name;
627 r->target_sheet_index = opts->sheet_index;
631 printf ("%s:%d\n", __FILE__, __LINE__);
634 /* If CELLRANGE was given, then we know how many variables should be read */
635 if ( r->stop_col != -1 )
637 assert (var_spec == NULL);
638 n_var_specs = r->stop_col - r->start_col + 1;
639 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
640 memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
643 printf ("%s:%d Number of var specs %d\n", __FILE__, __LINE__, n_var_specs);
644 printf ("%s:%d Target Sjheet %d\n", __FILE__, __LINE__, r->target_sheet_index);
647 /* Advance to the start of the cells for the target sheet */
648 while ( r->sheet_index < r->target_sheet_index - 1 ||
649 r->state != STATE_TABLE
652 if (1 != (ret = xmlTextReaderRead (r->xtr)))
658 printf ("%s:%d\n", __FILE__, __LINE__);
662 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
663 spreadsheet->file_name);
667 printf ("%s:%d\n", __FILE__, __LINE__);
669 if ( opts->read_names)
671 while (1 == (ret = xmlTextReaderRead (r->xtr)))
677 /* If the row is finished then stop for now */
678 if (r->state == STATE_TABLE && r->row > r->start_row)
681 idx = r->col - r->start_col - 1;
683 if (r->state == STATE_CELL_CONTENT
685 XML_READER_TYPE_TEXT == r->node_type)
687 xmlChar *value = xmlTextReaderValue (r->xtr);
689 if ( idx >= n_var_specs)
691 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
693 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
694 memset (var_spec + n_var_specs,
696 (n_var_specs - idx + 1) * sizeof (*var_spec));
697 n_var_specs = idx + 1;
699 var_spec[idx].firstval.text = 0;
700 var_spec[idx].firstval.value = 0;
701 var_spec[idx].firstval.type = 0;
703 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
709 printf ("%s:%d N varspecs %d\n", __FILE__, __LINE__, n_var_specs);
711 /* Read in the first row of data */
712 while (1 == xmlTextReaderRead (r->xtr))
717 /* If the row is finished then stop for now */
718 if (r->state == STATE_TABLE && r->row > r->start_row + 1)
721 idx = r->col - r->start_col - 1;
723 if ( r->state == STATE_CELL &&
724 XML_READER_TYPE_ELEMENT == r->node_type)
726 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
727 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
730 if ( r->state == STATE_CELL_CONTENT &&
731 XML_READER_TYPE_TEXT == r->node_type)
734 if ( idx >= n_var_specs)
736 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
737 var_spec [idx].name = NULL;
738 n_var_specs = idx + 1;
741 var_spec [idx].firstval.type = type;
742 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
743 var_spec [idx].firstval.value = val_string;
750 printf ("%s:%d N varspecs %d\n", __FILE__, __LINE__, n_var_specs);
752 /* Create the dictionary and populate it */
753 r->spreadsheet.dict = r->dict = dict_create (
754 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
756 for (i = 0; i < n_var_specs ; ++i )
759 struct variable *var = NULL;
760 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
761 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
762 dict_create_var (r->dict, name, width);
765 var = dict_get_var (r->dict, i);
767 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
774 fmt = fmt_default_for_width (width);
776 var_set_both_formats (var, &fmt);
779 /* Create the first case, and cache it */
780 r->used_first_case = false;
782 if ( n_var_specs == 0 )
784 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
785 spreadsheet->file_name);
789 r->proto = caseproto_ref (dict_get_proto (r->dict));
790 r->first_case = case_create (r->proto);
791 case_set_missing (r->first_case);
793 printf ("%s:%d Hello %d\n", __FILE__, __LINE__, n_var_specs);
795 for ( i = 0 ; i < n_var_specs ; ++i )
797 const struct variable *var = dict_get_var (r->dict, i);
799 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
802 printf ("%s:%d Hello %d\n", __FILE__, __LINE__, n_var_specs);
804 // zip_reader_destroy (zreader);
807 for ( i = 0 ; i < n_var_specs ; ++i )
809 free (var_spec[i].firstval.type);
810 free (var_spec[i].firstval.value);
811 free (var_spec[i].firstval.text);
812 free (var_spec[i].name);
818 printf ("%s:%d Creating reader\n", __FILE__, __LINE__);
819 return casereader_create_sequential
823 &ods_file_casereader_class, r);
827 // zip_reader_destroy (zreader);
829 for ( i = 0 ; i < n_var_specs ; ++i )
831 free (var_spec[i].firstval.type);
832 free (var_spec[i].firstval.value);
833 free (var_spec[i].firstval.text);
834 free (var_spec[i].name);
839 dict_destroy (r->spreadsheet.dict);
840 r->spreadsheet.dict = NULL;
841 ods_file_casereader_destroy (NULL, r);
848 /* Reads and returns one case from READER's file. Returns a null
849 pointer on failure. */
850 static struct ccase *
851 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
853 struct ccase *c = NULL;
854 xmlChar *val_string = NULL;
855 struct ods_reader *r = r_;
856 int current_row = r->row;
858 printf ("%s:%d Reading CASE\n", __FILE__, __LINE__);
860 if ( !r->used_first_case )
862 r->used_first_case = true;
863 return r->first_case;
866 printf ("%s:%d Reading CASE\n", __FILE__, __LINE__);
868 if ( r->state > STATE_INIT)
870 c = case_create (r->proto);
871 case_set_missing (c);
874 if (r->state == STATE_SPREADSHEET)
876 printf ("%s:%d CASES ARE ENDED\n", __FILE__, __LINE__);
880 while (1 == xmlTextReaderRead (r->xtr))
883 if ( r->row > current_row)
887 if ( r->col < r->start_col || (r->stop_col != -1 && r->col > r->stop_col))
891 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
895 if ( r->stop_row != -1 && r->row > r->stop_row)
899 if ( r->state == STATE_CELL &&
900 r->node_type == XML_READER_TYPE_ELEMENT )
902 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
905 if ( r->state == STATE_CELL_CONTENT && r->node_type == XML_READER_TYPE_TEXT )
908 struct xml_value *xmv = xzalloc (sizeof *xmv);
909 xmv->text = xmlTextReaderValue (r->xtr);
910 xmv->value = val_string;
914 for (col = 0; col < r->span ; ++col)
916 const int idx = r->col + col - r->start_col;
918 const struct variable *var = dict_get_var (r->dict, idx);
920 convert_xml_to_value (c, var, xmv);
928 if ( r->state < STATE_TABLE)
932 if (NULL == c || (r->stop_row != -1 && r->row > r->stop_row + 1))