xml-io.c 36.1 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 "xml-io.h"
18
#include "file.h"
19 20 21 22 23

/*
 * A parsing context.
 */
typedef struct parseXmlContext {
24 25 26 27 28 29 30
	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 ... */
	Sheet *sheet;		/* the associated sheet */
	Workbook *wb;		/* the associated sheet */
31 32
} parseXmlContext, *parseXmlContextPtr;

Arturo Espinosa's avatar
Arturo Espinosa committed
33 34 35 36
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);
37
/* static guint       ptrHash          (gconstpointer a);
38 39 40
   static gint        ptrCompare       (gconstpointer a, gconstpointer b); */
static void nameFree                (gpointer key, gpointer value,
				     gpointer user_data);
41

42 43 44
/*
 * Internal stuff: xml helper functions.
 */
45 46 47 48 49

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

56
	ret = (char *) xmlGetProp (node, name);
57
	if (ret != NULL)
58
		return strdup (ret);
59 60 61
	child = node->childs;

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

	return NULL;
74 75
}

76 77 78 79
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
80
static String *
81 82
xmlGetStringValue (xmlNodePtr node, const char *name)
{
83
	char *val;
84 85
	String *ret;

86 87 88 89 90
	val = xmlGetValue(node, name);
	if (val == NULL) return(NULL);
        ret = string_get(val);
	free(val);
	return(ret);
91 92
}

93 94 95 96
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
97 98 99
static int
xmlGetIntValue (xmlNodePtr node, const char *name, int *val)
{
100
	char *ret;
101
	int i;
102
	int res;
103

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

/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
120
static int
121 122
xmlGetFloatValue (xmlNodePtr node, const char *name, float *val)
{
123 124
	int res;
	char *ret;
125 126 127
	xmlNodePtr child;
	float f;

128 129 130 131 132 133 134
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "%f", &f);
	free(ret);
	
	if (res == 1) {
	        *val = f;
135 136 137
		return 1;
	}
	return 0;
138 139 140 141 142 143
}

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
144 145 146
static int
xmlGetDoubleValue (xmlNodePtr node, const char *name, double *val)
{
147 148
	int res;
	char *ret;
149 150 151
	xmlNodePtr child;
	float f;

152 153 154 155 156 157 158
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "%f", &f);
	free(ret);
	
	if (res == 1) {
	        *val = f;
159 160 161
		return 1;
	}
	return 0;
162 163
}

164 165 166 167
/*
 * Get a set of coodinates for a node, carried as the content of a child.
 */

168
static int
169 170 171
xmlGetCoordinate (xmlNodePtr node, const char *name,
		  double *x, double *y)
{
172 173
	int res;
	char *ret;
174 175 176
	xmlNodePtr child;
	float X, Y;

177 178 179 180 181 182 183 184 185
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "(%f %f)", &X, &Y);
	free(ret);
	
	if (res == 2) {
		*x = X;
		*y = Y;
		return 1;
186 187
	}
	return 0;
188 189 190 191 192 193
}

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

194 195 196 197
static int
xmlGetCoordinates (xmlNodePtr node, const char *name,
		   double *x1, double *y1, double *x2, double *y2)
{
198 199
	int res;
	char *ret;
200 201 202
	xmlNodePtr child;
	float X1, Y1, X2, Y2;

203 204
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
205
	res = sscanf (ret, "(%lf %lf)(%lf %lf)", x1, y1, x2, y2);
206 207
	free(ret);
	
208
	if (res == 2) 
209
		return 1;
210

211
	return 0;
212 213 214 215 216 217
}

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

218
static GnomeCanvasPoints *
219 220
xmlGetGnomeCanvasPoints (xmlNodePtr node, const char *name)
{
221
	char *val;
222 223 224 225 226 227 228
	GnomeCanvasPoints *ret = NULL;
	int res;
	const char *ptr;
	xmlNodePtr child;
	int index = 0, i;
	float coord[20];	/* TODO: must be dynamic !!!! */

229
	val = xmlGetValue (node, name);
230
	if (val == NULL) return(NULL);
231 232 233 234 235
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
236
			break;
237 238 239 240 241 242 243 244 245 246 247 248 249 250
		res = sscanf (ptr, "(%f %f)", &coord[index], &coord[index + 1]);
		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];
251
	return ret;
252 253 254 255 256 257
}

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
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;
	base = str = malloc (val->num_points * 30 * sizeof (char));
	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;
	}
287
	xmlNewChild (node, NULL, name, xmlEncodeEntities(node->doc, base));
288
	free (base);
289 290 291
}


292 293 294 295
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
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);
315 316
}

317 318 319 320
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
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);
340 341
}

342 343 344 345
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
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);
368 369 370 371 372 373
}

/*
 * Set a float value for a node either carried as an attibute or as
 * the content of a child.
 */
374 375
static void
xmlSetFloatValue (xmlNodePtr node, const char *name, float val)
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
{
	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);
396 397 398 399 400 401
}

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
402 403
static void
xmlSetDoubleValue (xmlNodePtr node, const char *name, double val)
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
{
	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);
424 425 426 427 428
}

/*
 * Search a child by name, if needed go down the tree to find it. 
 */
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
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;
448 449 450 451 452 453 454 455 456 457
}

/*
 * 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 ...
 */
458 459 460 461
static int
xmlGetColorValue (xmlNodePtr node, const char *name,
			     StyleColor **color)
{
462
	char *ret;
463 464 465
	xmlNodePtr child;
	int red, green, blue;

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

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
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);
502
}
503 504 505 506 507

/**
 **
 ** Public functions : high level read and write.
 **
508
 **/
509 510 511 512 513 514 515

/*
 * 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.
 */

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 546 547 548 549 550 551 552 553 554
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;
	sheet = readXmlSheet (&ctxt, res->root);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	xmlFreeDoc (res);
555 556

	sheet->modified = FALSE;
557
	return sheet;
558 559 560 561 562 563 564 565
}

/*
 * 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.
 */

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
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;
	xml->root = writeXmlSheet (&ctxt, sheet);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	/*
	 * 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 646
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;
	wb = readXmlWorkbook (&ctxt, res->root);
647
	workbook_set_filename (wb, (char *) filename);
648 649 650 651 652 653
	workbook_recalc_all (wb);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

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

/*
 * 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.
 */

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
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;
	xml->root = writeXmlWorkbook (&ctxt, wb);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	/*
	 * Dump it.
	 */
	xmlSetDocCompressMode (xml, 9);
	ret = xmlSaveFile (filename, xml);
	xmlFreeDoc (xml);
	if (ret < 0)
		return -1;
	return 0;
698 699 700 701 702 703
}

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
704
 **/
705 706 707 708

/*
 * Free a name when cleaning up the name Hash table.
 */
709 710
static void
nameFree (gpointer key, gpointer value, gpointer user_data)
711
{
712
	g_free (key);
713 714
}

715 716
static int
style_is_default_fore (StyleColor *color)
717
{
718 719
	if (!color)
		return TRUE;
720

721 722 723 724
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
725 726
}

727 728
static int
style_is_default_back (StyleColor *color)
729
{
730 731
	if (!color)
		return TRUE;
732

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

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
742 743 744
static char *BorderTypes[8] =
{
	"none",
745 746 747 748 749 750 751
 	"thin",
 	"medium",
 	"dashed",
 	"dotted",
 	"thick",
 	"double",
	"hair"
752 753
};

754 755 756 757 758 759 760 761
static char *StyleSideNames[4] =
{
 	"Top",
 	"Bottom",
 	"Left",
 	"Right"
} ;

762 763 764 765 766
static xmlNodePtr
writeXmlStyleBorder (parseXmlContextPtr ctxt, StyleBorder *border)
{
	xmlNodePtr cur;
	xmlNodePtr side;
767 768 769 770 771 772 773
	int lp ;
       
	for (lp=3;lp>=0;lp--)
		if (border->type[lp] != BORDER_NONE)
			break ;
	if (lp<0)
		return NULL ;
774

775
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
776 777 778 779 780 781 782 783 784
	
	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]);
 		}
785 786
	}
	return cur;
787 788 789 790 791
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
792 793 794 795
static StyleBorder *
readXmlStyleBorder (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	StyleBorder *ret;
796 797
 	StyleBorderType style[4] = { BORDER_NONE, BORDER_NONE, BORDER_NONE, BORDER_NONE } ;
 	StyleColor *color[4] = { NULL, NULL, NULL, NULL } ;
798
	xmlNodePtr side;
799
	int lp ;
800 801 802 803 804 805

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
			 "readXmlStyleBorder: invalid element type %s, 'StyleBorder' expected`\n",
			 tree->name);
	}
806 807 808 809 810 811 812 813 814

 	for (lp=0;lp<4;lp++)
 	{
 		if ((side = xmlSearchChild (tree, StyleSideNames[lp])) != NULL)
 		{
 			/* FIXME: need to read the proper type */
 			style[lp] = BORDER_THICK ;
 			xmlGetColorValue (side, "Color", &color[lp]);
 		}
815
	}
816 817 818
	
	ret = style_border_new(style, color) ;

819
	return NULL;
820 821 822 823 824
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
825 826 827 828 829 830 831 832 833 834 835 836
static xmlNodePtr
writeXmlStyle (parseXmlContextPtr ctxt, Style * style)
{
	xmlNodePtr cur, child;
	char str[50];
	char *name;

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

837
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
	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){
		if ((name = (char *)
		     g_hash_table_lookup (ctxt->nameTable, style->font)) != NULL){
			child = xmlNewChild (cur, ctxt->ns, "Font", NULL);
			sprintf (str, "#%s", name);
			xmlNewProp (child, "HREF", str);
		} else {
861 862 863
			child = xmlNewChild (cur, ctxt->ns, "Font", 
			           xmlEncodeEntities(ctxt->doc,
				                     style->font->font_name));
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
			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);
		}

	}

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

	return cur;
879 880 881 882 883
}

/*
 * Create a Style equivalent to the XML subtree of doc.
 */
884 885 886 887 888 889 890 891 892 893
static Style *
readXmlStyle (parseXmlContextPtr ctxt, xmlNodePtr tree, Style * ret)
{
	xmlNodePtr child;
	const char *prop;
	int val;
	StyleColor *c;

	if (strcmp (tree->name, "Style")){
		fprintf (stderr,
894
			 "readXmlStyle: invalid element type %s, 'Style' expected\n",
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 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
			 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){
			ret->format = style_format_new ((char *) prop);
			ret->valid_flags |= STYLE_FORMAT;
936
		}
937
	}
938

939 940 941 942 943 944 945 946
	child = tree->childs;
	while (child != NULL){
		if (!strcmp (child->name, "Font")){
			const char *v;

			v = xmlGetValue (child, "NAME");
			if (v){
				int units = 14;
947
				char *font;
948 949

				xmlGetIntValue (child, "Unit", &units);
950 951 952 953 954
				font = xmlNodeGetContent(child);
				if (font != NULL) {
					ret->font = style_font_new (font, units);
					free(font);
				}
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
				if (ret->font){
					g_hash_table_insert (ctxt->nameTable, g_strdup (v), ret->font);
					ret->valid_flags |= STYLE_FONT;
				}
			} 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;
985 986
}

987 988 989
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
990
static xmlNodePtr
991 992 993 994
writeXmlStyleRegion (parseXmlContextPtr ctxt, StyleRegion *region)
{
	xmlNodePtr cur, child;

995
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006
	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;
1007 1008 1009 1010 1011
}

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
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);
1035 1036 1037

}

1038 1039 1040
/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
1041 1042 1043 1044 1045 1046
static xmlNodePtr
writeXmlColRowInfo (parseXmlContextPtr ctxt, ColRowInfo *info, int col)
{
	xmlNodePtr cur;

	if (col)
1047
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "ColInfo", NULL);
1048
	else
1049
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "RowInfo", NULL);
1050 1051 1052 1053 1054 1055 1056 1057

	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;
1058 1059 1060 1061 1062
}

/*
 * Create a ColRowInfo equivalent to the XML subtree of doc.
 */
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
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;
1088

1089 1090 1091 1092 1093 1094
	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;
1095

1096
	return ret;
1097 1098
}

1099 1100 1101
/*
 * Create an XML subtree of doc equivalent to the given Object.
 */
1102 1103 1104 1105 1106 1107 1108 1109 1110
static xmlNodePtr
writeXmlObject (parseXmlContextPtr ctxt, SheetObject *object)
{
	xmlNodePtr cur = NULL;

	switch (object->type){
	case SHEET_OBJECT_RECTANGLE:{
			SheetFilledObject *fo = (SheetFilledObject *) object;

1111
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Rectangle", NULL);
1112 1113 1114 1115 1116 1117 1118 1119
			if (fo->fill_color != NULL)
				xmlSetStringValue (cur, "FillColor", fo->fill_color);
			xmlSetIntValue (cur, "Pattern", fo->pattern);
			break;
		}
	case SHEET_OBJECT_ELLIPSE:{
			SheetFilledObject *fo = (SheetFilledObject *) object;

1120
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Ellipse", NULL);
1121 1122 1123 1124 1125
			if (fo->fill_color != NULL)
				xmlSetStringValue (cur, "FillColor", fo->fill_color);
			xmlSetIntValue (cur, "Pattern", fo->pattern);
			break;
		}
1126
	case SHEET_OBJECT_ARROW:
1127
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Arrow", NULL);
1128
		break;
1129
	case SHEET_OBJECT_LINE:
1130
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Line", NULL);
1131 1132 1133 1134 1135 1136 1137 1138 1139
		break;
	}
	if (cur == NULL)
		return NULL;
	
	xmlSetGnomeCanvasPoints (cur, "Points", object->points);
	xmlSetIntValue (cur, "Width", object->width);
	xmlSetStringValue (cur, "Color", object->color);
	return cur;
1140 1141 1142 1143 1144
}

/*
 * Create a Object equivalent to the XML subtree of doc.
 */
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
static SheetObject *
readXmlObject (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	SheetObject *ret;
	SheetFilledObject *fo;
	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);
		ret = sheet_object_create_filled (ctxt->sheet, type,
			       x1, y1, x2, y2, fill_color, color, width);
		if (ret != NULL){
			fo = (SheetFilledObject *) ret;
			fo->pattern = pattern;
		}
	} else {
		ret = sheet_object_create_line (ctxt->sheet, type,
					   x1, y1, x2, y2, color, width);
	}
	sheet_object_realize (ctxt->sheet, ret);
	return ret;
1191 1192
}

1193 1194 1195
/*
 * Create an XML subtree of doc equivalent to the given Cell.
 */
1196 1197 1198 1199
static xmlNodePtr
writeXmlCell (parseXmlContextPtr ctxt, Cell *cell)
{
	xmlNodePtr cur, child;
1200 1201
	char *text;
	
1202
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Cell", NULL);
1203 1204 1205 1206 1207 1208
	xmlSetIntValue (cur, "Col", cell->col->pos);
	xmlSetIntValue (cur, "Row", cell->row->pos);
	child = writeXmlStyle (ctxt, cell->style);

	if (child)
		xmlAddChild (cur, child);
1209
	text = cell_get_content (cell);
1210 1211
	xmlNewChild(cur, ctxt->ns, "Content",
	            xmlEncodeEntities(ctxt->doc, text));
1212
	g_free (text);
1213 1214

	return cur;
1215 1216 1217 1218 1219
}

/*
 * Create a Cell equivalent to the XML subtree of doc.
 */
1220 1221 1222 1223 1224
static Cell *
readXmlCell (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	Style *style;
	Cell *ret;
1225
	xmlNodePtr childs;
1226
	int row = 0, col = 0;
1227
	char *content = NULL;
1228 1229 1230

	if (strcmp (tree->name, "Cell")){
		fprintf (stderr,
1231
		 "readXmlCell: invalid element type %s, 'Cell' expected`\n",
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
			 tree->name);
		return NULL;
	}
	xmlGetIntValue (tree, "Col", &col);
	xmlGetIntValue (tree, "Row", &row);

	ret = sheet_cell_get (ctxt->sheet, col, row);
	if (ret == NULL)
		ret = sheet_cell_new (ctxt->sheet, col, row);
	if (ret == NULL)
		return NULL;

1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
	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;
			}
		}
1254 1255
	        if (!strcmp (childs->name, "Content"))
		        content = xmlNodeGetContent(childs);
1256 1257
		childs = childs->next;
	}
1258 1259
	if (content == NULL)
		content = xmlNodeGetContent(tree);
1260 1261
	if (content != NULL) {
		char *p = content + strlen (content);
1262

1263
		while (p > content){
1264 1265 1266 1267 1268 1269
			p--;
			if (*p != ' ' && *p != '\n')
				break;
			*p = 0;
		}

1270 1271
		cell_set_text_simple (ret, content);
		free (content);
1272 1273 1274 1275
	} else
		cell_set_text_simple (ret, "");

	return ret;
1276 1277 1278 1279 1280
}

/*
 * Create an XML subtree equivalent to the given cell and add it to the parent
 */
1281 1282 1283 1284 1285
static void
writeXmlCellTo (gpointer key, gpointer value, gpointer data)
{
	parseXmlContextPtr ctxt = (parseXmlContextPtr) data;
	xmlNodePtr cur;
1286

1287 1288
	cur = writeXmlCell (ctxt, (Cell *) value);
	xmlAddChild (ctxt->parent, cur);
1289 1290 1291 1292 1293
}

/*
 * Create an XML subtree of doc equivalent to the given Sheet.
 */
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
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.
	 */
1309
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Sheet", NULL);
1310 1311
	if (cur == NULL)
		return NULL;
1312 1313
	xmlNewChild (cur, ctxt->ns, "Name", 
	             xmlEncodeEntities(ctxt->doc, sheet->name));
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 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
	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);
1375
	sheet->modified = 0;
1376
	return cur;
1377 1378 1379 1380 1381
}

/*
 * Create a Sheet equivalent to the XML subtree of doc.
 */
1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
static Sheet *
readXmlSheet (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	xmlNodePtr child;
	xmlNodePtr rows;
	xmlNodePtr cols;
	xmlNodePtr regions;
	/* xmlNodePtr styles; */
	xmlNodePtr cells;
	xmlNodePtr objects;
1392
	Sheet *ret = NULL;
1393 1394 1395 1396
	const char *val;

	if (strcmp (tree->name, "Sheet")){
		fprintf (stderr,
1397
			 "readXmlSheet: invalid element type %s, 'Sheet' expected\n",
1398 1399 1400
			 tree->name);
	}
	child = tree->childs;
1401

1402
	/*
1403 1404 1405
	 * 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).
1406 1407 1408
	 */
	val = xmlGetValue (tree, "Name");
	if (val != NULL){
1409 1410 1411 1412
		ret = workbook_sheet_lookup (ctxt->wb, (const char *) val);
		if (ret == NULL)
			ret = sheet_new (ctxt->wb, (const char *) val);
	} 
1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472

	if (ret == NULL)
		return NULL;

	ctxt->sheet = ret;

	xmlGetIntValue (tree, "MaxCol", &ret->max_col_used);
	xmlGetIntValue (tree, "MaxRow", &ret->max_row_used);
	xmlGetDoubleValue (tree, "Zoom", &ret->last_zoom_factor_used);
	child = xmlSearchChild (tree, "Styles");
	if (child != NULL){
		regions = child->childs;
		while (regions != NULL){
			readXmlStyleRegion (ctxt, regions);
			regions = regions->next;
		}
	}
	child = xmlSearchChild (tree, "Cols");
	if (child != NULL){
		ColRowInfo *info;

		cols = child->childs;
		while (cols != NULL){
			info = readXmlColRowInfo (ctxt, cols, NULL);
			if (info != NULL)
				sheet_col_add (ret, info);
			cols = cols->next;
		}
	}
	child = xmlSearchChild (tree, "Rows");
	if (child != NULL){
		ColRowInfo *info;

		rows = child->childs;
		while (rows != NULL){
			info = readXmlColRowInfo (ctxt, rows, NULL);
			if (info != NULL)
				sheet_row_add (ret, info);
			rows = rows->next;
		}
	}
	child = xmlSearchChild (tree, "Objects");
	if (child != NULL){
		objects = child->childs;
		while (objects != NULL){
			readXmlObject (ctxt, objects);
			objects = objects->next;
		}
	}
	child = xmlSearchChild (tree, "Cells");
	if (child != NULL){
		cells = child->childs;
		while (cells != NULL){
			readXmlCell (ctxt, cells);
			cells = cells->next;
		}
	}
	/* Initialize the ColRowInfo's ->pixels data */
	sheet_set_zoom_factor (ret, ret->last_zoom_factor_used);
	return ret;
1473 1474 1475
}

/*
1476 1477
 * Create an XML subtree equivalent to the given sheet
 * and add it to the parent
1478
 */
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
static void
writeXmlSheetTo (gpointer key, gpointer value, gpointer data)
{
	parseXmlContextPtr ctxt = (parseXmlContextPtr) data;
	xmlNodePtr cur, parent;
	Sheet *sheet = (Sheet *) value;

	parent = ctxt->parent;
	cur = writeXmlSheet (ctxt, sheet);
	ctxt->parent = parent;
	xmlAddChild (parent, cur);
1490 1491 1492 1493 1494
}

/*
 * Create an XML subtree of doc equivalent to the given Workbook.
 */
1495 1496 1497 1498 1499 1500 1501 1502 1503
static xmlNodePtr
writeXmlWorkbook (parseXmlContextPtr ctxt, Workbook *wb)
{
	xmlNodePtr cur;
	xmlNodePtr child;

	/*
	 * General informations about the Sheet.
	 */
1504
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Workbook", NULL);	/* the Workbook name !!! */
1505 1506 1507 1508 1509 1510 1511
	if (cur == NULL)
		return NULL;

	child = writeXmlStyle (ctxt, &wb->style);
	if (child)
		xmlAddChild (cur, child);

1512
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "Geometry", NULL);
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
	xmlSetIntValue (child, "Width", wb->toplevel->allocation.width);
	xmlSetIntValue (child, "Height", wb->toplevel->allocation.height);
	xmlAddChild (cur, child);

	/*
	 * Cells informations
	 */
	child = xmlNewChild (cur, ctxt->ns, "Sheets", NULL);
	ctxt->parent = child;
	g_hash_table_foreach (wb->sheets, writeXmlSheetTo, ctxt);
	
	return cur;
1525 1526
}

1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
static void
createXmlSheet (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	const char *val;
	xmlNodePtr child;
	
	if (strcmp (tree->name, "Sheet")){
		fprintf (stderr,
			 "createXmlSheet: invalid element type %s, 'Sheet' expected\n",
			 tree->name);
		return;
	}
	child = tree->childs;
	val = xmlGetValue (tree, "Name");
	if (val != NULL){
		Sheet *sheet;
		
		sheet = sheet_new (ctxt->wb, (char *) val);
		workbook_attach_sheet (ctxt->wb, sheet);
	}
	return;
}

1550 1551 1552
/*
 * Create a Workbook equivalent to the XML subtree of doc.
 */
1553 1554 1555 1556 1557
static Workbook *
readXmlWorkbook (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	Workbook *ret;
	Sheet *sheet;
1558
	xmlNodePtr child, c;
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583

	if (strcmp (tree->name, "Workbook")){
		fprintf (stderr,
			 "readXmlWorkbook: invalid element type %s, 'Workbook' expected`\n",
			 tree->name);
		return NULL;
	}
	ret = workbook_new ();
	ctxt->wb = ret;

	child = xmlSearchChild (tree, "Geometry");
	if (child){
		int width, height;

		xmlGetIntValue (child, "Width", &width);
		xmlGetIntValue (child, "Height", &height);
/*      gtk_widget_set_usize(ret->toplevel, width, height); */
	}
	child = xmlSearchChild (tree, "Style");
	if (child != NULL)
		readXmlStyle (ctxt, child, &ret->style);

	child = xmlSearchChild (tree, "Sheets");
	if (child == NULL)
		return ret;
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602

	/*
	 * Pass 1: Create all the sheets, to make sure
	 * all of the references to forward sheets are properly
	 * handled
	 */
	c = child->childs;
	while (c != NULL){
		createXmlSheet (ctxt, c);
		c = c->next;
	}
	
	/*
	 * Pass 2: read the contents
	 */
	c = child->childs;
	while (c != NULL){
		sheet = readXmlSheet (ctxt, c);
		c = c->next;
1603 1604 1605
	}
	
	return ret;
1606
}
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641

/*
 * We parse and do some limited validation of the XML file, if this
 * passes, then we return TRUE
 */
static gboolean
xml_probe (const char *filename)
{
	xmlDocPtr res;
	xmlNsPtr gmr;
	
	res = xmlParseFile (filename);
	if (res == NULL)
		return FALSE;

	if (res->root == NULL)
		return FALSE;

	gmr = xmlSearchNsByHref (res, res->root, "http://www.gnome.org/gnumeric/");
	if (strcmp (res->root->name, "Workbook") || (gmr == NULL)){
		xmlFreeDoc (res);
		return FALSE;
	}
	xmlFreeDoc (res);
	return TRUE;
}

void
xml_init (void)
{
	char *desc = _("Gnumeric XML file format");
	
	file_format_register_open (50, desc, xml_probe, gnumericReadXmlWorkbook);
	file_format_register_save (".gnumeric", desc, gnumericWriteXmlWorkbook);
}