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(filename, options) ⇒ Object

Creates a new Builder that will write to a file.

  • filename (String) filename to write to

  • options - (Hash) formating options

    • :indent (Fixnum) indentaion level, negative values excludes terminating newline

    • :size (Fixnum) the initial size of the string buffer



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'ext/ox/builder.c', line 365

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), "w"))) {
        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(io, options) ⇒ Object

Creates a new Builder that will write to an IO instance.

  • io (String) IO to write to

  • options - (Hash) formating options

    • :indent (Fixnum) indentaion level, negative values excludes terminating newline

    • :size (Fixnum) the initial size of the string buffer



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'ext/ox/builder.c', line 418

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(options) ⇒ Object

Creates a new Builder that will write to a string that can be retrieved with the to_s() method. If a block is given it is executed with a single parameter which is the builder instance. The return value is then the generated string.

  • options - (Hash) formating options

    • :indent (Fixnum) indentaion level, negative values excludes terminating newline

    • :size (Fixnum) the initial size of the string buffer



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

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

Adds a CDATA element to the XML string being formed.

  • data - (String) contents of the CDATA element



721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
# File 'ext/ox/builder.c', line 721

static VALUE builder_cdata(VALUE self, VALUE data) {
    Builder        b;
    volatile VALUE v = data;
    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, false);
    append_indent(b);
    buf_append_string(&b->buf, "<![CDATA[", 9);
    b->col += 9;
    b->pos += 9;
    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;
    buf_append_string(&b->buf, "]]>", 3);
    b->col += 3;
    b->pos += 3;

    return Qnil;
}

#closeObject

Closes the all elements and the document.



882
883
884
885
886
887
888
889
# File 'ext/ox/builder.c', line 882

static VALUE builder_close(VALUE self) {
    Builder b;

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

    return Qnil;
}

#columnObject

Returns the current column in the output. The first character in a line is at column 1.



818
819
820
821
822
823
# File 'ext/ox/builder.c', line 818

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

Adds a comment element to the XML string being formed.

  • text - (String) contents of the comment



645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
# File 'ext/ox/builder.c', line 645

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

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

    return Qnil;
}

#doctype(text) ⇒ Object

Adds a DOCTYPE element to the XML string being formed.

  • text - (String) contents of the doctype



668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
# File 'ext/ox/builder.c', line 668

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

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    rb_check_type(text, T_STRING);
    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), RSTRING_LEN(text), xml_element_chars, false);
    buf_append(&b->buf, '>');
    b->col++;
    b->pos++;

    return Qnil;
}

#element(name, attributes) ⇒ Object

Adds an element with the name and attributes provided. If a block is given then on closing of the block a pop() is called.

  • name - (String) name of the element

  • attributes - (Hash) of the element



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

static VALUE builder_element(int argc, VALUE *argv, VALUE self) {
    Builder     b;
    Element     e;
    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");
    }
    i_am_a_child(b, false);
    append_indent(b);
    b->depth++;
    if (MAX_DEPTH <= b->depth) {
        rb_raise(ox_arg_error_class, "XML too deeply nested");
    }
    switch (rb_type(*argv)) {
    case T_STRING:
        name = StringValuePtr(*argv);
        len  = RSTRING_LEN(*argv);
        break;
    case T_SYMBOL:
        name = rb_id2name(SYM2ID(*argv));
        len  = strlen(name);
        break;
    default: rb_raise(ox_arg_error_class, "expected a Symbol or String for an element name"); break;
    }
    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, 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

Returns the indentation level



829
830
831
832
833
834
# File 'ext/ox/builder.c', line 829

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

Sets the indentation level

  • indent (Fixnum) indentaion level, negative values excludes terminating newline



842
843
844
845
846
847
848
849
850
851
852
853
# File 'ext/ox/builder.c', line 842

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(decl, options) ⇒ Object

Adds the top level <?xml?> element.

  • decl - (String) ‘xml’ expected

  • options - (Hash) version or encoding



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

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

    TypedData_Get_Struct(self, struct _builder, &ox_builder_type, b);
    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

Returns the current line in the output. The first line is line 1.



806
807
808
809
810
811
# File 'ext/ox/builder.c', line 806

static VALUE builder_line(VALUE self) {
    Builder b;

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

#popObject

Closes the current element.



870
871
872
873
874
875
876
# File 'ext/ox/builder.c', line 870

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

    return Qnil;
}

#posObject

Returns the number of bytes written.



859
860
861
862
863
864
# File 'ext/ox/builder.c', line 859

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

Adds the provided string directly to the XML without formatting or modifications.

  • text - (String) contents to be added



763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'ext/ox/builder.c', line 763

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(text) ⇒ Object

Adds a text element to the XML string being formed.

  • text - (String) contents of the text field

  • strip_invalid_chars - [true|false] strips any characters invalid for XML, defaults to false



692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'ext/ox/builder.c', line 692

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

    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);
    i_am_a_child(b, true);
    append_string(b, StringValuePtr(v), RSTRING_LEN(v), xml_element_chars, RTEST(strip_invalid_chars));

    return Qnil;
}

#to_sObject

Returns the JSON document string in what ever state the construction is at.



795
796
797
798
799
800
# File 'ext/ox/builder.c', line 795

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(name, attributes) ⇒ Object

Adds an void element with the name and attributes provided.

  • name - (String) name of the element

  • attributes - (Hash) of the element



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

static VALUE builder_void_element(int argc, VALUE *argv, VALUE self) {
    Builder     b;
    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");
    }
    i_am_a_child(b, false);
    append_indent(b);
    switch (rb_type(*argv)) {
    case T_STRING:
        name = StringValuePtr(*argv);
        len  = RSTRING_LEN(*argv);
        break;
    case T_SYMBOL:
        name = rb_id2name(SYM2ID(*argv));
        len  = strlen(name);
        break;
    default: rb_raise(ox_arg_error_class, "expected a Symbol or String for an element name"); break;
    }
    buf_append(&b->buf, '<');
    b->col++;
    b->pos++;
    append_string(b, name, len, xml_element_chars, 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;
}