Class: Ox::Builder

Inherits:
Object
  • Object
show all
Defined in:
ext/ox/builder.c,
ext/ox/builder.c

Overview

An XML builder.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.file(*args) ⇒ Object



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

static VALUE builder_file(int argc, VALUE *argv, VALUE self) {
    Builder b        = ALLOC(struct _builder);
    int     indent   = ox_default_options.indent;
    long    buf_size = 0;
    FILE   *f;

    if (1 > argc) {
        rb_raise(ox_arg_error_class, "missing filename");
    }
    Check_Type(*argv, T_STRING);
    if (NULL == (f = fopen(StringValuePtr(*argv), "wb"))) {
        xfree(b);
        rb_raise(rb_eIOError, "%s\n", strerror(errno));
    }
    if (2 == argc) {
        volatile VALUE v;

        rb_check_type(argv[1], T_HASH);
        if (Qnil != (v = rb_hash_lookup(argv[1], ox_indent_sym))) {
            if (rb_cInteger != rb_obj_class(v)) {
                rb_raise(ox_parse_error_class, ":indent must be a fixnum.\n");
            }
            indent = NUM2INT(v);
        }
        if (Qnil != (v = rb_hash_lookup(argv[1], ox_size_sym))) {
            if (rb_cInteger != rb_obj_class(v)) {
                rb_raise(ox_parse_error_class, ":size must be a fixnum.\n");
            }
            buf_size = NUM2LONG(v);
        }
    }
    b->file = f;
    init(b, fileno(f), indent, buf_size);

    if (rb_block_given_p()) {
        volatile VALUE rb = TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
        rb_yield(rb);
        bclose(b);
        return Qnil;
    } else {
        return TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
    }
}

.io(*args) ⇒ Object



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

static VALUE builder_io(int argc, VALUE *argv, VALUE self) {
    Builder        b        = ALLOC(struct _builder);
    int            indent   = ox_default_options.indent;
    long           buf_size = 0;
    int            fd;
    volatile VALUE v;

    if (1 > argc) {
        rb_raise(ox_arg_error_class, "missing IO object");
    }
    if (!rb_respond_to(*argv, ox_fileno_id) || Qnil == (v = rb_funcall(*argv, ox_fileno_id, 0)) ||
        0 == (fd = FIX2INT(v))) {
        rb_raise(rb_eIOError, "expected an IO that has a fileno.");
    }
    if (2 == argc) {
        volatile VALUE v;

        rb_check_type(argv[1], T_HASH);
        if (Qnil != (v = rb_hash_lookup(argv[1], ox_indent_sym))) {
            if (rb_cInteger != rb_obj_class(v)) {
                rb_raise(ox_parse_error_class, ":indent must be a fixnum.\n");
            }
            indent = NUM2INT(v);
        }
        if (Qnil != (v = rb_hash_lookup(argv[1], ox_size_sym))) {
            if (rb_cInteger != rb_obj_class(v)) {
                rb_raise(ox_parse_error_class, ":size must be a fixnum.\n");
            }
            buf_size = NUM2LONG(v);
        }
    }
    b->file = NULL;
    init(b, fd, indent, buf_size);

    if (rb_block_given_p()) {
        volatile VALUE rb = TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
        rb_yield(rb);
        bclose(b);
        return Qnil;
    } else {
        return TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
    }
}

.new(*args) ⇒ Object



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

static VALUE builder_new(int argc, VALUE *argv, VALUE self) {
    Builder b        = ALLOC(struct _builder);
    int     indent   = ox_default_options.indent;
    long    buf_size = 0;

    if (1 == argc) {
        volatile VALUE v;

        rb_check_type(*argv, T_HASH);
        if (Qnil != (v = rb_hash_lookup(*argv, ox_indent_sym))) {
            if (rb_cInteger != rb_obj_class(v)) {
                rb_raise(ox_parse_error_class, ":indent must be a fixnum.\n");
            }
            indent = NUM2INT(v);
        }
        if (Qnil != (v = rb_hash_lookup(*argv, ox_size_sym))) {
            if (rb_cInteger != rb_obj_class(v)) {
                rb_raise(ox_parse_error_class, ":size must be a fixnum.\n");
            }
            buf_size = NUM2LONG(v);
        }
    }
    b->file = NULL;
    init(b, 0, indent, buf_size);

    if (rb_block_given_p()) {
        volatile VALUE rb = TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);

        rb_yield(rb);
        bclose(b);

        return to_s(b);
    } else {
        return TypedData_Wrap_Struct(builder_class, &ox_builder_type, b);
    }
}

Instance Method Details

#cdata(data) ⇒ Object



863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
# File 'ext/ox/builder.c', line 863

static VALUE builder_cdata(VALUE self, VALUE data) {
    Builder        b;
    volatile VALUE v = data;
    const char    *str;
    const char    *s;
    const char    *end;
    const char    *from;
    const char    *split;
    size_t         extra = 0;
    int            len;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

    v    = rb_String(v);
    str  = StringValuePtr(v);
    len  = (int)RSTRING_LEN(v);
    s    = str;
    end  = str + len;
    from = str;
    check_unescaped(str, (size_t)len);
    i_am_a_child(b, false);
    append_indent(b);
    buf_append_string(&b->buf, "<![CDATA[", 9);
    b->col += 9;
    b->pos += 9;
    // See xml_cdata_end(). The ']]' stays in this section and the '>' opens the
    // next one, so from lands on the '>' each time round.
    while (NULL != (split = xml_cdata_end(from, end))) {
        buf_append_string(&b->buf, from, (split + 2) - from);
        buf_append_string(&b->buf, "]]><![CDATA[", CDATA_SPLIT_EXTRA);
        extra += CDATA_SPLIT_EXTRA;
        from = split + 2;
    }
    buf_append_string(&b->buf, from, end - from);
    b->col += len + extra;
    s = strchr(s, '\n');
    while (NULL != s) {
        b->line++;
        b->col = end - s;
        s      = strchr(s + 1, '\n');
    }
    b->pos += len + extra;
    buf_append_string(&b->buf, "]]>", 3);
    b->col += 3;
    b->pos += 3;

    return Qnil;
}

#closeObject



1037
1038
1039
1040
1041
1042
1043
1044
# File 'ext/ox/builder.c', line 1037

static VALUE builder_close(VALUE self) {
    Builder b;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    bclose(b);

    return Qnil;
}

#columnObject



973
974
975
976
977
978
# File 'ext/ox/builder.c', line 973

static VALUE builder_column(VALUE self) {
    Builder b;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    return LONG2NUM(b->col);
}

#comment(text) ⇒ Object



772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
# File 'ext/ox/builder.c', line 772

static VALUE builder_comment(VALUE self, VALUE text) {
    Builder b;
    size_t  size;
    size_t  xsize;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    rb_check_type(text, T_STRING);
    size  = (size_t)RSTRING_LEN(text);
    xsize = check_string(StringValuePtr(text), size, xml_element_chars);
    i_am_a_child(b, false);
    append_indent(b);
    buf_append_string(&b->buf, "<!--", 4);
    b->col += 4;
    b->pos += 4;
    append_string(b, StringValuePtr(text), size, xml_element_chars, xsize, false);
    buf_append_string(&b->buf, "-->", 3);
    b->col += 3;
    b->pos += 3;

    return Qnil;
}

#doctype(text) ⇒ Object



799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# File 'ext/ox/builder.c', line 799

static VALUE builder_doctype(VALUE self, VALUE text) {
    Builder b;
    size_t  size;
    size_t  xsize;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    rb_check_type(text, T_STRING);
    size  = (size_t)RSTRING_LEN(text);
    xsize = check_string(StringValuePtr(text), size, xml_element_chars);
    i_am_a_child(b, false);
    append_indent(b);
    buf_append_string(&b->buf, "<!DOCTYPE ", 10);
    b->col += 10;
    b->pos += 10;
    append_string(b, StringValuePtr(text), size, xml_element_chars, xsize, false);
    buf_append(&b->buf, '>');
    b->col++;
    b->pos++;

    return Qnil;
}

#element(*args) ⇒ Object



678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
# File 'ext/ox/builder.c', line 678

static VALUE builder_element(int argc, VALUE *argv, VALUE self) {
    Builder        b;
    Element        e;
    volatile VALUE sym   = Qnil;
    size_t         xsize = 0;
    const char    *name;
    long           len;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

    if (1 > argc) {
        rb_raise(ox_arg_error_class, "missing element name");
    }
    // Everything that can raise has to run before the first write, or the raise
    // leaves an element started that no later call can finish or take back. It
    // also has to run before b->depth++, or the stack slot is never filled in.
    len = check_name(*argv, &sym, &name, &xsize, "expected a Symbol or String for an element name");
    check_name_chars(name, (size_t)len, false);
    if (MAX_DEPTH <= b->depth + 1) {
        rb_raise(ox_arg_error_class, "XML too deeply nested");
    }
    i_am_a_child(b, false);
    append_indent(b);
    b->depth++;
    e = &b->stack[b->depth];
    if (sizeof(e->buf) <= (size_t)len) {
        e->name = strdup(name);
        *e->buf = '\0';
    } else {
        strcpy(e->buf, name);
        e->name = e->buf;
    }
    e->len            = len;
    e->has_child      = false;
    e->non_text_child = false;

    buf_append(&b->buf, '<');
    b->col++;
    b->pos++;
    append_string(b, e->name, len, xml_element_chars, xsize, false);
    if (1 < argc && T_HASH == rb_type(argv[1])) {
        rb_hash_foreach(argv[1], append_attr, (VALUE)b);
    }
    // Do not close with > or /> yet. That is done with i_am_a_child() or pop().
    if (rb_block_given_p()) {
        rb_yield(self);
        pop(b);
    }
    return Qnil;
}

#indentObject



984
985
986
987
988
989
# File 'ext/ox/builder.c', line 984

static VALUE builder_get_indent(VALUE self) {
    Builder b;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    return INT2NUM(b->indent);
}

#indent=(indent) ⇒ Object



997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
# File 'ext/ox/builder.c', line 997

static VALUE builder_set_indent(VALUE self, VALUE indent) {
    Builder b;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

    if (rb_cInteger != rb_obj_class(indent)) {
        rb_raise(ox_parse_error_class, "indent must be a fixnum.\n");
    }

    b->indent = NUM2INT(indent);
    return Qnil;
}

#instruct(*args) ⇒ Object



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

static VALUE builder_instruct(int argc, VALUE *argv, VALUE self) {
    Builder b;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    if (0 < argc) {
        volatile VALUE nsym = Qnil;
        const char    *nstr;
        size_t         nx;

        check_name(*argv, &nsym, &nstr, &nx, "expected a Symbol or String");
        if (1 < argc && rb_cHash == rb_obj_class(argv[1])) {
            // The "<?" and the target are out by the time the values are
            // written, so a raise down there would leave them in the buffer.
            check_instruct_value(argv[1], ox_version_sym);
            check_instruct_value(argv[1], ox_encoding_sym);
            check_instruct_value(argv[1], ox_standalone_sym);
        }
    }
    i_am_a_child(b, false);
    append_indent(b);
    if (0 == argc) {
        buf_append_string(&b->buf, "<?xml?>", 7);
        b->col += 7;
        b->pos += 7;
    } else {
        volatile VALUE v;

        buf_append_string(&b->buf, "<?", 2);
        b->col += 2;
        b->pos += 2;
        append_sym_str(b, *argv);
        if (1 < argc && rb_cHash == rb_obj_class(argv[1])) {
            int len;

            if (Qnil != (v = rb_hash_lookup(argv[1], ox_version_sym))) {
                if (rb_cString != rb_obj_class(v)) {
                    rb_raise(ox_parse_error_class, ":version must be a Symbol.\n");
                }
                len = (int)RSTRING_LEN(v);
                buf_append_string(&b->buf, " version=\"", 10);
                buf_append_string(&b->buf, StringValuePtr(v), len);
                buf_append(&b->buf, '"');
                b->col += len + 11;
                b->pos += len + 11;
            }
            if (Qnil != (v = rb_hash_lookup(argv[1], ox_encoding_sym))) {
                if (rb_cString != rb_obj_class(v)) {
                    rb_raise(ox_parse_error_class, ":encoding must be a Symbol.\n");
                }
                len = (int)RSTRING_LEN(v);
                buf_append_string(&b->buf, " encoding=\"", 11);
                buf_append_string(&b->buf, StringValuePtr(v), len);
                buf_append(&b->buf, '"');
                b->col += len + 12;
                b->pos += len + 12;
                strncpy(b->encoding, StringValuePtr(v), sizeof(b->encoding));
                b->encoding[sizeof(b->encoding) - 1] = '\0';
            }
            if (Qnil != (v = rb_hash_lookup(argv[1], ox_standalone_sym))) {
                if (rb_cString != rb_obj_class(v)) {
                    rb_raise(ox_parse_error_class, ":standalone must be a Symbol.\n");
                }
                len = (int)RSTRING_LEN(v);
                buf_append_string(&b->buf, " standalone=\"", 13);
                buf_append_string(&b->buf, StringValuePtr(v), len);
                buf_append(&b->buf, '"');
                b->col += len + 14;
                b->pos += len + 14;
            }
        }
        buf_append_string(&b->buf, "?>", 2);
        b->col += 2;
        b->pos += 2;
    }
    return Qnil;
}

#lineObject



961
962
963
964
965
966
# File 'ext/ox/builder.c', line 961

static VALUE builder_line(VALUE self) {
    Builder b;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    return LONG2NUM(b->line);
}

#popObject



1025
1026
1027
1028
1029
1030
1031
# File 'ext/ox/builder.c', line 1025

static VALUE builder_pop(VALUE self) {
    Builder b;
    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    pop(b);

    return Qnil;
}

#posObject



1014
1015
1016
1017
1018
1019
# File 'ext/ox/builder.c', line 1014

static VALUE builder_pos(VALUE self) {
    Builder b;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    return LONG2NUM(b->pos);
}

#raw(text) ⇒ Object



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/builder.c', line 918

static VALUE builder_raw(VALUE self, VALUE text) {
    Builder        b;
    volatile VALUE v = text;
    const char    *str;
    const char    *s;
    const char    *end;
    int            len;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    v   = rb_String(v);
    str = StringValuePtr(v);
    len = (int)RSTRING_LEN(v);
    s   = str;
    end = str + len;
    i_am_a_child(b, true);
    buf_append_string(&b->buf, str, len);
    b->col += len;
    s = strchr(s, '\n');
    while (NULL != s) {
        b->line++;
        b->col = end - s;
        s      = strchr(s + 1, '\n');
    }
    b->pos += len;

    return Qnil;
}

#text(*args) ⇒ Object



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
# File 'ext/ox/builder.c', line 827

static VALUE builder_text(int argc, VALUE *argv, VALUE self) {
    Builder        b;
    volatile VALUE v;
    volatile VALUE strip_invalid_chars;
    size_t         size;
    size_t         xsize;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

    if ((0 == argc) || (argc > 2)) {
        rb_raise(rb_eArgError, "wrong number of arguments (given %d, expected 1..2)", argc);
    }
    v = argv[0];
    if (2 == argc) {
        strip_invalid_chars = argv[1];
    } else {
        strip_invalid_chars = Qfalse;
    }

    v    = rb_String(v);
    size = (size_t)RSTRING_LEN(v);
    // Stripping is allowed to drop the byte, so it is the one writer that does
    // not have to refuse the value up front.
    xsize = RTEST(strip_invalid_chars) ? xml_str_len((const unsigned char *)StringValuePtr(v), size, xml_element_chars)
                                       : check_string(StringValuePtr(v), size, xml_element_chars);
    i_am_a_child(b, true);
    append_string(b, StringValuePtr(v), size, xml_element_chars, xsize, RTEST(strip_invalid_chars));

    return Qnil;
}

#to_sObject



950
951
952
953
954
955
# File 'ext/ox/builder.c', line 950

static VALUE builder_to_s(VALUE self) {
    Builder b;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    return to_s(b);
}

#void_element(*args) ⇒ Object



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

static VALUE builder_void_element(int argc, VALUE *argv, VALUE self) {
    Builder        b;
    volatile VALUE sym   = Qnil;
    size_t         xsize = 0;
    const char    *name;
    long           len;

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);

    if (1 > argc) {
        rb_raise(ox_arg_error_class, "missing element name");
    }
    len = check_name(*argv, &sym, &name, &xsize, "expected a Symbol or String for an element name");
    check_name_chars(name, (size_t)len, false);
    i_am_a_child(b, false);
    append_indent(b);
    buf_append(&b->buf, '<');
    b->col++;
    b->pos++;
    append_string(b, name, len, xml_element_chars, xsize, false);
    if (1 < argc && T_HASH == rb_type(argv[1])) {
        rb_hash_foreach(argv[1], append_attr, (VALUE)b);
    }
    buf_append_string(&b->buf, ">", 1);
    b->col++;
    ;
    b->pos++;

    return Qnil;
}