xml-io.c 15.4 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 "tree.h"
#include "parser.h"
15 16 17 18 19 20 21

#include "xml-io.h"

/*
 * A parsing context.
 */
typedef struct parseXmlContext {
Arturo Espinosa's avatar
Arturo Espinosa committed
22
    xmlDocPtr  doc;        /* Xml document */
23
    xmlNsPtr  ns;         /* Main name space */
24 25
    xmlNodePtr parent;     /* used only for g_hash_table_foreach callbacks */
    GHashTable *nameTable; /* to reproduce multiple refs with HREFs */
Arturo Espinosa's avatar
Arturo Espinosa committed
26
    int        fontIdx;    /* for Font refs names ... */
27 28
} parseXmlContext, *parseXmlContextPtr;

Arturo Espinosa's avatar
Arturo Espinosa committed
29 30 31 32 33 34 35
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);
static guint       ptrHash          (gconstpointer a);
static gint        ptrCompare       (gconstpointer a, gconstpointer b);
static void        nameFree         (gpointer key, gpointer value, gpointer user_data);
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52


/**
 **
 ** Public functions : high level read and write.
 **
 **/ 

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

Sheet *gnumericReadXmlSheet(const char *filename) {
    Sheet *sheet;
    xmlDocPtr res;
53
    xmlNsPtr gmr;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    parseXmlContext ctxt;

    /*
     * 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 chech the top elem.
     */
70
    gmr = xmlSearchNs(res, res->root, "http://www.gnome.org/gnumeric/");
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
    if (strcmp(res->root->name, "Sheet") || (gmr == NULL)) {
        fprintf(stderr, "gnumericReadXmlSheet %s: not an Sheet file\n",
	        filename);
	xmlFreeDoc(res);
        return(NULL);
    }

    ctxt.doc = res;
    ctxt.ns = gmr;
    ctxt.nameTable = g_hash_table_new(ptrHash, ptrCompare);
    ctxt.fontIdx = 1;
    sheet = readXmlSheet(&ctxt, res->root);
    g_hash_table_foreach(ctxt.nameTable, nameFree, NULL);
    g_hash_table_destroy(ctxt.nameTable);

    xmlFreeDoc(res);
    return(sheet);
}

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

int gnumericWriteXmlSheet(Sheet *sheet, const char *filename) {
    FILE *output;
    xmlDocPtr xml;
99
    xmlNsPtr gmr;
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    parseXmlContext ctxt;

    /*
     * Open in write mode, !!! Save a bak ?
     */
    output = fopen(filename, "w");
    if (output == NULL) {
	perror("fopen");
        fprintf(stderr, "gnumericWriteXmlSheet: couldn't save to file %s\n",
	        filename);
	return(-1);
    }
    
    /*
     * Create the tree
     */
    xml = xmlNewDoc("1.0");
    if (xml == NULL) {
	fclose(output);
	return(-1);
    }
121
    gmr = xmlNewGlobalNs(xml, "http://www.gnome.org/gnumeric/", "gmr");
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    ctxt.doc = xml;
    ctxt.ns = gmr;
    ctxt.nameTable = g_hash_table_new(ptrHash, ptrCompare);
    ctxt.fontIdx = 1;
    xml->root = writeXmlSheet(&ctxt, sheet);
    g_hash_table_foreach(ctxt.nameTable, nameFree, NULL);
    g_hash_table_destroy(ctxt.nameTable);

    /*
     * Dump it.
     */
    xmlDocDump(output, xml); /* !!! Should add a return code and check */
    xmlFreeDoc(xml);
    fclose(output);
    return(0);
}

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

Workbook *gnumericReadXmlWorkbook(const char *filename) {
    Workbook *sheet;
    xmlDocPtr res;
148
    xmlNsPtr gmr;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    parseXmlContext ctxt;

    /*
     * 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.
     */
165
    gmr = xmlSearchNs(res, res->root, "http://www.gnome.org/gnumeric/");
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    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(ptrHash, ptrCompare);
    ctxt.fontIdx = 1;
    sheet = readXmlWorkbook(&ctxt, res->root);
    g_hash_table_foreach(ctxt.nameTable, nameFree, NULL);
    g_hash_table_destroy(ctxt.nameTable);

    xmlFreeDoc(res);
    return(sheet);
}

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

int gnumericWriteXmlWorkbook(Workbook *sheet, const char *filename) {
    FILE *output;
    xmlDocPtr xml;
194
    xmlNsPtr gmr;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    parseXmlContext ctxt;

    /*
     * Open in write mode, !!! Save a bak ?
     */
    output = fopen(filename, "w");
    if (output == NULL) {
	perror("fopen");
        fprintf(stderr, "gnumericWriteXmlWorkbook: couldn't save to file %s\n",
	        filename);
	return(-1);
    }
    
    /*
     * Create the tree
     */
    xml = xmlNewDoc("1.0");
    if (xml == NULL) {
	fclose(output);
	return(-1);
    }
216
    gmr = xmlNewGlobalNs(xml, "http://www.gnome.org/gnumeric/", "gmr");
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
    ctxt.doc = xml;
    ctxt.ns = gmr;
    ctxt.nameTable = g_hash_table_new(ptrHash, ptrCompare);
    ctxt.fontIdx = 1;
    xml->root = writeXmlWorkbook(&ctxt, sheet);
    g_hash_table_foreach(ctxt.nameTable, nameFree, NULL);
    g_hash_table_destroy(ctxt.nameTable);

    /*
     * Dump it.
     */
    xmlDocDump(output, xml); /* !!! Should add a return code and check */
    xmlFreeDoc(xml);
    fclose(output);
    return(0);
}

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
 **/ 

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

/*
 * Hash functions for pointers.
 */
static guint ptrHash(gconstpointer a)
{
    guint res = (guint) a;
    res >>= 2;
    res &= 0xFFFF;
    return(res);
}

/*
 * Comparison functions for pointers.
 */
static gint ptrCompare(gconstpointer a, gconstpointer b)
{
    char *ca, *cb;

    ca = (char *) a;
    cb = (char *) b;

    if (ca != cb)
	    return 0;
    
    return 1;
}

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
static char *BorderTypes[8] = { "none", "solid", "unknown", "unknown",
				"unknown", "unknown", "unknown", "unknown"};
static xmlNodePtr writeXmlStyleBorder(parseXmlContextPtr ctxt,
                                      StyleBorder *border) {
    xmlNodePtr cur;
    xmlNodePtr side;
    char str[50];
    
    if ((border->left == BORDER_NONE) && (border->right == BORDER_NONE) &&
        (border->left == BORDER_NONE) && (border->right == BORDER_NONE))
	return(NULL);
    cur = xmlNewNode(ctxt->ns, "StyleBorder", NULL);
    if (border->left != BORDER_NONE) {
        side = xmlNewChild(cur, ctxt->ns, "Left", BorderTypes[border->left]);
	sprintf(str, "%X:%X:%X", border->left_color.red, 
	        border->left_color.green, border->left_color.blue);
	xmlNewProp(side, "Color", str);
    }
    if (border->right != BORDER_NONE) {
        side = xmlNewChild(cur, ctxt->ns, "Right", BorderTypes[border->right]);
	sprintf(str, "%X:%X:%X", border->right_color.red, 
	        border->right_color.green, border->right_color.blue);
	xmlNewProp(side, "Color", str);
    }
    if (border->top != BORDER_NONE) {
        side = xmlNewChild(cur, ctxt->ns, "Top", BorderTypes[border->top]);
	sprintf(str, "%X:%X:%X", border->top_color.red, 
	        border->top_color.green, border->top_color.blue);
	xmlNewProp(side, "Color", str);
    }
    if (border->bottom != BORDER_NONE) {
        side = xmlNewChild(cur, ctxt->ns, "Bottom", BorderTypes[border->bottom]);
	sprintf(str, "%X:%X:%X", border->bottom_color.red, 
	        border->bottom_color.green, border->bottom_color.blue);
	xmlNewProp(side, "Color", str);
    }
    return(cur);
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
static StyleBorder *readXmlStyleBorder(parseXmlContextPtr ctxt,
                                       xmlNodePtr tree) {
    return(NULL);
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
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) &&
        (style->shading == NULL)) return(NULL);

    cur = xmlNewNode(ctxt->ns, "Style", NULL);
    sprintf(str, "%d", style->halign);
    xmlNewProp(cur, "HAlign", str);
    sprintf(str, "%d", style->valign);
    xmlNewProp(cur, "VAlign", str);
    sprintf(str, "%d", style->orientation);
    xmlNewProp(cur, "Orient", str);

    if (style->format != NULL) {
	xmlNewProp(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 {
	    child = xmlNewChild(cur, ctxt->ns, "Font", style->font->font_name);
	    sprintf(str, "%d", style->font->units);
	    xmlNewProp(child, "Unit", str);
	    sprintf(str, "FontDef%d", ctxt->fontIdx++);
	    xmlNewProp(child, "NAME", str);
	    g_hash_table_insert(ctxt->nameTable, style->font, g_strdup(str));
	}
	             
    }
    if (style->border != NULL) {
        child = writeXmlStyleBorder(ctxt, style->border);
        if (child) xmlAddChild(cur, child);
    }
    if (style->shading != NULL) {
	sprintf(str, "%d", style->shading->pattern);
        xmlNewChild(cur, ctxt->ns, "Shade", str);
    }
    return(cur);
}

/*
 * Create a Style equivalent to the XML subtree of doc.
 */
static Style *readXmlStyle(parseXmlContextPtr ctxt, xmlNodePtr tree) {
    return(NULL);
}

/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
static xmlNodePtr writeXmlColRowInfo(parseXmlContextPtr ctxt,
                                     ColRowInfo *info) {
    xmlNodePtr cur, child;
    char str[50];
    
    cur = xmlNewNode(ctxt->ns, "ColRowInfo", NULL);
    sprintf(str, "%d", info->pos);
    xmlNewProp(cur, "No", str);
    sprintf(str, "%d", info->units);
    xmlNewProp(cur, "Unit", str);
    sprintf(str, "%d", info->margin_a);
    xmlNewProp(cur, "MarginA", str);
    sprintf(str, "%d", info->margin_b);
    xmlNewProp(cur, "MarginB", str);
    if (info->style) {
	child = writeXmlStyle(ctxt, info->style);
	if (child) xmlAddChild(cur, child);
    }

    return(cur);
}

/*
 * Create a ColRowInfo equivalent to the XML subtree of doc.
 */
static ColRowInfo *readXmlColRowInfo(parseXmlContextPtr ctxt,
                                     xmlNodePtr tree) {
    return(NULL);
}

/*
 * Create an XML subtree of doc equivalent to the given Cell.
 */
static xmlNodePtr writeXmlCell(parseXmlContextPtr ctxt, Cell *cell) {
    xmlNodePtr cur;
    char str[50];
    
Arturo Espinosa's avatar
Arturo Espinosa committed
423
    cur = xmlNewNode(ctxt->ns, "Cell", cell->entered_text->str);
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    sprintf(str, "%d", cell->col->pos);
    xmlNewProp(cur, "Col", str);
    sprintf(str, "%d", cell->row->pos);
    xmlNewProp(cur, "Row", str);
    return(cur);
}

/*
 * Create a Cell equivalent to the XML subtree of doc.
 */
static Cell *readXmlCell(parseXmlContextPtr ctxt, xmlNodePtr tree) {
    return(NULL);
}

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

    cur = writeXmlCell(ctxt, (Cell *) value);
    xmlAddChild(ctxt->parent, cur);
}

/*
 * Create an XML subtree of doc equivalent to the given Sheet.
 */
static xmlNodePtr writeXmlSheet(parseXmlContextPtr ctxt, Sheet *sheet) {
    xmlNodePtr cur;
    xmlNodePtr child;
    xmlNodePtr rows;
    xmlNodePtr cols;
    xmlNodePtr cells;
    GList *l;
    char str[50];

    /*
     * General informations about the Sheet.
     */
    cur = xmlNewNode(ctxt->ns, "Sheet", NULL);
    if (cur == NULL) return(NULL);
    xmlNewChild(cur, ctxt->ns, "Name", sheet->name);
    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);
    child = writeXmlStyle(ctxt, &sheet->style);
    if (child) xmlAddChild(cur, child);

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

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

    /*
     * Cells informations
     */
    cells = xmlNewChild(cur, ctxt->ns, "Cells", NULL);
    ctxt->parent = cells;
    g_hash_table_foreach(sheet->cell_hash, writeXmlCellTo, ctxt);
    return(cur);
}

/*
 * Create a Sheet equivalent to the XML subtree of doc.
 */
static Sheet *readXmlSheet(parseXmlContextPtr ctxt, xmlNodePtr tree) {
    return(NULL);
}

/*
 * Create an XML subtree equivalent to the given cell and add it to the parent
 */
static void writeXmlSheetTo(gpointer key, gpointer value, gpointer data) {
    parseXmlContextPtr ctxt = (parseXmlContextPtr) data;
    xmlNodePtr cur;
    Sheet *sheet = (Sheet *) value;
    char *filename;
    char *p;

    cur = xmlNewNode(ctxt->ns, "Sheet", sheet->name);
    xmlAddChild(ctxt->parent, cur);

    filename = g_strdup(sheet->name);
    if (filename == NULL) {
        fprintf(stderr, "Out of memory, couln't save %s\n", sheet->name);
	return;
    }
    for (p = filename;*p;p++)
        if ((*p == ' ') || (*p == '\t') || (*p == '\n') || (*p == '\r'))
	    *p = '_';
    gnumericWriteXmlSheet(sheet, filename);
    g_free(filename);
}

/*
 * Create an XML subtree of doc equivalent to the given Workbook.
 */
static xmlNodePtr writeXmlWorkbook(parseXmlContextPtr ctxt, Workbook *wb) {
    xmlNodePtr cur;
    xmlNodePtr child;

    /*
     * General informations about the Sheet.
     */
    cur = xmlNewNode(ctxt->ns, "Workbook", NULL); /* the Workbook name !!! */
    if (cur == NULL) return(NULL);
    child = writeXmlStyle(ctxt, &wb->style);
    if (child) 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);
}

/*
 * Create a Workbook equivalent to the XML subtree of doc.
 */
static Workbook *readXmlWorkbook(parseXmlContextPtr ctxt, xmlNodePtr tree) {
    return(NULL);
}