ox 1.4.4 → 1.4.5
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of ox might be problematic. Click here for more details.
- data/README.md +2 -2
- data/ext/ox/dump.c +12 -3
- data/ext/ox/gen_load.c +25 -1
- data/ext/ox/obj_load.c +40 -8
- data/ext/ox/ox.c +5 -0
- data/ext/ox/ox.h +12 -1
- data/ext/ox/parse.c +0 -2
- data/ext/ox/sax.c +17 -5
- data/lib/ox/version.rb +1 -1
- data/test/bug3.rb +11 -13
- data/test/bug4.rb +54 -5
- data/test/func.rb +32 -0
- data/test/sax_example.rb +37 -0
- data/test/sax_test.rb +18 -0
- metadata +4 -5
- data/test/big.rb +0 -24
data/README.md
CHANGED
@@ -26,9 +26,9 @@ A fast XML parser and Object marshaller as a Ruby gem.
|
|
26
26
|
|
27
27
|
## <a name="release">Release Notes</a>
|
28
28
|
|
29
|
-
### Release 1.4.
|
29
|
+
### Release 1.4.5
|
30
30
|
|
31
|
-
- Fixed
|
31
|
+
- Fixed encoding issues with all parsers so that attribute and element names can now be encoded with something other than ASCII.
|
32
32
|
|
33
33
|
## <a name="description">Description</a>
|
34
34
|
|
data/ext/ox/dump.c
CHANGED
@@ -697,7 +697,7 @@ dump_obj(ID aid, VALUE obj, unsigned int depth, Out out) {
|
|
697
697
|
out->w_end(out, &e);
|
698
698
|
}
|
699
699
|
#else
|
700
|
-
#if (defined JRUBY || defined
|
700
|
+
#if (defined JRUBY || defined RUBINIUS)
|
701
701
|
VALUE vars = rb_funcall2(obj, rb_intern("instance_variables"), 0, 0);
|
702
702
|
#else
|
703
703
|
VALUE vars = rb_obj_instance_variables(obj);
|
@@ -856,6 +856,15 @@ dump_gen_doc(VALUE obj, unsigned int depth, Out out) {
|
|
856
856
|
VALUE attrs = rb_attr_get(obj, attributes_id);
|
857
857
|
VALUE nodes = rb_attr_get(obj, nodes_id);
|
858
858
|
|
859
|
+
if ('\0' == *out->opts->encoding && Qnil != attrs) {
|
860
|
+
VALUE renc = rb_hash_lookup(attrs, encoding_sym);
|
861
|
+
|
862
|
+
if (Qnil != renc) {
|
863
|
+
const char *enc = StringValuePtr(renc);
|
864
|
+
|
865
|
+
strncpy(out->opts->encoding, enc, sizeof(out->opts->encoding) - 1);
|
866
|
+
}
|
867
|
+
}
|
859
868
|
if (Yes == out->opts->with_xml) {
|
860
869
|
dump_value(out, "<?xml", 5);
|
861
870
|
if (Qnil != attrs) {
|
@@ -1022,9 +1031,9 @@ dump_obj_to_xml(VALUE obj, Options copts, Out out) {
|
|
1022
1031
|
}
|
1023
1032
|
out->indent = copts->indent;
|
1024
1033
|
if (ox_document_clas == clas) {
|
1025
|
-
|
1034
|
+
dump_gen_doc(obj, -1, out);
|
1026
1035
|
} else if (ox_element_clas == clas) {
|
1027
|
-
|
1036
|
+
dump_gen_element(obj, 0, out);
|
1028
1037
|
} else {
|
1029
1038
|
out->w_start = dump_start;
|
1030
1039
|
out->w_end = dump_end;
|
data/ext/ox/gen_load.c
CHANGED
@@ -105,6 +105,7 @@ create_prolog_doc(PInfo pi, const char *target, Attr attrs) {
|
|
105
105
|
VALUE doc;
|
106
106
|
VALUE ah;
|
107
107
|
VALUE nodes;
|
108
|
+
VALUE sym;
|
108
109
|
|
109
110
|
if (0 != pi->h) { // top level object
|
110
111
|
rb_raise(rb_eSyntaxError, "Prolog must be the first element in an XML document.\n");
|
@@ -113,7 +114,19 @@ create_prolog_doc(PInfo pi, const char *target, Attr attrs) {
|
|
113
114
|
doc = rb_obj_alloc(ox_document_clas);
|
114
115
|
ah = rb_hash_new();
|
115
116
|
for (; 0 != attrs->name; attrs++) {
|
116
|
-
|
117
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
118
|
+
if (0 != pi->encoding) {
|
119
|
+
VALUE rstr = rb_str_new2(attrs->name);
|
120
|
+
|
121
|
+
rb_enc_associate(rstr, pi->encoding);
|
122
|
+
sym = rb_funcall(rstr, to_sym_id, 0);
|
123
|
+
} else {
|
124
|
+
sym = ID2SYM(rb_intern(attrs->name));
|
125
|
+
}
|
126
|
+
#else
|
127
|
+
sym = ID2SYM(rb_intern(attrs->name));
|
128
|
+
#endif
|
129
|
+
rb_hash_aset(ah, sym, rb_str_new2(attrs->value));
|
117
130
|
#ifdef HAVE_RUBY_ENCODING_H
|
118
131
|
if (0 == strcmp("encoding", attrs->name)) {
|
119
132
|
pi->encoding = rb_enc_find(attrs->value);
|
@@ -266,7 +279,18 @@ add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
|
|
266
279
|
VALUE *slot;
|
267
280
|
|
268
281
|
if (Qundef == (sym = ox_cache_get(symbol_cache, attrs->name, &slot))) {
|
282
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
283
|
+
if (0 != pi->encoding) {
|
284
|
+
VALUE rstr = rb_str_new2(attrs->name);
|
285
|
+
|
286
|
+
rb_enc_associate(rstr, pi->encoding);
|
287
|
+
sym = rb_funcall(rstr, to_sym_id, 0);
|
288
|
+
} else {
|
289
|
+
sym = ID2SYM(rb_intern(attrs->name));
|
290
|
+
}
|
291
|
+
#else
|
269
292
|
sym = ID2SYM(rb_intern(attrs->name));
|
293
|
+
#endif
|
270
294
|
*slot = sym;
|
271
295
|
}
|
272
296
|
s = rb_str_new2(attrs->value);
|
data/ext/ox/obj_load.c
CHANGED
@@ -49,7 +49,7 @@ static VALUE parse_xsd_time(const char *text, VALUE clas);
|
|
49
49
|
static VALUE parse_double_time(const char *text, VALUE clas);
|
50
50
|
static VALUE parse_regexp(const char *text);
|
51
51
|
|
52
|
-
static VALUE get_var_sym_from_attrs(Attr a);
|
52
|
+
static VALUE get_var_sym_from_attrs(Attr a, void *encoding);
|
53
53
|
static VALUE get_obj_from_attrs(Attr a, PInfo pi);
|
54
54
|
static VALUE get_class_from_attrs(Attr a, PInfo pi);
|
55
55
|
static VALUE classname2class(const char *name, PInfo pi);
|
@@ -78,15 +78,47 @@ ParseCallbacks ox_obj_callbacks = &_ox_obj_callbacks;
|
|
78
78
|
extern ParseCallbacks ox_gen_callbacks;
|
79
79
|
|
80
80
|
|
81
|
+
inline static VALUE
|
82
|
+
str2sym(const char *str, void *encoding) {
|
83
|
+
VALUE sym;
|
84
|
+
|
85
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
86
|
+
if (0 != encoding) {
|
87
|
+
VALUE rstr = rb_str_new2(str);
|
88
|
+
|
89
|
+
rb_enc_associate(rstr, (rb_encoding*)encoding);
|
90
|
+
sym = rb_funcall(rstr, to_sym_id, 0);
|
91
|
+
} else {
|
92
|
+
sym = ID2SYM(rb_intern(str));
|
93
|
+
}
|
94
|
+
#else
|
95
|
+
sym = ID2SYM(rb_intern(str));
|
96
|
+
#endif
|
97
|
+
return sym;
|
98
|
+
}
|
99
|
+
|
81
100
|
inline static ID
|
82
|
-
name2var(const char *name) {
|
101
|
+
name2var(const char *name, void *encoding) {
|
83
102
|
VALUE *slot;
|
84
103
|
ID var_id;
|
85
104
|
|
86
105
|
if ('0' <= *name && *name <= '9') {
|
87
106
|
var_id = INT2NUM(atoi(name));
|
88
107
|
} else if (Qundef == (var_id = ox_cache_get(attr_cache, name, &slot))) {
|
89
|
-
|
108
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
109
|
+
if (0 != encoding) {
|
110
|
+
VALUE rstr = rb_str_new2(name);
|
111
|
+
VALUE sym;
|
112
|
+
|
113
|
+
rb_enc_associate(rstr, (rb_encoding*)encoding);
|
114
|
+
sym = rb_funcall(rstr, to_sym_id, 0);
|
115
|
+
var_id = SYM2ID(sym);
|
116
|
+
} else {
|
117
|
+
var_id = rb_intern(name);
|
118
|
+
}
|
119
|
+
#else
|
120
|
+
var_id = rb_intern(name);
|
121
|
+
#endif
|
90
122
|
*slot = var_id;
|
91
123
|
}
|
92
124
|
return var_id;
|
@@ -205,10 +237,10 @@ classname2class(const char *name, PInfo pi) {
|
|
205
237
|
}
|
206
238
|
|
207
239
|
static VALUE
|
208
|
-
get_var_sym_from_attrs(Attr a) {
|
240
|
+
get_var_sym_from_attrs(Attr a, void *encoding) {
|
209
241
|
for (; 0 != a->name; a++) {
|
210
242
|
if ('a' == *a->name && '\0' == *(a->name + 1)) {
|
211
|
-
return name2var(a->value);
|
243
|
+
return name2var(a->value, encoding);
|
212
244
|
}
|
213
245
|
}
|
214
246
|
return Qundef;
|
@@ -420,7 +452,7 @@ add_text(PInfo pi, char *text, int closed) {
|
|
420
452
|
VALUE *slot;
|
421
453
|
|
422
454
|
if (Qundef == (sym = ox_cache_get(symbol_cache, text, &slot))) {
|
423
|
-
|
455
|
+
sym = str2sym(text, pi->encoding);
|
424
456
|
*slot = sym;
|
425
457
|
}
|
426
458
|
pi->h->obj = sym;
|
@@ -472,7 +504,7 @@ add_text(PInfo pi, char *text, int closed) {
|
|
472
504
|
}
|
473
505
|
from_base64(text, (u_char*)str);
|
474
506
|
if (Qundef == (sym = ox_cache_get(symbol_cache, str, &slot))) {
|
475
|
-
|
507
|
+
sym = str2sym(str, pi->encoding);
|
476
508
|
*slot = sym;
|
477
509
|
}
|
478
510
|
pi->h->obj = sym;
|
@@ -548,7 +580,7 @@ add_element(PInfo pi, const char *ename, Attr attrs, int hasChildren) {
|
|
548
580
|
}
|
549
581
|
h = pi->h;
|
550
582
|
h->type = *ename;
|
551
|
-
h->var = get_var_sym_from_attrs(attrs);
|
583
|
+
h->var = get_var_sym_from_attrs(attrs, pi->encoding);
|
552
584
|
switch (h->type) {
|
553
585
|
case NilClassCode:
|
554
586
|
h->obj = Qnil;
|
data/ext/ox/ox.c
CHANGED
@@ -57,6 +57,7 @@ ID end_element_id;
|
|
57
57
|
ID end_id;
|
58
58
|
ID error_id;
|
59
59
|
ID excl_id;
|
60
|
+
ID fileno_id;
|
60
61
|
ID inspect_id;
|
61
62
|
ID instruct_id;
|
62
63
|
ID keys_id;
|
@@ -70,6 +71,7 @@ ID start_element_id;
|
|
70
71
|
ID text_id;
|
71
72
|
ID to_c_id;
|
72
73
|
ID to_s_id;
|
74
|
+
ID to_sym_id;
|
73
75
|
ID tv_sec_id;
|
74
76
|
ID tv_usec_id;
|
75
77
|
ID value_id;
|
@@ -97,6 +99,7 @@ VALUE with_dtd_sym;
|
|
97
99
|
VALUE with_instruct_sym;
|
98
100
|
VALUE convert_special_sym;
|
99
101
|
VALUE with_xml_sym;
|
102
|
+
|
100
103
|
VALUE empty_string;
|
101
104
|
VALUE zero_fixnum;
|
102
105
|
|
@@ -645,6 +648,7 @@ void Init_ox() {
|
|
645
648
|
end_id = rb_intern("@end");
|
646
649
|
error_id = rb_intern("error");
|
647
650
|
excl_id = rb_intern("@excl");
|
651
|
+
fileno_id = rb_intern("fileno");
|
648
652
|
inspect_id = rb_intern("inspect");
|
649
653
|
instruct_id = rb_intern("instruct");
|
650
654
|
keys_id = rb_intern("keys");
|
@@ -658,6 +662,7 @@ void Init_ox() {
|
|
658
662
|
text_id = rb_intern("text");
|
659
663
|
to_c_id = rb_intern("to_c");
|
660
664
|
to_s_id = rb_intern("to_s");
|
665
|
+
to_sym_id = rb_intern("to_sym");
|
661
666
|
tv_sec_id = rb_intern("tv_sec");
|
662
667
|
tv_usec_id = rb_intern("tv_usec");
|
663
668
|
value_id = rb_intern("@value");
|
data/ext/ox/ox.h
CHANGED
@@ -48,7 +48,14 @@ extern "C" {
|
|
48
48
|
#ifdef JRUBY
|
49
49
|
#define NO_RSTRUCT 1
|
50
50
|
#endif
|
51
|
-
|
51
|
+
|
52
|
+
#if (defined RBX_Qnil && !defined RUBINIUS)
|
53
|
+
#define RUBINIUS
|
54
|
+
#endif
|
55
|
+
|
56
|
+
#ifdef RUBINIUS
|
57
|
+
#undef T_RATIONAL
|
58
|
+
#undef T_COMPLEX
|
52
59
|
#define NO_RSTRUCT 1
|
53
60
|
#endif
|
54
61
|
|
@@ -176,6 +183,8 @@ struct _PInfo {
|
|
176
183
|
CircArray circ_array;
|
177
184
|
#ifdef HAVE_RUBY_ENCODING_H
|
178
185
|
rb_encoding *encoding;
|
186
|
+
#else
|
187
|
+
void *encoding;
|
179
188
|
#endif
|
180
189
|
unsigned long id; /* set for text types when cirs_array is set */
|
181
190
|
int trace;
|
@@ -219,6 +228,7 @@ extern ID end_element_id;
|
|
219
228
|
extern ID end_id;
|
220
229
|
extern ID error_id;
|
221
230
|
extern ID excl_id;
|
231
|
+
extern ID fileno_id;
|
222
232
|
extern ID inspect_id;
|
223
233
|
extern ID instruct_id;
|
224
234
|
extern ID keys_id;
|
@@ -232,6 +242,7 @@ extern ID start_element_id;
|
|
232
242
|
extern ID text_id;
|
233
243
|
extern ID to_c_id;
|
234
244
|
extern ID to_s_id;
|
245
|
+
extern ID to_sym_id;
|
235
246
|
extern ID tv_sec_id;
|
236
247
|
extern ID tv_usec_id;
|
237
248
|
extern ID value_id;
|
data/ext/ox/parse.c
CHANGED
@@ -113,9 +113,7 @@ parse(char *xml, ParseCallbacks pcb, char **endp, int trace, Effort effort) {
|
|
113
113
|
pi.pcb = pcb;
|
114
114
|
pi.obj = Qnil;
|
115
115
|
pi.circ_array = 0;
|
116
|
-
#ifdef ENCODING_INLINE_MAX
|
117
116
|
pi.encoding = 0;
|
118
|
-
#endif
|
119
117
|
pi.trace = trace;
|
120
118
|
pi.effort = effort;
|
121
119
|
while (1) {
|
data/ext/ox/sax.c
CHANGED
@@ -170,12 +170,23 @@ is_white(char c) {
|
|
170
170
|
}
|
171
171
|
|
172
172
|
inline static VALUE
|
173
|
-
str2sym(const char *str) {
|
173
|
+
str2sym(const char *str, SaxDrive dr) {
|
174
174
|
VALUE *slot;
|
175
175
|
VALUE sym;
|
176
176
|
|
177
177
|
if (Qundef == (sym = ox_cache_get(symbol_cache, str, &slot))) {
|
178
|
+
#ifdef HAVE_RUBY_ENCODING_H
|
179
|
+
if (0 != dr->encoding) {
|
180
|
+
VALUE rstr = rb_str_new2(str);
|
181
|
+
|
182
|
+
rb_enc_associate(rstr, dr->encoding);
|
183
|
+
sym = rb_funcall(rstr, to_sym_id, 0);
|
184
|
+
} else {
|
185
|
+
sym = ID2SYM(rb_intern(str));
|
186
|
+
}
|
187
|
+
#else
|
178
188
|
sym = ID2SYM(rb_intern(str));
|
189
|
+
#endif
|
179
190
|
*slot = sym;
|
180
191
|
}
|
181
192
|
return sym;
|
@@ -208,7 +219,7 @@ sax_drive_init(SaxDrive dr, VALUE handler, VALUE io, int convert) {
|
|
208
219
|
if (rb_respond_to(io, readpartial_id)) {
|
209
220
|
VALUE rfd;
|
210
221
|
|
211
|
-
if (rb_respond_to(io,
|
222
|
+
if (rb_respond_to(io, fileno_id) && Qnil != (rfd = rb_funcall(io, fileno_id, 0))) {
|
212
223
|
dr->read_func = read_from_fd;
|
213
224
|
dr->fd = FIX2INT(rfd);
|
214
225
|
} else {
|
@@ -218,7 +229,7 @@ sax_drive_init(SaxDrive dr, VALUE handler, VALUE io, int convert) {
|
|
218
229
|
} else if (rb_respond_to(io, read_id)) {
|
219
230
|
VALUE rfd;
|
220
231
|
|
221
|
-
if (rb_respond_to(io,
|
232
|
+
if (rb_respond_to(io, fileno_id) && Qnil != (rfd = rb_funcall(io, fileno_id, 0))) {
|
222
233
|
dr->read_func = read_from_fd;
|
223
234
|
dr->fd = FIX2INT(rfd);
|
224
235
|
} else {
|
@@ -565,7 +576,8 @@ read_element(SaxDrive dr) {
|
|
565
576
|
if ('\0' == (c = read_name_token(dr))) {
|
566
577
|
return -1;
|
567
578
|
}
|
568
|
-
|
579
|
+
// TBD encode is needed
|
580
|
+
name = str2sym(dr->str, dr);
|
569
581
|
if (dr->has_start_element) {
|
570
582
|
VALUE args[1];
|
571
583
|
|
@@ -669,7 +681,7 @@ read_attrs(SaxDrive dr, char c, char termc, char term2, int is_xml) {
|
|
669
681
|
is_encoding = 1;
|
670
682
|
}
|
671
683
|
if (dr->has_attr) {
|
672
|
-
name = str2sym(dr->str);
|
684
|
+
name = str2sym(dr->str, dr);
|
673
685
|
}
|
674
686
|
if (is_white(c)) {
|
675
687
|
c = next_non_white(dr);
|
data/lib/ox/version.rb
CHANGED
data/test/bug3.rb
CHANGED
@@ -3,21 +3,19 @@
|
|
3
3
|
$: << '../lib'
|
4
4
|
$: << '../ext'
|
5
5
|
|
6
|
-
if __FILE__ == $0
|
7
|
-
if (i = ARGV.index('-I'))
|
8
|
-
x,path = ARGV.slice!(i, 2)
|
9
|
-
$: << path
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
6
|
require 'ox'
|
14
7
|
|
15
|
-
def
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
8
|
+
def name_it()
|
9
|
+
begin
|
10
|
+
"x".foo
|
11
|
+
rescue Exception => e
|
12
|
+
#puts e.message
|
13
|
+
xml = Ox.dump(e, effort: :tolerant)
|
14
|
+
puts xml
|
15
|
+
o = Ox.load(xml, mode: :object)
|
16
|
+
puts o.message
|
17
|
+
puts Ox.dump(e)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
23
|
-
|
21
|
+
name_it()
|
data/test/bug4.rb
CHANGED
@@ -1,11 +1,60 @@
|
|
1
1
|
#!/usr/bin/env ruby -wW1
|
2
|
+
# encoding: UTF-8
|
2
3
|
|
3
|
-
$: <<
|
4
|
-
$: <<
|
4
|
+
$: << File.join(File.dirname(__FILE__), "../lib")
|
5
|
+
$: << File.join(File.dirname(__FILE__), "../ext")
|
5
6
|
|
7
|
+
require 'stringio'
|
6
8
|
require 'ox'
|
7
9
|
|
8
|
-
|
9
|
-
xml
|
10
|
+
x1 = %(<?xml version="1.0" encoding="ISO-8859-1" ?><tag key="value">Français</tag>).encode("ISO-8859-1")
|
11
|
+
# => "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?><tag key=\"value\">Fran\xE7ais</tag>"
|
12
|
+
x1.encoding
|
13
|
+
# => #<Encoding:ISO-8859-1>
|
14
|
+
|
15
|
+
x2 = %(<?xml version="1.0" encoding="UTF-8" ?><tag key="value">Français</tag>)
|
16
|
+
# => "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><tag key=\"value\">Fran\xC3\xA7ais</tag>"
|
17
|
+
x2.encoding
|
18
|
+
# => #<Encoding:UTF-8>
|
19
|
+
|
20
|
+
class OH < ::Ox::Sax
|
21
|
+
def start_element(name)
|
22
|
+
puts "EL: #{name} (#{name.encoding})"
|
23
|
+
end
|
24
|
+
|
25
|
+
def end_element(name)
|
26
|
+
end
|
27
|
+
|
28
|
+
def attr(key, value)
|
29
|
+
puts "AT: #{key} => #{value} (#{key.encoding} => #{value.encoding})"
|
30
|
+
end
|
31
|
+
|
32
|
+
def text(value)
|
33
|
+
puts "TX: #{value} (#{value.encoding})"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
::Ox.sax_parse OH.new, StringIO.new(x1)
|
38
|
+
# => AT: version => 1.0 (US-ASCII => ASCII-8BIT)
|
39
|
+
# AT: encoding => ISO-8859-1 (US-ASCII => ISO-8859-1)
|
40
|
+
# EL: tag (US-ASCII)
|
41
|
+
# AT: key => value (US-ASCII => ISO-8859-1)
|
42
|
+
# TX: Fran�ais (ISO-8859-1)
|
43
|
+
|
44
|
+
puts
|
45
|
+
|
46
|
+
::Ox.sax_parse OH.new, StringIO.new(x2)
|
47
|
+
# => AT: version => 1.0 (US-ASCII => ASCII-8BIT)
|
48
|
+
# AT: encoding => UTF-8 (US-ASCII => UTF-8)
|
49
|
+
# EL: tag (US-ASCII)
|
50
|
+
# AT: key => value (US-ASCII => UTF-8)
|
51
|
+
# TX: Français (UTF-8)
|
52
|
+
|
53
|
+
puts
|
54
|
+
x3 = %(<?xml version="1.0" encoding="ISO-8859-1" ?><tag Português="Español">Français</tag>).encode("ISO-8859-1")
|
55
|
+
::Ox.sax_parse OH.new, StringIO.new(x3)
|
56
|
+
|
57
|
+
puts
|
58
|
+
x4 = %(<?xml version="1.0" encoding="UTF-8" ?><tag Português="Español">Français</tag>)
|
59
|
+
::Ox.sax_parse OH.new, StringIO.new(x4)
|
10
60
|
|
11
|
-
doc = Ox.parse(xml)
|
data/test/func.rb
CHANGED
@@ -391,6 +391,38 @@ class Func < ::Test::Unit::TestCase
|
|
391
391
|
end
|
392
392
|
end
|
393
393
|
|
394
|
+
def test_full_encoding
|
395
|
+
if RUBY_VERSION.start_with?('1.8')
|
396
|
+
assert(true)
|
397
|
+
else
|
398
|
+
xml = %{<?xml version="1.0" encoding="UTF-8"?>
|
399
|
+
<いち name="ピーター" つま="まきえ">ピーター</いち>
|
400
|
+
}
|
401
|
+
obj = Ox.load(xml)
|
402
|
+
dumped = Ox.dump(obj, :with_xml => true)
|
403
|
+
assert_equal('UTF-8', dumped.encoding.to_s)
|
404
|
+
assert_equal(xml, dumped)
|
405
|
+
end
|
406
|
+
end
|
407
|
+
|
408
|
+
def test_obj_encoding
|
409
|
+
if RUBY_VERSION.start_with?('1.8')
|
410
|
+
assert(true)
|
411
|
+
else
|
412
|
+
orig = Ox.default_options
|
413
|
+
opts = orig.clone
|
414
|
+
opts[:encoding] = 'UTF-8'
|
415
|
+
opts[:with_xml] = true
|
416
|
+
Ox.default_options = opts
|
417
|
+
begin
|
418
|
+
dump_and_load(Bag.new(:@tsuma => :まきえ), false)
|
419
|
+
dump_and_load(Bag.new(:@つま => :まきえ), false)
|
420
|
+
ensure
|
421
|
+
Ox.default_options = orig
|
422
|
+
end
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
394
426
|
def test_instructions
|
395
427
|
xml = Ox.dump("test", :with_instructions => true)
|
396
428
|
#puts xml
|
data/test/sax_example.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby -wW1
|
2
|
+
|
3
|
+
$: << '../lib'
|
4
|
+
$: << '../ext'
|
5
|
+
|
6
|
+
require 'stringio'
|
7
|
+
require 'ox'
|
8
|
+
|
9
|
+
class Sample < ::Ox::Sax
|
10
|
+
def start_element(name); puts "start: #{name}"; end
|
11
|
+
def end_element(name); puts "end: #{name}"; end
|
12
|
+
def attr(name, value); puts " #{name} => #{value}"; end
|
13
|
+
def text(value); puts "text #{value}"; end
|
14
|
+
end
|
15
|
+
|
16
|
+
io = StringIO.new(%{
|
17
|
+
<top name="sample">
|
18
|
+
<middle name="second">
|
19
|
+
<bottom name="third"/>
|
20
|
+
</middle>
|
21
|
+
</top>
|
22
|
+
})
|
23
|
+
|
24
|
+
handler = Sample.new()
|
25
|
+
Ox.sax_parse(handler, io)
|
26
|
+
|
27
|
+
# outputs
|
28
|
+
# start: top
|
29
|
+
# name => sample
|
30
|
+
# start: middle
|
31
|
+
# name => second
|
32
|
+
# start: bottom
|
33
|
+
# name => third
|
34
|
+
# end: bottom
|
35
|
+
# end: middle
|
36
|
+
# end: top
|
37
|
+
|
data/test/sax_test.rb
CHANGED
@@ -447,4 +447,22 @@ encoding = "UTF-8" ?>},
|
|
447
447
|
end
|
448
448
|
end
|
449
449
|
|
450
|
+
def test_sax_full_encoding
|
451
|
+
if RUBY_VERSION.start_with?('1.8')
|
452
|
+
assert(true)
|
453
|
+
else
|
454
|
+
parse_compare(%{<?xml version="1.0" encoding="UTF-8"?>
|
455
|
+
<いち name="ピーター" つま="まきえ">ピーター</いち>
|
456
|
+
},
|
457
|
+
[[:instruct, "xml"],
|
458
|
+
[:attr, :version, "1.0"],
|
459
|
+
[:attr, :encoding, "UTF-8"],
|
460
|
+
[:start_element, :いち],
|
461
|
+
[:attr, :name, 'ピーター'],
|
462
|
+
[:attr, :つま, 'まきえ'],
|
463
|
+
[:text, 'ピーター'],
|
464
|
+
[:end_element, :いち]])
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
450
468
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-01-
|
12
|
+
date: 2012-01-19 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! "A fast XML parser and object serializer that uses only standard C
|
15
15
|
lib.\n \nOptimized XML (Ox), as the name implies was written to provide
|
@@ -51,7 +51,6 @@ files:
|
|
51
51
|
- ext/ox/parse.c
|
52
52
|
- ext/ox/sax.c
|
53
53
|
- test/bench.rb
|
54
|
-
- test/big.rb
|
55
54
|
- test/bug1.rb
|
56
55
|
- test/bug2.rb
|
57
56
|
- test/bug3.rb
|
@@ -83,6 +82,7 @@ files:
|
|
83
82
|
- test/perf_sax.rb
|
84
83
|
- test/perf_write.rb
|
85
84
|
- test/sample.rb
|
85
|
+
- test/sax_example.rb
|
86
86
|
- test/sax_test.rb
|
87
87
|
- test/test.rb
|
88
88
|
- test/Sample.graffle
|
@@ -111,9 +111,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
111
|
version: '0'
|
112
112
|
requirements: []
|
113
113
|
rubyforge_project: ox
|
114
|
-
rubygems_version: 1.8.
|
114
|
+
rubygems_version: 1.8.15
|
115
115
|
signing_key:
|
116
116
|
specification_version: 3
|
117
117
|
summary: A fast XML parser and object serializer.
|
118
118
|
test_files: []
|
119
|
-
has_rdoc: true
|
data/test/big.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby -wW1
|
2
|
-
|
3
|
-
$: << '../lib'
|
4
|
-
$: << '../ext'
|
5
|
-
|
6
|
-
if __FILE__ == $0
|
7
|
-
while (i = ARGV.index('-I'))
|
8
|
-
x,path = ARGV.slice!(i, 2)
|
9
|
-
$: << path
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
require 'ox'
|
14
|
-
|
15
|
-
def dump(cnt = 10000)
|
16
|
-
h = { }
|
17
|
-
cnt.times do |i|
|
18
|
-
h[i] = [i * 2, "this is #{i}"]
|
19
|
-
end
|
20
|
-
xml = Ox.dump(h)
|
21
|
-
puts "size: #{xml.size}"
|
22
|
-
end
|
23
|
-
|
24
|
-
dump(200000)
|