Module: Ox
- Defined in:
- lib/ox.rb,
lib/ox/bag.rb,
lib/ox/raw.rb,
lib/ox/sax.rb,
lib/ox/node.rb,
lib/ox/cdata.rb,
lib/ox/error.rb,
lib/ox/comment.rb,
lib/ox/doctype.rb,
lib/ox/element.rb,
lib/ox/version.rb,
lib/ox/document.rb,
lib/ox/hasattrs.rb,
lib/ox/instruct.rb,
lib/ox/xmlrpc_adapter.rb,
ext/ox/ox.c,
ext/ox/sax_as.c
Overview
Description:
Ox handles XML documents in two ways. It is a generic XML parser and writer as well as a fast Object / XML marshaller. Ox was written for speed as a replacement for Nokogiri and for Marshal.
As an XML parser it is 2 or more times faster than Nokogiri and as a generic XML writer it is 14 times faster than Nokogiri. Of course different files may result in slightly different times.
As an Object serializer Ox is 4 times faster than the standard Ruby Marshal.dump(). Ox is 3 times faster than Marshal.load().
Object Dump Sample:
require 'ox'
class Sample
attr_accessor :a, :b, :c
def initialize(a, b, c)
@a = a
@b = b
@c = c
end
end
# Create Object
obj = Sample.new(1, "bee", ['x', :y, 7.0])
# Now dump the Object to an XML String.
xml = Ox.dump(obj)
# Convert the object back into a Sample Object.
obj2 = Ox.parse_obj(xml)
Generic XML Writing and Parsing:
require 'ox'
doc = Ox::Document.new(:version => '1.0')
top = Ox::Element.new('top')
top[:name] = 'sample'
doc << top
mid = Ox::Element.new('middle')
mid[:name] = 'second'
top << mid
bot = Ox::Element.new('bottom')
bot[:name] = 'third'
mid << bot
xml = Ox.dump(doc)
puts xml
doc2 = Ox.parse(xml)
puts "Same? #{doc == doc2}"
Defined Under Namespace
Modules: HasAttrs Classes: ArgError, Bag, Builder, CData, Comment, DocType, Document, Element, Error, Instruct, InvalidPath, Node, ParseError, Raw, Sax, StreamParser, SyntaxError
Constant Summary collapse
- VERSION =
Current version of the module.
'2.13.4'
Class Method Summary collapse
-
.default_options ⇒ Hash
Returns the default load and dump options as a Hash.
-
.default_options=(opts) ⇒ Object
Sets the default options for load and dump.
- .dump ⇒ Object
-
.load(xml, options) ⇒ Ox::Document, ...
Parses and XML document String into an Ox::Document, or Ox::Element, or Object depending on the options.
-
.load_file(file_path, options) ⇒ Ox::Document, ...
Parses and XML document from a file into an Ox::Document, or Ox::Element, or Object depending on the options.
-
.parse(xml) ⇒ Ox::Document, Ox::Element
Parses and XML document String into an Ox::Document or Ox::Element.
-
.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.
-
.sax_html(handler, io, options) ⇒ Object
Parses an IO stream or file containing an XML document.
-
.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.
-
.sax_parse(handler, io, options) ⇒ Object
Parses an IO stream or file containing an XML document.
- .to_file ⇒ Object
- .to_xml ⇒ Object
Class Method Details
.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
-
: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.
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 |
# File 'ext/ox/ox.c', line 307
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);
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;
}
|
.default_options=(opts) ⇒ Object
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.
-
-
: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]
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 |
# File 'ext/ox/ox.c', line 444
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);
#if HAVE_RB_ENC_FIND
ox_default_options.rb_enc = rb_enc_find(ox_default_options.encoding);
#endif
}
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);
}
}
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;
}
|
.dump ⇒ Object
.load(xml, options) ⇒ Ox::Document, ...
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.
-
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 |
# File 'ext/ox/ox.c', line 912
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);
}
#if HAVE_RB_OBJ_ENCODING
encoding = rb_obj_encoding(*argv);
#else
encoding = Qnil;
#endif
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;
}
|
.load_file(file_path, options) ⇒ Ox::Document, ...
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.
-
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 |
# File 'ext/ox/ox.c', line 968
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;
}
|
.parse(xml) ⇒ Ox::Document, 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.
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 |
# File 'ext/ox/ox.c', line 693
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;
}
|
.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.
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 |
# File 'ext/ox/ox.c', line 649
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);
#ifdef RB_GC_GUARD
rb_gc_disable();
#endif
obj = ox_parse(xml, len - 1, ox_obj_callbacks, 0, &options, &err);
if (SMALL_XML < len) {
xfree(xml);
}
#ifdef RB_GC_GUARD
RB_GC_GUARD(obj);
rb_gc_enable();
#endif
if (err_has(&err)) {
ox_err_raise(&err);
}
return obj;
}
|
.sax_html(handler, io, options) ⇒ Object
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
-
-
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 |
# File 'ext/ox/ox.c', line 1102
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;
}
|
.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
407 408 409 410 |
# File 'ext/ox/ox.c', line 407 static VALUE (VALUE self) { return (ox_hints_html()); } |
.sax_parse(handler, io, options) ⇒ Object
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.
-
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 |
# File 'ext/ox/ox.c', line 1021
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;
}
|