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"
22 #include "data/data-in.h"
24 #include "gl/minmax.h"
27 #define _(msgid) gettext (msgid)
28 #define N_(msgid) (msgid)
30 #include "ods-reader.h"
31 #include "spreadsheet-reader.h"
36 ods_open_reader (const struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts,
37 struct dictionary **dict)
39 msg (ME, _("Support for %s files was not compiled into this installation of PSPP"), "OpenDocument");
46 #include "libpspp/zip-reader.h"
52 #include <libxml/xmlreader.h>
55 #include "data/format.h"
56 #include "data/case.h"
57 #include "data/casereader-provider.h"
58 #include "data/dictionary.h"
59 #include "data/identifier.h"
60 #include "data/value.h"
61 #include "data/variable.h"
62 #include "libpspp/i18n.h"
63 #include "libpspp/str.h"
65 #include "gl/xalloc.h"
67 static void ods_file_casereader_destroy (struct casereader *, void *);
69 static struct ccase *ods_file_casereader_read (struct casereader *, void *);
71 static const struct casereader_class ods_file_casereader_class =
73 ods_file_casereader_read,
74 ods_file_casereader_destroy,
81 STATE_INIT = 0, /* Initial state */
82 STATE_SPREADSHEET, /* Found the start of the spreadsheet doc */
83 STATE_TABLE, /* Found the sheet that we actually want */
84 STATE_ROW, /* Found the start of the cell array */
85 STATE_CELL, /* Found a cell */
86 STATE_CELL_CONTENT /* Found a the text within a cell */
91 struct spreadsheet spreadsheet;
95 enum reader_state state;
102 const xmlChar *target_sheet;
103 int target_sheet_index;
110 struct caseproto *proto;
111 struct dictionary *dict;
112 struct ccase *first_case;
113 bool used_first_case;
116 struct string ods_errs;
120 static void process_node (struct ods_reader *r);
123 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
125 struct ods_reader *r = r_;
130 xmlFreeTextReader (r->xtr);
132 if ( ! ds_is_empty (&r->ods_errs))
133 msg (ME, "%s", ds_cstr (&r->ods_errs));
135 ds_destroy (&r->ods_errs);
137 if ( ! r->used_first_case )
138 case_unref (r->first_case);
140 caseproto_unref (r->proto);
146 process_node (struct ods_reader *r)
148 xmlChar *name = xmlTextReaderName (r->xtr);
150 name = xmlStrdup (_xml ("--"));
152 r->node_type = xmlTextReaderNodeType (r->xtr);
157 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
158 XML_READER_TYPE_ELEMENT == r->node_type)
160 r->state = STATE_SPREADSHEET;
163 case STATE_SPREADSHEET:
164 if (0 == xmlStrcasecmp (name, _xml("table:table")))
166 if (XML_READER_TYPE_ELEMENT == r->node_type)
171 if ( r->target_sheet != NULL)
173 xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
174 if ( 0 == xmlStrcmp (value, r->target_sheet))
176 r->sheet_found = true;
177 r->state = STATE_TABLE;
181 else if (r->target_sheet_index == r->sheet_index)
183 r->sheet_found = true;
184 r->state = STATE_TABLE;
186 else if ( r->target_sheet_index == -1)
187 r->state = STATE_TABLE;
190 else if (XML_READER_TYPE_END_ELEMENT == r->node_type
193 r->state = STATE_INIT;
197 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) )
199 if ( XML_READER_TYPE_ELEMENT == r->node_type)
201 if (! xmlTextReaderIsEmptyElement (r->xtr))
203 r->state = STATE_ROW;
209 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
211 r->state = STATE_SPREADSHEET;
215 if (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
217 if ( XML_READER_TYPE_ELEMENT == r->node_type)
220 xmlTextReaderGetAttribute (r->xtr,
221 _xml ("table:number-columns-repeated"));
223 r->span = value ? _xmlchar_to_int (value) : 1;
225 if (! xmlTextReaderIsEmptyElement (r->xtr))
227 r->state = STATE_CELL;
231 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
233 r->state = STATE_TABLE;
235 /* Set the span back to the default */
240 if (0 == xmlStrcasecmp (name, _xml("text:p")))
242 if ( XML_READER_TYPE_ELEMENT == r->node_type)
244 r->state = STATE_CELL_CONTENT;
247 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
249 r->state = STATE_ROW;
252 case STATE_CELL_CONTENT:
253 if (XML_READER_TYPE_TEXT != r->node_type)
254 r->state = STATE_CELL;
264 A struct containing the parameters of a cell's value
277 struct xml_value firstval;
281 /* Determine the width that a xmv should probably have */
283 xmv_to_width (const struct xml_value *xmv, int fallback)
285 int width = SPREADSHEET_DEFAULT_WIDTH;
287 /* Non-strings always have zero width */
288 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
295 width = ROUND_UP (xmlStrlen (xmv->value),
296 SPREADSHEET_DEFAULT_WIDTH);
298 width = ROUND_UP (xmlStrlen (xmv->text),
299 SPREADSHEET_DEFAULT_WIDTH);
305 Sets the VAR of case C, to the value corresponding to the xml data
308 convert_xml_to_value (struct ccase *c, const struct variable *var,
309 const struct xml_value *xmv)
311 union value *v = case_data_rw (c, var);
313 if (xmv->value == NULL && xmv->text == NULL)
314 value_set_missing (v, var_get_width (var));
315 else if ( var_is_alpha (var))
316 /* Use the text field, because it seems that there is no
317 value field for strings */
318 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
322 const struct fmt_spec *fmt = var_get_write_format (var);
323 enum fmt_category fc = fmt_get_category (fmt->type);
325 assert ( fc != FMT_CAT_STRING);
328 xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
330 free (data_in (ss_cstr (text), "UTF-8",
339 /* Try to find out how many sheets there are in the "workbook" */
341 get_sheet_count (struct zip_reader *zreader)
343 xmlTextReaderPtr mxtr;
344 struct zip_member *meta = NULL;
345 meta = zip_member_open (zreader, "meta.xml");
350 mxtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
351 (xmlInputCloseCallback) zip_member_finish,
352 meta, NULL, NULL, XML_PARSE_RECOVER);
354 while (1 == xmlTextReaderRead (mxtr))
356 xmlChar *name = xmlTextReaderName (mxtr);
357 if ( 0 == xmlStrcmp (name, _xml("meta:document-statistic")))
359 xmlChar *attr = xmlTextReaderGetAttribute (mxtr, _xml ("meta:table-count"));
363 int s = _xmlchar_to_int (attr);
371 struct spreadsheet *ods_probe (const char *filename)
373 struct ods_reader *r;
375 xmlTextReaderPtr xtr ;
377 struct zip_member *content = NULL;
379 struct zip_reader *zreader = NULL;
381 ds_init_empty (&errs);
383 zreader = zip_reader_create (filename, &errs);
388 content = zip_member_open (zreader, "content.xml");
390 if ( content == NULL)
393 zip_member_ref (content);
395 sheet_count = get_sheet_count (zreader);
397 xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
398 (xmlInputCloseCallback) zip_member_finish,
399 content, NULL, NULL, XML_PARSE_RECOVER);
404 r = xzalloc (sizeof *r);
406 r->spreadsheet.type = SPREADSHEET_ODS;
407 r->spreadsheet.sheets = sheet_count;
411 r->spreadsheet.file_name = filename;
412 return &r->spreadsheet;
415 zip_reader_destroy (zreader);
421 ods_make_reader (struct spreadsheet *spreadsheet,
422 const struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts)
425 xmlChar *type = NULL;
426 unsigned long int vstart = 0;
427 casenumber n_cases = CASENUMBER_MAX;
429 struct var_spec *var_spec = NULL;
432 struct ods_reader *r = (struct ods_reader *) spreadsheet;
433 xmlChar *val_string = NULL;
436 r->read_names = gri->read_names;
437 ds_init_empty (&r->ods_errs);
439 if ( opts->cell_range )
441 if ( ! convert_cell_ref (opts->cell_range,
442 &r->start_col, &r->start_row,
443 &r->stop_col, &r->stop_row))
445 msg (SE, _("Invalid cell range `%s'"),
458 r->state = STATE_INIT;
459 r->target_sheet = BAD_CAST opts->sheet_name;
460 r->target_sheet_index = opts->sheet_index;
461 r->row = r->col = -1;
465 /* If CELLRANGE was given, then we know how many variables should be read */
466 if ( r->stop_col != -1 )
468 assert (var_spec == NULL);
469 n_var_specs = r->stop_col - r->start_col + 1;
470 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
471 memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
475 /* Advance to the start of the cells for the target sheet */
476 while ( (r->row < r->start_row ))
478 if (1 != (ret = xmlTextReaderRead (r->xtr)))
486 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
487 spreadsheet->file_name);
491 if ( gri->read_names)
493 while (1 == (ret = xmlTextReaderRead (r->xtr)))
497 if ( r->row > r->start_row)
500 if (r->col == -1 && r->row == r->start_row)
503 if ( r->col < r->start_col)
506 idx = r->col - r->start_col;
508 if (r->state == STATE_CELL_CONTENT
510 XML_READER_TYPE_TEXT == r->node_type)
512 xmlChar *value = xmlTextReaderValue (r->xtr);
513 if ( idx >= n_var_specs)
516 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
518 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
519 memset (var_spec + n_var_specs,
521 (n_var_specs - idx + 1) * sizeof (*var_spec));
522 n_var_specs = idx + 1;
524 var_spec[idx].firstval.text = 0;
525 var_spec[idx].firstval.value = 0;
526 var_spec[idx].firstval.type = 0;
528 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
535 /* Read in the first row of data */
536 while (1 == xmlTextReaderRead (r->xtr))
540 if ( r->row >= r->start_row + 1 + gri->read_names)
543 if ( r->col < r->start_col)
546 if ( r->col - r->start_col + 1 > n_var_specs)
549 idx = r->col - r->start_col;
551 if ( r->state == STATE_CELL &&
552 XML_READER_TYPE_ELEMENT == r->node_type)
554 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
555 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
558 if ( r->state == STATE_CELL_CONTENT &&
559 XML_READER_TYPE_TEXT == r->node_type)
561 var_spec [idx].firstval.type = type;
562 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
563 var_spec [idx].firstval.value = val_string;
569 /* Create the dictionary and populate it */
570 r->spreadsheet.dict = r->dict = dict_create (
571 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
573 for (i = 0 ; i < n_var_specs ; ++i )
576 struct variable *var = NULL;
577 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
578 int width = xmv_to_width (&var_spec[i].firstval, gri->asw);
579 dict_create_var (r->dict, name, width);
582 var = dict_get_var (r->dict, i);
584 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
591 fmt = fmt_default_for_width (width);
593 var_set_both_formats (var, &fmt);
596 /* Create the first case, and cache it */
597 r->used_first_case = false;
599 if ( n_var_specs == 0 )
601 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
602 spreadsheet->file_name);
606 r->proto = caseproto_ref (dict_get_proto (r->dict));
607 r->first_case = case_create (r->proto);
608 case_set_missing (r->first_case);
610 for ( i = 0 ; i < n_var_specs ; ++i )
612 const struct variable *var = dict_get_var (r->dict, i);
614 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
617 // zip_reader_destroy (zreader);
619 for ( i = 0 ; i < n_var_specs ; ++i )
621 free (var_spec[i].firstval.type);
622 free (var_spec[i].firstval.value);
623 free (var_spec[i].firstval.text);
624 free (var_spec[i].name);
629 return casereader_create_sequential
633 &ods_file_casereader_class, r);
637 // zip_reader_destroy (zreader);
639 for ( i = 0 ; i < n_var_specs ; ++i )
641 free (var_spec[i].firstval.type);
642 free (var_spec[i].firstval.value);
643 free (var_spec[i].firstval.text);
644 free (var_spec[i].name);
649 dict_destroy (r->spreadsheet.dict);
650 r->spreadsheet.dict = NULL;
651 ods_file_casereader_destroy (NULL, r);
658 /* Reads and returns one case from READER's file. Returns a null
659 pointer on failure. */
660 static struct ccase *
661 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
663 struct ccase *c = NULL;
664 xmlChar *val_string = NULL;
665 struct ods_reader *r = r_;
666 int current_row = r->row;
671 if ( !r->used_first_case )
673 r->used_first_case = true;
674 return r->first_case;
677 if ( r->state > STATE_INIT)
679 c = case_create (r->proto);
680 case_set_missing (c);
683 while (1 == xmlTextReaderRead (r->xtr))
686 if ( r->row > current_row)
690 if ( r->col < r->start_col || (r->stop_col != -1 && r->col > r->stop_col))
694 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
698 if ( r->stop_row != -1 && r->row > r->stop_row)
702 if ( r->state == STATE_CELL &&
703 r->node_type == XML_READER_TYPE_ELEMENT )
705 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
708 if ( r->state == STATE_CELL_CONTENT && r->node_type == XML_READER_TYPE_TEXT )
711 struct xml_value *xmv = xzalloc (sizeof *xmv);
712 xmv->text = xmlTextReaderValue (r->xtr);
713 xmv->value = val_string;
716 for (col = 0; col < r->span ; ++col)
718 const int idx = r->col + col - r->start_col;
720 const struct variable *var = dict_get_var (r->dict, idx);
722 convert_xml_to_value (c, var, xmv);
729 if ( r->state < STATE_TABLE)
733 if (NULL == c || (r->stop_row != -1 && r->row > r->stop_row + 1))