Class: Wankel::SaxParser

Inherits:
Object
  • Object
show all
Defined in:
lib/wankel/ex_sax_parser.rb,
ext/wankel/wankel_sax_parser.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'ext/wankel/wankel_sax_parser.c', line 32

VALUE sax_parser_initialize(int argc, VALUE * argv, VALUE self) {
    VALUE defaults = rb_const_get(c_wankel, intern_DEFAULTS);
    VALUE klass = rb_funcall(self, rb_intern("class"), 0);
    VALUE options, rbufsize;
    sax_parser * p;

    rb_scan_args(argc, argv, "01", &options);
    if (rb_const_defined(klass, intern_DEFAULTS)) { 
        defaults = rb_funcall(defaults, intern_merge, 1, rb_const_get(klass, intern_DEFAULTS));
    }
    if(options == Qnil) {
        rb_iv_set(self, "@options", rb_funcall(defaults, intern_clone, 0) );
    } else {
        Check_Type(options, T_HASH);
        rb_iv_set(self, "@options", rb_funcall(defaults, intern_merge, 1, options) );
    }
    options = rb_iv_get(self, "@options");

    Data_Get_Struct(self, sax_parser, p);
    p->callbacks = sax_parser_callbacks(self);
    p->alloc_funcs.malloc = yajl_helper_malloc;
    p->alloc_funcs.realloc = yajl_helper_realloc;
    p->alloc_funcs.free = yajl_helper_free;
    p->h = yajl_alloc(&p->callbacks, &p->alloc_funcs, (void *)self);
    
    yajl_configure(p->h, options);
        
    rbufsize = rb_hash_aref(options, sym_read_buffer_size);
    Check_Type(rbufsize, T_FIXNUM);
    p->rbufsize = rbufsize;
    
    if(rb_hash_aref(options, sym_symbolize_keys) == Qtrue) {
        p->symbolize_keys = 1;
    } else {
        p->symbolize_keys = 0;
    }
    
    return self;
}

Instance Method Details

#completeObject



120
121
122
123
124
125
126
127
128
129
# File 'ext/wankel/wankel_sax_parser.c', line 120

static VALUE sax_parser_complete(VALUE self) {
    yajl_status status;
    sax_parser * p;
    Data_Get_Struct(self, sax_parser, p);
    
    status = yajl_complete_parse(p->h);
    yajl_helper_check_status(p->h, status, 0, NULL, 0);
    
    return Qnil;
}

#on_array_endObject



39
40
41
# File 'lib/wankel/ex_sax_parser.rb', line 39

def on_array_end
  puts "end array"
end

#on_array_startObject



35
36
37
# File 'lib/wankel/ex_sax_parser.rb', line 35

def on_array_start
  puts "start array"
end

#on_boolean(value) ⇒ Object



19
20
21
# File 'lib/wankel/ex_sax_parser.rb', line 19

def on_boolean(value)
  puts value
end

#on_double(d) ⇒ Object



27
28
29
# File 'lib/wankel/ex_sax_parser.rb', line 27

def on_double(d)
  puts "double"
end

#on_integer(i) ⇒ Object



23
24
25
# File 'lib/wankel/ex_sax_parser.rb', line 23

def on_integer(i)
  puts "integer"
end

#on_map_endObject



7
8
9
# File 'lib/wankel/ex_sax_parser.rb', line 7

def on_map_end
  puts 'end map'
end

#on_map_key(key) ⇒ Object



11
12
13
# File 'lib/wankel/ex_sax_parser.rb', line 11

def on_map_key(key)
  puts 'key'
end

#on_map_startObject



3
4
5
# File 'lib/wankel/ex_sax_parser.rb', line 3

def on_map_start
  puts 'start map'
end

#on_nullObject



15
16
17
# File 'lib/wankel/ex_sax_parser.rb', line 15

def on_null
  puts "null"
end

#on_string(s) ⇒ Object



31
32
33
# File 'lib/wankel/ex_sax_parser.rb', line 31

def on_string(s)
  puts "string"
end

#parse(*args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/wankel/wankel_sax_parser.c', line 72

static VALUE sax_parser_parse(int argc, VALUE * argv, VALUE self) {
    const char * cptr;
    size_t len;
    yajl_status status;
    sax_parser * p;
    VALUE input;
    Data_Get_Struct(self, sax_parser, p);
	
    rb_scan_args(argc, argv, "10", &input);
    if (TYPE(input) == T_STRING) {
        cptr = RSTRING_PTR(input);
        len = RSTRING_LEN(input);
        status = yajl_parse(p->h, (const unsigned char*)cptr, len);
        yajl_helper_check_status(p->h, status, 1, (const unsigned char*)cptr, len);
    } else if (rb_respond_to(input, intern_io_read)) {
        VALUE chunk = rb_str_new(0, FIX2LONG(p->rbufsize));
        while (rb_funcall(input, intern_io_read, 2, p->rbufsize, chunk) != Qnil) {
            cptr = RSTRING_PTR(chunk);
            len = RSTRING_LEN(chunk);
            status = yajl_parse(p->h, (const unsigned char*)cptr, len);
            yajl_helper_check_status(p->h, status, 1, (const unsigned char*)cptr, len);
        }
    } else {
        rb_raise(e_parseError, "input must be a string or an IO");
    }
    
    status = yajl_complete_parse(p->h);
    yajl_helper_check_status(p->h, status, 0, NULL, 0);
    
    return Qnil;
}

#write(input) ⇒ Object Also known as: <<



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/wankel/wankel_sax_parser.c', line 104

static VALUE sax_parser_write(VALUE self, VALUE input) {
    const char * cptr;
    size_t len;
    yajl_status status;
    sax_parser * p;
    Data_Get_Struct(self, sax_parser, p);
    
    Check_Type(input, T_STRING);
    cptr = RSTRING_PTR(input);
    len = RSTRING_LEN(input);
    status = yajl_parse(p->h, (const unsigned char*)cptr, len);
    yajl_helper_check_status(p->h, status, 1, (const unsigned char*)cptr, len);
    
    return Qnil;
}