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 */
93 enum reader_state state;
100 const xmlChar *target_sheet;
101 int target_sheet_index;
108 struct caseproto *proto;
109 struct dictionary *dict;
110 struct ccase *first_case;
111 bool used_first_case;
114 struct string ods_errs;
118 static void process_node (struct ods_reader *r);
121 ods_file_casereader_destroy (struct casereader *reader UNUSED, void *r_)
123 struct ods_reader *r = r_;
128 xmlFreeTextReader (r->xtr);
130 if ( ! ds_is_empty (&r->ods_errs))
131 msg (ME, "%s", ds_cstr (&r->ods_errs));
133 ds_destroy (&r->ods_errs);
135 if ( ! r->used_first_case )
136 case_unref (r->first_case);
138 caseproto_unref (r->proto);
144 process_node (struct ods_reader *r)
146 xmlChar *name = xmlTextReaderName (r->xtr);
148 name = xmlStrdup (_xml ("--"));
150 r->node_type = xmlTextReaderNodeType (r->xtr);
155 if (0 == xmlStrcasecmp (name, _xml("office:spreadsheet")) &&
156 XML_READER_TYPE_ELEMENT == r->node_type)
158 r->state = STATE_SPREADSHEET;
161 case STATE_SPREADSHEET:
162 if (0 == xmlStrcasecmp (name, _xml("table:table")))
164 if (XML_READER_TYPE_ELEMENT == r->node_type)
169 if ( r->target_sheet != NULL)
171 xmlChar *value = xmlTextReaderGetAttribute (r->xtr, _xml ("table:name"));
172 if ( 0 == xmlStrcmp (value, r->target_sheet))
174 r->sheet_found = true;
175 r->state = STATE_TABLE;
179 else if (r->target_sheet_index == r->sheet_index)
181 r->sheet_found = true;
182 r->state = STATE_TABLE;
184 else if ( r->target_sheet_index == -1)
185 r->state = STATE_TABLE;
188 else if (XML_READER_TYPE_END_ELEMENT == r->node_type
191 r->state = STATE_INIT;
195 if (0 == xmlStrcasecmp (name, _xml("table:table-row")) )
197 if ( XML_READER_TYPE_ELEMENT == r->node_type)
199 if (! xmlTextReaderIsEmptyElement (r->xtr))
201 r->state = STATE_ROW;
207 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
209 r->state = STATE_SPREADSHEET;
213 if (0 == xmlStrcasecmp (name, _xml ("table:table-cell")))
215 if ( XML_READER_TYPE_ELEMENT == r->node_type)
218 xmlTextReaderGetAttribute (r->xtr,
219 _xml ("table:number-columns-repeated"));
221 r->span = value ? _xmlchar_to_int (value) : 1;
223 if (! xmlTextReaderIsEmptyElement (r->xtr))
225 r->state = STATE_CELL;
229 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
231 r->state = STATE_TABLE;
233 /* Set the span back to the default */
238 if (0 == xmlStrcasecmp (name, _xml("text:p")))
240 if ( XML_READER_TYPE_ELEMENT == r->node_type)
242 r->state = STATE_CELL_CONTENT;
245 else if (XML_READER_TYPE_END_ELEMENT == r->node_type)
247 r->state = STATE_ROW;
250 case STATE_CELL_CONTENT:
251 if (XML_READER_TYPE_TEXT != r->node_type)
252 r->state = STATE_CELL;
262 A struct containing the parameters of a cell's value
275 struct xml_value firstval;
279 /* Determine the width that a xmv should probably have */
281 xmv_to_width (const struct xml_value *xmv, int fallback)
283 int width = SPREADSHEET_DEFAULT_WIDTH;
285 /* Non-strings always have zero width */
286 if (xmv->type != NULL && 0 != xmlStrcmp (xmv->type, _xml("string")))
293 width = ROUND_UP (xmlStrlen (xmv->value),
294 SPREADSHEET_DEFAULT_WIDTH);
296 width = ROUND_UP (xmlStrlen (xmv->text),
297 SPREADSHEET_DEFAULT_WIDTH);
303 Sets the VAR of case C, to the value corresponding to the xml data
306 convert_xml_to_value (struct ccase *c, const struct variable *var,
307 const struct xml_value *xmv)
309 union value *v = case_data_rw (c, var);
311 if (xmv->value == NULL && xmv->text == NULL)
312 value_set_missing (v, var_get_width (var));
313 else if ( var_is_alpha (var))
314 /* Use the text field, because it seems that there is no
315 value field for strings */
316 value_copy_str_rpad (v, var_get_width (var), xmv->text, ' ');
320 const struct fmt_spec *fmt = var_get_write_format (var);
321 enum fmt_category fc = fmt_get_category (fmt->type);
323 assert ( fc != FMT_CAT_STRING);
326 xmv->value ? CHAR_CAST (const char *, xmv->value) : CHAR_CAST (const char *, xmv->text);
328 free (data_in (ss_cstr (text), "UTF-8",
338 ods_open_reader (const struct spreadsheet_read_info *gri, struct spreadsheet_read_options *opts,
339 struct dictionary **dict)
342 xmlChar *type = NULL;
343 unsigned long int vstart = 0;
344 casenumber n_cases = CASENUMBER_MAX;
346 struct var_spec *var_spec = NULL;
349 struct ods_reader *r = xzalloc (sizeof *r);
350 struct zip_member *content = NULL;
351 struct zip_reader *zreader ;
352 xmlChar *val_string = NULL;
354 r->read_names = gri->read_names;
355 ds_init_empty (&r->ods_errs);
357 zreader = zip_reader_create (gri->file_name, &r->ods_errs);
359 if ( NULL == zreader)
361 msg (ME, _("Error opening `%s' for reading as a OpenDocument spreadsheet file: %s."),
362 gri->file_name, ds_cstr (&r->ods_errs));
367 content = zip_member_open (zreader, "content.xml");
368 if ( NULL == content)
370 msg (ME, _("Could not extract OpenDocument spreadsheet from file `%s': %s."),
371 gri->file_name, ds_cstr (&r->ods_errs));
376 zip_member_ref (content);
378 r->xtr = xmlReaderForIO ((xmlInputReadCallback) zip_member_read,
379 (xmlInputCloseCallback) zip_member_finish,
380 content, NULL, NULL, XML_PARSE_RECOVER);
387 if ( opts->cell_range )
389 if ( ! convert_cell_ref (opts->cell_range,
390 &r->start_col, &r->start_row,
391 &r->stop_col, &r->stop_row))
393 msg (SE, _("Invalid cell range `%s'"),
406 r->state = STATE_INIT;
407 r->target_sheet = BAD_CAST opts->sheet_name;
408 r->target_sheet_index = opts->sheet_index;
409 r->row = r->col = -1;
413 /* If CELLRANGE was given, then we know how many variables should be read */
414 if ( r->stop_col != -1 )
416 assert (var_spec == NULL);
417 n_var_specs = r->stop_col - r->start_col + 1;
418 var_spec = xrealloc (var_spec, sizeof (*var_spec) * n_var_specs);
419 memset (var_spec, '\0', sizeof (*var_spec) * n_var_specs);
423 /* Advance to the start of the cells for the target sheet */
424 while ( (r->row < r->start_row ))
426 if (1 != (ret = xmlTextReaderRead (r->xtr)))
434 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
439 if ( gri->read_names)
441 while (1 == (ret = xmlTextReaderRead (r->xtr)))
445 if ( r->row > r->start_row)
448 if (r->col == -1 && r->row == r->start_row)
451 if ( r->col < r->start_col)
454 idx = r->col - r->start_col;
456 if (r->state == STATE_CELL_CONTENT
458 XML_READER_TYPE_TEXT == r->node_type)
460 xmlChar *value = xmlTextReaderValue (r->xtr);
461 if ( idx >= n_var_specs)
464 var_spec = xrealloc (var_spec, sizeof (*var_spec) * (idx + 1));
466 /* xrealloc (unlike realloc) doesn't initialise its memory to 0 */
467 memset (var_spec + n_var_specs,
469 (n_var_specs - idx + 1) * sizeof (*var_spec));
470 n_var_specs = idx + 1;
472 var_spec[idx].firstval.text = 0;
473 var_spec[idx].firstval.value = 0;
474 var_spec[idx].firstval.type = 0;
476 var_spec [idx].name = strdup (CHAR_CAST (const char *, value));
483 /* Read in the first row of data */
484 while (1 == xmlTextReaderRead (r->xtr))
488 if ( r->row >= r->start_row + 1 + gri->read_names)
491 if ( r->col < r->start_col)
494 if ( r->col - r->start_col + 1 > n_var_specs)
497 idx = r->col - r->start_col;
499 if ( r->state == STATE_CELL &&
500 XML_READER_TYPE_ELEMENT == r->node_type)
502 type = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value-type"));
503 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
506 if ( r->state == STATE_CELL_CONTENT &&
507 XML_READER_TYPE_TEXT == r->node_type)
509 var_spec [idx].firstval.type = type;
510 var_spec [idx].firstval.text = xmlTextReaderValue (r->xtr);
511 var_spec [idx].firstval.value = val_string;
517 /* Create the dictionary and populate it */
518 *dict = r->dict = dict_create (
519 CHAR_CAST (const char *, xmlTextReaderConstEncoding (r->xtr)));
521 for (i = 0 ; i < n_var_specs ; ++i )
524 struct variable *var = NULL;
525 char *name = dict_make_unique_var_name (r->dict, var_spec[i].name, &vstart);
526 int width = xmv_to_width (&var_spec[i].firstval, gri->asw);
527 dict_create_var (r->dict, name, width);
530 var = dict_get_var (r->dict, i);
532 if ( 0 == xmlStrcmp (var_spec[i].firstval.type, _xml("date")))
539 fmt = fmt_default_for_width (width);
541 var_set_both_formats (var, &fmt);
544 /* Create the first case, and cache it */
545 r->used_first_case = false;
547 if ( n_var_specs == 0 )
549 msg (MW, _("Selected sheet or range of spreadsheet `%s' is empty."),
554 r->proto = caseproto_ref (dict_get_proto (r->dict));
555 r->first_case = case_create (r->proto);
556 case_set_missing (r->first_case);
558 for ( i = 0 ; i < n_var_specs ; ++i )
560 const struct variable *var = dict_get_var (r->dict, i);
562 convert_xml_to_value (r->first_case, var, &var_spec[i].firstval);
565 zip_reader_destroy (zreader);
567 for ( i = 0 ; i < n_var_specs ; ++i )
569 free (var_spec[i].firstval.type);
570 free (var_spec[i].firstval.value);
571 free (var_spec[i].firstval.text);
572 free (var_spec[i].name);
577 return casereader_create_sequential
581 &ods_file_casereader_class, r);
585 zip_reader_destroy (zreader);
587 for ( i = 0 ; i < n_var_specs ; ++i )
589 free (var_spec[i].firstval.type);
590 free (var_spec[i].firstval.value);
591 free (var_spec[i].firstval.text);
592 free (var_spec[i].name);
597 dict_destroy (r->dict);
598 ods_file_casereader_destroy (NULL, r);
605 /* Reads and returns one case from READER's file. Returns a null
606 pointer on failure. */
607 static struct ccase *
608 ods_file_casereader_read (struct casereader *reader UNUSED, void *r_)
610 struct ccase *c = NULL;
611 xmlChar *val_string = NULL;
612 struct ods_reader *r = r_;
613 int current_row = r->row;
618 if ( !r->used_first_case )
620 r->used_first_case = true;
621 return r->first_case;
625 if ( r->state > STATE_INIT)
627 c = case_create (r->proto);
628 case_set_missing (c);
631 while (1 == xmlTextReaderRead (r->xtr))
634 if ( r->row > current_row)
638 if ( r->col < r->start_col || (r->stop_col != -1 && r->col > r->stop_col))
642 if ( r->col - r->start_col >= caseproto_get_n_widths (r->proto))
646 if ( r->stop_row != -1 && r->row > r->stop_row)
650 if ( r->state == STATE_CELL &&
651 r->node_type == XML_READER_TYPE_ELEMENT )
653 val_string = xmlTextReaderGetAttribute (r->xtr, _xml ("office:value"));
656 if ( r->state == STATE_CELL_CONTENT && r->node_type == XML_READER_TYPE_TEXT )
659 struct xml_value *xmv = xzalloc (sizeof *xmv);
660 xmv->text = xmlTextReaderValue (r->xtr);
661 xmv->value = val_string;
664 for (col = 0; col < r->span ; ++col)
666 const int idx = r->col + col - r->start_col;
668 const struct variable *var = dict_get_var (r->dict, idx);
670 convert_xml_to_value (c, var, xmv);
677 if ( r->state < STATE_TABLE)
681 if (NULL == c || (r->stop_row != -1 && r->row > r->stop_row + 1))