| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 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
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 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
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 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
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 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
 423
 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
 573
 574
 575
 576
 577
 578
 579
 580
 581
 
 | /* -*- mode: C; indent-tabs-mode: t; tab-width: 8; c-basic-offset: 2; -*- */
/*
 * This file is part of Seed, the GObject Introspection<->Javascript bindings.
 *
 * Seed is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2 of
 * the License, or (at your option) any later version.
 * Seed is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License
 * along with Seed.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright (C) Robert Carr 2009 <carrr@rpi.edu>
 */
#include <seed.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h> // for xmlXPathRegisterNs
SeedObject namespace_ref;
SeedEngine *eng;
SeedClass xml_doc_class;
SeedClass xml_node_class;
SeedClass xml_attr_class;
SeedClass xml_xpath_class;
SeedClass xml_xpathobj_class;
#define XML_DOC_PRIV(obj) ((xmlDocPtr)seed_object_get_private(obj))
#define XML_NODE_PRIV(obj) ((xmlNodePtr)seed_object_get_private(obj))
#define XML_XPATH_PRIV(obj) ((xmlXPathContextPtr)seed_object_get_private (obj))
#define XML_XPATHOBJ_PRIV(obj) ((xmlXPathObjectPtr)seed_object_get_private (obj))
static SeedObject
seed_make_xml_doc (SeedContext ctx,
		    xmlDocPtr doc)
{
  SeedObject ret;
  if (doc->_private)
    return (SeedObject) doc->_private;
  ret = seed_make_object (ctx, xml_doc_class, doc);
  doc->_private = ret;
  return ret;
}
static SeedObject
seed_make_xml_node (SeedContext ctx,
		xmlNodePtr node)
{
  SeedObject ret;
  if (node == NULL)
    return seed_make_null (ctx);
  if (node->_private)
    return (SeedObject) node->_private;
  ret = seed_make_object (ctx, xml_node_class, node);
  node->_private = ret;
  return ret;
}
static SeedObject
seed_make_xml_attr (SeedContext ctx,
		    xmlAttrPtr attr)
{
 SeedObject ret;
  if (attr == NULL)
    return seed_make_null (ctx);
  if (attr->_private)
    return (SeedObject) attr->_private;
  ret = seed_make_object (ctx, xml_attr_class, attr);
  attr->_private = ret;
  return ret;
}
static gchar *
seed_xml_element_type_to_string (xmlElementType type)
{
  if (type == XML_ELEMENT_NODE)
    return "element";
  else if (type == XML_ATTRIBUTE_NODE)
    return "attribute";
  else if (type == XML_TEXT_NODE)
    return "text";
  else
    return "Implement more types! racarr is lazy.";
}
static SeedValue
seed_xml_parse_file (SeedContext ctx,
		     SeedObject function,
		     SeedObject this_object,
		     gsize argument_count,
		     const SeedValue arguments[],
		     SeedException * exception)
{
  SeedObject ret;
  xmlDocPtr doc;
  gchar *path;
  if (argument_count != 1)
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "parseFile expected 1 argument, got %zd",
			   argument_count);
      return seed_make_null (ctx);
    }
  path = seed_value_to_string (ctx, arguments[0], exception);
  doc = xmlParseFile (path);
  if (!doc)
    {
      seed_make_exception (ctx, exception, "XMLError",
			   "Document not parsed successfully");
      g_free (path);
      return seed_make_null (ctx);
    }
  ret = seed_make_xml_doc (ctx, doc);
  g_free (path);
  return ret;
}
static SeedValue
seed_xml_parse_string (SeedContext ctx,
		       SeedObject function,
		       SeedObject this_object,
		       gsize argument_count,
		       const SeedValue arguments[],
		       SeedException * exception)
{
  SeedObject ret;
  xmlDocPtr doc;
  gchar *string;
  if (argument_count != 1)
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "parseString expected 1 argument, got %zd",
			   argument_count);
      return seed_make_null (ctx);
    }
  string = seed_value_to_string (ctx, arguments[0], exception);
  doc = xmlParseMemory (string, strlen (string));
  if (!doc)
    {
      seed_make_exception (ctx, exception, "XMLError",
			   "Document not parsed successfully");
      g_free (string);
      return seed_make_null (ctx);
    }
  ret = seed_make_xml_doc (ctx, doc);
  g_free (string);
  return ret;
}
static SeedValue
seed_xml_doc_get_root (SeedContext ctx,
		       SeedObject object,
		       SeedString property_name,
		       SeedException *exception)
{
  xmlDocPtr doc = XML_DOC_PRIV (object);
  return seed_make_xml_node (ctx, xmlDocGetRootElement (doc));
}
static void
seed_xml_doc_finalize (SeedObject object)
{
  xmlDocPtr ptr = XML_DOC_PRIV (object);
  xmlFreeDoc (ptr);
}
static SeedValue
seed_xml_node_get_name (SeedContext ctx,
			SeedObject object,
			SeedString property_name,
			SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_value_from_string (ctx, (gchar *)node->name, exception);
}
static SeedValue
seed_xml_node_get_children (SeedContext ctx,
			   SeedObject object,
			   SeedString property_name,
			   SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_make_xml_node (ctx, node->children);
}
static SeedValue
seed_xml_node_get_parent (SeedContext ctx,
			  SeedObject object,
			  SeedString property_name,
			  SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_make_xml_node (ctx, node->parent);
}
static SeedValue
seed_xml_node_get_next (SeedContext ctx,
			SeedObject object,
			SeedString property_name,
			SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_make_xml_node (ctx, node->next);
}
static SeedValue
seed_xml_node_get_prev (SeedContext ctx,
			SeedObject object,
			SeedString property_name,
			SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_make_xml_node (ctx, node->prev);
}
static SeedValue
seed_xml_node_get_last (SeedContext ctx,
			SeedObject object,
			SeedString property_name,
			SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_make_xml_node (ctx, node->last);
}
static SeedValue
seed_xml_node_get_doc (SeedContext ctx,
		       SeedObject object,
		       SeedString property_name,
		       SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_make_xml_doc (ctx, node->doc);
}
static SeedValue
seed_xml_node_get_content (SeedContext ctx,
			SeedObject object,
			SeedString property_name,
			SeedException *exception)
{
  SeedValue ret;
  gchar *content;
  xmlNodePtr node = XML_NODE_PRIV (object);
  content = (gchar *)xmlNodeGetContent (node);
  ret = seed_value_from_string (ctx, content, exception);
  g_free (content);
  return ret;
}
static SeedValue
seed_xml_node_get_type (SeedContext ctx,
			SeedObject object,
			SeedString property_name,
			SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_value_from_string (ctx,
				 seed_xml_element_type_to_string
				 (node->type), exception);
}
static SeedValue
seed_xml_node_get_properties (SeedContext ctx,
			      SeedObject object,
			      SeedString property_name,
			      SeedException *exception)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  return seed_make_xml_attr (ctx, node->properties);
}
static void
seed_xml_node_init (SeedContext ctx,
		    SeedObject object)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  if (node && node->doc->_private)
    seed_value_protect (ctx, node->doc->_private);
}
static void
seed_xml_node_finalize (SeedObject object)
{
  xmlNodePtr node = XML_NODE_PRIV (object);
  if (!node)
    return;
  node->_private = NULL;
  // This might be invalid.
  if (node->doc->_private)
    seed_value_unprotect (eng->context, node->doc->_private);
}
static SeedValue
seed_xml_xpath_eval (SeedContext ctx,
		     SeedObject function,
		     SeedObject this_object,
		     gsize argument_count,
		     const SeedValue arguments[],
		     SeedException * exception)
{
  xmlXPathObjectPtr xpath_obj;
  xmlXPathContextPtr xpath_ctx;
  guchar *xpath;
  if (argument_count != 1)
    {
      seed_make_exception (ctx, exception,
			   "ArgumentError",
			   "xpathEval expected 1 argument, got %zd",
			   argument_count);
      return seed_make_null (ctx);
    }
  xpath_ctx = XML_XPATH_PRIV (this_object);
  xpath = (guchar *)seed_value_to_string (ctx, arguments[0], exception);
  xpath_obj = xmlXPathEval (xpath, xpath_ctx);
  g_free (xpath);
  return seed_make_object (ctx, xml_xpathobj_class, xpath_obj);
}
static SeedValue
seed_xml_xpath_register_ns (SeedContext ctx,
			    SeedObject function,
			    SeedObject this_object,
			    gsize argument_count,
			    const SeedValue arguments[],
			    SeedException * exception)
{
  xmlXPathContextPtr xpath;
  guchar *prefix;
  guchar *ns_uri;
  if (argument_count != 2)
    {
      seed_make_exception (ctx, exception, "ArgumentError",
			   "xpathRegisterNs expects 2 arguments, got %zd",
			   argument_count);
      return seed_make_undefined (ctx);
    }
  xpath = XML_XPATH_PRIV (this_object);
  prefix = (guchar *)seed_value_to_string (ctx, arguments[0], exception);
  ns_uri = (guchar *)seed_value_to_string (ctx, arguments[1], exception);
  xmlXPathRegisterNs (xpath, prefix, ns_uri);
  g_free (prefix);
  g_free (ns_uri);
  return seed_make_undefined (ctx);
}
static SeedValue
seed_xml_construct_xpath_context (SeedContext ctx,
				  SeedObject function,
				  SeedObject this_object,
				  gsize argument_count,
				  const SeedValue arguments[],
				  SeedException * exception)
{
  xmlXPathContextPtr xpath;
  xmlDocPtr doc;
  doc = XML_DOC_PRIV (this_object);
  xpath = xmlXPathNewContext (doc);
  seed_value_protect (ctx, this_object);
  return seed_make_object (ctx, xml_xpath_class, xpath);
}
static void
seed_xml_xpath_finalize (SeedObject object)
{
  xmlXPathContextPtr xpath = XML_XPATH_PRIV (object);
  // Maybe unsafe. Seems to work.
  seed_value_unprotect (eng->context, xpath->doc->_private);
  xmlXPathFreeContext (xpath);
}
static void
seed_xml_xpathobj_finalize (SeedObject object)
{
  xmlXPathObjectPtr xpath = XML_XPATHOBJ_PRIV (object);
  xmlXPathFreeObject (xpath);
}
static SeedValue
seed_xml_array_from_nodeset (SeedContext ctx,
			     xmlNodeSetPtr nodeset,
			     SeedException *exception)
{
  SeedValue *ary = g_alloca (nodeset->nodeNr * sizeof (SeedValue));
  int i;
  for (i = 0; i < nodeset->nodeNr; i++)
    {
      ary[i] = seed_make_xml_node (ctx, nodeset->nodeTab[i]);
    }
  return seed_make_array (ctx, ary, nodeset->nodeNr, exception);
}
static SeedValue
seed_xml_xpathobj_get_value (SeedContext ctx,
			     SeedObject object,
			     SeedString property_name,
			     SeedException *exception)
{
  xmlXPathObjectPtr xpath = XML_XPATHOBJ_PRIV (object);
  switch (xpath->type)
    {
/*    case XPATH_BOOLEAN:
      return seed_value_from_boolean (ctx, xpath->boolval, exception);
    case XPATH_NUMBER:
      return seed_value_from_double (ctx, xpath->floatval, exception);
    case XPATH_STRING:
    return seed_value_from_string (ctx, xpath->stringval, exception);*/
    case XPATH_NODESET:
      return seed_xml_array_from_nodeset (ctx, xpath->nodesetval, exception);
    default:
      return seed_make_null (ctx);
    }
}
seed_static_function doc_funcs[] = {
  {"xpathNewContext", seed_xml_construct_xpath_context, 0},
  {NULL, NULL, 0}
};
seed_static_value doc_values[] = {
  {"root", seed_xml_doc_get_root, NULL, 0},
  {"name", seed_xml_node_get_name, NULL, 0},
  {"children", seed_xml_node_get_children, NULL, 0},
  {"parent", seed_xml_node_get_parent, NULL, 0},
  {"next", seed_xml_node_get_next, NULL, 0},
  {"prev", seed_xml_node_get_prev, NULL, 0},
  {"last", seed_xml_node_get_last, NULL, 0},
  {"doc", seed_xml_node_get_doc, NULL, 0},
  {"type", seed_xml_node_get_type, NULL, 0},
  {NULL, NULL, NULL, 0}
};
seed_static_function node_funcs[] = {
  {NULL, NULL, 0}
};
seed_static_value node_values[] = {
  {"name", seed_xml_node_get_name, NULL, 0},
  {"children", seed_xml_node_get_children, NULL, 0},
  {"parent", seed_xml_node_get_parent, NULL, 0},
  {"next", seed_xml_node_get_next, NULL, 0},
  {"prev", seed_xml_node_get_prev, NULL, 0},
  {"content", seed_xml_node_get_content, NULL, 0},
  {"last", seed_xml_node_get_last, NULL, 0},
  {"doc", seed_xml_node_get_doc, NULL, 0},
  {"type", seed_xml_node_get_type, NULL, 0},
  {"properties", seed_xml_node_get_properties, NULL, 0},
  {NULL, NULL, NULL, 0}
};
seed_static_function attr_funcs[] = {
  {NULL, NULL, 0}
};
seed_static_value attr_values[] = {
  {"name", seed_xml_node_get_name, NULL, 0},
  {"children", seed_xml_node_get_children, NULL, 0},
  {"parent", seed_xml_node_get_parent, NULL, 0},
  {"next", seed_xml_node_get_next, NULL, 0},
  {"prev", seed_xml_node_get_prev, NULL, 0},
  {"last", seed_xml_node_get_last, NULL, 0},
  {"doc", seed_xml_node_get_doc, NULL, 0},
  {"type", seed_xml_node_get_type, NULL, 0},
  {NULL, NULL, NULL, 0}
};
seed_static_function xpath_funcs[] = {
  {"xpathEval", seed_xml_xpath_eval, 0},
  {"xpathRegisterNs", seed_xml_xpath_register_ns, 0},
  {NULL, NULL, 0}
};
seed_static_value xpathobj_values[] = {
  {"value", seed_xml_xpathobj_get_value, NULL, 0},
  {NULL, NULL, NULL, 0}
};
static void
seed_libxml_define_stuff ()
{
  SeedObject node_proto;
  seed_class_definition xml_doc_class_def = seed_empty_class;
  seed_class_definition xml_node_class_def = seed_empty_class;
  seed_class_definition xml_attr_class_def = seed_empty_class;
  seed_class_definition xml_xpath_class_def = seed_empty_class;
  seed_class_definition xml_xpathobj_class_def = seed_empty_class;
  xml_doc_class_def.class_name="XMLDocument";
  xml_doc_class_def.static_functions = doc_funcs;
  xml_doc_class_def.static_values = doc_values;
  xml_doc_class_def.finalize = seed_xml_doc_finalize;
  xml_doc_class = seed_create_class (&xml_doc_class_def);
  xml_node_class_def.class_name="XMLNode";
  xml_node_class_def.static_functions = node_funcs;
  xml_node_class_def.static_values = node_values;
  xml_node_class_def.finalize = seed_xml_node_finalize;
  xml_node_class_def.initialize = seed_xml_node_init;
  xml_node_class = seed_create_class (&xml_node_class_def);
  xml_attr_class_def.class_name="XMLAttribute";
  xml_attr_class_def.static_functions = attr_funcs;
  xml_attr_class_def.static_values = attr_values;
  xml_attr_class_def.finalize = seed_xml_node_finalize;
  xml_attr_class_def.initialize = seed_xml_node_init;
  xml_attr_class = seed_create_class (&xml_attr_class_def);
  xml_xpath_class_def.class_name = "XMLXPathContext";
  xml_xpath_class_def.finalize = seed_xml_xpath_finalize;
  xml_xpath_class_def.static_functions = xpath_funcs;
  xml_xpath_class = seed_create_class (&xml_xpath_class_def);
  xml_xpathobj_class_def.class_name = "XMLXPathObj";
  xml_xpathobj_class_def.finalize = seed_xml_xpathobj_finalize;
  xml_xpathobj_class_def.static_values = xpathobj_values;
  xml_xpathobj_class = seed_create_class (&xml_xpathobj_class_def);
  seed_create_function (eng->context, "parseFile",
			(SeedFunctionCallback) seed_xml_parse_file,
			namespace_ref);
  seed_create_function (eng->context, "parseString",
			(SeedFunctionCallback) seed_xml_parse_string,
			namespace_ref);
  node_proto = seed_object_get_prototype (eng->context,
					  seed_make_object (eng->context,
							    xml_node_class,
							    NULL));
  seed_make_object (eng->context, xml_node_class, NULL);
  seed_object_set_property (eng->context, namespace_ref, "_nodeProto", node_proto);
  seed_simple_evaluate (eng->context, "imports.extensions.xml", NULL);
}
SeedObject
seed_module_init(SeedEngine *local_eng)
{
  eng = local_eng;
  namespace_ref = seed_make_object (eng->context, NULL, NULL);
  seed_value_protect (eng->context, namespace_ref);
  seed_libxml_define_stuff();
  return namespace_ref;
}
 |