1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2012 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) */
97 STATE_INIT = 0, /* Initial state */
98 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
99 STATE_TABLE, /* Found the sheet that we actually want */
100 STATE_ROW, /* Found the start of the cell array */
101 STATE_CELL, /* Found a cell */
102 STATE_CELL_CONTENT /* Found a the text within a cell */
107 struct spreadsheet spreadsheet;
109 xmlTextReaderPtr xtr;
111 enum reader_state state;
120 const xmlChar *target_sheet;
121 int target_sheet_index;
128 struct sheet_detail *sheets;
129 int n_allocated_sheets;
131 struct caseproto *proto;
132 struct dictionary *dict;
133 struct ccase *first_case;
134 bool used_first_case;
137 struct string ods_errs;
141 static void process_node (struct ods_reader *r);
145 ods_get_sheet_name (struct spreadsheet *s, int n)
148 struct ods_reader *or = (struct ods_reader *) s;
150 assert (n < s->n_sheets);
153 (or->n_allocated_sheets <= n)
155 (1 == (ret = xmlTextReaderRead (or->xtr)))
161 return or->sheets[n].name;
165 ods_get_sheet_range (struct spreadsheet *s, int n)
168 struct ods_reader *or = (struct ods_reader *) s;
170 assert (n < s->n_sheets);
173 (or->n_allocated_sheets <= n || or->sheets[n].stop_col == -1)
175 (1 == (ret = xmlTextReaderRead (or->xtr)))
181 return create_cell_ref (
182 or->sheets[n].start_col,
183 or->sheets[n].start_row,
184 or->sheets[n].stop_col,
185 or->sheets[n].stop_row);
190 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
192 struct ods_reader *r = r_;
197 xmlFreeTextReader (r->xtr);
199 if ( ! ds_is_empty (&r->ods_errs))
200 msg (ME, "%s", ds_cstr (&r->ods_errs));
202 ds_destroy (&r->ods_errs);
204 if ( ! r->used_first_case )
205 case_unref (r->first_case);
207 caseproto_unref (r->proto);
213 process_node (struct ods_reader *r)
215 xmlChar *name = xmlTextReaderName (r->xtr);
217 name = xmlStrdup (_xml ("--"));
220 r->node_type = xmlTextReaderNodeType (r->xtr);
225 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
226 XML_READER_TYPE_ELEMENT == r->node_type)
228 printf ("%s:%d Start of Workbook %d: Rows %d\n", __FILE__, __LINE__,
229 r->sheet_index, r->row);
231 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"));
240 r->sheets = xrealloc (r->sheets, sizeof (*r->sheets) * ++r->n_allocated_sheets);
241 r->sheets[r->n_allocated_sheets - 1].start_col = -1;
242 r->sheets[r->n_allocated_sheets - 1].stop_col = -1;
243 r->sheets[r->n_allocated_sheets - 1].start_row = -1;
244 r->sheets[r->n_allocated_sheets - 1].stop_row = -1;
245 r->sheets[r->n_allocated_sheets - 1].name = value;
249 r->min_col = INT_MAX;
252 printf ("%s:%d Start of SHEET %d: Rows %d\n", __FILE__, __LINE__,
253 r->sheet_index, r->row);
255 if ( r->target_sheet != NULL)
257 if ( 0 == xmlStrcmp (value, r->target_sheet))
259 r->sheet_found = true;
262 else if (r->target_sheet_index == r->sheet_index)
264 r->sheet_found = true;
266 r->state = STATE_TABLE;
268 else if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
269 XML_READER_TYPE_ELEMENT == r->node_type)
271 r->state = STATE_INIT;
272 printf ("%s:%d End of Workbook %d: Rows %d Cols %d\n", __FILE__, __LINE__,
273 r->sheet_index, r->row, r->col);
277 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) &&
278 (XML_READER_TYPE_ELEMENT == r->node_type))
281 xmlTextReaderGetAttribute (r->xtr,
282 _xml ("table:number-rows-repeated"));
284 int row_span = value ? _xmlchar_to_int (value) : 1;
286 printf ("%s:%d Start of Row %d Span %d\n", __FILE__, __LINE__, r->row, row_span);
290 if (! xmlTextReaderIsEmptyElement (r->xtr))
291 r->state = STATE_ROW;
293 else if (0 == xmlStrcasecmp (name, _xml("table:table")) &&
294 (XML_READER_TYPE_END_ELEMENT == r->node_type))
296 printf ("%s:%d End of SHEET %d %d,%d\n", __FILE__, __LINE__, r->sheet_index, r->row, r->col);
297 r->sheets[r->sheet_index].stop_row = r->row;
298 r->sheets[r->sheet_index].stop_col = r->max_col - 1;
299 r->sheets[r->sheet_index].start_col = r->min_col;
300 r->state = STATE_SPREADSHEET;
305 if ( (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
307 (XML_READER_TYPE_ELEMENT == r->node_type))
310 xmlTextReaderGetAttribute (r->xtr,
311 _xml ("table:number-columns-repeated"));
313 int col_span = value ? _xmlchar_to_int (value) : 0;
318 if (r->min_col > r->col)
321 printf ("%s:%d Start of Cell %d, %d\n", __FILE__, __LINE__, r->row, r->col);
322 if (! xmlTextReaderIsEmptyElement (r->xtr))
323 r->state = STATE_CELL;
325 else if ( (0 == xmlStrcasecmp (name, _xml ("table:table-row")))
327 (XML_READER_TYPE_END_ELEMENT == r->node_type))
329 /* Set the span back to the default */
330 printf ("%s:%d End of Cell: %d, %d\n", __FILE__, __LINE__, r->row, r->col);
331 if ( r->max_col < r->col)
333 r->state = STATE_TABLE;
337 if ( (0 == xmlStrcasecmp (name, _xml("text:p")))
339 ( XML_READER_TYPE_ELEMENT == r->node_type))
341 // printf ("%s:%d Start of Cell Contents %d\n", __FILE__, __LINE__, r->row);
342 if (! xmlTextReaderIsEmptyElement (r->xtr))
343 r->state = STATE_CELL_CONTENT;
346 ( (0 == xmlStrcasecmp (name, _xml("table:table-cell")))
348 (XML_READER_TYPE_END_ELEMENT == r->node_type)
351 // printf ("%s:%d End of Cell contents: Rows %d\n", __FILE__, __LINE__, r->row);
352 r->state = STATE_ROW;
355 case STATE_CELL_CONTENT:
356 if (r->sheets[r->sheet_index].start_row == -1)
357 r->sheets[r->sheet_index].start_row = r->row;
358 // printf ("%s:%d Cell contents: Rows %d\n", __FILE__, __LINE__, r->row);
359 /* if (XML_READER_TYPE_TEXT != r->node_type) */
360 r->state = STATE_CELL;
371 A struct containing the parameters of a cell's value
384 struct xml_value firstval;
388 /* Determine the width that a xmv should probably have */
390 xmv_to_width (const struct xml_value *xmv, int fallback)
392 int width = SPREADSHEET_DEFAULT_WIDTH;
394 /* Non-strings always have zero width */
395 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
402 width = ROUND_UP (xmlStrlen (xmv->value),
403 SPREADSHEET_DEFAULT_WIDTH);
405 width = ROUND_UP (xmlStrlen (xmv->text),
406 SPREADSHEET_DEFAULT_WIDTH);
412 Sets the VAR of case C, to the value corresponding to the xml data
415 convert_xml_to_value (struct ccase *c, const struct variable *var,
416 const struct xml_value *xmv)
418 union value *v = case_data_rw (c, var);
420 if (xmv->value == NULL && xmv->text == NULL)
421 value_set_missing (v, var_get_width (var));
422 else if ( var_is_alpha (var))
423 /* Use the text field, because it seems that there is no
424 value field for strings */
425 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
429 const struct fmt_spec *fmt = var_get_write_format (var);
430 enum fmt_category fc = fmt_get_category (fmt->type);
432 assert ( fc != FMT_CAT_STRING);
435 xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
437 free (data_in (ss_cstr (text), "UTF-8",
446 /* Try to find out how many sheets there are in the "workbook" */
448 get_sheet_count (struct zip_reader *zreader)
450 xmlTextReaderPtr mxtr;
451 struct zip_member *meta = NULL;
452 meta = zip_member_open (zreader, "meta.xml");
457 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
458 (xmlInputCloseCallback) zip_member_finish,
459 meta, NULL, NULL, 0);
461 while (1 == xmlTextReaderRead (mxtr))
463 xmlChar *name = xmlTextReaderName (mxtr);
464 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
466 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
470 int s = _xmlchar_to_int (attr);
479 ods_error_handler (void *ctx, const char *mesg,
480 UNUSED xmlParserSeverities sev, xmlTextReaderLocatorPtr loc)
482 struct ods_reader *r = ctx;
484 msg (MW, _("There was a problem whilst reading the %s file `%s' (near line %d): `%s'"),
486 r->spreadsheet.file_name,
487 xmlTextReaderLocatorLineNumber (loc),
493 ods_probe (const char *filename, bool report_errors)
495 struct ods_reader *r;
497 xmlTextReaderPtr xtr ;
499 struct zip_member *content = NULL;
501 struct zip_reader *zreader = NULL;
503 ds_init_empty (&errs);
505 zreader = zip_reader_create (filename, &errs);
510 content = zip_member_open (zreader, "content.xml");
512 if ( content == NULL)
515 zip_member_ref (content);
517 sheet_count = get_sheet_count (zreader);
519 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
520 (xmlInputCloseCallback) zip_member_finish,
522 report_errors ? 0 : (XML_PARSE_NOERROR | XML_PARSE_NOWARNING) );
527 r = xzalloc (sizeof *r);
529 r->spreadsheet.type = SPREADSHEET_ODS;
530 r->spreadsheet.n_sheets = sheet_count;
531 r->n_allocated_sheets = 0;
536 xmlTextReaderSetErrorHandler (xtr, ods_error_handler, r);
541 printf ("%s:%d\n", __FILE__, __LINE__);
543 r->spreadsheet.file_name = filename;
544 return &r->spreadsheet;
547 zip_reader_destroy (zreader);
553 ods_make_reader (struct spreadsheet *spreadsheet,
554 const struct spreadsheet_read_options *opts)
557 xmlChar *type = NULL;
558 unsigned long int vstart = 0;
559 casenumber n_cases = CASENUMBER_MAX;
561 struct var_spec *var_spec = NULL;
564 struct ods_reader *r = (struct ods_reader *) spreadsheet;
565 xmlChar *val_string = NULL;
568 r->read_names = opts->read_names;
569 ds_init_empty (&r->ods_errs);
571 if ( opts->cell_range )
573 if ( ! convert_cell_ref (opts->cell_range,
574 &r->start_col, &r->start_row,
575 &r->stop_col, &r->stop_row))
577 msg (SE, _("Invalid cell range `%s'"),
590 r->state = STATE_INIT;
591 r->target_sheet = BAD_CAST opts->sheet_name;
592 r->target_sheet_index = opts->sheet_index;
593 r->row = r->col = -1;
597 /* If CELLRANGE was given, then we know how many variables should be read */
598 if ( r->stop_col != -1 )
600 assert (var_spec == NULL);
601 n_var_specs = r->stop_col - r->start_col + 1;
602 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
603 memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
607 /* Advance to the start of the cells for the target sheet */
608 while ( (r->row < r->start_row ))
610 if (1 != (ret = xmlTextReaderRead (r->xtr)))
618 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
619 spreadsheet->file_name);
623 if ( opts->read_names)
625 while (1 == (ret = xmlTextReaderRead (r->xtr)))
629 if ( r->row > r->start_row)
632 if (r->col == -1 && r->row == r->start_row)
635 if ( r->col < r->start_col)
638 idx = r->col - r->start_col;
640 if (r->state == STATE_CELL_CONTENT
642 XML_READER_TYPE_TEXT == r->node_type)
644 xmlChar *value = xmlTextReaderValue (r->xtr);
645 if ( idx >= n_var_specs)
648 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
650 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
651 memset (var_spec + n_var_specs,
653 (n_var_specs - idx + 1) * sizeof (*var_spec));
654 n_var_specs = idx + 1;
656 var_spec[idx].firstval.text = 0;
657 var_spec[idx].firstval.value = 0;
658 var_spec[idx].firstval.type = 0;
660 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
667 /* Read in the first row of data */
668 while (1 == xmlTextReaderRead (r->xtr))
672 if ( r->row >= r->start_row + 1 + opts->read_names)
675 if ( r->col < r->start_col)
678 if ( r->col - r->start_col + 1 > n_var_specs)
681 idx = r->col - r->start_col;
683 if ( r->state == STATE_CELL &&
684 XML_READER_TYPE_ELEMENT == r->node_type)
686 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
687 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
690 if ( r->state == STATE_CELL_CONTENT &&
691 XML_READER_TYPE_TEXT == r->node_type)
693 var_spec [idx].firstval.type = type;
694 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
695 var_spec [idx].firstval.value = val_string;
701 /* Create the dictionary and populate it */
702 r->spreadsheet.dict = r->dict = dict_create (
703 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
705 for (i = 0 ; i < n_var_specs ; ++i )
708 struct variable *var = NULL;
709 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
710 int width = xmv_to_width (&var_spec[i].firstval, opts->asw);
711 dict_create_var (r->dict, name, width);
714 var = dict_get_var (r->dict, i);
716 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
723 fmt = fmt_default_for_width (width);
725 var_set_both_formats (var, &fmt);
728 /* Create the first case, and cache it */
729 r->used_first_case = false;
731 if ( n_var_specs == 0 )
733 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
734 spreadsheet->file_name);
738 r->proto = caseproto_ref (dict_get_proto (r->dict));
739 r->first_case = case_create (r->proto);
740 case_set_missing (r->first_case);
742 for ( i = 0 ; i < n_var_specs ; ++i )
744 const struct variable *var = dict_get_var (r->dict, i);
746 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
749 // zip_reader_destroy (zreader);
751 for ( i = 0 ; i < n_var_specs ; ++i )
753 free (var_spec[i].firstval.type);
754 free (var_spec[i].firstval.value);
755 free (var_spec[i].firstval.text);
756 free (var_spec[i].name);
761 return casereader_create_sequential
765 &ods_file_casereader_class, r);
769 // zip_reader_destroy (zreader);
771 for ( i = 0 ; i < n_var_specs ; ++i )
773 free (var_spec[i].firstval.type);
774 free (var_spec[i].firstval.value);
775 free (var_spec[i].firstval.text);
776 free (var_spec[i].name);
781 dict_destroy (r->spreadsheet.dict);
782 r->spreadsheet.dict = NULL;
783 ods_file_casereader_destroy (NULL, r);
790 /* Reads and returns one case from READER's file. Returns a null
791 pointer on failure. */
792 static struct ccase *
793 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
795 struct ccase *c = NULL;
796 xmlChar *val_string = NULL;
797 struct ods_reader *r = r_;
798 int current_row = r->row;
803 if ( !r->used_first_case )
805 r->used_first_case = true;
806 return r->first_case;
809 if ( r->state > STATE_INIT)
811 c = case_create (r->proto);
812 case_set_missing (c);
815 while (1 == xmlTextReaderRead (r->xtr))
818 if ( r->row > current_row)
822 if ( r->col < r->start_col || (r->stop_col != -1 && r->col > r->stop_col))
826 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
830 if ( r->stop_row != -1 && r->row > r->stop_row)
834 if ( r->state == STATE_CELL &&
835 r->node_type == XML_READER_TYPE_ELEMENT )
837 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
840 if ( r->state == STATE_CELL_CONTENT && r->node_type == XML_READER_TYPE_TEXT )
843 struct xml_value *xmv = xzalloc (sizeof *xmv);
844 xmv->text = xmlTextReaderValue (r->xtr);
845 xmv->value = val_string;
849 for (col = 0; col < r->span ; ++col)
851 const int idx = r->col + col - r->start_col;
853 const struct variable *var = dict_get_var (r->dict, idx);
855 convert_xml_to_value (c, var, xmv);
863 if ( r->state < STATE_TABLE)
867 if (NULL == c || (r->stop_row != -1 && r->row > r->stop_row + 1))