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,
ext/ox/builder.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, Cache, Comment, DocType, Document, Element, Error, Instruct, InvalidPath, Node, ParseError, Raw, Sax, StreamParser, SyntaxError

Constant Summary collapse

VERSION =

Current version of the module.

'2.14.29'

Class Method Summary collapse

Class Method Details

.default_optionsObject



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

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;
    }
    switch (ox_default_options.allow_invalid) {
    case Yes: rb_hash_aset(opts, invalid_replace_sym, Qnil); break;
    case No:
        rb_hash_aset(opts,
                     invalid_replace_sym,
                     rb_str_new(ox_default_options.inv_repl + 1, (int)*ox_default_options.inv_repl));
        break;
    default: rb_hash_aset(opts, invalid_replace_sym, Qfalse); break;
    }
    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



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

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.parse_obj and Ox.parse read rb_enc and never re-derive it from the
        // name, so clearing only the name leaves them on the old encoding.
        *ox_default_options.encoding = '\0';
        ox_default_options.rb_enc    = 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) {
        int indent;

        Check_Type(v, T_FIXNUM);
        indent = FIX2INT(v);
        // Bound the indent so that depth * indent (depth up to MAX_DEPTH) cannot
        // overflow the int arithmetic in dump.c and smash the output buffer.
        // Any negative value already means "tight, no newlines"; normalize it.
        if (indent < 0) {
            indent = -1;
        } else if (OX_MAX_INDENT < indent) {
            rb_raise(ox_parse_error_class, ":indent can be no larger than %d.\n", OX_MAX_INDENT);
        }
        ox_default_options.indent = indent;
    }

    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_lookup(opts, invalid_replace_sym);
    if (Qnil == v) {
        // A key that is not there asks for the default, an explicit nil asks for
        // the character to be written out, so the two cannot share a branch.
        if (Qtrue == rb_funcall(opts, has_key_id, 1, invalid_replace_sym)) {
            ox_default_options.allow_invalid = Yes;
        } else {
            ox_default_options.allow_invalid = NotSet;
            *ox_default_options.inv_repl     = '\0';
        }
    } else if (Qfalse == v) {
        ox_default_options.allow_invalid = NotSet;
        *ox_default_options.inv_repl     = '\0';
    } 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;
}

.dump(*args) ⇒ Object



1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
# File 'ext/ox/ox.c', line 1418

static VALUE dump(int argc, VALUE *argv, VALUE self) {
    char           *xml;
    struct _options copts = ox_default_options;
    VALUE           rstr;

    if (1 > argc) {
        rb_raise(ox_arg_error_class, "missing object to dump");
    }
    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;
}

.load(*args) ⇒ Object



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

static VALUE load_str(int argc, VALUE *argv, VALUE self) {
    char       *xml;
    size_t      len;
    VALUE       obj;
    VALUE       encoding;
    struct _err err;

    if (1 > argc) {
        rb_raise(ox_arg_error_class, "missing XML string");
    }
    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;
}

.load_file(*args) ⇒ Object



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

static VALUE load_file(int argc, VALUE *argv, VALUE self) {
    char       *path;
    char       *xml;
    FILE       *f;
    off_t       len;
    struct stat st;
    VALUE       obj;
    struct _err err;

    if (1 > argc) {
        rb_raise(ox_arg_error_class, "missing file path");
    }
    err_init(&err);
    Check_Type(*argv, T_STRING);
    path = StringValuePtr(*argv);
    if (0 == (f = fopen(path, "rb"))) {
        rb_raise(rb_eIOError, "%s\n", strerror(errno));
    }
    // A stream that can not seek leaves len at -1, which allocates nothing and
    // then asks fread for SIZE_MAX bytes.
    // Only a regular file has a size worth sizing a read from. A FIFO reports
    // -1, and a directory can report anything at all, including off_t's
    // maximum.
    if (0 != fstat(fileno(f), &st) || !S_ISREG(st.st_mode)) {
        fclose(f);
        rb_raise(rb_eIOError, "%s is not a regular file.\n", path);
    }
    len = st.st_size;
    if (SMALL_XML < len) {
        xml = ALLOC_N(char, (size_t)len + 1);
    } else {
        xml = ALLOCA_N(char, (size_t)len + 1);
    }
    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(ruby_xml) ⇒ Object



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

static VALUE to_gen(VALUE self, VALUE ruby_xml) {
    char           *xml, *x, *str;
    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 */
    str = StringValuePtr(ruby_xml);
    len = RSTRING_LEN(ruby_xml) + 1;
    x   = defuse_bom(str, &options);
    // Drop the BOM from len as well, otherwise the copy below reads past the
    // end of the Ruby string and xml is left without a terminating '\0'.
    len -= (size_t)(x - str);
    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(ruby_xml) ⇒ Object



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

static VALUE to_obj(VALUE self, VALUE ruby_xml) {
    char            *xml, *x, *str;
    size_t           len;
    VALUE            obj;
    VALUE            was_disabled;
    struct _options  options = ox_default_options;
    struct _err      err;
    struct _objParse args;

    err_init(&err);
    Check_Type(ruby_xml, T_STRING);
    /* the xml string gets modified so make a copy of it */
    str = StringValuePtr(ruby_xml);
    len = RSTRING_LEN(ruby_xml) + 1;
    x   = defuse_bom(str, &options);
    // Drop the BOM from len as well, otherwise the copy below reads past the
    // end of the Ruby string and xml is left without a terminating '\0'.
    len -= (size_t)(x - str);
    if (SMALL_XML < len) {
        xml = ALLOC_N(char, len);
    } else {
        xml = ALLOCA_N(char, len);
    }
    memcpy(xml, x, len);
    args.xml     = xml;
    args.len     = len - 1;
    args.options = &options;
    args.err     = &err;
    was_disabled = rb_gc_disable();
    obj          = rb_ensure(obj_parse_body, (VALUE)&args, obj_parse_reenable, was_disabled);
    if (SMALL_XML < len) {
        xfree(xml);
    }
    RB_GC_GUARD(obj);
    if (err_has(&err)) {
        ox_err_raise(&err);
    }
    return obj;
}

.sax_html(*args) ⇒ Object



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

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_overlayObject



435
436
437
# File 'ext/ox/ox.c', line 435

static VALUE sax_html_overlay(VALUE self) {
    return hints_to_overlay(ox_hints_html());
}

.sax_parse(*args) ⇒ Object



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

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_file(*args) ⇒ Object



1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
# File 'ext/ox/ox.c', line 1485

static VALUE to_file(int argc, VALUE *argv, VALUE self) {
    struct _options copts = ox_default_options;

    if (2 > argc) {
        rb_raise(ox_arg_error_class, "missing file path or object to write");
    }
    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;
}

.to_xml(*args) ⇒ Object



1461
1462
1463
# File 'ext/ox/ox.c', line 1461

static VALUE to_xml(int argc, VALUE *argv, VALUE self) {
    return dump(argc, argv, self);
}