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

Constant Summary collapse

VERSION =

Current version of the module.

'2.8.2'

Class Method Summary collapse

Class Method Details

.default_optionsHash

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

  • :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 &lt; 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.

  • :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.

Returns:

  • (Hash)


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
# File 'ext/ox/ox.c', line 300

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, 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);
    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

    • :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]



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
# File 'ext/ox/ox.c', line 432

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 HAS_ENCODING_SUPPORT
	ox_default_options.rb_enc = rb_enc_find(ox_default_options.encoding);
#elif HAS_PRIVATE_ENCODING
	ox_default_options.rb_enc = rb_str_new2(ox_default_options.encoding);
	rb_gc_register_address(&ox_default_options.rb_enc);
#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_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);
	}
    }
    return Qnil;
}

.dumpObject

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

Returns:



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
# File 'ext/ox/ox.c', line 894

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 HAS_ENCODING_SUPPORT
#ifdef MACRUBY_RUBY
    encoding = rb_funcall(*argv, encoding_id, 0);
#else
    encoding = rb_obj_encoding(*argv);
#endif
#elif HAS_PRIVATE_ENCODING
    encoding = rb_funcall(*argv, encoding_id, 0);
#else
    encoding = Qnil;
#endif
    memcpy(xml, StringValuePtr(*argv), len);
    obj = load(xml, 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.

Returns:



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
# File 'ext/ox/ox.c', line 955

static VALUE
load_file(int argc, VALUE *argv, VALUE self) {
    char	*path;
    char	*xml;
    FILE	*f;
    size_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 = ftell(f);
    if (SMALL_XML < len) {
	xml = ALLOC_N(char, len + 1);
    } else {
	xml = ALLOCA_N(char, len + 1);
    }
    fseek(f, 0, SEEK_SET);
    if (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, 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.



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
# File 'ext/ox/ox.c', line 670

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, 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.

Returns:

  • (Object)


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
# File 'ext/ox/ox.c', line 626

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);
#if HAS_GC_GUARD
    rb_gc_disable();
#endif
    obj = ox_parse(xml, ox_obj_callbacks, 0, &options, &err);
    if (SMALL_XML < len) {
	xfree(xml);
    }
#if HAS_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 &lt; 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



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
# File 'ext/ox/ox.c', line 1089

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_overlayHash

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

Returns:

  • (Hash)


397
398
399
400
# File 'ext/ox/ox.c', line 397

static VALUE
sax_html_overlay(VALUE self) {
    return hints_to_overlay(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 &lt; 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.



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
# File 'ext/ox/ox.c', line 1008

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;
}

.to_fileObject

.to_xmlObject