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 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 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 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 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 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 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 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 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 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 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 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 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 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 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 1550 1551 1552 1553 1554 1555 1556 1557 1558 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 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 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 1642 1643 1644 1645 1646 1647 1648 1649
|
/* ox.c
* Copyright (c) 2011, Peter Ohler
* All rights reserved.
*/
#include "ox.h"
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "intern.h"
#include "ruby.h"
#include "sax.h"
/* maximum to allocate on the stack, arbitrary limit */
#define SMALL_XML 4096
#define WITH_CACHE_TESTS 0
typedef struct _yesNoOpt {
VALUE sym;
char *attr;
} *YesNoOpt;
void Init_ox();
VALUE Ox = Qnil;
ID ox_abort_id;
ID ox_at_column_id;
ID ox_at_content_id;
ID ox_at_id;
ID ox_at_line_id;
ID ox_at_pos_id;
ID ox_at_value_id;
ID ox_attr_id;
ID ox_attr_value_id;
ID ox_attributes_id;
ID ox_attrs_done_id;
ID ox_beg_id;
ID ox_bigdecimal_id;
ID ox_call_id;
ID ox_cdata_id;
ID ox_comment_id;
ID ox_den_id;
ID ox_doctype_id;
ID ox_end_element_id;
ID ox_end_id;
ID ox_end_instruct_id;
ID ox_error_id;
ID ox_excl_id;
ID ox_external_encoding_id;
ID ox_fileno_id;
ID ox_force_encoding_id;
ID ox_inspect_id;
ID ox_instruct_id;
ID ox_jd_id;
ID ox_keys_id;
ID ox_local_id;
ID ox_mesg_id;
ID ox_message_id;
ID ox_new_id;
ID ox_nodes_id;
ID ox_num_id;
ID ox_parse_id;
ID ox_pos_id;
ID ox_read_id;
ID ox_readpartial_id;
ID ox_start_element_id;
ID ox_string_id;
ID ox_text_id;
ID ox_to_c_id;
ID ox_value_id;
VALUE ox_encoding_sym;
VALUE ox_version_sym;
VALUE ox_standalone_sym;
VALUE ox_indent_sym;
VALUE ox_size_sym;
VALUE ox_empty_string;
VALUE ox_zero_fixnum;
VALUE ox_sym_bank; // Array
VALUE ox_arg_error_class;
VALUE ox_bag_clas;
VALUE ox_cdata_clas;
VALUE ox_comment_clas;
VALUE ox_raw_clas;
VALUE ox_date_class;
VALUE ox_doctype_clas;
VALUE ox_document_clas;
VALUE ox_element_clas;
VALUE ox_instruct_clas;
VALUE ox_parse_error_class;
VALUE ox_stringio_class;
VALUE ox_struct_class;
VALUE ox_syntax_error_class;
VALUE ox_time_class;
SlotCache ox_class_cache = 0;
static VALUE abort_sym;
static VALUE active_sym;
static VALUE attr_key_mod_sym;
static VALUE auto_define_sym;
static VALUE auto_sym;
static VALUE block_sym;
static VALUE circular_sym;
static VALUE convert_special_sym;
static VALUE effort_sym;
static VALUE generic_sym;
static VALUE hash_no_attrs_sym;
static VALUE hash_sym;
static VALUE inactive_sym;
static VALUE invalid_replace_sym;
static VALUE limited_sym;
static VALUE margin_sym;
static VALUE mode_sym;
static VALUE nest_ok_sym;
static VALUE no_empty_sym;
static VALUE object_sym;
static VALUE off_sym;
static VALUE opt_format_sym;
static VALUE optimized_sym;
static VALUE overlay_sym;
static VALUE skip_none_sym;
static VALUE skip_off_sym;
static VALUE skip_return_sym;
static VALUE skip_sym;
static VALUE skip_white_sym;
static VALUE smart_sym;
static VALUE strict_sym;
static VALUE strip_namespace_sym;
static VALUE symbolize_keys_sym;
static VALUE symbolize_sym;
static VALUE tolerant_sym;
static VALUE trace_sym;
static VALUE with_cdata_sym;
static VALUE with_dtd_sym;
static VALUE with_instruct_sym;
static VALUE with_xml_sym;
static VALUE xsd_date_sym;
static VALUE element_key_mod_sym;
static ID encoding_id;
static ID has_key_id;
rb_encoding *ox_utf8_encoding = 0;
struct _options ox_default_options = {
{'\0'}, // encoding
{'\0'}, // margin
2, // indent
0, // trace
0, // margin_len
No, // with_dtd
No, // with_xml
No, // with_instruct
No, // circular
No, // xsd_date
NoMode, // mode
StrictEffort, // effort
Yes, // sym_keys
SpcSkip, // skip
No, // smart
true, // convert_special
No, // allow_invalid
false, // no_empty
false, // with_cdata
{'\0'}, // inv_repl
{'\0'}, // strip_ns
NULL, // html_hints
Qnil, // attr_key_mod;
Qnil, // element_key_mod;
0 // rb_enc
};
extern ParseCallbacks ox_obj_callbacks;
extern ParseCallbacks ox_gen_callbacks;
extern ParseCallbacks ox_limited_callbacks;
extern ParseCallbacks ox_nomode_callbacks;
extern ParseCallbacks ox_hash_callbacks;
extern ParseCallbacks ox_hash_cdata_callbacks;
extern ParseCallbacks ox_hash_no_attrs_callbacks;
extern ParseCallbacks ox_hash_no_attrs_cdata_callbacks;
static void parse_dump_options(VALUE ropts, Options copts);
static char *defuse_bom(char *xml, Options options) {
switch ((uint8_t)*xml) {
case 0xEF: // UTF-8
if (0xBB == (uint8_t)xml[1] && 0xBF == (uint8_t)xml[2]) {
options->rb_enc = ox_utf8_encoding;
xml += 3;
} else {
rb_raise(ox_parse_error_class, "Invalid BOM in XML string.\n");
}
break;
#if 0
case 0xFE: // UTF-16BE
if (0xFF == (uint8_t)xml[1]) {
options->rb_enc = ox_utf16be_encoding;
xml += 2;
} else {
rb_raise(ox_parse_error_class, "Invalid BOM in XML string.\n");
}
break;
case 0xFF: // UTF-16LE or UTF-32LE
if (0xFE == (uint8_t)xml[1]) {
if (0x00 == (uint8_t)xml[2] && 0x00 == (uint8_t)xml[3]) {
options->rb_enc = ox_utf32le_encoding;
xml += 4;
} else {
options->rb_enc = ox_utf16le_encoding;
xml += 2;
}
} else {
rb_raise(ox_parse_error_class, "Invalid BOM in XML string.\n");
}
break;
case 0x00: // UTF-32BE
if (0x00 == (uint8_t)xml[1] && 0xFE == (uint8_t)xml[2] && 0xFF == (uint8_t)xml[3]) {
options->rb_enc = ox_utf32be_encoding;
xml += 4;
} else {
rb_raise(ox_parse_error_class, "Invalid BOM in XML string.\n");
}
break;
#endif
default:
// Let it fail if there is a BOM that is not UTF-8. Other BOM options
// are not ASCII compatible.
break;
}
return xml;
}
static VALUE hints_to_overlay(Hints hints) {
volatile VALUE overlay = rb_hash_new();
Hint h;
int i;
VALUE ov;
for (i = hints->size, h = hints->hints; 0 < i; i--, h++) {
switch (h->overlay) {
case InactiveOverlay: ov = inactive_sym; break;
case BlockOverlay: ov = block_sym; break;
case OffOverlay: ov = off_sym; break;
case AbortOverlay: ov = abort_sym; break;
case NestOverlay: ov = nest_ok_sym; break;
case ActiveOverlay:
default: ov = active_sym; break;
}
rb_hash_aset(overlay, rb_str_new2(h->name), ov);
}
return overlay;
}
/* call-seq: default_options() => Hash
*
* Returns the default load and dump options as a Hash. The options are
* - _:margin_ [String] left margin to inset when dumping
* - _:indent_ [Fixnum] number of spaces to indent each element in an XML document
* - _:trace_ [Fixnum] trace level where 0 is silent
* - _:encoding_ [String] character encoding for the XML file
* - _:with_dtd_ [true|false|nil] include DTD in the dump
* - _:with_instruct_ [true|false|nil] include instructions in the dump
* - _:with_xml_ [true|false|nil] include XML prolog in the dump
* - _:circular_ [true|false|nil] support circular references while dumping
* - _:xsd_date_ [true|false|nil] use XSD date format instead of decimal format
* - _:mode_ [:object|:generic|:limited|:hash|:hash_no_attrs|nil] load method to use for XML
* - _:effort_ [:strict|:tolerant|:auto_define] set the tolerance level for loading
* - _:symbolize_keys_ [true|false|nil] symbolize element attribute keys or leave as Strings
* - _:element_key_mod_ [Proc|nil] converts element keys on parse if not nil
* - _:attr_key_mod_ [Proc|nil] converts attribute keys on parse if not nil
* - _:skip_ [:skip_none|:skip_return|:skip_white|:skip_off] determines how to handle white space in text
* - _:smart_ [true|false|nil] flag indicating the SAX parser uses hints if available (use with html)
* - _:convert_special_ [true|false|nil] flag indicating special characters like < are converted with the SAX parser
* - _:invalid_replace_ [nil|String] replacement string for invalid XML characters on dump. nil indicates include anyway
* as hex. A string, limited to 10 characters will replace the invalid character with the replace.
* - _:no_empty_ [true|false|nil] flag indicating there should be no empty elements in a dump
* - _:with_cdata_ [true|false] includes cdata in hash_load results
* - _:strip_namespace_ [String|true|false] false or "" results in no namespace stripping. A string of "*" or true will
* strip all namespaces. Any other non-empty string indicates that matching namespaces will be stripped.
* - _:overlay_ [Hash] a Hash of keys that match html element names and values that are one of
* - _:active_ - make the normal callback for the element
* - _:nest_ok_ - active but the nesting check is ignored
* - _:inactive_ - do not make the element start, end, or attribute callbacks for this element only
* - _:block_ - block this and all children callbacks
* - _:off_ - block this element and it's children unless the child element is active
* - _:abort_ - abort the html processing and return
*
* *return* [Hash] all current option settings.
*
* Note that an indent of less than zero will result in a tight one line output
* unless the text in the XML fields contain new line characters.
*/
static VALUE get_def_opts(VALUE self) {
VALUE opts = rb_hash_new();
int elen = (int)strlen(ox_default_options.encoding);
rb_hash_aset(opts, ox_encoding_sym, (0 == elen) ? Qnil : rb_str_new(ox_default_options.encoding, elen));
rb_hash_aset(opts, margin_sym, rb_str_new(ox_default_options.margin, ox_default_options.margin_len));
rb_hash_aset(opts, ox_indent_sym, INT2FIX(ox_default_options.indent));
rb_hash_aset(opts, trace_sym, INT2FIX(ox_default_options.trace));
rb_hash_aset(opts,
with_dtd_sym,
(Yes == ox_default_options.with_dtd) ? Qtrue : ((No == ox_default_options.with_dtd) ? Qfalse : Qnil));
rb_hash_aset(opts,
with_xml_sym,
(Yes == ox_default_options.with_xml) ? Qtrue : ((No == ox_default_options.with_xml) ? Qfalse : Qnil));
rb_hash_aset(
opts,
with_instruct_sym,
(Yes == ox_default_options.with_instruct) ? Qtrue : ((No == ox_default_options.with_instruct) ? Qfalse : Qnil));
rb_hash_aset(opts,
circular_sym,
(Yes == ox_default_options.circular) ? Qtrue : ((No == ox_default_options.circular) ? Qfalse : Qnil));
rb_hash_aset(opts,
xsd_date_sym,
(Yes == ox_default_options.xsd_date) ? Qtrue : ((No == ox_default_options.xsd_date) ? Qfalse : Qnil));
rb_hash_aset(opts,
symbolize_keys_sym,
(Yes == ox_default_options.sym_keys) ? Qtrue : ((No == ox_default_options.sym_keys) ? Qfalse : Qnil));
rb_hash_aset(opts, attr_key_mod_sym, ox_default_options.attr_key_mod);
rb_hash_aset(opts, element_key_mod_sym, ox_default_options.element_key_mod);
rb_hash_aset(opts,
smart_sym,
(Yes == ox_default_options.smart) ? Qtrue : ((No == ox_default_options.smart) ? Qfalse : Qnil));
rb_hash_aset(opts, convert_special_sym, (ox_default_options.convert_special) ? Qtrue : Qfalse);
rb_hash_aset(opts, no_empty_sym, (ox_default_options.no_empty) ? Qtrue : Qfalse);
rb_hash_aset(opts, with_cdata_sym, (ox_default_options.with_cdata) ? Qtrue : Qfalse);
switch (ox_default_options.mode) {
case ObjMode: rb_hash_aset(opts, mode_sym, object_sym); break;
case GenMode: rb_hash_aset(opts, mode_sym, generic_sym); break;
case LimMode: rb_hash_aset(opts, mode_sym, limited_sym); break;
case HashMode: rb_hash_aset(opts, mode_sym, hash_sym); break;
case HashNoAttrMode: rb_hash_aset(opts, mode_sym, hash_no_attrs_sym); break;
case NoMode:
default: rb_hash_aset(opts, mode_sym, Qnil); break;
}
switch (ox_default_options.effort) {
case StrictEffort: rb_hash_aset(opts, effort_sym, strict_sym); break;
case TolerantEffort: rb_hash_aset(opts, effort_sym, tolerant_sym); break;
case AutoEffort: rb_hash_aset(opts, effort_sym, auto_define_sym); break;
case NoEffort:
default: rb_hash_aset(opts, effort_sym, Qnil); break;
}
switch (ox_default_options.skip) {
case OffSkip: rb_hash_aset(opts, skip_sym, skip_off_sym); break;
case NoSkip: rb_hash_aset(opts, skip_sym, skip_none_sym); break;
case CrSkip: rb_hash_aset(opts, skip_sym, skip_return_sym); break;
case SpcSkip: rb_hash_aset(opts, skip_sym, skip_white_sym); break;
default: rb_hash_aset(opts, skip_sym, Qnil); break;
}
if (Yes == ox_default_options.allow_invalid) {
rb_hash_aset(opts, invalid_replace_sym, Qnil);
} else {
rb_hash_aset(opts,
invalid_replace_sym,
rb_str_new(ox_default_options.inv_repl + 1, (int)*ox_default_options.inv_repl));
}
if ('\0' == *ox_default_options.strip_ns) {
rb_hash_aset(opts, strip_namespace_sym, Qfalse);
} else if ('*' == *ox_default_options.strip_ns && '\0' == ox_default_options.strip_ns[1]) {
rb_hash_aset(opts, strip_namespace_sym, Qtrue);
} else {
rb_hash_aset(opts,
strip_namespace_sym,
rb_str_new(ox_default_options.strip_ns, strlen(ox_default_options.strip_ns)));
}
if (NULL == ox_default_options.html_hints) {
// rb_hash_aset(opts, overlay_sym, hints_to_overlay(ox_hints_html()));
rb_hash_aset(opts, overlay_sym, Qnil);
} else {
rb_hash_aset(opts, overlay_sym, hints_to_overlay(ox_default_options.html_hints));
}
return opts;
}
static int set_overlay(VALUE key, VALUE value, VALUE ctx) {
Hints hints = (Hints)ctx;
Hint hint;
if (NULL != (hint = ox_hint_find(hints, StringValuePtr(key)))) {
if (active_sym == value) {
hint->overlay = ActiveOverlay;
} else if (inactive_sym == value) {
hint->overlay = InactiveOverlay;
} else if (block_sym == value) {
hint->overlay = BlockOverlay;
} else if (nest_ok_sym == value) {
hint->overlay = NestOverlay;
} else if (off_sym == value) {
hint->overlay = OffOverlay;
} else if (abort_sym == value) {
hint->overlay = AbortOverlay;
}
}
return ST_CONTINUE;
}
/* call-seq: sax_html_overlay() => Hash
*
* Returns an overlay hash that can be modified and used as an overlay in the
* default options or in the sax_html() function call. Values for the keys are:
* - _:active_ - make the normal callback for the element
* - _:nest_ok_ - active but ignore nest check
* - _:inactive_ - do not make the element start, end, or attribute callbacks for this element only
* - _:block_ - block this and all children callbacks
* - _:off_ - block this element and it's children unless the child element is active
* - _:abort_ - abort the html processing and return
*
* *return* [Hash] default SAX HTML settings
*/
static VALUE sax_html_overlay(VALUE self) {
return hints_to_overlay(ox_hints_html());
}
/* call-seq: default_options=(opts)
*
* Sets the default options for load and dump.
* - +opts+ [Hash] opts options to change
* - _:margin_ [String] left margin to inset when dumping
* - _:indent_ [Fixnum] number of spaces to indent each element in an XML document
* - _:trace_ [Fixnum] trace level where 0 is silent
* - _:encoding_ [String] character encoding for the XML file
* - _:with_dtd_ [true|false|nil] include DTD in the dump
* - _:with_instruct_ [true|false|nil] include instructions in the dump
* - _:with_xml_ [true|false|nil] include XML prolog in the dump
* - _:circular_ [true|false|nil] support circular references while dumping
* - _:xsd_date_ [true|false|nil] use XSD date format instead of decimal format
* - _:mode_ [:object|:generic|:limited|:hash|:hash_no_attrs|nil] load method to use for XML
* - _:effort_ [:strict|:tolerant|:auto_define] set the tolerance level for loading
* - _:symbolize_keys_ [true|false|nil] symbolize element attribute keys or leave as Strings
* - _:element_key_mod_ [Proc|nil] converts element keys on parse if not nil
* - _:attr_key_mod_ [Proc|nil] converts attribute keys on parse if not nil
* - _:skip_ [:skip_none|:skip_return|:skip_white|:skip_off] determines how to handle white space in text
* - _:smart_ [true|false|nil] flag indicating the SAX parser uses hints if available (use with html)
* - _:invalid_replace_ [nil|String] replacement string for invalid XML characters on dump. nil indicates include
* anyway as hex. A string, limited to 10 characters will replace the invalid character with the replace.
* - _:strip_namespace_ [nil|String|true|false] "" or false result in no namespace stripping. A string of "*" or true
* will strip all namespaces. Any other non-empty string indicates that matching namespaces will be stripped.
* - _:with_cdata_ [true|false] includes cdata in hash_load results
* - _:overlay_ [Hash] a Hash of keys that match html element names and values that are one of
* - _:active_ - make the normal callback for the element
* - _:nest_ok_ - active but ignore nest check
* - _:inactive_ - do not make the element start, end, or attribute callbacks for this element only
* - _:block_ - block this and all children callbacks
* - _:off_ - block this element and it's children unless the child element is active
* - _:abort_ - abort the html processing and return
*
* *return* [nil]
*/
static VALUE set_def_opts(VALUE self, VALUE opts) {
struct _yesNoOpt ynos[] = {{with_xml_sym, &ox_default_options.with_xml},
{with_dtd_sym, &ox_default_options.with_dtd},
{with_instruct_sym, &ox_default_options.with_instruct},
{xsd_date_sym, &ox_default_options.xsd_date},
{circular_sym, &ox_default_options.circular},
{symbolize_keys_sym, &ox_default_options.sym_keys},
{smart_sym, &ox_default_options.smart},
{Qnil, 0}};
YesNoOpt o;
VALUE v;
Check_Type(opts, T_HASH);
v = rb_hash_aref(opts, ox_encoding_sym);
if (Qnil == v) {
*ox_default_options.encoding = '\0';
} else {
Check_Type(v, T_STRING);
strncpy(ox_default_options.encoding, StringValuePtr(v), sizeof(ox_default_options.encoding) - 1);
ox_default_options.rb_enc = rb_enc_find(ox_default_options.encoding);
}
v = rb_hash_aref(opts, ox_indent_sym);
if (Qnil != v) {
Check_Type(v, T_FIXNUM);
ox_default_options.indent = FIX2INT(v);
}
v = rb_hash_aref(opts, trace_sym);
if (Qnil != v) {
Check_Type(v, T_FIXNUM);
ox_default_options.trace = FIX2INT(v);
}
v = rb_hash_aref(opts, mode_sym);
if (Qnil == v) {
ox_default_options.mode = NoMode;
} else if (object_sym == v) {
ox_default_options.mode = ObjMode;
} else if (generic_sym == v) {
ox_default_options.mode = GenMode;
} else if (limited_sym == v) {
ox_default_options.mode = LimMode;
} else if (hash_sym == v) {
ox_default_options.mode = HashMode;
} else if (hash_no_attrs_sym == v) {
ox_default_options.mode = HashNoAttrMode;
} else {
rb_raise(ox_parse_error_class, ":mode must be :object, :generic, :limited, :hash, :hash_no_attrs, or nil.\n");
}
v = rb_hash_aref(opts, effort_sym);
if (Qnil == v) {
ox_default_options.effort = NoEffort;
} else if (strict_sym == v) {
ox_default_options.effort = StrictEffort;
} else if (tolerant_sym == v) {
ox_default_options.effort = TolerantEffort;
} else if (auto_define_sym == v) {
ox_default_options.effort = AutoEffort;
} else {
rb_raise(ox_parse_error_class, ":effort must be :strict, :tolerant, :auto_define, or nil.\n");
}
v = rb_hash_aref(opts, skip_sym);
if (Qnil == v) {
ox_default_options.skip = NoSkip;
} else if (skip_off_sym == v) {
ox_default_options.skip = OffSkip;
} else if (skip_none_sym == v) {
ox_default_options.skip = NoSkip;
} else if (skip_return_sym == v) {
ox_default_options.skip = CrSkip;
} else if (skip_white_sym == v) {
ox_default_options.skip = SpcSkip;
} else {
rb_raise(ox_parse_error_class, ":skip must be :skip_none, :skip_return, :skip_white, :skip_off, or nil.\n");
}
v = rb_hash_lookup(opts, convert_special_sym);
if (Qnil == v) {
// no change
} else if (Qtrue == v) {
ox_default_options.convert_special = 1;
} else if (Qfalse == v) {
ox_default_options.convert_special = 0;
} else {
rb_raise(ox_parse_error_class, ":convert_special must be true or false.\n");
}
v = rb_hash_lookup(opts, no_empty_sym);
if (Qnil == v) {
// no change
} else if (Qtrue == v) {
ox_default_options.no_empty = 1;
} else if (Qfalse == v) {
ox_default_options.no_empty = 0;
} else {
rb_raise(ox_parse_error_class, ":no_empty must be true or false.\n");
}
v = rb_hash_aref(opts, invalid_replace_sym);
if (Qnil == v) {
ox_default_options.allow_invalid = Yes;
} else {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(ox_default_options.inv_repl) - 2 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":invalid_replace can be no longer than %d characters.",
(int)sizeof(ox_default_options.inv_repl) - 2);
}
strncpy(ox_default_options.inv_repl + 1, StringValuePtr(v), sizeof(ox_default_options.inv_repl) - 1);
ox_default_options.inv_repl[sizeof(ox_default_options.inv_repl) - 1] = '\0';
*ox_default_options.inv_repl = (char)slen;
ox_default_options.allow_invalid = No;
}
v = rb_hash_aref(opts, strip_namespace_sym);
if (Qfalse == v) {
*ox_default_options.strip_ns = '\0';
} else if (Qtrue == v) {
*ox_default_options.strip_ns = '*';
ox_default_options.strip_ns[1] = '\0';
} else if (Qnil != v) {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(ox_default_options.strip_ns) - 1 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":strip_namespace can be no longer than %d characters.",
(int)sizeof(ox_default_options.strip_ns) - 1);
}
strncpy(ox_default_options.strip_ns, StringValuePtr(v), sizeof(ox_default_options.strip_ns) - 1);
ox_default_options.strip_ns[sizeof(ox_default_options.strip_ns) - 1] = '\0';
}
v = rb_hash_aref(opts, margin_sym);
if (Qnil != v) {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(ox_default_options.margin) - 1 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":margin can be no longer than %d characters.",
(int)sizeof(ox_default_options.margin) - 1);
}
strncpy(ox_default_options.margin, StringValuePtr(v), sizeof(ox_default_options.margin) - 1);
ox_default_options.margin[sizeof(ox_default_options.margin) - 1] = '\0';
ox_default_options.margin_len = strlen(ox_default_options.margin);
}
for (o = ynos; 0 != o->attr; o++) {
v = rb_hash_lookup(opts, o->sym);
if (Qnil == v) {
*o->attr = NotSet;
} else if (Qtrue == v) {
*o->attr = Yes;
} else if (Qfalse == v) {
*o->attr = No;
} else {
rb_raise(ox_parse_error_class, "%s must be true or false.\n", rb_id2name(SYM2ID(o->sym)));
}
}
v = rb_hash_aref(opts, overlay_sym);
if (Qnil == v) {
ox_hints_destroy(ox_default_options.html_hints);
ox_default_options.html_hints = NULL;
} else {
int cnt;
Check_Type(v, T_HASH);
cnt = (int)RHASH_SIZE(v);
if (0 == cnt) {
ox_hints_destroy(ox_default_options.html_hints);
ox_default_options.html_hints = NULL;
} else {
ox_hints_destroy(ox_default_options.html_hints);
ox_default_options.html_hints = ox_hints_dup(ox_hints_html());
rb_hash_foreach(v, set_overlay, (VALUE)ox_default_options.html_hints);
}
}
if (Qnil != (v = rb_hash_lookup(opts, with_cdata_sym))) {
ox_default_options.with_cdata = (Qtrue == v);
}
ox_default_options.element_key_mod = rb_hash_lookup2(opts, element_key_mod_sym, ox_default_options.element_key_mod);
ox_default_options.attr_key_mod = rb_hash_lookup2(opts, attr_key_mod_sym, ox_default_options.attr_key_mod);
return Qnil;
}
/* call-seq: parse_obj(xml) => Object
*
* Parses an XML document String that is in the object format and returns an
* Object of the type represented by the XML. This function expects an
* optimized XML formated String. For other formats use the more generic
* Ox.load() method. Raises an exception if the XML is malformed or the
* classes specified in the file are not valid.
* - +xml+ [String] XML String in optimized Object format.
* *return* [Object] deserialized Object.
*/
static VALUE to_obj(VALUE self, VALUE ruby_xml) {
char *xml, *x;
size_t len;
VALUE obj;
struct _options options = ox_default_options;
struct _err err;
err_init(&err);
Check_Type(ruby_xml, T_STRING);
/* the xml string gets modified so make a copy of it */
len = RSTRING_LEN(ruby_xml) + 1;
x = defuse_bom(StringValuePtr(ruby_xml), &options);
if (SMALL_XML < len) {
xml = ALLOC_N(char, len);
} else {
xml = ALLOCA_N(char, len);
}
memcpy(xml, x, len);
rb_gc_disable();
obj = ox_parse(xml, len - 1, ox_obj_callbacks, 0, &options, &err);
if (SMALL_XML < len) {
xfree(xml);
}
RB_GC_GUARD(obj);
rb_gc_enable();
if (err_has(&err)) {
ox_err_raise(&err);
}
return obj;
}
/* call-seq: parse(xml) => Ox::Document or Ox::Element
*
* Parses and XML document String into an Ox::Document or Ox::Element.
* - +xml+ [String] xml XML String
* *return* [Ox::Document or Ox::Element] parsed XML document.
*
* _raise_ [Exception] if the XML is malformed.
*/
static VALUE to_gen(VALUE self, VALUE ruby_xml) {
char *xml, *x;
size_t len;
VALUE obj;
struct _options options = ox_default_options;
struct _err err;
err_init(&err);
Check_Type(ruby_xml, T_STRING);
/* the xml string gets modified so make a copy of it */
len = RSTRING_LEN(ruby_xml) + 1;
x = defuse_bom(StringValuePtr(ruby_xml), &options);
if (SMALL_XML < len) {
xml = ALLOC_N(char, len);
} else {
xml = ALLOCA_N(char, len);
}
memcpy(xml, x, len);
obj = ox_parse(xml, len - 1, ox_gen_callbacks, 0, &options, &err);
if (SMALL_XML < len) {
xfree(xml);
}
if (err_has(&err)) {
ox_err_raise(&err);
}
return obj;
}
static int load_options_cb(VALUE k, VALUE v, VALUE opts) {
Options copts = (Options)opts;
if (mode_sym == k) {
if (object_sym == v) {
copts->mode = ObjMode;
} else if (optimized_sym == v) {
copts->mode = ObjMode;
} else if (generic_sym == v) {
copts->mode = GenMode;
} else if (limited_sym == v) {
copts->mode = LimMode;
} else if (hash_sym == v) {
copts->mode = HashMode;
} else if (hash_no_attrs_sym == v) {
copts->mode = HashNoAttrMode;
} else {
rb_raise(ox_parse_error_class, ":mode must be :generic, :object, :limited, :hash, :hash_no_attrs.\n");
}
} else if (effort_sym == k) {
if (auto_define_sym == v) {
copts->effort = AutoEffort;
} else if (tolerant_sym == v) {
copts->effort = TolerantEffort;
} else if (strict_sym == v) {
copts->effort = StrictEffort;
} else {
rb_raise(ox_parse_error_class, ":effort must be :strict, :tolerant, or :auto_define.\n");
}
} else if (skip_sym == k) {
if (skip_none_sym == v) {
copts->skip = NoSkip;
} else if (skip_off_sym == v) {
copts->skip = OffSkip;
} else if (skip_return_sym == v) {
copts->skip = CrSkip;
} else if (skip_white_sym == v) {
copts->skip = SpcSkip;
} else {
rb_raise(ox_parse_error_class, ":skip must be :skip_none, :skip_return, :skip_white, or :skip_off.\n");
}
} else if (trace_sym == k) {
Check_Type(v, T_FIXNUM);
copts->trace = FIX2INT(v);
} else if (symbolize_keys_sym == k) {
copts->sym_keys = (Qfalse == v) ? No : Yes;
} else if (element_key_mod_sym == k) {
copts->element_key_mod = v;
} else if (attr_key_mod_sym == k) {
copts->attr_key_mod = v;
} else if (convert_special_sym == k) {
copts->convert_special = (Qfalse != v);
} else if (no_empty_sym == k) {
copts->no_empty = (Qfalse != v);
} else if (invalid_replace_sym == k) {
if (Qnil == v) {
copts->allow_invalid = Yes;
} else {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(copts->inv_repl) - 2 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":invalid_replace can be no longer than %d characters.",
(int)sizeof(copts->inv_repl) - 2);
}
strncpy(copts->inv_repl + 1, StringValuePtr(v), sizeof(copts->inv_repl) - 1);
copts->inv_repl[sizeof(copts->inv_repl) - 1] = '\0';
*copts->inv_repl = (char)slen;
copts->allow_invalid = No;
}
} else if (strip_namespace_sym == k) {
if (Qfalse == v) {
*copts->strip_ns = '\0';
} else if (Qtrue == v) {
*copts->strip_ns = '*';
copts->strip_ns[1] = '\0';
} else if (Qnil != v) {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(copts->strip_ns) - 1 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":strip_namespace can be no longer than %d characters.",
(int)sizeof(copts->strip_ns) - 1);
}
strncpy(copts->strip_ns, StringValuePtr(v), sizeof(copts->strip_ns) - 1);
copts->strip_ns[sizeof(copts->strip_ns) - 1] = '\0';
}
} else if (margin_sym == k) {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(copts->margin) - 1 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":margin can be no longer than %d characters.",
(int)sizeof(copts->margin) - 1);
}
strncpy(copts->margin, StringValuePtr(v), sizeof(copts->margin) - 1);
copts->margin[sizeof(copts->margin) - 1] = '\0';
copts->margin_len = strlen(copts->margin);
} else if (with_cdata_sym == k) {
copts->with_cdata = (Qtrue == v);
}
return ST_CONTINUE;
}
static VALUE load(char *xml, size_t len, int argc, VALUE *argv, VALUE self, VALUE encoding, Err err) {
VALUE obj;
struct _options options = ox_default_options;
if (1 == argc && rb_cHash == rb_obj_class(*argv)) {
rb_hash_foreach(*argv, load_options_cb, (VALUE)&options);
}
if ('\0' == *options.encoding) {
if (Qnil != encoding) {
options.rb_enc = rb_enc_from_index(rb_enc_get_index(encoding));
} else {
options.rb_enc = 0;
}
} else if (0 == options.rb_enc) {
options.rb_enc = rb_enc_find(options.encoding);
}
xml = defuse_bom(xml, &options);
switch (options.mode) {
case ObjMode:
rb_gc_disable();
obj = ox_parse(xml, len, ox_obj_callbacks, 0, &options, err);
RB_GC_GUARD(obj);
rb_gc_enable();
break;
case GenMode: obj = ox_parse(xml, len, ox_gen_callbacks, 0, &options, err); break;
case LimMode: obj = ox_parse(xml, len, ox_limited_callbacks, 0, &options, err); break;
case HashMode:
if (options.with_cdata) {
obj = ox_parse(xml, len, ox_hash_cdata_callbacks, 0, &options, err);
} else {
obj = ox_parse(xml, len, ox_hash_callbacks, 0, &options, err);
}
break;
case HashNoAttrMode:
if (options.with_cdata) {
obj = ox_parse(xml, len, ox_hash_no_attrs_cdata_callbacks, 0, &options, err);
} else {
obj = ox_parse(xml, len, ox_hash_no_attrs_callbacks, 0, &options, err);
}
break;
case NoMode: obj = ox_parse(xml, len, ox_nomode_callbacks, 0, &options, err); break;
default: obj = ox_parse(xml, len, ox_gen_callbacks, 0, &options, err); break;
}
return obj;
}
/* call-seq: load(xml, options) => Ox::Document or Ox::Element or Object
*
* Parses and XML document String into an Ox::Document, or Ox::Element, or
* Object depending on the options. Raises an exception if the XML is malformed
* or the classes specified are not valid. If a block is given it will be called
* on the completion of each complete top level entity with that entity as it's
* only argument.
*
* - +xml+ [String] XML String
* - +options+ [Hash] load options
* - *:mode* [:object|:generic|:limited] format expected
* - _:object_ - object format
* - _:generic_ - read as a generic XML file
* - _:limited_ - read as a generic XML file but with callbacks on text and elements events only
* - _:hash_ - read and convert to a Hash and core class objects only
* - _:hash_no_attrs_ - read and convert to a Hash and core class objects only without capturing attributes
* - *:effort* [:strict|:tolerant|:auto_define] effort to use when an undefined class is encountered, default: :strict
* - _:strict_ - raise an NameError for missing classes and modules
* - _:tolerant_ - return nil for missing classes and modules
* - _:auto_define_ - auto define missing classes and modules
* - *:trace* [Fixnum] trace level as a Fixnum, default: 0 (silent)
* - *:symbolize_keys* [true|false|nil] symbolize element attribute keys or leave as Strings
* - *:invalid_replace* [nil|String] replacement string for invalid XML characters on dump. nil indicates include
* anyway as hex. A string, limited to 10 characters will replace the invalid character with the replace.
* - *:strip_namespace* [String|true|false] "" or false result in no namespace stripping. A string of "*" or true will
* strip all namespaces. Any other non-empty string indicates that matching namespaces will be stripped.
* - *:with_cdata* [true|false] if true cdata is included in hash_load output otherwise it is not.
*/
static VALUE load_str(int argc, VALUE *argv, VALUE self) {
char *xml;
size_t len;
VALUE obj;
VALUE encoding;
struct _err err;
err_init(&err);
Check_Type(*argv, T_STRING);
/* the xml string gets modified so make a copy of it */
len = RSTRING_LEN(*argv) + 1;
if (SMALL_XML < len) {
xml = ALLOC_N(char, len);
} else {
xml = ALLOCA_N(char, len);
}
encoding = rb_obj_encoding(*argv);
memcpy(xml, StringValuePtr(*argv), len);
xml[len - 1] = '\0';
obj = load(xml, len - 1, argc - 1, argv + 1, self, encoding, &err);
if (SMALL_XML < len) {
xfree(xml);
}
if (err_has(&err)) {
ox_err_raise(&err);
}
return obj;
}
/* call-seq: load_file(file_path, options) => Ox::Document or Ox::Element or Object
*
* Parses and XML document from a file into an Ox::Document, or Ox::Element,
* or Object depending on the options. Raises an exception if the XML is
* malformed or the classes specified are not valid.
* - +file_path+ [String] file path to read the XML document from
* - +options+ [Hash] load options
* - *:mode* [:object|:generic|:limited] format expected
* - _:object_ - object format
* - _:generic_ - read as a generic XML file
* - _:limited_ - read as a generic XML file but with callbacks on text and elements events only
* - _:hash_ - read and convert to a Hash and core class objects only
* - _:hash_no_attrs_ - read and convert to a Hash and core class objects only without capturing attributes
* - *:effort* [:strict|:tolerant|:auto_define] effort to use when an undefined class is encountered, default: :strict
* - _:strict_ - raise an NameError for missing classes and modules
* - _:tolerant_ - return nil for missing classes and modules
* - _:auto_define_ - auto define missing classes and modules
* - *:trace* [Fixnum] trace level as a Fixnum, default: 0 (silent)
* - *:symbolize_keys* [true|false|nil] symbolize element attribute keys or leave as Strings
* - *:invalid_replace* [nil|String] replacement string for invalid XML characters on dump. nil indicates include
* anyway as hex. A string, limited to 10 characters will replace the invalid character with the replace.
* - *:strip_namespace* [String|true|false] "" or false result in no namespace stripping. A string of "*" or true will
* strip all namespaces. Any other non-empty string indicates that matching namespaces will be stripped.
*/
static VALUE load_file(int argc, VALUE *argv, VALUE self) {
char *path;
char *xml;
FILE *f;
off_t len;
VALUE obj;
struct _err err;
err_init(&err);
Check_Type(*argv, T_STRING);
path = StringValuePtr(*argv);
if (0 == (f = fopen(path, "r"))) {
rb_raise(rb_eIOError, "%s\n", strerror(errno));
}
fseek(f, 0, SEEK_END);
len = ftello(f);
if (SMALL_XML < len) {
xml = ALLOC_N(char, len + 1);
} else {
xml = ALLOCA_N(char, len + 1);
}
fseek(f, 0, SEEK_SET);
if ((size_t)len != fread(xml, 1, len, f)) {
ox_err_set(&err, rb_eLoadError, "Failed to read %ld bytes from %s.\n", (long)len, path);
obj = Qnil;
} else {
xml[len] = '\0';
obj = load(xml, len, argc - 1, argv + 1, self, Qnil, &err);
}
fclose(f);
if (SMALL_XML < len) {
xfree(xml);
}
if (err_has(&err)) {
ox_err_raise(&err);
}
return obj;
}
/* call-seq: sax_parse(handler, io, options)
*
* Parses an IO stream or file containing an XML document. Raises an exception
* if the XML is malformed or the classes specified are not valid.
* - +handler+ [Ox::Sax] SAX (responds to OX::Sax methods) like handler
* - +io+ [IO|String] IO Object to read from
* - +options+ [Hash] options parse options
* - *:convert_special* [true|false] flag indicating special characters like < are converted
* - *:symbolize* [true|false] flag indicating the parser symbolize element and attribute names
* - *:smart* [true|false] flag indicating the parser uses hints if available (use with html)
* - *:skip* [:skip_none|:skip_return|:skip_white|:skip_off] flag indicating the parser skips \\r or collpase white
* space into a single space. Default (skip space)
* - *:strip_namespace* [nil|String|true|false] "" or false result in no namespace stripping. A string of "*" or true
* will strip all namespaces. Any other non-empty string indicates that matching namespaces will be stripped.
*/
static VALUE sax_parse(int argc, VALUE *argv, VALUE self) {
struct _saxOptions options;
options.symbolize = (No != ox_default_options.sym_keys);
options.convert_special = ox_default_options.convert_special;
options.smart = (Yes == ox_default_options.smart);
options.skip = ox_default_options.skip;
options.hints = NULL;
strcpy(options.strip_ns, ox_default_options.strip_ns);
if (argc < 2) {
rb_raise(ox_parse_error_class, "Wrong number of arguments to sax_parse.\n");
}
if (3 <= argc && rb_cHash == rb_obj_class(argv[2])) {
VALUE h = argv[2];
VALUE v;
if (Qnil != (v = rb_hash_lookup(h, convert_special_sym))) {
options.convert_special = (Qtrue == v);
}
if (Qnil != (v = rb_hash_lookup(h, smart_sym))) {
options.smart = (Qtrue == v);
}
if (Qnil != (v = rb_hash_lookup(h, symbolize_sym))) {
options.symbolize = (Qtrue == v);
}
if (Qnil != (v = rb_hash_lookup(h, skip_sym))) {
if (skip_return_sym == v) {
options.skip = CrSkip;
} else if (skip_white_sym == v) {
options.skip = SpcSkip;
} else if (skip_none_sym == v) {
options.skip = NoSkip;
} else if (skip_off_sym == v) {
options.skip = OffSkip;
}
}
if (Qnil != (v = rb_hash_lookup(h, strip_namespace_sym))) {
if (Qfalse == v) {
*options.strip_ns = '\0';
} else if (Qtrue == v) {
*options.strip_ns = '*';
options.strip_ns[1] = '\0';
} else {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(options.strip_ns) - 1 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":strip_namespace can be no longer than %d characters.",
(int)sizeof(options.strip_ns) - 1);
}
strncpy(options.strip_ns, StringValuePtr(v), sizeof(options.strip_ns) - 1);
options.strip_ns[sizeof(options.strip_ns) - 1] = '\0';
}
}
}
ox_sax_parse(argv[0], argv[1], &options);
return Qnil;
}
/* call-seq: sax_html(handler, io, options)
*
* Parses an IO stream or file containing an XML document. Raises an exception
* if the XML is malformed or the classes specified are not valid.
* - +handler+ [Ox::Sax] SAX (responds to OX::Sax methods) like handler
* - +io+ [IO|String] IO Object to read from
* - +options+ [Hash] options parse options
* - *:convert_special* [true|false] flag indicating special characters like < are converted
* - *:symbolize* [true|false] flag indicating the parser symbolize element and attribute names
* - *:skip* [:skip_none|:skip_return|:skip_white|:skip_off] flag indicating the parser skips \\r or collapse white
* space into a single space. Default (skip space)
* - *:overlay* [Hash] a Hash of keys that match html element names and values that are one of
* - _:active_ - make the normal callback for the element
* - _:nest_ok_ - active but ignore nest check
* - _:inactive_ - do not make the element start, end, or attribute callbacks for this element only
* - _:block_ - block this and all children callbacks
* - _:off_ - block this element and it's children unless the child element is active
* - _:abort_ - abort the html processing and return
*/
static VALUE sax_html(int argc, VALUE *argv, VALUE self) {
struct _saxOptions options;
bool free_hints = false;
options.symbolize = (No != ox_default_options.sym_keys);
options.convert_special = ox_default_options.convert_special;
options.smart = true;
options.skip = ox_default_options.skip;
options.hints = ox_default_options.html_hints;
if (NULL == options.hints) {
options.hints = ox_hints_html();
}
*options.strip_ns = '\0';
if (argc < 2) {
rb_raise(ox_parse_error_class, "Wrong number of arguments to sax_html.\n");
}
if (3 <= argc && rb_cHash == rb_obj_class(argv[2])) {
volatile VALUE h = argv[2];
volatile VALUE v;
if (Qnil != (v = rb_hash_lookup(h, convert_special_sym))) {
options.convert_special = (Qtrue == v);
}
if (Qnil != (v = rb_hash_lookup(h, symbolize_sym))) {
options.symbolize = (Qtrue == v);
}
if (Qnil != (v = rb_hash_lookup(h, skip_sym))) {
if (skip_return_sym == v) {
options.skip = CrSkip;
} else if (skip_white_sym == v) {
options.skip = SpcSkip;
} else if (skip_none_sym == v) {
options.skip = NoSkip;
} else if (skip_off_sym == v) {
options.skip = OffSkip;
}
}
if (Qnil != (v = rb_hash_lookup(h, overlay_sym))) {
int cnt;
Check_Type(v, T_HASH);
cnt = (int)RHASH_SIZE(v);
if (0 == cnt) {
options.hints = ox_hints_html();
} else {
options.hints = ox_hints_dup(options.hints);
free_hints = true;
rb_hash_foreach(v, set_overlay, (VALUE)options.hints);
}
}
}
ox_sax_parse(argv[0], argv[1], &options);
if (free_hints) {
ox_hints_destroy(options.hints);
}
return Qnil;
}
static void parse_dump_options(VALUE ropts, Options copts) {
struct _yesNoOpt ynos[] = {{with_xml_sym, &copts->with_xml},
{with_dtd_sym, &copts->with_dtd},
{with_instruct_sym, &copts->with_instruct},
{xsd_date_sym, &copts->xsd_date},
{circular_sym, &copts->circular},
{Qnil, 0}};
YesNoOpt o;
if (rb_cHash == rb_obj_class(ropts)) {
VALUE v;
if (Qnil != (v = rb_hash_lookup(ropts, ox_indent_sym))) {
if (rb_cInteger != rb_obj_class(v) && T_FIXNUM != rb_type(v)) {
rb_raise(ox_parse_error_class, ":indent must be a Fixnum.\n");
}
copts->indent = NUM2INT(v);
}
if (Qnil != (v = rb_hash_lookup(ropts, trace_sym))) {
if (rb_cInteger != rb_obj_class(v) && T_FIXNUM != rb_type(v)) {
rb_raise(ox_parse_error_class, ":trace must be a Fixnum.\n");
}
copts->trace = NUM2INT(v);
}
if (Qnil != (v = rb_hash_lookup(ropts, ox_encoding_sym))) {
if (rb_cString != rb_obj_class(v)) {
rb_raise(ox_parse_error_class, ":encoding must be a String.\n");
}
strncpy(copts->encoding, StringValuePtr(v), sizeof(copts->encoding) - 1);
}
if (Qnil != (v = rb_hash_lookup(ropts, no_empty_sym))) {
copts->no_empty = (v == Qtrue);
}
if (Qnil != (v = rb_hash_lookup(ropts, effort_sym))) {
if (auto_define_sym == v) {
copts->effort = AutoEffort;
} else if (tolerant_sym == v) {
copts->effort = TolerantEffort;
} else if (strict_sym == v) {
copts->effort = StrictEffort;
} else {
rb_raise(ox_parse_error_class, ":effort must be :strict, :tolerant, or :auto_define.\n");
}
}
v = rb_hash_lookup(ropts, invalid_replace_sym);
if (Qnil == v) {
if (Qtrue == rb_funcall(ropts, has_key_id, 1, invalid_replace_sym)) {
copts->allow_invalid = Yes;
}
} else {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(copts->inv_repl) - 2 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":invalid_replace can be no longer than %d characters.",
(int)sizeof(copts->inv_repl) - 2);
}
strncpy(copts->inv_repl + 1, StringValuePtr(v), sizeof(copts->inv_repl) - 1);
copts->inv_repl[sizeof(copts->inv_repl) - 1] = '\0';
*copts->inv_repl = (char)slen;
copts->allow_invalid = No;
}
v = rb_hash_lookup(ropts, margin_sym);
if (Qnil != v) {
long slen;
Check_Type(v, T_STRING);
slen = RSTRING_LEN(v);
if (sizeof(copts->margin) - 2 < (size_t)slen) {
rb_raise(ox_parse_error_class,
":margin can be no longer than %d characters.",
(int)sizeof(copts->margin) - 2);
}
strncpy(copts->margin, StringValuePtr(v), sizeof(copts->margin) - 1);
copts->margin[sizeof(copts->margin) - 1] = '\0';
copts->margin_len = (char)slen;
}
for (o = ynos; 0 != o->attr; o++) {
if (Qnil != (v = rb_hash_lookup(ropts, o->sym))) {
VALUE c = rb_obj_class(v);
if (rb_cTrueClass == c) {
*o->attr = Yes;
} else if (rb_cFalseClass == c) {
*o->attr = No;
} else {
rb_raise(ox_parse_error_class, "%s must be true or false.\n", rb_id2name(SYM2ID(o->sym)));
}
}
}
}
}
/* call-seq: dump(obj, options) => xml-string
*
* Dumps an Object (obj) to a string.
* - +obj+ [Object] Object to serialize as an XML document String
* - +options+ [Hash] formating options
* - *:indent* [Fixnum] format expected
* - *:no_empty* [true|false] if true don't output empty elements
* - *:xsd_date* [true|false] use XSD date format if true, default: false
* - *:circular* [true|false] allow circular references, default: false
* - *:strict|:tolerant]* [ :effort effort to use when an undumpable object (e.g., IO) is encountered, default:
* :strict
* - _:strict_ - raise an NotImplementedError if an undumpable object is encountered
* - _:tolerant_ - replaces undumplable objects with nil
* - *:with_dtd* [true|false|nil] include DTD in the dump
* - *:with_instruct* [true|false|nil] include instructions in the dump
* - *:with_xml* [true|false|nil] include XML prolog in the dump
*
* Note that an indent of less than zero will result in a tight one line output
* unless the text in the XML fields contain new line characters.
*/
static VALUE dump(int argc, VALUE *argv, VALUE self) {
char *xml;
struct _options copts = ox_default_options;
VALUE rstr;
if (2 == argc) {
parse_dump_options(argv[1], &copts);
}
if (0 == (xml = ox_write_obj_to_str(*argv, &copts))) {
rb_raise(rb_eNoMemError, "Not enough memory.\n");
}
rstr = rb_str_new2(xml);
if ('\0' != *copts.encoding) {
rb_enc_associate(rstr, rb_enc_find(copts.encoding));
}
xfree(xml);
return rstr;
}
/* call-seq: to_xml(obj, options) => xml-string
*
* Dumps an Object (obj) to a string.
* - +obj+ [Object] Object to serialize as an XML document String
* - +options+ [Hash] formating options
* - *:indent* [Fixnum] format expected
* - *:no_empty* [true|false] if true don't output empty elements
* - *:xsd_date* [true|false] use XSD date format if true, default: false
* - *:circular* [true|false] allow circular references, default: false
* - *:strict|:tolerant]* [ :effort effort to use when an undumpable object (e.g., IO) is encountered, default:
* :strict
* - _:strict_ - raise an NotImplementedError if an undumpable object is encountered
* - _:tolerant_ - replaces undumplable objects with nil
* - *:with_dtd* [true|false|nil] include DTD in the dump
* - *:with_instruct* [true|false|nil] include instructions in the dump
* - *:with_xml* [true|false|nil] include XML prolog in the dump
*
* Note that an indent of less than zero will result in a tight one line output
* unless the text in the XML fields contain new line characters.
*/
static VALUE to_xml(int argc, VALUE *argv, VALUE self) {
return dump(argc, argv, self);
}
/* call-seq: to_file(file_path, obj, options) => Object
*
* Dumps an Object to the specified file.
* - +file_path+ [String] file path to write the XML document to
* - +obj+ [Object] Object to serialize as an XML document String
* - +options+ [Hash] formating options
* - *:indent* [Fixnum] format expected
* - *:xsd_date* [true|false] use XSD date format if true, default: false
* - *:circular* [true|false] allow circular references, default: false
* - *:strict|:tolerant]* [ :effort effort to use when an undumpable object (e.g., IO) is encountered, default:
* :strict
* - _:strict_ - raise an NotImplementedError if an undumpable object is encountered
* - _:tolerant_ - replaces undumplable objects with nil
* - *:with_dtd* [true|false|nil] include DTD in the dump
* - *:with_instruct* [true|false|nil] include instructions in the dump
* - *:with_xml* [true|false|nil] include XML prolog in the dump
*
* Note that an indent of less than zero will result in a tight one line output
* unless the text in the XML fields contain new line characters.
*/
static VALUE to_file(int argc, VALUE *argv, VALUE self) {
struct _options copts = ox_default_options;
if (3 == argc) {
parse_dump_options(argv[2], &copts);
}
Check_Type(*argv, T_STRING);
ox_write_obj_to_file(argv[1], StringValuePtr(*argv), &copts);
return Qnil;
}
#if WITH_CACHE_TESTS
extern void ox_cache_test(void);
static VALUE cache_test(VALUE self) {
ox_cache_test();
return Qnil;
}
extern void ox_cache8_test(void);
static VALUE cache8_test(VALUE self) {
ox_cache8_test();
return Qnil;
}
#endif
void Init_ox(void) {
#if HAVE_RB_EXT_RACTOR_SAFE
rb_ext_ractor_safe(true);
#endif
Ox = rb_define_module("Ox");
rb_define_module_function(Ox, "default_options", get_def_opts, 0);
rb_define_module_function(Ox, "default_options=", set_def_opts, 1);
rb_define_module_function(Ox, "parse_obj", to_obj, 1);
rb_define_module_function(Ox, "parse", to_gen, 1);
rb_define_module_function(Ox, "load", load_str, -1);
rb_define_module_function(Ox, "sax_parse", sax_parse, -1);
rb_define_module_function(Ox, "sax_html", sax_html, -1);
rb_define_module_function(Ox, "to_xml", to_xml, -1);
rb_define_module_function(Ox, "dump", dump, -1);
rb_define_module_function(Ox, "load_file", load_file, -1);
rb_define_module_function(Ox, "to_file", to_file, -1);
rb_define_module_function(Ox, "sax_html_overlay", sax_html_overlay, 0);
ox_init_builder(Ox);
rb_require("time");
rb_require("date");
// rb_require("bigdecimal");
rb_require("stringio");
ox_abort_id = rb_intern("abort");
ox_at_column_id = rb_intern("@column");
ox_at_content_id = rb_intern("@content");
ox_at_id = rb_intern("at");
ox_at_line_id = rb_intern("@line");
ox_at_pos_id = rb_intern("@pos");
ox_at_value_id = rb_intern("@value");
ox_attr_id = rb_intern("attr");
ox_attr_value_id = rb_intern("attr_value");
ox_attributes_id = rb_intern("@attributes");
ox_attrs_done_id = rb_intern("attrs_done");
ox_beg_id = rb_intern("@beg");
ox_bigdecimal_id = rb_intern("BigDecimal");
ox_call_id = rb_intern("call");
ox_cdata_id = rb_intern("cdata");
ox_comment_id = rb_intern("comment");
ox_den_id = rb_intern("@den");
ox_doctype_id = rb_intern("doctype");
ox_end_element_id = rb_intern("end_element");
ox_end_id = rb_intern("@end");
ox_end_instruct_id = rb_intern("end_instruct");
ox_error_id = rb_intern("error");
ox_excl_id = rb_intern("@excl");
ox_external_encoding_id = rb_intern("external_encoding");
ox_fileno_id = rb_intern("fileno");
ox_force_encoding_id = rb_intern("force_encoding");
ox_inspect_id = rb_intern("inspect");
ox_instruct_id = rb_intern("instruct");
ox_jd_id = rb_intern("jd");
ox_keys_id = rb_intern("keys");
ox_local_id = rb_intern("local");
ox_mesg_id = rb_intern("mesg");
ox_message_id = rb_intern("message");
ox_nodes_id = rb_intern("@nodes");
ox_new_id = rb_intern("new");
ox_num_id = rb_intern("@num");
ox_parse_id = rb_intern("parse");
ox_pos_id = rb_intern("pos");
ox_read_id = rb_intern("read");
ox_readpartial_id = rb_intern("readpartial");
ox_start_element_id = rb_intern("start_element");
ox_string_id = rb_intern("string");
ox_text_id = rb_intern("text");
ox_to_c_id = rb_intern("to_c");
ox_value_id = rb_intern("value");
encoding_id = rb_intern("encoding");
has_key_id = rb_intern("has_key?");
rb_require("ox/version");
rb_require("ox/error");
rb_require("ox/hasattrs");
rb_require("ox/node");
rb_require("ox/comment");
rb_require("ox/instruct");
rb_require("ox/cdata");
rb_require("ox/doctype");
rb_require("ox/element");
rb_require("ox/document");
rb_require("ox/bag");
rb_require("ox/sax");
ox_time_class = rb_const_get(rb_cObject, rb_intern("Time"));
ox_date_class = rb_const_get(rb_cObject, rb_intern("Date"));
ox_parse_error_class = rb_const_get_at(Ox, rb_intern("ParseError"));
ox_syntax_error_class = rb_const_get_at(Ox, rb_intern("SyntaxError"));
ox_arg_error_class = rb_const_get_at(Ox, rb_intern("ArgError"));
ox_struct_class = rb_const_get(rb_cObject, rb_intern("Struct"));
ox_stringio_class = rb_const_get(rb_cObject, rb_intern("StringIO"));
abort_sym = ID2SYM(rb_intern("abort"));
rb_gc_register_address(&abort_sym);
active_sym = ID2SYM(rb_intern("active"));
rb_gc_register_address(&active_sym);
attr_key_mod_sym = ID2SYM(rb_intern("attr_key_mod"));
rb_gc_register_address(&attr_key_mod_sym);
auto_define_sym = ID2SYM(rb_intern("auto_define"));
rb_gc_register_address(&auto_define_sym);
auto_sym = ID2SYM(rb_intern("auto"));
rb_gc_register_address(&auto_sym);
block_sym = ID2SYM(rb_intern("block"));
rb_gc_register_address(&block_sym);
circular_sym = ID2SYM(rb_intern("circular"));
rb_gc_register_address(&circular_sym);
convert_special_sym = ID2SYM(rb_intern("convert_special"));
rb_gc_register_address(&convert_special_sym);
effort_sym = ID2SYM(rb_intern("effort"));
rb_gc_register_address(&effort_sym);
element_key_mod_sym = ID2SYM(rb_intern("element_key_mod"));
rb_gc_register_address(&element_key_mod_sym);
generic_sym = ID2SYM(rb_intern("generic"));
rb_gc_register_address(&generic_sym);
hash_no_attrs_sym = ID2SYM(rb_intern("hash_no_attrs"));
rb_gc_register_address(&hash_no_attrs_sym);
hash_sym = ID2SYM(rb_intern("hash"));
rb_gc_register_address(&hash_sym);
inactive_sym = ID2SYM(rb_intern("inactive"));
rb_gc_register_address(&inactive_sym);
invalid_replace_sym = ID2SYM(rb_intern("invalid_replace"));
rb_gc_register_address(&invalid_replace_sym);
limited_sym = ID2SYM(rb_intern("limited"));
rb_gc_register_address(&limited_sym);
margin_sym = ID2SYM(rb_intern("margin"));
rb_gc_register_address(&margin_sym);
mode_sym = ID2SYM(rb_intern("mode"));
rb_gc_register_address(&mode_sym);
nest_ok_sym = ID2SYM(rb_intern("nest_ok"));
rb_gc_register_address(&nest_ok_sym);
no_empty_sym = ID2SYM(rb_intern("no_empty"));
rb_gc_register_address(&no_empty_sym);
object_sym = ID2SYM(rb_intern("object"));
rb_gc_register_address(&object_sym);
off_sym = ID2SYM(rb_intern("off"));
rb_gc_register_address(&off_sym);
opt_format_sym = ID2SYM(rb_intern("opt_format"));
rb_gc_register_address(&opt_format_sym);
optimized_sym = ID2SYM(rb_intern("optimized"));
rb_gc_register_address(&optimized_sym);
overlay_sym = ID2SYM(rb_intern("overlay"));
rb_gc_register_address(&overlay_sym);
ox_encoding_sym = ID2SYM(rb_intern("encoding"));
rb_gc_register_address(&ox_encoding_sym);
ox_indent_sym = ID2SYM(rb_intern("indent"));
rb_gc_register_address(&ox_indent_sym);
ox_size_sym = ID2SYM(rb_intern("size"));
rb_gc_register_address(&ox_size_sym);
ox_standalone_sym = ID2SYM(rb_intern("standalone"));
rb_gc_register_address(&ox_standalone_sym);
ox_version_sym = ID2SYM(rb_intern("version"));
rb_gc_register_address(&ox_version_sym);
skip_none_sym = ID2SYM(rb_intern("skip_none"));
rb_gc_register_address(&skip_none_sym);
skip_off_sym = ID2SYM(rb_intern("skip_off"));
rb_gc_register_address(&skip_off_sym);
skip_return_sym = ID2SYM(rb_intern("skip_return"));
rb_gc_register_address(&skip_return_sym);
skip_sym = ID2SYM(rb_intern("skip"));
rb_gc_register_address(&skip_sym);
skip_white_sym = ID2SYM(rb_intern("skip_white"));
rb_gc_register_address(&skip_white_sym);
smart_sym = ID2SYM(rb_intern("smart"));
rb_gc_register_address(&smart_sym);
strict_sym = ID2SYM(rb_intern("strict"));
rb_gc_register_address(&strict_sym);
strip_namespace_sym = ID2SYM(rb_intern("strip_namespace"));
rb_gc_register_address(&strip_namespace_sym);
symbolize_keys_sym = ID2SYM(rb_intern("symbolize_keys"));
rb_gc_register_address(&symbolize_keys_sym);
symbolize_sym = ID2SYM(rb_intern("symbolize"));
rb_gc_register_address(&symbolize_sym);
tolerant_sym = ID2SYM(rb_intern("tolerant"));
rb_gc_register_address(&tolerant_sym);
trace_sym = ID2SYM(rb_intern("trace"));
rb_gc_register_address(&trace_sym);
with_cdata_sym = ID2SYM(rb_intern("with_cdata"));
rb_gc_register_address(&with_cdata_sym);
with_dtd_sym = ID2SYM(rb_intern("with_dtd"));
rb_gc_register_address(&with_dtd_sym);
with_instruct_sym = ID2SYM(rb_intern("with_instructions"));
rb_gc_register_address(&with_instruct_sym);
with_xml_sym = ID2SYM(rb_intern("with_xml"));
rb_gc_register_address(&with_xml_sym);
xsd_date_sym = ID2SYM(rb_intern("xsd_date"));
rb_gc_register_address(&xsd_date_sym);
ox_empty_string = rb_str_new2("");
rb_gc_register_address(&ox_empty_string);
ox_zero_fixnum = INT2NUM(0);
rb_gc_register_address(&ox_zero_fixnum);
ox_sym_bank = rb_ary_new();
rb_gc_register_address(&ox_sym_bank);
ox_document_clas = rb_const_get_at(Ox, rb_intern("Document"));
ox_element_clas = rb_const_get_at(Ox, rb_intern("Element"));
ox_instruct_clas = rb_const_get_at(Ox, rb_intern("Instruct"));
ox_comment_clas = rb_const_get_at(Ox, rb_intern("Comment"));
ox_raw_clas = rb_const_get_at(Ox, rb_intern("Raw"));
ox_doctype_clas = rb_const_get_at(Ox, rb_intern("DocType"));
ox_cdata_clas = rb_const_get_at(Ox, rb_intern("CData"));
ox_bag_clas = rb_const_get_at(Ox, rb_intern("Bag"));
// Classes can move in more recent versions so register them all.
rb_gc_register_address(&Ox);
rb_gc_register_address(&ox_arg_error_class);
rb_gc_register_address(&ox_bag_clas);
rb_gc_register_address(&ox_bag_clas);
rb_gc_register_address(&ox_cdata_clas);
rb_gc_register_address(&ox_cdata_clas);
rb_gc_register_address(&ox_comment_clas);
rb_gc_register_address(&ox_comment_clas);
rb_gc_register_address(&ox_date_class);
rb_gc_register_address(&ox_doctype_clas);
rb_gc_register_address(&ox_doctype_clas);
rb_gc_register_address(&ox_document_clas);
rb_gc_register_address(&ox_document_clas);
rb_gc_register_address(&ox_element_clas);
rb_gc_register_address(&ox_element_clas);
rb_gc_register_address(&ox_encoding_sym);
rb_gc_register_address(&ox_indent_sym);
rb_gc_register_address(&ox_instruct_clas);
rb_gc_register_address(&ox_instruct_clas);
rb_gc_register_address(&ox_parse_error_class);
rb_gc_register_address(&ox_raw_clas);
rb_gc_register_address(&ox_raw_clas);
rb_gc_register_address(&ox_size_sym);
rb_gc_register_address(&ox_standalone_sym);
rb_gc_register_address(&ox_stringio_class);
rb_gc_register_address(&ox_struct_class);
rb_gc_register_address(&ox_syntax_error_class);
rb_gc_register_address(&ox_time_class);
rb_gc_register_address(&ox_version_sym);
slot_cache_new(&ox_class_cache);
ox_sax_define();
ox_hash_init();
#if WITH_CACHE_TESTS
// space added to stop yardoc from trying to document
rb_define _module_function(Ox, "cache_test", cache_test, 0);
rb_define _module_function(Ox, "cache8_test", cache8_test, 0);
#endif
ox_utf8_encoding = rb_enc_find("UTF-8");
}
#if __GNUC__ > 4
_Noreturn void
#else
void
#endif
_ox_raise_error(const char *msg, const char *xml, const char *current, const char *file, int line) {
int xline = 1;
int col = 1;
for (; xml < current && '\n' != *current; current--) {
col++;
}
for (; xml < current; current--) {
if ('\n' == *current) {
xline++;
}
}
rb_gc_enable();
rb_raise(ox_parse_error_class, "%s at line %d, column %d [%s:%d]\n", msg, xline, col, file, line);
}
|