xml-io.c 37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * xml-io.c: save/read gnumeric Sheets using an XML encoding.
 *
 * Daniel Veillard <Daniel.Veillard@w3.org>
 *
 * $Id$
 */

#include <config.h>
#include <stdio.h>
#include <gnome.h>
#include "gnumeric.h"
13 14
#include "gnome-xml/tree.h"
#include "gnome-xml/parser.h"
15
#include "color.h"
16
#include "sheet-object.h"
17
#include "sheet-object-graphic.h"
18
#include "xml-io.h"
19
#include "file.h"
20 21 22 23 24

/*
 * A parsing context.
 */
typedef struct parseXmlContext {
25 26 27 28 29 30 31 32
	xmlDocPtr doc;			/* Xml document */
	xmlNsPtr ns;			/* Main name space */
	xmlNodePtr parent;		/* used only for g_hash_table_foreach callbacks */
	GHashTable *nameTable;		/* to reproduce multiple refs with HREFs */
	int fontIdx;			/* for Font refs names ... */
	GHashTable *fontname2name;	/* For re-using fonts during writes */
	Sheet *sheet;			/* the associated sheet */
	Workbook *wb;			/* the associated sheet */
33 34
} parseXmlContext, *parseXmlContextPtr;

Arturo Espinosa's avatar
Arturo Espinosa committed
35 36 37 38
static Sheet      *readXmlSheet     (parseXmlContextPtr ctxt, xmlNodePtr tree);
static xmlNodePtr  writeXmlSheet    (parseXmlContextPtr ctxt, Sheet *sheet);
static Workbook   *readXmlWorkbook  (parseXmlContextPtr ctxt, xmlNodePtr tree);
static xmlNodePtr  writeXmlWorkbook (parseXmlContextPtr ctxt, Workbook *wb);
39
/* static guint       ptrHash          (gconstpointer a);
40 41 42
   static gint        ptrCompare       (gconstpointer a, gconstpointer b); */
static void nameFree                (gpointer key, gpointer value,
				     gpointer user_data);
43 44
static void fontname_nameFree       (gpointer key, gpointer value,
				     gpointer user_data);
45

46 47 48
/*
 * Internal stuff: xml helper functions.
 */
49 50 51 52 53

/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
 */
54
static char *
55 56
xmlGetValue (xmlNodePtr node, const char *name)
{
57
	char *ret;
58 59
	xmlNodePtr child;

60
	ret = (char *) xmlGetProp (node, name);
61
	if (ret != NULL)
Morten Welinder's avatar
Morten Welinder committed
62
		return ret;
63 64 65
	child = node->childs;

	while (child != NULL) {
66 67 68 69 70 71 72 73
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
			ret = xmlNodeGetContent(child);
			if (ret != NULL)
			    return (ret);
		}
74 75 76 77
		child = child->next;
	}

	return NULL;
78 79
}

80
#if 0
81 82 83 84
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
85
static String *
86 87
xmlGetStringValue (xmlNodePtr node, const char *name)
{
88
	char *val;
89 90
	String *ret;

91 92 93 94 95
	val = xmlGetValue(node, name);
	if (val == NULL) return(NULL);
        ret = string_get(val);
	free(val);
	return(ret);
96
}
97
#endif
98

99 100 101 102
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
103 104 105
static int
xmlGetIntValue (xmlNodePtr node, const char *name, int *val)
{
106
	char *ret;
107
	int i;
108
	int res;
109

110 111 112 113 114 115 116
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "%d", &i);
	free(ret);
	
	if (res == 1) {
	        *val = i;
117 118 119
		return 1;
	}
	return 0;
120 121
}

122
#if 0
123 124 125 126
/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
127
static int
128 129
xmlGetFloatValue (xmlNodePtr node, const char *name, float *val)
{
130 131
	int res;
	char *ret;
132 133
	float f;

134 135 136 137 138 139 140
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "%f", &f);
	free(ret);
	
	if (res == 1) {
	        *val = f;
141 142 143
		return 1;
	}
	return 0;
144
}
145
#endif
146 147 148 149 150

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
151 152 153
static int
xmlGetDoubleValue (xmlNodePtr node, const char *name, double *val)
{
154 155
	int res;
	char *ret;
156

157 158
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
159
	res = sscanf (ret, "%lf", val);
160 161
	free(ret);
	
162
	return (res == 1);
163 164
}

165
#if 0
166 167 168
/*
 * Get a set of coodinates for a node, carried as the content of a child.
 */
169
static int
170 171 172
xmlGetCoordinate (xmlNodePtr node, const char *name,
		  double *x, double *y)
{
173 174
	int res;
	char *ret;
175

176 177
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
178
	res = sscanf (ret, "(%lf %lf)", x, y);
179 180
	free(ret);
	
181
	return (res == 2);
182
}
183
#endif
184 185 186 187 188

/*
 * Get a pair of coodinates for a node, carried as the content of a child.
 */

189 190 191 192
static int
xmlGetCoordinates (xmlNodePtr node, const char *name,
		   double *x1, double *y1, double *x2, double *y2)
{
193 194
	int res;
	char *ret;
195

196 197
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
198
	res = sscanf (ret, "(%lf %lf)(%lf %lf)", x1, y1, x2, y2);
199 200
	free(ret);
	
201
	if (res == 4) 
202
		return 1;
203

204
	return 0;
205 206
}

207
#if 0
208 209 210
/*
 * Get a GnomeCanvasPoints for a node, carried as the content of a child.
 */
211
static GnomeCanvasPoints *
212 213
xmlGetGnomeCanvasPoints (xmlNodePtr node, const char *name)
{
214
	char *val;
215 216 217 218
	GnomeCanvasPoints *ret = NULL;
	int res;
	const char *ptr;
	int index = 0, i;
219
	double coord[20];	/* TODO: must be dynamic !!!! */
220

221
	val = xmlGetValue (node, name);
222
	if (val == NULL) return(NULL);
223 224 225 226 227
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
228
			break;
229
		res = sscanf (ptr, "(%lf %lf)", &coord[index], &coord[index + 1]);
230 231 232 233 234 235 236 237 238 239 240 241 242
		if (res != 2)
			break;
		index += 2;
		ptr++;
	} while (res > 0);
	free(val);

	if (index >= 2)
		ret = gnome_canvas_points_new (index / 2);
	if (ret == NULL)
		return NULL;
	for (i = 0; i < index; i++)
		ret->coords[i] = coord[i];
243
	return ret;
244
}
245
#endif
246 247 248 249 250

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
251 252 253 254 255 256 257 258 259 260 261 262
static void
xmlSetGnomeCanvasPoints (xmlNodePtr node, const char *name,
			 GnomeCanvasPoints *val)
{
	xmlNodePtr child;
	char *str, *base;
	int i;

	if (val == NULL)
		return;
	if ((val->num_points < 0) || (val->num_points > 5000))
		return;
263
	base = str = g_malloc (val->num_points * 30 * sizeof (char));
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	if (str == NULL)
		return;
	for (i = 0; i < val->num_points; i++){
		str += sprintf (str, "(%f %f)", val->coords[2 * i],
				val->coords[2 * i + 1]);
	}

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, base);
			free (base);
			return;
		}
		child = child->next;
	}
280
	xmlNewChild (node, NULL, name, xmlEncodeEntities(node->doc, base));
281
	g_free (base);
282 283 284
}


285 286 287 288
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static void xmlSetValue (xmlNodePtr node, const char *name, const char *val)
{
	const char *ret;
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, val);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val);
308 309
}

310 311 312 313
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
static void xmlSetStringValue (xmlNodePtr node, const char *name, String *val)
{
	const char *ret;
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, val->str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val->str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val->str);
333 334
}

335 336 337 338
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
static void
xmlSetIntValue (xmlNodePtr node, const char *name, int val)
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%d", val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
361 362
}

363
#if 0
364 365 366 367
/*
 * Set a float value for a node either carried as an attibute or as
 * the content of a child.
 */
368 369
static void
xmlSetFloatValue (xmlNodePtr node, const char *name, float val)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%f", val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
390
}
391
#endif
392

393
#if 0
394 395 396 397
/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
398 399
static void
xmlSetDoubleValue (xmlNodePtr node, const char *name, double val)
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%f", (float) val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
420
}
421
#endif
422 423 424 425

/*
 * Search a child by name, if needed go down the tree to find it. 
 */
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
static xmlNodePtr xmlSearchChild (xmlNodePtr node, const char *name)
{
	xmlNodePtr ret;
	xmlNodePtr child;

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name))
			return child;
		child = child->next;
	}
	child = node->childs;
	while (child != NULL){
		ret = xmlSearchChild (child, name);
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
445 446 447 448 449 450 451 452 453 454
}

/*
 * Get a color value for a node either carried as an attibute or as
 * the content of a child.
 *
 * TODO PBM: at parse time one doesn't have yet a widget, so we have
 *           to retrieve the default colormap, but this may be a bad
 *           option ...
 */
455 456 457 458
static int
xmlGetColorValue (xmlNodePtr node, const char *name,
			     StyleColor **color)
{
459
	char *ret;
460 461
	int red, green, blue;

462 463 464
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
465
		*color = style_color_new (red, green, blue);
466
		free(ret);
467 468 469
		return 1;
	}
	return 0;
470 471 472 473 474 475
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
static void xmlSetColorValue (xmlNodePtr node, const char *name,
			      StyleColor *val)
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%X:%X:%X", val->color.red, val->color.green, val->color.blue);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
498
}
499 500 501 502 503

/**
 **
 ** Public functions : high level read and write.
 **
504
 **/
505 506 507 508 509 510 511

/*
 * Open an XML file and read a Sheet
 * One parse the XML file, getting a tree, then analyze the tree to build
 * the actual in-memory structure.
 */

512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
Sheet *gnumericReadXmlSheet (const char *filename)
{
	Sheet *sheet;
	xmlDocPtr res;
	xmlNsPtr gmr;
	parseXmlContext ctxt;

	g_return_val_if_fail (filename != NULL, NULL);

	/*
	 * Load the file into an XML tree.
	 */
	res = xmlParseFile (filename);
	if (res == NULL)
		return NULL;
	if (res->root == NULL){
		fprintf (stderr, "gnumericReadXmlSheet %s: tree is empty\n", filename);
		xmlFreeDoc (res);
		return NULL;
	}
	/*
	 * Do a bit of checking, get the namespaces, and check the top elem.
	 */
	gmr = xmlSearchNsByHref (res, res->root, "http://www.gnome.org/gnumeric/");
	if (strcmp (res->root->name, "Sheet") || (gmr == NULL)){
		fprintf (stderr, "gnumericReadXmlSheet %s: not a Sheet file\n",
			 filename);
		xmlFreeDoc (res);
		return NULL;
	}
	ctxt.doc = res;
	ctxt.ns = gmr;
	ctxt.nameTable = g_hash_table_new (g_str_hash, g_str_equal);
	ctxt.fontIdx = 1;
546
	ctxt.fontname2name = NULL;
547 548 549 550 551
	sheet = readXmlSheet (&ctxt, res->root);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	xmlFreeDoc (res);
552 553

	sheet->modified = FALSE;
554
	return sheet;
555 556 557 558 559 560 561 562
}

/*
 * Save a Sheet in an XML file
 * One build an in-memory XML tree and save it to a file.
 * returns 0 in case of success, -1 otherwise.
 */

563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
int
gnumericWriteXmlSheet (Sheet * sheet, const char *filename)
{
	xmlDocPtr xml;
	xmlNsPtr gmr;
	parseXmlContext ctxt;
	int ret;

	g_return_val_if_fail (sheet != NULL, -1);
	g_return_val_if_fail (IS_SHEET (sheet), -1);
	g_return_val_if_fail (filename != NULL, -1);

	/*
	 * Create the tree
	 */
	xml = xmlNewDoc ("1.0");
	if (xml == NULL){
		return -1;
	}
	gmr = xmlNewGlobalNs (xml, "http://www.gnome.org/gnumeric/", "gmr");
	ctxt.doc = xml;
	ctxt.ns = gmr;
	ctxt.nameTable = g_hash_table_new (g_str_hash, g_str_equal);
	ctxt.fontIdx = 1;
587
	ctxt.fontname2name = g_hash_table_new (g_str_hash, g_str_equal);
588 589 590
	xml->root = writeXmlSheet (&ctxt, sheet);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);
591 592
	g_hash_table_foreach (ctxt.fontname2name, fontname_nameFree, NULL);
	g_hash_table_destroy (ctxt.fontname2name);
593 594 595 596 597 598 599

	/*
	 * Dump it.
	 */
	xmlSetDocCompressMode (xml, 9);
	ret = xmlSaveFile (filename, xml);
	xmlFreeDoc (xml);
600
	sheet->modified = FALSE;
601 602 603
	if (ret < 0)
		return -1;
	return 0;
604 605 606 607 608 609 610 611
}

/*
 * Open an XML file and read a Workbook
 * One parse the XML file, getting a tree, then analyze the tree to build
 * the actual in-memory structure.
 */

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
Workbook *gnumericReadXmlWorkbook (const char *filename)
{
	Workbook *wb;
	xmlDocPtr res;
	xmlNsPtr gmr;
	parseXmlContext ctxt;

	g_return_val_if_fail (filename != NULL, NULL);

	/*
	 * Load the file into an XML tree.
	 */
	res = xmlParseFile (filename);
	if (res == NULL)
		return NULL;
	if (res->root == NULL){
		fprintf (stderr, "gnumericReadXmlWorkbook %s: tree is empty\n", filename);
		xmlFreeDoc (res);
		return NULL;
	}
	/*
	 * Do a bit of checking, get the namespaces, and chech the top elem.
	 */
	gmr = xmlSearchNsByHref (res, res->root, "http://www.gnome.org/gnumeric/");
	if (strcmp (res->root->name, "Workbook") || (gmr == NULL)){
		fprintf (stderr, "gnumericReadXmlWorkbook %s: not an Workbook file\n",
			 filename);
		xmlFreeDoc (res);
		return NULL;
	}
	ctxt.doc = res;
	ctxt.ns = gmr;
	ctxt.nameTable = g_hash_table_new (g_str_hash, g_str_equal);
	ctxt.fontIdx = 1;
646
	ctxt.fontname2name = NULL;
647
	wb = readXmlWorkbook (&ctxt, res->root);
648
	workbook_set_filename (wb, (char *) filename);
649 650 651 652 653 654
	workbook_recalc_all (wb);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	xmlFreeDoc (res);
	return wb;
655 656 657 658 659 660 661 662
}

/*
 * Save a Workbook in an XML file
 * One build an in-memory XML tree and save it to a file.
 * returns 0 in case of success, -1 otherwise.
 */

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
int
gnumericWriteXmlWorkbook (Workbook *wb, const char *filename)
{
	xmlDocPtr xml;
	xmlNsPtr gmr;
	parseXmlContext ctxt;
	int ret;

	g_return_val_if_fail (wb != NULL, -1);
	g_return_val_if_fail (filename != NULL, -1);

	/*
	 * Create the tree
	 */
	xml = xmlNewDoc ("1.0");
	if (xml == NULL){
		return -1;
	}
	gmr = xmlNewGlobalNs (xml, "http://www.gnome.org/gnumeric/", "gmr");
	ctxt.doc = xml;
	ctxt.ns = gmr;
	ctxt.nameTable = g_hash_table_new (g_str_hash, g_str_equal);
	ctxt.fontIdx = 1;
686
	ctxt.fontname2name = g_hash_table_new (g_str_hash, g_str_equal);
687 688 689
	xml->root = writeXmlWorkbook (&ctxt, wb);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);
690 691
	g_hash_table_foreach (ctxt.fontname2name, fontname_nameFree, NULL);
	g_hash_table_destroy (ctxt.fontname2name);
692 693 694 695 696 697 698 699 700 701

	/*
	 * Dump it.
	 */
	xmlSetDocCompressMode (xml, 9);
	ret = xmlSaveFile (filename, xml);
	xmlFreeDoc (xml);
	if (ret < 0)
		return -1;
	return 0;
702 703 704 705 706 707
}

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
708
 **/
709 710 711 712

/*
 * Free a name when cleaning up the name Hash table.
 */
713 714
static void
nameFree (gpointer key, gpointer value, gpointer user_data)
715
{
716
	g_free (key);
717 718
}

719 720 721 722 723 724 725 726 727 728
/*
 * Free a fontname and name when cleaning up the name Hash table.
 */
static void
fontname_nameFree (gpointer key, gpointer value, gpointer user_data)
{
	g_free (key);
	g_free (value);
}

729 730
static int
style_is_default_fore (StyleColor *color)
731
{
732 733
	if (!color)
		return TRUE;
734

735 736 737 738
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
739 740
}

741 742
static int
style_is_default_back (StyleColor *color)
743
{
744 745
	if (!color)
		return TRUE;
746

747 748 749 750
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
751 752 753 754 755
}

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
756 757 758
static char *BorderTypes[8] =
{
	"none",
759 760 761 762 763 764 765
 	"thin",
 	"medium",
 	"dashed",
 	"dotted",
 	"thick",
 	"double",
	"hair"
766 767
};

768 769 770 771 772 773
static char *StyleSideNames[4] =
{
 	"Top",
 	"Bottom",
 	"Left",
 	"Right"
Arturo Espinosa's avatar
Arturo Espinosa committed
774
};
775

776 777 778 779 780
static xmlNodePtr
writeXmlStyleBorder (parseXmlContextPtr ctxt, StyleBorder *border)
{
	xmlNodePtr cur;
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
781
	int lp;
782
       
Arturo Espinosa's avatar
Arturo Espinosa committed
783 784 785
	for (lp = 3; lp >= 0; lp--)
		if (border->type [lp] != BORDER_NONE)
			break;
786
	if (lp < 0)
Arturo Espinosa's avatar
Arturo Espinosa committed
787
		return NULL;
788

789
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
790
	
791 792 793 794 795
	for (lp = 0; lp < 4; lp++){
 		if (border->type[lp] != BORDER_NONE){
 			side = xmlNewChild (cur, ctxt->ns, StyleSideNames [lp],
 					    BorderTypes [border->type [lp]]);
 			xmlSetColorValue (side, "Color", border->color [lp]);
796
 		}
797 798
	}
	return cur;
799 800 801 802 803
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
804 805 806 807
static StyleBorder *
readXmlStyleBorder (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	StyleBorder *ret;
Arturo Espinosa's avatar
Arturo Espinosa committed
808 809
 	StyleBorderType style [4] = { BORDER_NONE, BORDER_NONE, BORDER_NONE, BORDER_NONE };
 	StyleColor *color [4] = { NULL, NULL, NULL, NULL };
810
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
811
	int lp;
812 813 814 815 816 817

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
			 "readXmlStyleBorder: invalid element type %s, 'StyleBorder' expected`\n",
			 tree->name);
	}
818

Arturo Espinosa's avatar
Arturo Espinosa committed
819
 	for (lp = 0; lp < 4; lp++)
820
 	{
Arturo Espinosa's avatar
Arturo Espinosa committed
821
 		if ((side = xmlSearchChild (tree, StyleSideNames [lp])) != NULL)
822 823
 		{
 			/* FIXME: need to read the proper type */
Arturo Espinosa's avatar
Arturo Espinosa committed
824 825
 			style [lp] = BORDER_THICK ;
 			xmlGetColorValue (side, "Color", &color [lp]);
826
 		}
827
	}
828
	
Arturo Espinosa's avatar
Arturo Espinosa committed
829
	ret = style_border_new (style, color);
830

831
	return NULL;
832 833 834 835 836
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
837 838 839 840 841 842 843 844 845 846 847
static xmlNodePtr
writeXmlStyle (parseXmlContextPtr ctxt, Style * style)
{
	xmlNodePtr cur, child;
	char str[50];

	if ((style->halign == 0) && (style->valign == 0) &&
	    (style->orientation == 0) && (style->format == NULL) &&
	    (style->font == NULL) && (style->border == NULL))
		return NULL;

848
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	xmlSetIntValue (cur, "HAlign", style->halign);
	xmlSetIntValue (cur, "VAlign", style->valign);
	xmlSetIntValue (cur, "Fit", style->fit_in_cell);
	xmlSetIntValue (cur, "Orient", style->orientation);
	xmlSetIntValue (cur, "Shade", style->pattern);

	if (!style_is_default_fore (style->fore_color))
		xmlSetColorValue (cur, "Fore", style->fore_color);

	if (!style_is_default_back (style->back_color))
		xmlSetColorValue (cur, "Back", style->back_color);

	if (style->format != NULL){
		xmlSetValue (cur, "Format", style->format->format);
	}

	if (style->font != NULL){
866 867 868 869
		const char *name;
		name = (const char *) g_hash_table_lookup (ctxt->fontname2name,
							   style->font->font_name);
		if (name) {
870 871 872 873
			child = xmlNewChild (cur, ctxt->ns, "Font", NULL);
			sprintf (str, "#%s", name);
			xmlNewProp (child, "HREF", str);
		} else {
874 875 876
			child = xmlNewChild (cur, ctxt->ns, "Font", 
			           xmlEncodeEntities(ctxt->doc,
				                     style->font->font_name));
877 878 879 880
			xmlSetIntValue (child, "Unit", style->font->units);
			sprintf (str, "FontDef%d", ctxt->fontIdx++);
			xmlNewProp (child, "NAME", str);
			g_hash_table_insert (ctxt->nameTable, g_strdup (str), style->font);
881 882 883
			g_hash_table_insert (ctxt->fontname2name,
					     g_strdup (style->font->font_name),
					     g_strdup (str));
884 885 886 887 888 889 890 891 892 893
		}
	}

	if (style->border != NULL){
		child = writeXmlStyleBorder (ctxt, style->border);
		if (child)
			xmlAddChild (cur, child);
	}

	return cur;
894 895 896 897 898
}

/*
 * Create a Style equivalent to the XML subtree of doc.
 */
899 900 901 902
static Style *
readXmlStyle (parseXmlContextPtr ctxt, xmlNodePtr tree, Style * ret)
{
	xmlNodePtr child;
Morten Welinder's avatar
Morten Welinder committed
903
	char *prop;
904 905 906 907 908
	int val;
	StyleColor *c;

	if (strcmp (tree->name, "Style")){
		fprintf (stderr,
909
			 "readXmlStyle: invalid element type %s, 'Style' expected\n",
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
			 tree->name);
	}
	if (ret == NULL)
		ret = style_new_empty ();
	
	if (ret == NULL)
		return NULL;

	if (xmlGetIntValue (tree, "HAlign", &val)){
		ret->halign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
	if (xmlGetIntValue (tree, "Fit", &val)){
		ret->fit_in_cell = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
	if (xmlGetIntValue (tree, "VAlign", &val)){
		ret->valign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
	if (xmlGetIntValue (tree, "Orient", &val)){
		ret->orientation = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
	if (xmlGetIntValue (tree, "Shade", &val)){
		ret->pattern = val;
		ret->valid_flags |= STYLE_PATTERN;
	}
	if (xmlGetColorValue (tree, "Fore", &c)){
		ret->fore_color = c;
		ret->valid_flags |= STYLE_FORE_COLOR;
	}
	if (xmlGetColorValue (tree, "Back", &c)){
		ret->back_color = c;
		ret->valid_flags |= STYLE_BACK_COLOR;
	}
	prop = xmlGetValue (tree, "Format");
	if (prop != NULL){
		if (ret->format == NULL){
Morten Welinder's avatar
Morten Welinder committed
949
			ret->format = style_format_new ((const char *) prop);
950
			ret->valid_flags |= STYLE_FORMAT;
951
		}
Morten Welinder's avatar
Morten Welinder committed
952
		free (prop);
953
	}
954

955 956 957
	child = tree->childs;
	while (child != NULL){
		if (!strcmp (child->name, "Font")){
Morten Welinder's avatar
Morten Welinder committed
958
			char *v;
959 960 961 962

			v = xmlGetValue (child, "NAME");
			if (v){
				int units = 14;
963
				char *font;
964 965

				xmlGetIntValue (child, "Unit", &units);
966 967 968 969 970
				font = xmlNodeGetContent(child);
				if (font != NULL) {
					ret->font = style_font_new (font, units);
					free(font);
				}
971 972 973 974
				if (ret->font){
					g_hash_table_insert (ctxt->nameTable, g_strdup (v), ret->font);
					ret->valid_flags |= STYLE_FONT;
				}
Morten Welinder's avatar
Morten Welinder committed
975
				free (v);
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
			} else {
				StyleFont *font;

				v = xmlGetValue (child, "HREF");
				if (v){
					font = g_hash_table_lookup (ctxt->nameTable, v + 1);
					if (font){
						ret->font = font;
						style_font_ref (font);
						ret->valid_flags |= STYLE_FONT;
					}
				}
			}
		} else if (!strcmp (child->name, "StyleBorder")){
			StyleBorder *sb;

			sb = readXmlStyleBorder (ctxt, child);
		} else {
			fprintf (stderr, "readXmlStyle: unknown type '%s'\n",
				 child->name);
		}
		child = child->next;
	}

	/* Now add defaults to any style that was not loaded */
	return ret;
1002 1003
}

1004
#if 0
1005 1006 1007
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
1008
static xmlNodePtr
1009 1010 1011 1012
writeXmlStyleRegion (parseXmlContextPtr ctxt, StyleRegion *region)
{
	xmlNodePtr cur, child;

1013
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
	xmlSetIntValue (cur, "startCol", region->range.start_col);
	xmlSetIntValue (cur, "endCol", region->range.end_col);
	xmlSetIntValue (cur, "startRow", region->range.start_row);
	xmlSetIntValue (cur, "endRow", region->range.end_row);

	if (region->style != NULL){
		child = writeXmlStyle (ctxt, region->style);
		if (child)
			xmlAddChild (cur, child);
	}
	return cur;
1025
}
1026
#endif
1027 1028 1029 1030

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
static void
readXmlStyleRegion (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	xmlNodePtr child;
	Style *style = NULL;
	int start_col = 0, start_row = 0, end_col = 0, end_row = 0;

	if (strcmp (tree->name, "StyleRegion")){
		fprintf (stderr,
			 "readXmlStyleRegion: invalid element type %s, 'StyleRegion' expected`\n",
			 tree->name);
		return;
	}
	xmlGetIntValue (tree, "startCol", &start_col);
	xmlGetIntValue (tree, "startRow", &start_row);
	xmlGetIntValue (tree, "endCol", &end_col);
	xmlGetIntValue (tree, "endRow", &end_row);
	child = tree->childs;
	if (child != NULL)
		style = readXmlStyle (ctxt, child, NULL);
	if (style != NULL)
		sheet_style_attach (ctxt->sheet, start_col, start_row, end_col,
				    end_row, style);
1054 1055 1056

}

1057 1058 1059
/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
1060 1061 1062 1063 1064 1065
static xmlNodePtr
writeXmlColRowInfo (parseXmlContextPtr ctxt, ColRowInfo *info, int col)
{
	xmlNodePtr cur;

	if (col)
1066
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "ColInfo", NULL);
1067
	else
1068
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "RowInfo", NULL);
1069 1070 1071 1072 1073 1074 1075 1076

	xmlSetIntValue (cur, "No", info->pos);
	xmlSetIntValue (cur, "Unit", info->units);
	xmlSetIntValue (cur, "MarginA", info->margin_a);
	xmlSetIntValue (cur, "MarginB", info->margin_b);
	xmlSetIntValue (cur, "HardSize", info->hard_size);

	return cur;
1077 1078 1079 1080 1081
}

/*
 * Create a ColRowInfo equivalent to the XML subtree of doc.
 */
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
static ColRowInfo *
readXmlColRowInfo (parseXmlContextPtr ctxt,
		   xmlNodePtr tree, ColRowInfo *ret)
{
	int col = 0;
	int val;

	if (!strcmp (tree->name, "ColInfo")){
		col = 1;
	} else if (!strcmp (tree->name, "RowInfo")){
		col = 0;
	} else {
		fprintf (stderr,
			 "readXmlColRowInfo: invalid element type %s, 'ColInfo/RowInfo' expected`\n",
			 tree->name);
		return NULL;
	}
	if (ret == NULL){
		if (col)
			ret = sheet_col_new (ctxt->sheet);
		else
			ret = sheet_row_new (ctxt->sheet);
	}
	if (ret == NULL)
		return NULL;
1107

1108 1109 1110 1111 1112 1113
	xmlGetIntValue (tree, "No", &ret->pos);
	xmlGetIntValue (tree, "Unit", &ret->units);
	xmlGetIntValue (tree, "MarginA", &ret->margin_a);
	xmlGetIntValue (tree, "MarginB", &ret->margin_b);
	if (xmlGetIntValue (tree, "HardSize", &val))
		ret->hard_size = val;
1114

1115
	return ret;
1116 1117
}

1118 1119 1120
/*
 * Create an XML subtree of doc equivalent to the given Object.
 */
1121 1122 1123
static xmlNodePtr
writeXmlObject (parseXmlContextPtr ctxt, SheetObject *object)
{
1124
	SheetObjectGraphic *sog = SHEET_OBJECT_GRAPHIC (object);
1125 1126
	xmlNodePtr cur = NULL;

1127
	switch (sog->type){
1128
	case SHEET_OBJECT_RECTANGLE:{
1129
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1130

1131
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Rectangle", NULL);
1132 1133 1134
			if (sof->fill_color != NULL)
				xmlSetStringValue (cur, "FillColor", sof->fill_color);
			xmlSetIntValue (cur, "Pattern", sof->pattern);
1135 1136
			break;
		}
1137

1138
	case SHEET_OBJECT_ELLIPSE:{
1139
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1140

1141
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Ellipse", NULL);
1142 1143 1144
			if (sof->fill_color != NULL)
				xmlSetStringValue (cur, "FillColor", sof->fill_color);
			xmlSetIntValue (cur, "Pattern", sof->pattern);
1145 1146
			break;
		}
1147

1148
	case SHEET_OBJECT_ARROW:
1149
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Arrow", NULL);
1150
		break;
1151

1152
	case SHEET_OBJECT_LINE:
1153
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Line", NULL);
1154 1155 1156 1157 1158
		break;
	}
	if (cur == NULL)
		return NULL;
	
1159
	xmlSetGnomeCanvasPoints (cur, "Points", object->bbox_points);
1160 1161 1162
	xmlSetIntValue (cur, "Width", sog->width);
	xmlSetStringValue (cur, "Color", sog->color);

1163
	return cur;
1164 1165 1166 1167 1168
}

/*
 * Create a Object equivalent to the XML subtree of doc.
 */
1169 1170 1171 1172
static SheetObject *
readXmlObject (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	SheetObject *ret;
1173
	SheetObjectFilled *sof;
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
	char *color;
	char *fill_color;
	int type;
	double x1, y1, x2, y2;
	int width = 1;
	int pattern;

	if (!strcmp (tree->name, "Rectangle")){
		type = SHEET_OBJECT_RECTANGLE;
	} else if (!strcmp (tree->name, "Ellipse")){
		type = SHEET_OBJECT_ELLIPSE;
	} else if (!strcmp (tree->name, "Arrow")){
		type = SHEET_OBJECT_ARROW;
	} else if (!strcmp (tree->name, "Line")){
		type = SHEET_OBJECT_LINE;
	} else {
		fprintf (stderr,
			 "readXmlObject: invalid element type %s, 'Rectangle/Ellipse ...' expected`\n",
			 tree->name);
		return NULL;
	}
	
	color = (char *) xmlGetValue (tree, "Color");
	xmlGetCoordinates (tree, "Points", &x1, &y1, &x2, &y2);
	xmlGetIntValue (tree, "Width", &width);
	if ((type == SHEET_OBJECT_RECTANGLE) ||
	    (type == SHEET_OBJECT_ELLIPSE)){
		fill_color = (char *) xmlGetValue (tree, "FillColor");
		xmlGetIntValue (tree, "Pattern", &pattern);
1203 1204 1205
		ret = sheet_object_create_filled (
			ctxt->sheet, type,
			x1, y1, x2, y2, fill_color, color, width);
1206
		if (ret != NULL){
1207 1208
			sof = SHEET_OBJECT_FILLED (ret);
			sof->pattern = pattern;
1209 1210
		}
	} else {
1211 1212 1213
		ret = sheet_object_create_line (
			ctxt->sheet, type,
			x1, y1, x2, y2, color, width);
1214
	}
1215
	sheet_object_realize (ret);
1216
	return ret;
1217 1218
}

1219 1220 1221
/*
 * Create an XML subtree of doc equivalent to the given Cell.
 */
1222 1223 1224 1225
static xmlNodePtr
writeXmlCell (parseXmlContextPtr ctxt, Cell *cell)
{
	xmlNodePtr cur, child;
1226 1227
	char *text;
	
1228
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Cell", NULL);
1229 1230 1231 1232 1233 1234
	xmlSetIntValue (cur, "Col", cell->col->pos);
	xmlSetIntValue (cur, "Row", cell->row->pos);
	child = writeXmlStyle (ctxt, cell->style);

	if (child)
		xmlAddChild (cur, child);
1235
	text = cell_get_content (cell);
1236 1237
	xmlNewChild(cur, ctxt->ns, "Content",
	            xmlEncodeEntities(ctxt->doc, text));
1238
	g_free (text);
1239 1240

	return cur;
1241 1242 1243 1244 1245
}

/*
 * Create a Cell equivalent to the XML subtree of doc.
 */
1246 1247 1248 1249 1250
static Cell *
readXmlCell (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	Style *style;
	Cell *ret;
1251
	xmlNodePtr childs;
1252
	int row = 0, col = 0;
1253
	char *content = NULL;
1254 1255 1256

	if (strcmp (tree->name, "Cell")){
		fprintf (stderr,
1257
		 "readXmlCell: invalid element type %s, 'Cell' expected`\n",
1258 1259 1260 1261 1262 1263
			 tree->name);
		return NULL;
	}
	xmlGetIntValue (tree, "Col", &col);
	xmlGetIntValue (tree, "Row", &row);

Morten Welinder's avatar
Morten Welinder committed
1264 1265
	cell_deep_freeze_redraws ();

1266 1267 1268 1269 1270 1271
	ret = sheet_cell_get (ctxt->sheet, col, row);
	if (ret == NULL)
		ret = sheet_cell_new (ctxt->sheet, col, row);
	if (ret == NULL)
		return NULL;

1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	childs = tree->childs;
	while (childs != NULL) {
	        if (!strcmp (childs->name, "Style")) {
		        style = readXmlStyle (ctxt, childs, NULL);
			if (style){
				style_merge_to (style, ret->style);
				style_destroy (ret->style);
				ret->style = style;
			}
		}
1282 1283
	        if (!strcmp (childs->name, "Content"))
		        content = xmlNodeGetContent(childs);
1284 1285
		childs = childs->next;
	}
1286 1287
	if (content == NULL)
		content = xmlNodeGetContent(tree);
1288 1289
	if (content != NULL) {
		char *p = content + strlen (content);
1290

1291
		while (p > content){
1292 1293 1294 1295 1296 1297
			p--;
			if (*p != ' ' && *p != '\n')
				break;
			*p = 0;
		}

1298 1299
		cell_set_text_simple (ret, content);
		free (content);
1300 1301 1302
	} else
		cell_set_text_simple (ret, "");

Michael Meeks's avatar
Michael Meeks committed
1303 1304
	cell_deep_thaw_redraws ();

1305
	return ret;
1306 1307 1308 1309 1310
}

/*
 * Create an XML subtree equivalent to the given cell and add it to the parent
 */
1311 1312 1313 1314 1315
static void
writeXmlCellTo (gpointer key, gpointer value, gpointer data)
{
	parseXmlContextPtr ctxt = (parseXmlContextPtr) data;
	xmlNodePtr cur;
1316

1317 1318
	cur = writeXmlCell (ctxt, (Cell *) value);
	xmlAddChild (ctxt->parent, cur);
1319 1320 1321 1322 1323
}

/*
 * Create an XML subtree of doc equivalent to the given Sheet.
 */
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338
static xmlNodePtr
writeXmlSheet (parseXmlContextPtr ctxt, Sheet *sheet)
{
	xmlNodePtr cur;
	xmlNodePtr child;
	xmlNodePtr rows;
	xmlNodePtr cols;
	xmlNodePtr cells;
	xmlNodePtr objects;
	GList *l;
	char str[50];

	/*
	 * General informations about the Sheet.
	 */
1339
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Sheet", NULL);
1340 1341
	if (cur == NULL)
		return NULL;
1342 1343
	xmlNewChild (cur, ctxt->ns, "Name", 
	             xmlEncodeEntities(ctxt->doc, sheet->name));
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404
	sprintf (str, "%d", sheet->max_col_used);
	xmlNewChild (cur, ctxt->ns, "MaxCol", str);
	sprintf (str, "%d", sheet->max_row_used);
	xmlNewChild (cur, ctxt->ns, "MaxRow", str);
	sprintf (str, "%f", sheet->last_zoom_factor_used);
	xmlNewChild (cur, ctxt->ns, "Zoom", str);

	/*
	 * Cols informations.
	 */
	cols = xmlNewChild (cur, ctxt->ns, "Cols", NULL);
	l = sheet->cols_info;
	while (l){
		child = writeXmlColRowInfo (ctxt, l->data, 1);
		if (child)
			xmlAddChild (cols, child);
		l = l->next;
	}

	/*
	 * Rows informations.
	 */
	rows = xmlNewChild (cur, ctxt->ns, "Rows", NULL);
	l = sheet->rows_info;
	while (l){
		child = writeXmlColRowInfo (ctxt, l->data, 0);
		if (child)
			xmlAddChild (rows, child);
		l = l->next;
	}

	/*
	 * Style : TODO ...
	 */

	/*
	 * Objects
	 * NOTE: seems that objects == NULL while current_object != NULL
	 * is possible
	 */
	if (sheet->objects != NULL){
		objects = xmlNewChild (cur, ctxt->ns, "Objects", NULL);
		l = sheet->objects;
		while (l){
			child = writeXmlObject (ctxt, l->data);
			if (child)
				xmlAddChild (objects, child);
			l = l->next;
		}
	} else if (sheet->current_object != NULL){
		objects = xmlNewChild (cur, ctxt->ns, "Objects", NULL);
		child = writeXmlObject (ctxt, sheet->current_object);
		if (child)
			xmlAddChild (objects, child);
	}
	/*
	 * Cells informations
	 */
	cells = xmlNewChild (cur, ctxt->ns, "Cells", NULL);
	ctxt->parent = cells;
	g_hash_table_foreach (sheet->cell_hash, writeXmlCellTo, ctxt);
1405
	sheet->modified = 0;
1406
	return cur;
1407 1408 1409 1410 1411
}

/*
 * Create a Sheet equivalent to the XML subtree of doc.
 */
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
static Sheet *
readXmlSheet (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	xmlNodePtr child;
	xmlNodePtr rows;
	xmlNodePtr cols;
	xmlNodePtr regions;
	/* xmlNodePtr styles; */
	xmlNodePtr cells;
	xmlNodePtr objects;
1422
	Sheet *ret = NULL;
Morten Welinder's avatar
Morten Welinder committed
1423
	char *val;
1424 1425 1426

	if (strcmp (tree->name, "Sheet")){
		fprintf (stderr,
1427
			 "readXmlSheet: invalid element type %s, 'Sheet' expected\n",
1428 1429 1430
			 tree->name);
	}
	child = tree->childs;
1431

1432
	/*
1433 1434 1435
	 * Get the name of the sheet.  If it does exist, use the existing
	 * name, otherwise create a sheet (ie, for the case of only reading
	 * a new sheet).
1436 1437 1438
	 */
	val = xmlGetValue (tree, "Name");
	if (val != NULL){
1439