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 *);
70 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 xmlTextReaderPtr xtr;
107 enum reader_state state;
111 xmlChar *current_sheet_name;
119 struct spreadsheet spreadsheet;
120 struct zip_reader *zreader;
122 int target_sheet_index;
123 xmlChar *target_sheet_name;
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;
145 ods_destroy (struct spreadsheet *s)
148 struct ods_reader *r = (struct ods_reader *) s;
150 if (--r->ref_cnt == 0)
154 for (i = 0; i < r->n_allocated_sheets; ++i)
156 xmlFree (r->sheets[i].name);
159 zip_reader_destroy (r->zreader);
169 reading_target_sheet (const struct ods_reader *r)
171 if (r->target_sheet_name != NULL)
173 if ( 0 == xmlStrcmp (r->target_sheet_name, r->foo.current_sheet_name))
177 if (r->target_sheet_index == r->foo.current_sheet + 1)
184 static void process_node (struct ods_reader *or, struct foo *r);
188 ods_get_sheet_name (struct spreadsheet *s, int n)
190 struct ods_reader *r = (struct ods_reader *) s;
191 struct foo *or = &r->foo;
193 assert (n < s->n_sheets);
196 (r->n_allocated_sheets <= n)
197 || or->state != STATE_SPREADSHEET
200 int ret = xmlTextReaderRead (or->xtr);
204 process_node (r, or);
207 return r->sheets[n].name;
211 ods_get_sheet_range (struct spreadsheet *s, int n)
213 struct ods_reader *r = (struct ods_reader *) s;
214 struct foo *or = &r->foo;
216 assert (n < s->n_sheets);
219 (r->n_allocated_sheets <= n)
220 || (r->sheets[n].stop_row == -1)
221 || or->state != STATE_SPREADSHEET
224 int ret = xmlTextReaderRead (or->xtr);
228 process_node (r, or);
231 return create_cell_ref (
232 r->sheets[n].start_col,
233 r->sheets[n].start_row,
234 r->sheets[n].stop_col,
235 r->sheets[n].stop_row);
240 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
243 struct ods_reader *r = r_;
248 xmlFreeTextReader (r->xtr);
251 if ( ! ds_is_empty (&r->ods_errs))
252 msg (ME, "%s", ds_cstr (&r->ods_errs));
254 ds_destroy (&r->ods_errs);
256 if ( ! r->used_first_case )
257 case_unref (r->first_case);
259 caseproto_unref (r->proto);
261 xmlFree (r->current_sheet_name);
262 xmlFree (r->target_sheet_name);
264 ods_destroy (&r->spreadsheet);
273 process_node (struct ods_reader *or, struct foo *r)
275 xmlChar *name = xmlTextReaderName (r->xtr);
277 name = xmlStrdup (_xml ("--"));
280 r->node_type = xmlTextReaderNodeType (r->xtr);
285 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
286 XML_READER_TYPE_ELEMENT == r->node_type)
288 r->state = STATE_SPREADSHEET;
289 r->current_sheet = -1;
290 r->current_sheet_name = NULL;
293 case STATE_SPREADSHEET:
294 if (0 == xmlStrcasecmp (name, _xml("table:table"))
296 (XML_READER_TYPE_ELEMENT == r->node_type))
298 xmlFree (r->current_sheet_name);
299 r->current_sheet_name = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
303 if (r->current_sheet >= or->n_allocated_sheets)
305 assert (r->current_sheet == or->n_allocated_sheets);
306 or->sheets = xrealloc (or->sheets, sizeof (*or->sheets) * ++or->n_allocated_sheets);
307 or->sheets[or->n_allocated_sheets - 1].start_col = -1;
308 or->sheets[or->n_allocated_sheets - 1].stop_col = -1;
309 or->sheets[or->n_allocated_sheets - 1].start_row = -1;
310 or->sheets[or->n_allocated_sheets - 1].stop_row = -1;
311 or->sheets[or->n_allocated_sheets - 1].name = CHAR_CAST (char *, xmlStrdup (r->current_sheet_name));
317 r->state = STATE_TABLE;
319 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
320 XML_READER_TYPE_ELEMENT == r->node_type)
322 r->state = STATE_INIT;
326 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
327 (XML_READER_TYPE_ELEMENT == r->node_type))
330 xmlTextReaderGetAttribute (r->xtr,
331 _xml ("table:number-rows-repeated"));
333 int row_span = value ? _xmlchar_to_int (value) : 1;
338 if (! xmlTextReaderIsEmptyElement (r->xtr))
339 r->state = STATE_ROW;
343 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
344 (XML_READER_TYPE_END_ELEMENT == r->node_type))
346 r->state = STATE_SPREADSHEET;
350 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
352 (XML_READER_TYPE_ELEMENT == r->node_type))
355 xmlTextReaderGetAttribute (r->xtr,
356 _xml ("table:number-columns-repeated"));
358 r->col_span = value ? _xmlchar_to_int (value) : 1;
359 r->col += r->col_span;
361 if (! xmlTextReaderIsEmptyElement (r->xtr))
362 r->state = STATE_CELL;
366 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
368 (XML_READER_TYPE_END_ELEMENT == r->node_type))
370 r->state = STATE_TABLE;
374 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
376 ( XML_READER_TYPE_ELEMENT == r->node_type))
378 if (! xmlTextReaderIsEmptyElement (r->xtr))
379 r->state = STATE_CELL_CONTENT;
382 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
384 (XML_READER_TYPE_END_ELEMENT == r->node_type)
387 r->state = STATE_ROW;
390 case STATE_CELL_CONTENT:
391 assert (r->current_sheet >= 0);
392 assert (r->current_sheet < or->n_allocated_sheets);
394 if (or->sheets[r->current_sheet].start_row == -1)
395 or->sheets[r->current_sheet].start_row = r->row - 1;
398 (or->sheets[r->current_sheet].start_col == -1)
400 (or->sheets[r->current_sheet].start_col >= r->col - 1)
402 or->sheets[r->current_sheet].start_col = r->col - 1;
404 or->sheets[r->current_sheet].stop_row = r->row - 1;
406 if ( or->sheets[r->current_sheet].stop_col < r->col - 1)
407 or->sheets[r->current_sheet].stop_col = r->col - 1;
409 if (XML_READER_TYPE_END_ELEMENT == r->node_type)
410 r->state = STATE_CELL;
421 A struct containing the parameters of a cell's value
434 struct xml_value firstval;
438 /* Determine the width that a xmv should probably have */
440 xmv_to_width (const struct xml_value *xmv, int fallback)
442 int width = SPREADSHEET_DEFAULT_WIDTH;
444 /* Non-strings always have zero width */
445 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
452 width = ROUND_UP (xmlStrlen (xmv->value),
453 SPREADSHEET_DEFAULT_WIDTH);
455 width = ROUND_UP (xmlStrlen (xmv->text),
456 SPREADSHEET_DEFAULT_WIDTH);
462 Sets the VAR of case C, to the value corresponding to the xml data
465 convert_xml_to_value (struct ccase *c, const struct variable *var,
466 const struct xml_value *xmv)
468 union value *v = case_data_rw (c, var);
470 if (xmv->value == NULL && xmv->text == NULL)
471 value_set_missing (v, var_get_width (var));
472 else if ( var_is_alpha (var))
473 /* Use the text field, because it seems that there is no
474 value field for strings */
475 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
478 const struct fmt_spec *fmt = var_get_write_format (var);
479 enum fmt_category fc = fmt_get_category (fmt->type);
481 assert ( fc != FMT_CAT_STRING);
483 if ( 0 == xmlStrcmp (xmv->type, _xml("float")))
485 v->f = c_strtod (CHAR_CAST (const char *, xmv->value), NULL);
489 const char *text = xmv->value ?
490 CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
493 free (data_in (ss_cstr (text), "UTF-8",
503 /* Try to find out how many sheets there are in the "workbook" */
505 get_sheet_count (struct zip_reader *zreader)
507 xmlTextReaderPtr mxtr;
508 struct zip_member *meta = NULL;
509 meta = zip_member_open (zreader, "meta.xml");
514 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
515 (xmlInputCloseCallback) NULL,
516 meta, NULL, NULL, 0);
518 while (1 == xmlTextReaderRead (mxtr))
520 xmlChar *name = xmlTextReaderName (mxtr);
521 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
523 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
527 int s = _xmlchar_to_int (attr);
528 xmlFreeTextReader (mxtr);
538 xmlFreeTextReader (mxtr);
543 ods_error_handler (void *ctx, const char *mesg,
544 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
546 struct ods_reader *r = ctx;
548 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
550 r->spreadsheet.file_name,
551 xmlTextReaderLocatorLineNumber (loc),
557 init_reader (struct ods_reader *r, bool report_errors)
559 struct zip_member *content = zip_member_open (r->zreader, "content.xml");
560 xmlTextReaderPtr xtr;
562 if ( content == NULL)
566 xmlFreeTextReader (r->foo.xtr);
568 zip_member_ref (content);
569 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
570 (xmlInputCloseCallback) zip_member_finish,
572 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
578 r->spreadsheet.type = SPREADSHEET_ODS;
581 r->foo.current_sheet = 0;
582 r->foo.state = STATE_INIT;
585 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
592 ods_probe (const char *filename, bool report_errors)
594 struct ods_reader *r;
595 struct string errs = DS_EMPTY_INITIALIZER;
597 struct zip_reader *zr = zip_reader_create (filename, &errs);
603 msg (ME, _("Cannot open %s as a OpenDocument file: %s"),
604 filename, ds_cstr (&errs));
609 sheet_count = get_sheet_count (zr);
611 r = xzalloc (sizeof *r);
615 if (! init_reader (r, report_errors))
620 r->spreadsheet.n_sheets = sheet_count;
621 r->n_allocated_sheets = 0;
626 r->spreadsheet.file_name = filename;
627 return &r->spreadsheet;
630 zip_reader_destroy (r->zreader);
637 ods_make_reader (struct spreadsheet *spreadsheet,
638 const struct spreadsheet_read_options *opts)
641 xmlChar *type = NULL;
642 unsigned long int vstart = 0;
643 casenumber n_cases = CASENUMBER_MAX;
645 struct var_spec *var_spec = NULL;
648 struct ods_reader *r = (struct ods_reader *) spreadsheet;
649 xmlChar *val_string = NULL;
652 r->read_names = opts->read_names;
653 ds_init_empty (&r->ods_errs);
656 if ( !init_reader (r, true))
659 if (opts->cell_range)
661 if ( ! convert_cell_ref (opts->cell_range,
662 &r->start_col, &r->start_row,
663 &r->stop_col, &r->stop_row))
665 msg (SE, _("Invalid cell range `%s'"),
678 r->foo.state = STATE_INIT;
679 r->target_sheet_name = xmlStrdup (BAD_CAST opts->sheet_name);
680 r->target_sheet_index = opts->sheet_index;
681 r->foo.row = r->foo.col = 0;
684 /* Advance to the start of the cells for the target sheet */
685 while ( ! reading_target_sheet (r)
686 || r->foo.state != STATE_ROW || r->foo.row <= r->start_row )
688 if (1 != (ret = xmlTextReaderRead (r->foo.xtr)))
691 process_node (r, &r->foo);
696 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
697 spreadsheet->file_name);
701 if ( opts->read_names)
703 while (1 == (ret = xmlTextReaderRead (r->foo.xtr)))
707 process_node (r, &r->foo);
709 /* If the row is finished then stop for now */
710 if (r->foo.state == STATE_TABLE && r->foo.row > r->start_row)
713 idx = r->foo.col - r->start_col -1 ;
718 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
721 if (r->foo.state == STATE_CELL_CONTENT
723 XML_READER_TYPE_TEXT == r->foo.node_type)
725 xmlChar *value = xmlTextReaderValue (r->foo.xtr);
727 if ( idx >= n_var_specs)
729 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
731 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
732 memset (var_spec + n_var_specs,
734 (idx - n_var_specs + 1) * sizeof (*var_spec));
735 n_var_specs = idx + 1;
737 var_spec[idx].firstval.text = 0;
738 var_spec[idx].firstval.value = 0;
739 var_spec[idx].firstval.type = 0;
741 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
748 /* Read in the first row of data */
749 while (1 == xmlTextReaderRead (r->foo.xtr))
752 process_node (r, &r->foo);
754 if ( ! reading_target_sheet (r) )
757 /* If the row is finished then stop for now */
758 if (r->foo.state == STATE_TABLE &&
759 r->foo.row > r->start_row + (opts->read_names ? 1 : 0))
762 idx = r->foo.col - r->start_col - 1;
766 if (r->stop_col != -1 && idx > r->stop_col - r->start_col)
769 if ( r->foo.state == STATE_CELL &&
770 XML_READER_TYPE_ELEMENT == r->foo.node_type)
772 type = xmlTextReaderGetAttribute (r->foo.xtr, _xml ("office:value-type"));
773 val_string = xmlTextReaderGetAttribute (r->foo.xtr, _xml ("office:value"));
776 if ( r->foo.state == STATE_CELL_CONTENT &&
777 XML_READER_TYPE_TEXT == r->foo.node_type)
779 if (idx >= n_var_specs)
781 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
782 memset (var_spec + n_var_specs,
784 (idx - n_var_specs + 1) * sizeof (*var_spec));
786 var_spec [idx].name = NULL;
787 n_var_specs = idx + 1;
790 var_spec [idx].firstval.type = type;
791 var_spec [idx].firstval.text = xmlTextReaderValue (r->foo.xtr);
792 var_spec [idx].firstval.value = val_string;
800 /* Create the dictionary and populate it */
801 r->spreadsheet.dict = r->dict = dict_create (
802 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->foo.xtr)));
804 for (i = 0; i < n_var_specs ; ++i )
807 struct variable *var = NULL;
808 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
809 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
810 dict_create_var (r->dict, name, width);
813 var = dict_get_var (r->dict, i);
815 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
822 fmt = fmt_default_for_width (width);
824 var_set_both_formats (var, &fmt);
827 /* Create the first case, and cache it */
828 r->used_first_case = false;
830 if ( n_var_specs == 0 )
832 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
833 spreadsheet->file_name);
837 r->proto = caseproto_ref (dict_get_proto (r->dict));
838 r->first_case = case_create (r->proto);
839 case_set_missing (r->first_case);
841 for (i = 0 ; i < n_var_specs; ++i)
843 const struct variable *var = dict_get_var (r->dict, i);
845 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
848 /* Read in the first row of data */
849 while (1 == xmlTextReaderRead (r->foo.xtr))
851 process_node (r, &r->foo);
853 if (r->foo.state == STATE_ROW)
858 for ( i = 0 ; i < n_var_specs ; ++i )
860 free (var_spec[i].firstval.type);
861 free (var_spec[i].firstval.value);
862 free (var_spec[i].firstval.text);
863 free (var_spec[i].name);
869 return casereader_create_sequential
873 &ods_file_casereader_class, r);
877 for ( i = 0 ; i < n_var_specs ; ++i )
879 free (var_spec[i].firstval.type);
880 free (var_spec[i].firstval.value);
881 free (var_spec[i].firstval.text);
882 free (var_spec[i].name);
887 dict_destroy (r->spreadsheet.dict);
888 r->spreadsheet.dict = NULL;
889 ods_file_casereader_destroy (NULL, r);
895 /* Reads and returns one case from READER's file. Returns a null
896 pointer on failure. */
897 static struct ccase *
898 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
900 struct ccase *c = NULL;
901 xmlChar *val_string = NULL;
902 xmlChar *type = NULL;
903 struct ods_reader *r = r_;
905 if (!r->used_first_case)
907 r->used_first_case = true;
908 return r->first_case;
912 /* Advance to the start of a row. (If there is one) */
913 while (r->foo.state != STATE_ROW
914 && 1 == xmlTextReaderRead (r->foo.xtr)
917 process_node (r, &r->foo);
921 if ( ! reading_target_sheet (r)
922 || r->foo.state < STATE_TABLE
923 || (r->stop_row != -1 && r->foo.row > r->stop_row + 1)
929 c = case_create (r->proto);
930 case_set_missing (c);
932 while (1 == xmlTextReaderRead (r->foo.xtr))
934 process_node (r, &r->foo);
936 if ( r->stop_row != -1 && r->foo.row > r->stop_row + 1)
939 if (r->foo.state == STATE_CELL &&
940 r->foo.node_type == XML_READER_TYPE_ELEMENT)
942 type = xmlTextReaderGetAttribute (r->foo.xtr, _xml ("office:value-type"));
943 val_string = xmlTextReaderGetAttribute (r->foo.xtr, _xml ("office:value"));
946 if (r->foo.state == STATE_CELL_CONTENT &&
947 r->foo.node_type == XML_READER_TYPE_TEXT)
950 struct xml_value *xmv = xzalloc (sizeof *xmv);
951 xmv->text = xmlTextReaderValue (r->foo.xtr);
952 xmv->value = val_string;
956 for (col = 0; col < r->foo.col_span; ++col)
958 const struct variable *var;
959 const int idx = r->foo.col - col - r->start_col - 1;
962 if (r->stop_col != -1 && idx > r->stop_col - r->start_col )
964 if (idx >= dict_get_var_cnt (r->dict))
967 var = dict_get_var (r->dict, idx);
968 convert_xml_to_value (c, var, xmv);
972 xmlFree (xmv->value);
976 if ( r->foo.state <= STATE_TABLE)