1 2 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 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 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 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
|
/* $Id: ruby_xml_parser_context.c 189 2007-09-26 15:04:47Z danj $ */
/* Please see the LICENSE file for copyright and distribution information */
#include "libxml.h"
#include "ruby_xml_parser_context.h"
/* TODO:
*
* *) xmlParserInput class/structure
* *) errNo and mappings
* *) validity context
* *) record_info or stats class/structure
* *) xmlParserNodeInfoSeq
* *) xmlParserInputState
*/
VALUE cXMLParserContext;
/*
* call-seq:
* context.data_directory => "dir"
*
* Obtain the data directory associated with this context.
*/
VALUE
ruby_xml_parser_context_data_directory_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->directory == NULL)
return(Qnil);
else
return(rb_str_new2(rxpc->ctxt->directory));
}
/*
* call-seq:
* context.depth => num
*
* Obtain the depth of this context.
*/
VALUE
ruby_xml_parser_context_depth_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->depth));
}
/*
* call-seq:
* context.disable_sax? => (true|false)
*
* Determine whether SAX-based processing is disabled
* in this context.
*/
VALUE
ruby_xml_parser_context_disable_sax_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->disableSAX)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.doc => document
*
* Obtain the +XML::Document+ associated with this context.
*/
VALUE
ruby_xml_parser_context_doc_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->myDoc == NULL)
return(Qnil);
return(ruby_xml_document_wrap(cXMLDocument, rxpc->ctxt->myDoc));
}
/*
* call-seq:
* context.docbook? => (true|false)
*
* Determine whether this is a docbook context.
*/
VALUE
ruby_xml_parser_context_docbook_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->html == 2) // TODO check this
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.encoding => "encoding"
*
* Obtain the character encoding identifier used in
* this context.
*/
VALUE
ruby_xml_parser_context_encoding_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->encoding == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxpc->ctxt->encoding));
}
/*
* call-seq:
* context.errno => num
*
* Obtain the last-error number in this context.
*/
VALUE
ruby_xml_parser_context_errno_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->errNo));
}
void
ruby_xml_parser_context_free(ruby_xml_parser_context *rxpc) {
if (rxpc->ctxt != NULL) {
xmlFreeParserCtxt(rxpc->ctxt);
rxpc->ctxt = NULL;
}
free(rxpc);
}
void
ruby_xml_parser_context_mark(void *v) {
if ( v == NULL ) return;
ruby_xml_state_marker();
}
/*
* call-seq:
* context.html? => (true|false)
*
* Determine whether this is an html context.
*/
VALUE
ruby_xml_parser_context_html_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->html == 1)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.max_num_streams => num
*
* Obtain the limit on the number of IO streams opened in
* this context.
*/
VALUE
ruby_xml_parser_context_io_max_num_streams_get(VALUE self) {
// TODO alias to max_streams and dep this?
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->inputMax));
}
/*
* call-seq:
* context.num_streams => "dir"
*
* Obtain the actual number of IO streams in this
* context.
*/
VALUE
ruby_xml_parser_context_io_num_streams_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->inputNr));
}
/*
* call-seq:
* context.keep_blanks? => (true|false)
*
* Determine whether parsers in this context retain
* whitespace.
*/
VALUE
ruby_xml_parser_context_keep_blanks_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->keepBlanks)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.name_depth => num
*
* Obtain the name depth for this context.
*/
VALUE
ruby_xml_parser_context_name_depth_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->nameNr));
}
/*
* call-seq:
* context.name_depth_max => num
*
* Obtain the maximum name depth for this context.
*/
VALUE
ruby_xml_parser_context_name_depth_max_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->nameMax));
}
/*
* call-seq:
* context.name_node => "name"
*
* Obtain the name node for this context.
*/
VALUE
ruby_xml_parser_context_name_node_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->name == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxpc->ctxt->name));
}
/*
* call-seq:
* context.name_tab => ["name", ..., "name"]
*
* Obtain the name table for this context.
*/
VALUE
ruby_xml_parser_context_name_tab_get(VALUE self) {
int i;
ruby_xml_parser_context *rxpc;
VALUE tab_ary;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->nameTab == NULL)
return(Qnil);
tab_ary = rb_ary_new();
for (i = (rxpc->ctxt->nameNr - 1); i >= 0; i--) {
if (rxpc->ctxt->nameTab[i] == NULL)
continue;
else
rb_ary_push(tab_ary, rb_str_new2((const char*)rxpc->ctxt->nameTab[i]));
}
return(tab_ary);
}
/*
* call-seq:
* context.node_depth => num
*
* Obtain the node depth for this context.
*/
VALUE
ruby_xml_parser_context_node_depth_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->nodeNr));
}
/*
* call-seq:
* context.node => node
*
* Obtain the root node of this context.
*/
VALUE
ruby_xml_parser_context_node_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->node == NULL)
return(Qnil);
else
return(ruby_xml_node2_wrap(cXMLNode,
rxpc->ctxt->node));
}
/*
* call-seq:
* context.node_depth_max => num
*
* Obtain the maximum node depth for this context.
*/
VALUE
ruby_xml_parser_context_node_depth_max_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->nodeMax));
}
/*
* call-seq:
* context.num_chars => num
*
* Obtain the number of characters in this context.
*/
VALUE
ruby_xml_parser_context_num_chars_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(LONG2NUM(rxpc->ctxt->nbChars));
}
VALUE
ruby_xml_parser_context_new(VALUE class, xmlParserCtxtPtr ctxt) {
ruby_xml_parser_context *rxpc;
rxpc = ALLOC(ruby_xml_parser_context);
rxpc->ctxt = ctxt;
return Data_Wrap_Struct(class,
ruby_xml_parser_context_mark,
ruby_xml_parser_context_free,
rxpc);
}
VALUE
ruby_xml_parser_context_new2(VALUE class) {
return(ruby_xml_parser_context_new(class, NULL));
}
VALUE
ruby_xml_parser_context_new3() {
return(ruby_xml_parser_context_new2(cXMLParserContext));
}
/*
* call-seq:
* context.replace_entities? => (true|false)
*
* Determine whether external entity replacement is enabled in this
* context.
*/
VALUE
ruby_xml_parser_context_replace_entities_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->replaceEntities)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.replace_entities = true|false
*
* Control whether external entity replacement is enabled in this
* context.
*/
VALUE
ruby_xml_parser_context_replace_entities_set(VALUE self, VALUE bool) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (TYPE(bool) == T_FALSE) {
rxpc->ctxt->replaceEntities = 0;
return(Qfalse);
} else {
rxpc->ctxt->replaceEntities = 1;
return(Qfalse);
}
}
/*
* call-seq:
* context.space_depth => num
*
* Obtain the space depth for this context.
*/
VALUE
ruby_xml_parser_context_space_depth_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->spaceNr));
}
/*
* call-seq:
* context.space_depth => num
*
* Obtain the maximum space depth for this context.
*/
VALUE
ruby_xml_parser_context_space_depth_max_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
return(INT2NUM(rxpc->ctxt->spaceMax));
}
/*
* call-seq:
* context.subset_external? => (true|false)
*
* Determine whether this context is a subset of an
* external context.
*/
VALUE
ruby_xml_parser_context_subset_external_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->inSubset == 2)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.subset_internal? => (true|false)
*
* Determine whether this context is a subset of an
* internal context.
*/
VALUE
ruby_xml_parser_context_subset_internal_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->inSubset == 1)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.subset_name => "name"
*
* Obtain this context's subset name (valid only if
* either of subset_external? or subset_internal?
* is true).
*/
VALUE
ruby_xml_parser_context_subset_name_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->intSubName == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxpc->ctxt->intSubName));
}
/*
* call-seq:
* context.subset_external_uri => "uri"
*
* Obtain this context's external subset URI. (valid only if
* either of subset_external? or subset_internal?
* is true).
*/
VALUE
ruby_xml_parser_context_subset_external_uri_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->extSubURI == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxpc->ctxt->extSubURI));
}
/*
* call-seq:
* context.subset_external_system_id => "system_id"
*
* Obtain this context's external subset system identifier.
* (valid only if either of subset_external? or subset_internal?
* is true).
*/
VALUE
ruby_xml_parser_context_subset_external_system_id_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->extSubSystem == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxpc->ctxt->extSubSystem));
}
/*
* call-seq:
* context.standalone? => (true|false)
*
* Determine whether this is a standalone context.
*/
VALUE
ruby_xml_parser_context_standalone_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->standalone)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.stats? => (true|false)
*
* Determine whether this context maintains statistics.
*/
VALUE
ruby_xml_parser_context_stats_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->record_info)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.valid? => (true|false)
*
* Determine whether this context is valid.
*/
VALUE
ruby_xml_parser_context_valid_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->valid)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.validate? => (true|false)
*
* Determine whether validation is enabled in this context.
*/
VALUE
ruby_xml_parser_context_validate_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->validate)
return(Qtrue);
else
return(Qfalse);
}
/*
* call-seq:
* context.version => "version"
*
* Obtain this context's version identifier.
*/
VALUE
ruby_xml_parser_context_version_get(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->version == NULL)
return(Qnil);
else
return(rb_str_new2((const char*)rxpc->ctxt->version));
}
/*
* call-seq:
* context.well_formed? => (true|false)
*
* Determine whether this context contains well-formed XML.
*/
VALUE
ruby_xml_parser_context_well_formed_q(VALUE self) {
ruby_xml_parser_context *rxpc;
Data_Get_Struct(self, ruby_xml_parser_context, rxpc);
if (rxpc->ctxt->wellFormed)
return(Qtrue);
else
return(Qfalse);
}
// Rdoc needs to know
#ifdef RDOC_NEVER_DEFINED
mXML = rb_define_module("XML");
cXMLParser = rb_define_class_under(mXML, "Parser", rb_cObject);
#endif
void
ruby_init_xml_parser_context(void) {
cXMLParserContext = rb_define_class_under(cXMLParser, "Context", rb_cObject);
rb_define_method(cXMLParserContext, "data_directory", ruby_xml_parser_context_data_directory_get, 0);
rb_define_method(cXMLParserContext, "depth", ruby_xml_parser_context_depth_get, 0);
rb_define_method(cXMLParserContext, "disable_sax?", ruby_xml_parser_context_disable_sax_q, 0);
rb_define_method(cXMLParserContext, "doc", ruby_xml_parser_context_doc_get, 0);
rb_define_method(cXMLParserContext, "docbook?", ruby_xml_parser_context_docbook_q, 0);
rb_define_method(cXMLParserContext, "encoding", ruby_xml_parser_context_encoding_get, 0);
rb_define_method(cXMLParserContext, "errno", ruby_xml_parser_context_errno_get, 0);
rb_define_method(cXMLParserContext, "html?", ruby_xml_parser_context_html_q, 0);
rb_define_method(cXMLParserContext, "io_max_num_streams", ruby_xml_parser_context_io_max_num_streams_get, 0);
rb_define_method(cXMLParserContext, "io_num_streams", ruby_xml_parser_context_io_num_streams_get, 0);
rb_define_method(cXMLParserContext, "keep_blanks?", ruby_xml_parser_context_keep_blanks_q, 0);
rb_define_method(cXMLParserContext, "name_node", ruby_xml_parser_context_name_node_get, 0);
rb_define_method(cXMLParserContext, "name_depth", ruby_xml_parser_context_name_depth_get, 0);
rb_define_method(cXMLParserContext, "name_depth_max", ruby_xml_parser_context_name_depth_max_get, 0);
rb_define_method(cXMLParserContext, "name_tab", ruby_xml_parser_context_name_tab_get, 0);
rb_define_method(cXMLParserContext, "node", ruby_xml_parser_context_node_get, 0);
rb_define_method(cXMLParserContext, "node_depth", ruby_xml_parser_context_node_depth_get, 0);
rb_define_method(cXMLParserContext, "node_depth_max", ruby_xml_parser_context_node_depth_max_get, 0);
rb_define_method(cXMLParserContext, "num_chars", ruby_xml_parser_context_num_chars_get, 0);
rb_define_method(cXMLParserContext, "replace_entities?", ruby_xml_parser_context_replace_entities_q, 0);
rb_define_method(cXMLParserContext, "replace_entities=", ruby_xml_parser_context_replace_entities_set, 1);
rb_define_method(cXMLParserContext, "space_depth", ruby_xml_parser_context_space_depth_get, 0);
rb_define_method(cXMLParserContext, "space_depth_max", ruby_xml_parser_context_space_depth_max_get, 0);
rb_define_method(cXMLParserContext, "subset_external?", ruby_xml_parser_context_subset_external_q, 0);
rb_define_method(cXMLParserContext, "subset_external_system_id", ruby_xml_parser_context_subset_external_system_id_get, 0);
rb_define_method(cXMLParserContext, "subset_external_uri", ruby_xml_parser_context_subset_name_get, 0);
rb_define_method(cXMLParserContext, "subset_internal?", ruby_xml_parser_context_subset_internal_q, 0);
rb_define_method(cXMLParserContext, "subset_internal_name", ruby_xml_parser_context_subset_name_get, 0);
rb_define_method(cXMLParserContext, "stats?", ruby_xml_parser_context_stats_q, 0);
rb_define_method(cXMLParserContext, "standalone?", ruby_xml_parser_context_standalone_q, 0);
rb_define_method(cXMLParserContext, "valid", ruby_xml_parser_context_valid_q, 0);
rb_define_method(cXMLParserContext, "validate?", ruby_xml_parser_context_validate_q, 0);
rb_define_method(cXMLParserContext, "version", ruby_xml_parser_context_version_get, 0);
rb_define_method(cXMLParserContext, "well_formed?", ruby_xml_parser_context_well_formed_q, 0);
}
|