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;
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);
168 (or->n_allocated_sheets <= n || or->state != STATE_SPREADSHEET)
170 (1 == (ret = xmlTextReaderRead (or->xtr)))
176 return create_cell_ref (
177 or->sheets[n].start_col,
178 or->sheets[n].start_row,
179 or->sheets[n].stop_col,
180 or->sheets[n].stop_row);
185 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
187 struct ods_reader *r = r_;
192 xmlFreeTextReader (r->xtr);
194 if ( ! ds_is_empty (&r->ods_errs))
195 msg (ME, "%s", ds_cstr (&r->ods_errs));
197 ds_destroy (&r->ods_errs);
199 if ( ! r->used_first_case )
200 case_unref (r->first_case);
202 caseproto_unref (r->proto);
208 process_node (struct ods_reader *r)
210 xmlChar *name = xmlTextReaderName (r->xtr);
212 name = xmlStrdup (_xml ("--"));
215 r->node_type = xmlTextReaderNodeType (r->xtr);
220 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
221 XML_READER_TYPE_ELEMENT == r->node_type)
223 printf ("%s:%d Start of Workbook %d: Rows %d\n", __FILE__, __LINE__,
224 r->sheet_index, r->row);
226 r->state = STATE_SPREADSHEET;
229 case STATE_SPREADSHEET:
230 if (0 == xmlStrcasecmp (name, _xml("table:table"))
232 (XML_READER_TYPE_ELEMENT == r->node_type))
234 xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
235 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
236 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
237 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
238 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
239 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
240 r->sheets[r->n_allocated_sheets - 1].name = value;
245 printf ("%s:%d Start of SHEET %d: Rows %d\n", __FILE__, __LINE__,
246 r->sheet_index, r->row);
248 if ( r->target_sheet != NULL)
250 if ( 0 == xmlStrcmp (value, r->target_sheet))
252 r->sheet_found = true;
255 else if (r->target_sheet_index == r->sheet_index)
257 r->sheet_found = true;
259 r->state = STATE_TABLE;
261 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
262 XML_READER_TYPE_ELEMENT == r->node_type)
264 r->state = STATE_INIT;
265 printf ("%s:%d End of Workbook %d: Rows %d Cols %d\n", __FILE__, __LINE__,
266 r->sheet_index, r->row, r->col);
270 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
271 (XML_READER_TYPE_ELEMENT == r->node_type))
274 xmlTextReaderGetAttribute (r->xtr,
275 _xml ("table:number-rows-repeated"));
277 int row_span = value ? _xmlchar_to_int (value) : 1;
279 printf ("%s:%d Start of Row %d Span %d\n", __FILE__, __LINE__, r->row, row_span);
283 if (! xmlTextReaderIsEmptyElement (r->xtr))
284 r->state = STATE_ROW;
286 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
287 (XML_READER_TYPE_END_ELEMENT == r->node_type))
289 printf ("%s:%d End of SHEET %d %d,%d\n", __FILE__, __LINE__, r->sheet_index, r->row, r->col);
290 r->state = STATE_SPREADSHEET;
295 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
297 (XML_READER_TYPE_ELEMENT == r->node_type))
300 xmlTextReaderGetAttribute (r->xtr,
301 _xml ("table:number-columns-repeated"));
303 int col_span = value ? _xmlchar_to_int (value) : 1;
307 printf ("%s:%d (span %d) Start of Cell %d, %d\n", __FILE__, __LINE__,
310 if (! xmlTextReaderIsEmptyElement (r->xtr))
311 r->state = STATE_CELL;
313 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
315 (XML_READER_TYPE_END_ELEMENT == r->node_type))
317 /* Set the span back to the default */
318 printf ("%s:%d End of Cell: %d, %d\n", __FILE__, __LINE__, r->row, r->col);
319 r->state = STATE_TABLE;
323 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
325 ( XML_READER_TYPE_ELEMENT == r->node_type))
327 // printf ("%s:%d Start of Cell Contents %d\n", __FILE__, __LINE__, r->row);
328 if (! xmlTextReaderIsEmptyElement (r->xtr))
329 r->state = STATE_CELL_CONTENT;
332 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
334 (XML_READER_TYPE_END_ELEMENT == r->node_type)
337 // printf ("%s:%d End of Cell contents: Rows %d\n", __FILE__, __LINE__, r->row);
338 r->state = STATE_ROW;
341 case STATE_CELL_CONTENT:
342 if (r->sheets[r->sheet_index].start_row == -1)
343 r->sheets[r->sheet_index].start_row = r->row - 1;
346 (r->sheets[r->sheet_index].start_col == -1)
348 (r->sheets[r->sheet_index].start_col >= r->col - 1)
350 r->sheets[r->sheet_index].start_col = r->col - 1;
352 r->sheets[r->sheet_index].stop_row = r->row - 1;
354 if ( r->sheets[r->sheet_index].stop_col < r->col - 1)
355 r->sheets[r->sheet_index].stop_col = r->col - 1;
357 r->state = STATE_CELL;
368 A struct containing the parameters of a cell's value
381 struct xml_value firstval;
385 /* Determine the width that a xmv should probably have */
387 xmv_to_width (const struct xml_value *xmv, int fallback)
389 int width = SPREADSHEET_DEFAULT_WIDTH;
391 /* Non-strings always have zero width */
392 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
399 width = ROUND_UP (xmlStrlen (xmv->value),
400 SPREADSHEET_DEFAULT_WIDTH);
402 width = ROUND_UP (xmlStrlen (xmv->text),
403 SPREADSHEET_DEFAULT_WIDTH);
409 Sets the VAR of case C, to the value corresponding to the xml data
412 convert_xml_to_value (struct ccase *c, const struct variable *var,
413 const struct xml_value *xmv)
415 union value *v = case_data_rw (c, var);
417 if (xmv->value == NULL && xmv->text == NULL)
418 value_set_missing (v, var_get_width (var));
419 else if ( var_is_alpha (var))
420 /* Use the text field, because it seems that there is no
421 value field for strings */
422 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
426 const struct fmt_spec *fmt = var_get_write_format (var);
427 enum fmt_category fc = fmt_get_category (fmt->type);
429 assert ( fc != FMT_CAT_STRING);
432 xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
434 free (data_in (ss_cstr (text), "UTF-8",
443 /* Try to find out how many sheets there are in the "workbook" */
445 get_sheet_count (struct zip_reader *zreader)
447 xmlTextReaderPtr mxtr;
448 struct zip_member *meta = NULL;
449 meta = zip_member_open (zreader, "meta.xml");
454 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
455 (xmlInputCloseCallback) zip_member_finish,
456 meta, NULL, NULL, 0);
458 while (1 == xmlTextReaderRead (mxtr))
460 xmlChar *name = xmlTextReaderName (mxtr);
461 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
463 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
467 int s = _xmlchar_to_int (attr);
476 ods_error_handler (void *ctx, const char *mesg,
477 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
479 struct ods_reader *r = ctx;
481 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
483 r->spreadsheet.file_name,
484 xmlTextReaderLocatorLineNumber (loc),
490 ods_probe (const char *filename, bool report_errors)
492 struct ods_reader *r;
494 xmlTextReaderPtr xtr ;
496 struct zip_member *content = NULL;
498 struct zip_reader *zreader = NULL;
500 ds_init_empty (&errs);
502 zreader = zip_reader_create (filename, &errs);
507 content = zip_member_open (zreader, "content.xml");
509 if ( content == NULL)
512 zip_member_ref (content);
514 sheet_count = get_sheet_count (zreader);
516 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
517 (xmlInputCloseCallback) zip_member_finish,
519 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
524 r = xzalloc (sizeof *r);
526 r->spreadsheet.type = SPREADSHEET_ODS;
527 r->spreadsheet.n_sheets = sheet_count;
528 r->n_allocated_sheets = 0;
533 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
538 printf ("%s:%d\n", __FILE__, __LINE__);
540 r->spreadsheet.file_name = filename;
541 return &r->spreadsheet;
544 zip_reader_destroy (zreader);
550 ods_make_reader (struct spreadsheet *spreadsheet,
551 const struct spreadsheet_read_options *opts)
554 xmlChar *type = NULL;
555 unsigned long int vstart = 0;
556 casenumber n_cases = CASENUMBER_MAX;
558 struct var_spec *var_spec = NULL;
561 struct ods_reader *r = (struct ods_reader *) spreadsheet;
562 xmlChar *val_string = NULL;
565 r->read_names = opts->read_names;
566 ds_init_empty (&r->ods_errs);
568 if ( opts->cell_range )
570 if ( ! convert_cell_ref (opts->cell_range,
571 &r->start_col, &r->start_row,
572 &r->stop_col, &r->stop_row))
574 msg (SE, _("Invalid cell range `%s'"),
587 r->state = STATE_INIT;
588 r->target_sheet = BAD_CAST opts->sheet_name;
589 r->target_sheet_index = opts->sheet_index;
594 /* If CELLRANGE was given, then we know how many variables should be read */
595 if ( r->stop_col != -1 )
597 assert (var_spec == NULL);
598 n_var_specs = r->stop_col - r->start_col + 1;
599 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
600 memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
604 /* Advance to the start of the cells for the target sheet */
605 while ( (r->row < r->start_row ))
607 if (1 != (ret = xmlTextReaderRead (r->xtr)))
615 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
616 spreadsheet->file_name);
620 if ( opts->read_names)
622 while (1 == (ret = xmlTextReaderRead (r->xtr)))
626 if ( r->row > r->start_row)
629 if (r->col == -1 && r->row == r->start_row)
632 if ( r->col < r->start_col)
635 idx = r->col - r->start_col;
637 if (r->state == STATE_CELL_CONTENT
639 XML_READER_TYPE_TEXT == r->node_type)
641 xmlChar *value = xmlTextReaderValue (r->xtr);
642 if ( idx >= n_var_specs)
645 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
647 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
648 memset (var_spec + n_var_specs,
650 (n_var_specs - idx + 1) * sizeof (*var_spec));
651 n_var_specs = idx + 1;
653 var_spec[idx].firstval.text = 0;
654 var_spec[idx].firstval.value = 0;
655 var_spec[idx].firstval.type = 0;
657 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
664 /* Read in the first row of data */
665 while (1 == xmlTextReaderRead (r->xtr))
669 if ( r->row >= r->start_row + 1 + opts->read_names)
672 if ( r->col < r->start_col)
675 if ( r->col - r->start_col + 1 > n_var_specs)
678 idx = r->col - r->start_col;
680 if ( r->state == STATE_CELL &&
681 XML_READER_TYPE_ELEMENT == r->node_type)
683 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
684 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
687 if ( r->state == STATE_CELL_CONTENT &&
688 XML_READER_TYPE_TEXT == r->node_type)
690 var_spec [idx].firstval.type = type;
691 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
692 var_spec [idx].firstval.value = val_string;
698 /* Create the dictionary and populate it */
699 r->spreadsheet.dict = r->dict = dict_create (
700 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
702 for (i = 0 ; i < n_var_specs ; ++i )
705 struct variable *var = NULL;
706 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
707 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
708 dict_create_var (r->dict, name, width);
711 var = dict_get_var (r->dict, i);
713 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
720 fmt = fmt_default_for_width (width);
722 var_set_both_formats (var, &fmt);
725 /* Create the first case, and cache it */
726 r->used_first_case = false;
728 if ( n_var_specs == 0 )
730 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
731 spreadsheet->file_name);
735 r->proto = caseproto_ref (dict_get_proto (r->dict));
736 r->first_case = case_create (r->proto);
737 case_set_missing (r->first_case);
739 for ( i = 0 ; i < n_var_specs ; ++i )
741 const struct variable *var = dict_get_var (r->dict, i);
743 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
746 // zip_reader_destroy (zreader);
748 for ( i = 0 ; i < n_var_specs ; ++i )
750 free (var_spec[i].firstval.type);
751 free (var_spec[i].firstval.value);
752 free (var_spec[i].firstval.text);
753 free (var_spec[i].name);
758 return casereader_create_sequential
762 &ods_file_casereader_class, r);
766 // zip_reader_destroy (zreader);
768 for ( i = 0 ; i < n_var_specs ; ++i )
770 free (var_spec[i].firstval.type);
771 free (var_spec[i].firstval.value);
772 free (var_spec[i].firstval.text);
773 free (var_spec[i].name);
778 dict_destroy (r->spreadsheet.dict);
779 r->spreadsheet.dict = NULL;
780 ods_file_casereader_destroy (NULL, r);
787 /* Reads and returns one case from READER's file. Returns a null
788 pointer on failure. */
789 static struct ccase *
790 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
792 struct ccase *c = NULL;
793 xmlChar *val_string = NULL;
794 struct ods_reader *r = r_;
795 int current_row = r->row;
800 if ( !r->used_first_case )
802 r->used_first_case = true;
803 return r->first_case;
806 if ( r->state > STATE_INIT)
808 c = case_create (r->proto);
809 case_set_missing (c);
812 while (1 == xmlTextReaderRead (r->xtr))
815 if ( r->row > current_row)
819 if ( r->col < r->start_col || (r->stop_col != -1 && r->col > r->stop_col))
823 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
827 if ( r->stop_row != -1 && r->row > r->stop_row)
831 if ( r->state == STATE_CELL &&
832 r->node_type == XML_READER_TYPE_ELEMENT )
834 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
837 if ( r->state == STATE_CELL_CONTENT && r->node_type == XML_READER_TYPE_TEXT )
840 struct xml_value *xmv = xzalloc (sizeof *xmv);
841 xmv->text = xmlTextReaderValue (r->xtr);
842 xmv->value = val_string;
846 for (col = 0; col < r->span ; ++col)
848 const int idx = r->col + col - r->start_col;
850 const struct variable *var = dict_get_var (r->dict, idx);
852 convert_xml_to_value (c, var, xmv);
860 if ( r->state < STATE_TABLE)
864 if (NULL == c || (r->stop_row != -1 && r->row > r->stop_row + 1))