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;
113 xmlChar *current_sheet_name;
115 const xmlChar *target_sheet_name;
116 int target_sheet_index;
119 int wanted_row_start;
120 int wanted_col_start;
131 struct sheet_detail *sheets;
132 int n_allocated_sheets;
134 struct caseproto *proto;
135 struct dictionary *dict;
136 struct ccase *first_case;
137 bool used_first_case;
140 struct string ods_errs;
145 reading_target_sheet (const struct ods_reader *r)
147 if (r->target_sheet_name != NULL)
149 if ( 0 == xmlStrcmp (r->target_sheet_name, r->current_sheet_name))
153 if (r->target_sheet_index == r->current_sheet + 1)
160 static void process_node (struct ods_reader *r);
164 ods_get_sheet_name (struct spreadsheet *s, int n)
167 struct ods_reader *or = (struct ods_reader *) s;
169 assert (n < s->n_sheets);
172 (or->n_allocated_sheets <= n)
174 (1 == (ret = xmlTextReaderRead (or->xtr)))
180 return or->sheets[n].name;
184 ods_get_sheet_range (struct spreadsheet *s, int n)
187 struct ods_reader *or = (struct ods_reader *) s;
189 assert (n < s->n_sheets);
193 (or->n_allocated_sheets <= n)
194 || (or->sheets[n].stop_row == -1) )
196 (1 == (ret = xmlTextReaderRead (or->xtr)))
203 return create_cell_ref (
204 or->sheets[n].start_col,
205 or->sheets[n].start_row,
206 or->sheets[n].stop_col,
207 or->sheets[n].stop_row);
212 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
214 struct ods_reader *r = r_;
219 xmlFreeTextReader (r->xtr);
222 if ( ! ds_is_empty (&r->ods_errs))
223 msg (ME, "%s", ds_cstr (&r->ods_errs));
225 ds_destroy (&r->ods_errs);
227 if ( ! r->used_first_case )
228 case_unref (r->first_case);
230 caseproto_unref (r->proto);
236 process_node (struct ods_reader *r)
238 xmlChar *name = xmlTextReaderName (r->xtr);
240 name = xmlStrdup (_xml ("--"));
243 r->node_type = xmlTextReaderNodeType (r->xtr);
248 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
249 XML_READER_TYPE_ELEMENT == r->node_type)
251 r->state = STATE_SPREADSHEET;
252 r->current_sheet = -1;
253 r->current_sheet_name = NULL;
256 case STATE_SPREADSHEET:
257 if (0 == xmlStrcasecmp (name, _xml("table:table"))
259 (XML_READER_TYPE_ELEMENT == r->node_type))
261 xmlFree (r->current_sheet_name);
262 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
266 if (r->current_sheet >= r->n_allocated_sheets)
268 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
269 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
270 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
271 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
272 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
273 r->sheets[r->n_allocated_sheets - 1].name = xmlStrdup (r->current_sheet_name);
279 r->state = STATE_TABLE;
281 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
282 XML_READER_TYPE_ELEMENT == r->node_type)
284 r->state = STATE_INIT;
288 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
289 (XML_READER_TYPE_ELEMENT == r->node_type))
292 xmlTextReaderGetAttribute (r->xtr,
293 _xml ("table:number-rows-repeated"));
295 int row_span = value ? _xmlchar_to_int (value) : 1;
300 if (! xmlTextReaderIsEmptyElement (r->xtr))
301 r->state = STATE_ROW;
303 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
304 (XML_READER_TYPE_END_ELEMENT == r->node_type))
306 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 r->col_span = value ? _xmlchar_to_int (value) : 1;
319 r->col += r->col_span;
321 if (! xmlTextReaderIsEmptyElement (r->xtr))
322 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, ' ');
437 const struct fmt_spec *fmt = var_get_write_format (var);
438 enum fmt_category fc = fmt_get_category (fmt->type);
440 assert ( fc != FMT_CAT_STRING);
443 xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
445 free (data_in (ss_cstr (text), "UTF-8",
454 /* Try to find out how many sheets there are in the "workbook" */
456 get_sheet_count (struct zip_reader *zreader)
458 xmlTextReaderPtr mxtr;
459 struct zip_member *meta = NULL;
460 meta = zip_member_open (zreader, "meta.xml");
465 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
466 (xmlInputCloseCallback) zip_member_finish,
467 meta, NULL, NULL, 0);
469 while (1 == xmlTextReaderRead (mxtr))
471 xmlChar *name = xmlTextReaderName (mxtr);
472 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
474 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
478 int s = _xmlchar_to_int (attr);
487 ods_error_handler (void *ctx, const char *mesg,
488 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
490 struct ods_reader *r = ctx;
492 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
494 r->spreadsheet.file_name,
495 xmlTextReaderLocatorLineNumber (loc),
501 init_reader (struct ods_reader *r, bool report_errors)
503 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
504 xmlTextReaderPtr xtr;
506 if ( content == NULL)
509 zip_member_ref (content);
512 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
513 (xmlInputCloseCallback) zip_member_finish,
515 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
521 r->spreadsheet.type = SPREADSHEET_ODS;
524 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
531 ods_probe (const char *filename, bool report_errors)
533 struct ods_reader *r;
534 struct string errs = DS_EMPTY_INITIALIZER;
536 struct zip_reader *zr = zip_reader_create (filename, &errs);
541 sheet_count = get_sheet_count (zr);
543 r = xzalloc (sizeof *r);
546 if (! init_reader (r, report_errors))
551 r->spreadsheet.n_sheets = sheet_count;
552 r->n_allocated_sheets = 0;
557 r->spreadsheet.file_name = filename;
558 return &r->spreadsheet;
561 zip_reader_destroy (r->zreader);
568 ods_make_reader (struct spreadsheet *spreadsheet,
569 const struct spreadsheet_read_options *opts)
572 xmlChar *type = NULL;
573 unsigned long int vstart = 0;
574 casenumber n_cases = CASENUMBER_MAX;
576 struct var_spec *var_spec = NULL;
579 struct ods_reader *r = (struct ods_reader *) spreadsheet;
580 xmlChar *val_string = NULL;
583 r->read_names = opts->read_names;
584 ds_init_empty (&r->ods_errs);
587 if ( !init_reader (r, true))
591 if ( opts->cell_range )
593 if ( ! convert_cell_ref (opts->cell_range,
594 &r->start_col, &r->start_row,
595 &r->stop_col, &r->stop_row))
597 msg (SE, _("Invalid cell range `%s'"),
611 r->state = STATE_INIT;
612 r->target_sheet_name = BAD_CAST opts->sheet_name;
613 r->target_sheet_index = opts->sheet_index;
617 /* If CELLRANGE was given, then we know how many variables should be read */
618 if ( r->stop_col != -1 )
620 assert (var_spec == NULL);
621 n_var_specs = r->stop_col - r->start_col + 1;
622 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
623 memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
627 /* Advance to the start of the cells for the target sheet */
628 while ( ! reading_target_sheet (r) || r->state != STATE_ROW )
630 if (1 != (ret = xmlTextReaderRead (r->xtr)))
639 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
640 spreadsheet->file_name);
644 if ( opts->read_names)
646 while (1 == (ret = xmlTextReaderRead (r->xtr)))
652 /* If the row is finished then stop for now */
653 if (r->state == STATE_TABLE && r->row > r->wanted_row_start)
656 idx = r->col - r->wanted_col_start - 1;
658 if (r->state == STATE_CELL_CONTENT
660 XML_READER_TYPE_TEXT == r->node_type)
662 xmlChar *value = xmlTextReaderValue (r->xtr);
664 if ( idx >= n_var_specs)
666 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
668 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
669 memset (var_spec + n_var_specs,
671 (n_var_specs - idx + 1) * sizeof (*var_spec));
672 n_var_specs = idx + 1;
674 var_spec[idx].firstval.text = 0;
675 var_spec[idx].firstval.value = 0;
676 var_spec[idx].firstval.type = 0;
678 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
684 /* Read in the first row of data */
685 while (1 == xmlTextReaderRead (r->xtr))
690 /* If the row is finished then stop for now */
691 if (r->state == STATE_TABLE && r->row > r->wanted_row_start + (opts->read_names ? 1 : 0))
694 idx = r->col - r->wanted_col_start - 1;
696 if ( r->state == STATE_CELL &&
697 XML_READER_TYPE_ELEMENT == r->node_type)
699 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
700 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
703 if ( r->state == STATE_CELL_CONTENT &&
704 XML_READER_TYPE_TEXT == r->node_type)
706 if ( idx >= n_var_specs)
708 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
709 var_spec [idx].name = NULL;
710 n_var_specs = idx + 1;
713 var_spec [idx].firstval.type = type;
714 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
715 var_spec [idx].firstval.value = val_string;
723 /* Create the dictionary and populate it */
724 r->spreadsheet.dict = r->dict = dict_create (
725 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
727 for (i = 0; i < n_var_specs ; ++i )
730 struct variable *var = NULL;
731 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
732 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
733 dict_create_var (r->dict, name, width);
736 var = dict_get_var (r->dict, i);
738 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
745 fmt = fmt_default_for_width (width);
747 var_set_both_formats (var, &fmt);
750 /* Create the first case, and cache it */
751 r->used_first_case = false;
753 if ( n_var_specs == 0 )
755 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
756 spreadsheet->file_name);
760 r->proto = caseproto_ref (dict_get_proto (r->dict));
761 r->first_case = case_create (r->proto);
762 case_set_missing (r->first_case);
764 for (i = 0 ; i < n_var_specs; ++i)
766 const struct variable *var = dict_get_var (r->dict, i);
768 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
771 /* Read in the first row of data */
772 while (1 == xmlTextReaderRead (r->xtr))
776 if (r->state == STATE_ROW)
780 // zip_reader_destroy (zreader);
783 for ( i = 0 ; i < n_var_specs ; ++i )
785 free (var_spec[i].firstval.type);
786 free (var_spec[i].firstval.value);
787 free (var_spec[i].firstval.text);
788 free (var_spec[i].name);
794 return casereader_create_sequential
798 &ods_file_casereader_class, r);
802 // zip_reader_destroy (zreader);
804 for ( i = 0 ; i < n_var_specs ; ++i )
806 free (var_spec[i].firstval.type);
807 free (var_spec[i].firstval.value);
808 free (var_spec[i].firstval.text);
809 free (var_spec[i].name);
814 dict_destroy (r->spreadsheet.dict);
815 r->spreadsheet.dict = NULL;
816 ods_file_casereader_destroy (NULL, r);
823 /* Reads and returns one case from READER's file. Returns a null
824 pointer on failure. */
825 static struct ccase *
826 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
828 struct ccase *c = NULL;
829 xmlChar *val_string = NULL;
830 struct ods_reader *r = r_;
831 int current_row = r->row;
833 if (!r->used_first_case)
835 r->used_first_case = true;
836 return r->first_case;
839 if (reading_target_sheet (r) && r->state != STATE_TABLE)
841 c = case_create (r->proto);
842 case_set_missing (c);
845 while (1 == xmlTextReaderRead (r->xtr))
849 if (r->row > current_row && r->state == STATE_ROW)
853 if ( r->state == STATE_CELL &&
854 r->node_type == XML_READER_TYPE_ELEMENT )
856 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
859 if ( r->state == STATE_CELL_CONTENT &&
860 r->node_type == XML_READER_TYPE_TEXT )
863 struct xml_value *xmv = xzalloc (sizeof *xmv);
864 xmv->text = xmlTextReaderValue (r->xtr);
865 xmv->value = val_string;
868 for (col = 0; col < r->col_span; ++col)
870 const int idx = r->col + col - r->wanted_col_start - 1;
871 const struct variable *var = dict_get_var (r->dict, idx);
872 convert_xml_to_value (c, var, xmv);
880 if ( r->state == STATE_TABLE)