ox 2.3.0 → 2.4.0
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 +10 -10
- data/ext/ox/buf.h +160 -0
- data/ext/ox/builder.c +685 -0
- data/ext/ox/ox.c +117 -100
- data/ext/ox/ox.h +11 -4
- data/ext/ox/sax_as.c +37 -0
- data/lib/ox.rb +1 -0
- data/lib/ox/bag.rb +21 -14
- data/lib/ox/cdata.rb +1 -1
- data/lib/ox/comment.rb +1 -1
- data/lib/ox/doctype.rb +1 -1
- data/lib/ox/document.rb +4 -4
- data/lib/ox/element.rb +17 -16
- data/lib/ox/error.rb +2 -0
- data/lib/ox/hasattrs.rb +7 -8
- data/lib/ox/instruct.rb +3 -4
- data/lib/ox/node.rb +2 -2
- data/lib/ox/raw.rb +1 -1
- data/lib/ox/sax.rb +8 -6
- data/lib/ox/version.rb +1 -1
- data/lib/ox/xmlrpc_adapter.rb +3 -1
- metadata +52 -47
- checksums.yaml +0 -7
- data/ext/ox/cache8_test.c +0 -44
- data/ext/ox/cache_test.c +0 -55
- data/ext/ox/encode.h +0 -26
data/README.md
CHANGED
@@ -34,27 +34,27 @@ A fast XML parser and Object marshaller as a Ruby gem.
|
|
34
34
|
|
35
35
|
## Release Notes
|
36
36
|
|
37
|
+
### Release 2.4.0
|
38
|
+
|
39
|
+
- Added Ox::Builder that constructs an XML string or writes XML to a stream
|
40
|
+
using builder methods.
|
41
|
+
|
37
42
|
### Release 2.3.0
|
38
43
|
|
39
|
-
- Added
|
44
|
+
- Added Ox::Element.replace_text() method.
|
40
45
|
|
41
|
-
-
|
46
|
+
- Ox::Element nodes variable is now always initialized to an empty Array.
|
42
47
|
|
43
|
-
-
|
48
|
+
- Ox::Element attributes variable is now always initialized to an empty Hash.
|
44
49
|
|
45
50
|
- A invalid_replace option has been added. It will replace invalid XML
|
46
51
|
character with a provided string. Strict effort now raises an exception if an
|
47
52
|
invalid character is encountered on dump or load.
|
48
53
|
|
49
|
-
-
|
54
|
+
- Ox.load and Ox.parse now allow for a callback block to handle multiple top
|
50
55
|
level entities in the input.
|
51
56
|
|
52
|
-
- The
|
53
|
-
|
54
|
-
### Release 2.2.4
|
55
|
-
|
56
|
-
- Changed the code to allow compilation on older compilers. No change in
|
57
|
-
functionality otherwise.
|
57
|
+
- The Ox SAX parser now supports strings as input directly without and IO wrapper.
|
58
58
|
|
59
59
|
## Description
|
60
60
|
|
data/ext/ox/buf.h
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
/* buf.h
|
2
|
+
* Copyright (c) 2014, Peter Ohler
|
3
|
+
* All rights reserved.
|
4
|
+
*
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
7
|
+
*
|
8
|
+
* - Redistributions of source code must retain the above copyright notice, this
|
9
|
+
* list of conditions and the following disclaimer.
|
10
|
+
*
|
11
|
+
* - Redistributions in binary form must reproduce the above copyright notice,
|
12
|
+
* this list of conditions and the following disclaimer in the documentation
|
13
|
+
* and/or other materials provided with the distribution.
|
14
|
+
*
|
15
|
+
* - Neither the name of Peter Ohler nor the names of its contributors may be
|
16
|
+
* used to endorse or promote products derived from this software without
|
17
|
+
* specific prior written permission.
|
18
|
+
*
|
19
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
*/
|
30
|
+
|
31
|
+
#ifndef __OX_BUF_H__
|
32
|
+
#define __OX_BUF_H__
|
33
|
+
|
34
|
+
#include <stdbool.h>
|
35
|
+
#include <unistd.h>
|
36
|
+
|
37
|
+
typedef struct _Buf {
|
38
|
+
char *head;
|
39
|
+
char *end;
|
40
|
+
char *tail;
|
41
|
+
int fd;
|
42
|
+
bool err;
|
43
|
+
char base[16384];
|
44
|
+
} *Buf;
|
45
|
+
|
46
|
+
inline static void
|
47
|
+
buf_init(Buf buf, int fd, long initial_size) {
|
48
|
+
if (sizeof(buf->base) < initial_size) {
|
49
|
+
buf->head = ALLOC_N(char, initial_size);
|
50
|
+
buf->end = buf->head + initial_size - 1;
|
51
|
+
} else {
|
52
|
+
buf->head = buf->base;
|
53
|
+
buf->end = buf->base + sizeof(buf->base) - 1;
|
54
|
+
}
|
55
|
+
buf->tail = buf->head;
|
56
|
+
buf->fd = fd;
|
57
|
+
buf->err = false;
|
58
|
+
}
|
59
|
+
|
60
|
+
inline static void
|
61
|
+
buf_reset(Buf buf) {
|
62
|
+
buf->head = buf->base;
|
63
|
+
buf->tail = buf->head;
|
64
|
+
}
|
65
|
+
|
66
|
+
inline static void
|
67
|
+
buf_cleanup(Buf buf) {
|
68
|
+
if (buf->base != buf->head) {
|
69
|
+
free(buf->head);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
inline static size_t
|
74
|
+
buf_len(Buf buf) {
|
75
|
+
return buf->tail - buf->head;
|
76
|
+
}
|
77
|
+
|
78
|
+
inline static void
|
79
|
+
buf_append_string(Buf buf, const char *s, size_t slen) {
|
80
|
+
if (buf->err) {
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
if (buf->end <= buf->tail + slen) {
|
84
|
+
if (0 != buf->fd) {
|
85
|
+
size_t len = buf->tail - buf->head;
|
86
|
+
|
87
|
+
if (len != (size_t)write(buf->fd, buf->head, len)) {
|
88
|
+
buf->err = true;
|
89
|
+
}
|
90
|
+
buf->tail = buf->head;
|
91
|
+
} else {
|
92
|
+
size_t len = buf->end - buf->head;
|
93
|
+
size_t toff = buf->tail - buf->head;
|
94
|
+
size_t new_len = len + slen + len / 2;
|
95
|
+
|
96
|
+
if (buf->base == buf->head) {
|
97
|
+
buf->head = ALLOC_N(char, new_len);
|
98
|
+
memcpy(buf->head, buf->base, len);
|
99
|
+
} else {
|
100
|
+
REALLOC_N(buf->head, char, new_len);
|
101
|
+
}
|
102
|
+
buf->tail = buf->head + toff;
|
103
|
+
buf->end = buf->head + new_len - 2;
|
104
|
+
}
|
105
|
+
}
|
106
|
+
if (0 < slen) {
|
107
|
+
memcpy(buf->tail, s, slen);
|
108
|
+
}
|
109
|
+
buf->tail += slen;
|
110
|
+
}
|
111
|
+
|
112
|
+
inline static void
|
113
|
+
buf_append(Buf buf, char c) {
|
114
|
+
if (buf->err) {
|
115
|
+
return;
|
116
|
+
}
|
117
|
+
if (buf->end <= buf->tail) {
|
118
|
+
if (0 != buf->fd) {
|
119
|
+
size_t len = buf->tail - buf->head;
|
120
|
+
|
121
|
+
if (len != (size_t)write(buf->fd, buf->head, len)) {
|
122
|
+
buf->err = true;
|
123
|
+
}
|
124
|
+
buf->tail = buf->head;
|
125
|
+
} else {
|
126
|
+
size_t len = buf->end - buf->head;
|
127
|
+
size_t toff = buf->tail - buf->head;
|
128
|
+
size_t new_len = len + len / 2;
|
129
|
+
|
130
|
+
if (buf->base == buf->head) {
|
131
|
+
buf->head = ALLOC_N(char, new_len);
|
132
|
+
memcpy(buf->head, buf->base, len);
|
133
|
+
} else {
|
134
|
+
REALLOC_N(buf->head, char, new_len);
|
135
|
+
}
|
136
|
+
buf->tail = buf->head + toff;
|
137
|
+
buf->end = buf->head + new_len - 2;
|
138
|
+
}
|
139
|
+
}
|
140
|
+
*buf->tail++ = c;
|
141
|
+
//*buf->tail = '\0'; // for debugging
|
142
|
+
}
|
143
|
+
|
144
|
+
inline static void
|
145
|
+
buf_finish(Buf buf) {
|
146
|
+
if (buf->err) {
|
147
|
+
return;
|
148
|
+
}
|
149
|
+
if (0 != buf->fd) {
|
150
|
+
size_t len = buf->tail - buf->head;
|
151
|
+
|
152
|
+
if (0 < len && len != (size_t)write(buf->fd, buf->head, len)) {
|
153
|
+
buf->err = true;
|
154
|
+
}
|
155
|
+
fsync(buf->fd);
|
156
|
+
buf->tail = buf->head;
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
#endif /* __OX_BUF_H__ */
|
data/ext/ox/builder.c
ADDED
@@ -0,0 +1,685 @@
|
|
1
|
+
/* builder.c
|
2
|
+
* Copyright (c) 2011, 2016 Peter Ohler
|
3
|
+
* All rights reserved.
|
4
|
+
*/
|
5
|
+
|
6
|
+
#include <errno.h>
|
7
|
+
#include <stdlib.h>
|
8
|
+
#include <stdio.h>
|
9
|
+
#include <string.h>
|
10
|
+
|
11
|
+
#include "ox.h"
|
12
|
+
#include "buf.h"
|
13
|
+
#include "err.h"
|
14
|
+
|
15
|
+
#define MAX_DEPTH 128
|
16
|
+
|
17
|
+
typedef struct _Element {
|
18
|
+
char *name;
|
19
|
+
char buf[64];
|
20
|
+
int len;
|
21
|
+
bool has_child;
|
22
|
+
bool non_text_child;
|
23
|
+
} *Element;
|
24
|
+
|
25
|
+
typedef struct _Builder {
|
26
|
+
struct _Buf buf;
|
27
|
+
int indent;
|
28
|
+
char encoding[64];
|
29
|
+
int depth;
|
30
|
+
FILE *file;
|
31
|
+
struct _Element stack[MAX_DEPTH];
|
32
|
+
} *Builder;
|
33
|
+
|
34
|
+
static VALUE builder_class = Qundef;
|
35
|
+
static const char indent_spaces[] = "\n "; // 128 spaces
|
36
|
+
|
37
|
+
// The : character is equivalent to 10. Used for replacement characters up to 10
|
38
|
+
// characters long such as ''.
|
39
|
+
static char xml_friendly_chars[257] = "\
|
40
|
+
:::::::::11::1::::::::::::::::::\
|
41
|
+
11611156111111111111111111114141\
|
42
|
+
11111111111111111111111111111111\
|
43
|
+
11111111111111111111111111111111\
|
44
|
+
11111111111111111111111111111111\
|
45
|
+
11111111111111111111111111111111\
|
46
|
+
11111111111111111111111111111111\
|
47
|
+
11111111111111111111111111111111";
|
48
|
+
|
49
|
+
inline static size_t
|
50
|
+
xml_str_len(const unsigned char *str, size_t len) {
|
51
|
+
size_t size = 0;
|
52
|
+
|
53
|
+
for (; 0 < len; str++, len--) {
|
54
|
+
size += xml_friendly_chars[*str];
|
55
|
+
}
|
56
|
+
return size - len * (size_t)'0';
|
57
|
+
}
|
58
|
+
|
59
|
+
static void
|
60
|
+
append_indent(Builder b) {
|
61
|
+
if (0 == b->indent) {
|
62
|
+
return;
|
63
|
+
}
|
64
|
+
if (b->buf.head < b->buf.tail) {
|
65
|
+
int cnt = (b->indent * (b->depth + 1)) + 1;
|
66
|
+
|
67
|
+
if (sizeof(indent_spaces) <= cnt) {
|
68
|
+
cnt = sizeof(indent_spaces) - 1;
|
69
|
+
}
|
70
|
+
buf_append_string(&b->buf, indent_spaces, cnt);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
74
|
+
static void
|
75
|
+
append_string(Buf b, const char *str, size_t size) {
|
76
|
+
size_t xsize = xml_str_len((const unsigned char*)str, size);
|
77
|
+
|
78
|
+
if (size == xsize) {
|
79
|
+
buf_append_string(b, str, size);
|
80
|
+
} else {
|
81
|
+
char buf[256];
|
82
|
+
char *end = buf + sizeof(buf) - 1;
|
83
|
+
char *bp = buf;
|
84
|
+
int i = size;
|
85
|
+
|
86
|
+
for (; '\0' != *str && 0 < i; i--, str++) {
|
87
|
+
if ('1' == xml_friendly_chars[(unsigned char)*str]) {
|
88
|
+
if (end <= bp) {
|
89
|
+
buf_append_string(b, buf, bp - buf);
|
90
|
+
bp = buf;
|
91
|
+
}
|
92
|
+
*bp++ = *str;
|
93
|
+
} else {
|
94
|
+
if (buf < bp) {
|
95
|
+
buf_append_string(b, buf, bp - buf);
|
96
|
+
bp = buf;
|
97
|
+
}
|
98
|
+
switch (*str) {
|
99
|
+
case '"':
|
100
|
+
buf_append_string(b, """, 6);
|
101
|
+
break;
|
102
|
+
case '&':
|
103
|
+
buf_append_string(b, "&", 5);
|
104
|
+
break;
|
105
|
+
case '\'':
|
106
|
+
buf_append_string(b, "'", 6);
|
107
|
+
break;
|
108
|
+
case '<':
|
109
|
+
buf_append_string(b, "<", 4);
|
110
|
+
break;
|
111
|
+
case '>':
|
112
|
+
buf_append_string(b, ">", 4);
|
113
|
+
break;
|
114
|
+
default:
|
115
|
+
// Must be one of the invalid characters.
|
116
|
+
rb_raise(rb_eSyntaxError, "'\\#x%02x' is not a valid XML character.", *str);
|
117
|
+
break;
|
118
|
+
}
|
119
|
+
}
|
120
|
+
}
|
121
|
+
if (buf < bp) {
|
122
|
+
buf_append_string(b, buf, bp - buf);
|
123
|
+
bp = buf;
|
124
|
+
}
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
static void
|
129
|
+
append_sym_str(Buf b, VALUE v) {
|
130
|
+
const char *s;
|
131
|
+
int len;
|
132
|
+
|
133
|
+
switch (rb_type(v)) {
|
134
|
+
case T_STRING:
|
135
|
+
s = StringValuePtr(v);
|
136
|
+
len = RSTRING_LEN(v);
|
137
|
+
break;
|
138
|
+
case T_SYMBOL:
|
139
|
+
s = rb_id2name(SYM2ID(v));
|
140
|
+
len = strlen(s);
|
141
|
+
break;
|
142
|
+
default:
|
143
|
+
rb_raise(ox_arg_error_class, "expected a Symbol or String");
|
144
|
+
break;
|
145
|
+
}
|
146
|
+
append_string(b, s, len);
|
147
|
+
}
|
148
|
+
|
149
|
+
static void
|
150
|
+
i_am_a_child(Builder b, bool is_text) {
|
151
|
+
if (0 <= b->depth) {
|
152
|
+
Element e = &b->stack[b->depth];
|
153
|
+
|
154
|
+
if (!e->has_child) {
|
155
|
+
e->has_child = true;
|
156
|
+
buf_append(&b->buf, '>');
|
157
|
+
}
|
158
|
+
if (!is_text) {
|
159
|
+
e->non_text_child = true;
|
160
|
+
}
|
161
|
+
}
|
162
|
+
}
|
163
|
+
|
164
|
+
static int
|
165
|
+
append_attr(VALUE key, VALUE value, Builder b) {
|
166
|
+
buf_append(&b->buf, ' ');
|
167
|
+
append_sym_str(&b->buf, key);
|
168
|
+
buf_append_string(&b->buf, "=\"", 2);
|
169
|
+
Check_Type(value, T_STRING);
|
170
|
+
buf_append_string(&b->buf, StringValuePtr(value), RSTRING_LEN(value));
|
171
|
+
buf_append(&b->buf, '"');
|
172
|
+
|
173
|
+
return ST_CONTINUE;
|
174
|
+
}
|
175
|
+
|
176
|
+
static void
|
177
|
+
init(Builder b, int fd, int indent, long initial_size) {
|
178
|
+
buf_init(&b->buf, fd, initial_size);
|
179
|
+
b->indent = indent;
|
180
|
+
*b->encoding = '\0';
|
181
|
+
b->depth = -1;
|
182
|
+
}
|
183
|
+
|
184
|
+
static void
|
185
|
+
builder_free(void *ptr) {
|
186
|
+
Builder b;
|
187
|
+
Element e;
|
188
|
+
int d;
|
189
|
+
|
190
|
+
if (0 == ptr) {
|
191
|
+
return;
|
192
|
+
}
|
193
|
+
b = (Builder)ptr;
|
194
|
+
buf_cleanup(&b->buf);
|
195
|
+
for (e = b->stack, d = b->depth; 0 < d; d--, e++) {
|
196
|
+
if (e->name != e->buf) {
|
197
|
+
free(e->name);
|
198
|
+
}
|
199
|
+
}
|
200
|
+
xfree(ptr);
|
201
|
+
}
|
202
|
+
|
203
|
+
static void
|
204
|
+
pop(Builder b) {
|
205
|
+
Element e;
|
206
|
+
|
207
|
+
if (0 > b->depth) {
|
208
|
+
rb_raise(ox_arg_error_class, "closed to many element");
|
209
|
+
}
|
210
|
+
e = &b->stack[b->depth];
|
211
|
+
b->depth--;
|
212
|
+
if (e->has_child) {
|
213
|
+
if (e->non_text_child) {
|
214
|
+
append_indent(b);
|
215
|
+
}
|
216
|
+
buf_append_string(&b->buf, "</", 2);
|
217
|
+
buf_append_string(&b->buf, e->name, e->len);
|
218
|
+
buf_append(&b->buf, '>');
|
219
|
+
if (e->buf != e->name) {
|
220
|
+
free(e->name);
|
221
|
+
e->name = 0;
|
222
|
+
}
|
223
|
+
} else {
|
224
|
+
buf_append_string(&b->buf, "/>", 2);
|
225
|
+
}
|
226
|
+
}
|
227
|
+
|
228
|
+
static void
|
229
|
+
bclose(Builder b) {
|
230
|
+
while (0 <= b->depth) {
|
231
|
+
pop(b);
|
232
|
+
}
|
233
|
+
buf_append(&b->buf, '\n');
|
234
|
+
buf_finish(&b->buf);
|
235
|
+
if (NULL != b->file) {
|
236
|
+
fclose(b->file);
|
237
|
+
}
|
238
|
+
}
|
239
|
+
|
240
|
+
static VALUE
|
241
|
+
to_s(Builder b) {
|
242
|
+
volatile VALUE rstr;
|
243
|
+
|
244
|
+
if (0 != b->buf.fd) {
|
245
|
+
rb_raise(ox_arg_error_class, "can not create a String with a stream or file builder.");
|
246
|
+
}
|
247
|
+
if ('\n' != *(b->buf.tail - 1)) {
|
248
|
+
buf_append(&b->buf, '\n');
|
249
|
+
}
|
250
|
+
*b->buf.tail = '\0'; // for debugging
|
251
|
+
rstr = rb_str_new(b->buf.head, buf_len(&b->buf));
|
252
|
+
|
253
|
+
if ('\0' != *b->encoding) {
|
254
|
+
#if HAS_ENCODING_SUPPORT
|
255
|
+
rb_enc_associate(rstr, rb_enc_find(b->encoding));
|
256
|
+
#endif
|
257
|
+
}
|
258
|
+
return rstr;
|
259
|
+
}
|
260
|
+
|
261
|
+
/* call-seq: new(options)
|
262
|
+
*
|
263
|
+
* Creates a new Builder that will write to a string that can be retrieved with
|
264
|
+
* the to_s() method. If a block is given it is executed with a single parameter
|
265
|
+
* which is the builder instance. The return value is then the generated string.
|
266
|
+
*
|
267
|
+
* - +options+ - (Hash) formating options
|
268
|
+
* - +:indent+ (Fixnum) indentaion level
|
269
|
+
* - +:size+ (Fixnum) the initial size of the string buffer
|
270
|
+
*/
|
271
|
+
static VALUE
|
272
|
+
builder_new(int argc, VALUE *argv, VALUE self) {
|
273
|
+
Builder b = ALLOC(struct _Builder);
|
274
|
+
int indent = ox_default_options.indent;
|
275
|
+
long buf_size = 0;
|
276
|
+
|
277
|
+
if (1 == argc) {
|
278
|
+
volatile VALUE v;
|
279
|
+
|
280
|
+
rb_check_type(*argv, T_HASH);
|
281
|
+
if (Qnil != (v = rb_hash_lookup(*argv, ox_indent_sym))) {
|
282
|
+
if (rb_cFixnum != rb_obj_class(v)) {
|
283
|
+
rb_raise(ox_parse_error_class, ":indent must be a fixnum.\n");
|
284
|
+
}
|
285
|
+
indent = NUM2INT(v);
|
286
|
+
}
|
287
|
+
if (Qnil != (v = rb_hash_lookup(*argv, ox_size_sym))) {
|
288
|
+
if (rb_cFixnum != rb_obj_class(v)) {
|
289
|
+
rb_raise(ox_parse_error_class, ":size must be a fixnum.\n");
|
290
|
+
}
|
291
|
+
buf_size = NUM2LONG(v);
|
292
|
+
}
|
293
|
+
}
|
294
|
+
b->file = NULL;
|
295
|
+
init(b, 0, indent, buf_size);
|
296
|
+
|
297
|
+
if (rb_block_given_p()) {
|
298
|
+
volatile VALUE rb = Data_Wrap_Struct(builder_class, NULL, builder_free, b);
|
299
|
+
rb_yield(rb);
|
300
|
+
bclose(b);
|
301
|
+
|
302
|
+
return to_s(b);
|
303
|
+
} else {
|
304
|
+
return Data_Wrap_Struct(builder_class, NULL, builder_free, b);
|
305
|
+
}
|
306
|
+
}
|
307
|
+
|
308
|
+
/* call-seq: file(filename, options)
|
309
|
+
*
|
310
|
+
* Creates a new Builder that will write to a file.
|
311
|
+
*
|
312
|
+
* - +filename+ (String) filename to write to
|
313
|
+
* - +options+ - (Hash) formating options
|
314
|
+
* - +:indent+ (Fixnum) indentaion level
|
315
|
+
* - +:size+ (Fixnum) the initial size of the string buffer
|
316
|
+
*/
|
317
|
+
static VALUE
|
318
|
+
builder_file(int argc, VALUE *argv, VALUE self) {
|
319
|
+
Builder b = ALLOC(struct _Builder);
|
320
|
+
int indent = ox_default_options.indent;
|
321
|
+
long buf_size = 0;
|
322
|
+
FILE *f;
|
323
|
+
|
324
|
+
if (1 > argc) {
|
325
|
+
rb_raise(ox_arg_error_class, "missing filename");
|
326
|
+
}
|
327
|
+
Check_Type(*argv, T_STRING);
|
328
|
+
if (NULL == (f = fopen(StringValuePtr(*argv), "w"))) {
|
329
|
+
xfree(b);
|
330
|
+
rb_raise(rb_eIOError, "%s\n", strerror(errno));
|
331
|
+
}
|
332
|
+
if (2 == argc) {
|
333
|
+
volatile VALUE v;
|
334
|
+
|
335
|
+
rb_check_type(argv[1], T_HASH);
|
336
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], ox_indent_sym))) {
|
337
|
+
if (rb_cFixnum != rb_obj_class(v)) {
|
338
|
+
rb_raise(ox_parse_error_class, ":indent must be a fixnum.\n");
|
339
|
+
}
|
340
|
+
indent = NUM2INT(v);
|
341
|
+
}
|
342
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], ox_size_sym))) {
|
343
|
+
if (rb_cFixnum != rb_obj_class(v)) {
|
344
|
+
rb_raise(ox_parse_error_class, ":size must be a fixnum.\n");
|
345
|
+
}
|
346
|
+
buf_size = NUM2LONG(v);
|
347
|
+
}
|
348
|
+
}
|
349
|
+
b->file = f;
|
350
|
+
init(b, fileno(f), indent, buf_size);
|
351
|
+
|
352
|
+
if (rb_block_given_p()) {
|
353
|
+
volatile VALUE rb = Data_Wrap_Struct(builder_class, NULL, builder_free, b);
|
354
|
+
rb_yield(rb);
|
355
|
+
bclose(b);
|
356
|
+
return Qnil;
|
357
|
+
} else {
|
358
|
+
return Data_Wrap_Struct(builder_class, NULL, builder_free, b);
|
359
|
+
}
|
360
|
+
}
|
361
|
+
|
362
|
+
/* call-seq: io(io, options)
|
363
|
+
*
|
364
|
+
* Creates a new Builder that will write to an IO instance.
|
365
|
+
*
|
366
|
+
* - +io+ (String) IO to write to
|
367
|
+
* - +options+ - (Hash) formating options
|
368
|
+
* - +:indent+ (Fixnum) indentaion level
|
369
|
+
* - +:size+ (Fixnum) the initial size of the string buffer
|
370
|
+
*/
|
371
|
+
static VALUE
|
372
|
+
builder_io(int argc, VALUE *argv, VALUE self) {
|
373
|
+
Builder b = ALLOC(struct _Builder);
|
374
|
+
int indent = ox_default_options.indent;
|
375
|
+
long buf_size = 0;
|
376
|
+
int fd;
|
377
|
+
volatile VALUE v;
|
378
|
+
|
379
|
+
if (1 > argc) {
|
380
|
+
rb_raise(ox_arg_error_class, "missing IO object");
|
381
|
+
}
|
382
|
+
if (!rb_respond_to(*argv, ox_fileno_id) ||
|
383
|
+
Qnil == (v = rb_funcall(*argv, ox_fileno_id, 0)) ||
|
384
|
+
0 == (fd = FIX2INT(v))) {
|
385
|
+
rb_raise(rb_eIOError, "expected an IO that has a fileno.");
|
386
|
+
}
|
387
|
+
if (2 == argc) {
|
388
|
+
volatile VALUE v;
|
389
|
+
|
390
|
+
rb_check_type(argv[1], T_HASH);
|
391
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], ox_indent_sym))) {
|
392
|
+
if (rb_cFixnum != rb_obj_class(v)) {
|
393
|
+
rb_raise(ox_parse_error_class, ":indent must be a fixnum.\n");
|
394
|
+
}
|
395
|
+
indent = NUM2INT(v);
|
396
|
+
}
|
397
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], ox_size_sym))) {
|
398
|
+
if (rb_cFixnum != rb_obj_class(v)) {
|
399
|
+
rb_raise(ox_parse_error_class, ":size must be a fixnum.\n");
|
400
|
+
}
|
401
|
+
buf_size = NUM2LONG(v);
|
402
|
+
}
|
403
|
+
}
|
404
|
+
b->file = NULL;
|
405
|
+
init(b, fd, indent, buf_size);
|
406
|
+
|
407
|
+
if (rb_block_given_p()) {
|
408
|
+
volatile VALUE rb = Data_Wrap_Struct(builder_class, NULL, builder_free, b);
|
409
|
+
rb_yield(rb);
|
410
|
+
bclose(b);
|
411
|
+
return Qnil;
|
412
|
+
} else {
|
413
|
+
return Data_Wrap_Struct(builder_class, NULL, builder_free, b);
|
414
|
+
}
|
415
|
+
}
|
416
|
+
|
417
|
+
/* call-seq: instruct(decl,options)
|
418
|
+
*
|
419
|
+
* Adds the top level <?xml?> element.
|
420
|
+
*
|
421
|
+
* - +decl+ - (String) 'xml' expected
|
422
|
+
* - +options+ - (Hash) version or encoding
|
423
|
+
*/
|
424
|
+
static VALUE
|
425
|
+
builder_instruct(int argc, VALUE *argv, VALUE self) {
|
426
|
+
Builder b = (Builder)DATA_PTR(self);
|
427
|
+
|
428
|
+
i_am_a_child(b, false);
|
429
|
+
append_indent(b);
|
430
|
+
if (0 == argc) {
|
431
|
+
buf_append_string(&b->buf, "<?xml?>", 7);
|
432
|
+
} else {
|
433
|
+
volatile VALUE v;
|
434
|
+
|
435
|
+
buf_append_string(&b->buf, "<?", 2);
|
436
|
+
append_sym_str(&b->buf, *argv);
|
437
|
+
if (1 < argc && rb_cHash == rb_obj_class(argv[1])) {
|
438
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], ox_version_sym))) {
|
439
|
+
if (rb_cString != rb_obj_class(v)) {
|
440
|
+
rb_raise(ox_parse_error_class, ":version must be a Symbol.\n");
|
441
|
+
}
|
442
|
+
buf_append_string(&b->buf, " version=\"", 10);
|
443
|
+
buf_append_string(&b->buf, StringValuePtr(v), RSTRING_LEN(v));
|
444
|
+
buf_append(&b->buf, '"');
|
445
|
+
}
|
446
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], ox_encoding_sym))) {
|
447
|
+
if (rb_cString != rb_obj_class(v)) {
|
448
|
+
rb_raise(ox_parse_error_class, ":encoding must be a Symbol.\n");
|
449
|
+
}
|
450
|
+
buf_append_string(&b->buf, " encoding=\"", 11);
|
451
|
+
buf_append_string(&b->buf, StringValuePtr(v), RSTRING_LEN(v));
|
452
|
+
buf_append(&b->buf, '"');
|
453
|
+
strncpy(b->encoding, StringValuePtr(v), sizeof(b->encoding));
|
454
|
+
b->encoding[sizeof(b->encoding) - 1] = '\0';
|
455
|
+
}
|
456
|
+
if (Qnil != (v = rb_hash_lookup(argv[1], ox_standalone_sym))) {
|
457
|
+
if (rb_cString != rb_obj_class(v)) {
|
458
|
+
rb_raise(ox_parse_error_class, ":standalone must be a Symbol.\n");
|
459
|
+
}
|
460
|
+
buf_append_string(&b->buf, " standalone=\"", 13);
|
461
|
+
buf_append_string(&b->buf, StringValuePtr(v), RSTRING_LEN(v));
|
462
|
+
buf_append(&b->buf, '"');
|
463
|
+
}
|
464
|
+
}
|
465
|
+
buf_append_string(&b->buf, "?>", 2);
|
466
|
+
}
|
467
|
+
return Qnil;
|
468
|
+
}
|
469
|
+
|
470
|
+
/* call-seq: element(name,attributes)
|
471
|
+
*
|
472
|
+
* Adds an element with the name and attributes provided. If a block is given
|
473
|
+
* then on closing of the block a pop() done at the close of the block.
|
474
|
+
*
|
475
|
+
* - +name+ - (String) name of the element
|
476
|
+
* - +attributes+ - (Hash) of the element
|
477
|
+
*/
|
478
|
+
static VALUE
|
479
|
+
builder_element(int argc, VALUE *argv, VALUE self) {
|
480
|
+
Builder b = (Builder)DATA_PTR(self);
|
481
|
+
Element e;
|
482
|
+
const char *name;
|
483
|
+
int len;
|
484
|
+
|
485
|
+
if (1 > argc) {
|
486
|
+
rb_raise(ox_arg_error_class, "missing element name");
|
487
|
+
}
|
488
|
+
i_am_a_child(b, false);
|
489
|
+
append_indent(b);
|
490
|
+
b->depth++;
|
491
|
+
if (MAX_DEPTH <= b->depth) {
|
492
|
+
rb_raise(ox_arg_error_class, "XML too deeply nested");
|
493
|
+
}
|
494
|
+
switch (rb_type(*argv)) {
|
495
|
+
case T_STRING:
|
496
|
+
name = StringValuePtr(*argv);
|
497
|
+
len = RSTRING_LEN(*argv);
|
498
|
+
break;
|
499
|
+
case T_SYMBOL:
|
500
|
+
name = rb_id2name(SYM2ID(*argv));
|
501
|
+
len = strlen(name);
|
502
|
+
break;
|
503
|
+
default:
|
504
|
+
rb_raise(ox_arg_error_class, "expected a Symbol or String for an element name");
|
505
|
+
break;
|
506
|
+
}
|
507
|
+
e = &b->stack[b->depth];
|
508
|
+
if (sizeof(e->buf) <= len) {
|
509
|
+
e->name = strdup(name);
|
510
|
+
*e->buf = '\0';
|
511
|
+
} else {
|
512
|
+
strcpy(e->buf, name);
|
513
|
+
e->name = e->buf;
|
514
|
+
}
|
515
|
+
e->len = len;
|
516
|
+
e->has_child = false;
|
517
|
+
e->non_text_child = false;
|
518
|
+
|
519
|
+
buf_append(&b->buf, '<');
|
520
|
+
buf_append_string(&b->buf, e->name, len);
|
521
|
+
if (1 < argc) {
|
522
|
+
rb_hash_foreach(argv[1], append_attr, (VALUE)b);
|
523
|
+
}
|
524
|
+
// Do not close with > or /> yet. That is done with i_am_a_child() or pop().
|
525
|
+
if (rb_block_given_p()) {
|
526
|
+
rb_yield(self);
|
527
|
+
pop(b);
|
528
|
+
}
|
529
|
+
return Qnil;
|
530
|
+
}
|
531
|
+
|
532
|
+
/* call-seq: comment(text)
|
533
|
+
*
|
534
|
+
* Adds a comment element to the XML string being formed.
|
535
|
+
* - +text+ - (String) contents of the comment
|
536
|
+
*/
|
537
|
+
static VALUE
|
538
|
+
builder_comment(VALUE self, VALUE text) {
|
539
|
+
Builder b = (Builder)DATA_PTR(self);
|
540
|
+
|
541
|
+
rb_check_type(text, T_STRING);
|
542
|
+
i_am_a_child(b, false);
|
543
|
+
append_indent(b);
|
544
|
+
buf_append_string(&b->buf, "<!-- ", 5);
|
545
|
+
buf_append_string(&b->buf, StringValuePtr(text), RSTRING_LEN(text));
|
546
|
+
buf_append_string(&b->buf, " --/> ", 5);
|
547
|
+
|
548
|
+
return Qnil;
|
549
|
+
}
|
550
|
+
|
551
|
+
/* call-seq: doctype(text)
|
552
|
+
*
|
553
|
+
* Adds a DOCTYPE element to the XML string being formed.
|
554
|
+
* - +text+ - (String) contents of the doctype
|
555
|
+
*/
|
556
|
+
static VALUE
|
557
|
+
builder_doctype(VALUE self, VALUE text) {
|
558
|
+
Builder b = (Builder)DATA_PTR(self);
|
559
|
+
|
560
|
+
rb_check_type(text, T_STRING);
|
561
|
+
i_am_a_child(b, false);
|
562
|
+
append_indent(b);
|
563
|
+
buf_append_string(&b->buf, "<!DOCTYPE ", 10);
|
564
|
+
buf_append_string(&b->buf, StringValuePtr(text), RSTRING_LEN(text));
|
565
|
+
buf_append(&b->buf, '>');
|
566
|
+
|
567
|
+
return Qnil;
|
568
|
+
}
|
569
|
+
|
570
|
+
/* call-seq: text(text)
|
571
|
+
*
|
572
|
+
* Adds a text element to the XML string being formed.
|
573
|
+
* - +text+ - (String) contents of the text field
|
574
|
+
*/
|
575
|
+
static VALUE
|
576
|
+
builder_text(VALUE self, VALUE text) {
|
577
|
+
Builder b = (Builder)DATA_PTR(self);
|
578
|
+
volatile VALUE v = text;
|
579
|
+
|
580
|
+
if (T_STRING != rb_type(v)) {
|
581
|
+
v = rb_funcall(v, ox_to_s_id, 0);
|
582
|
+
}
|
583
|
+
i_am_a_child(b, true);
|
584
|
+
append_string(&b->buf, StringValuePtr(v), RSTRING_LEN(v));
|
585
|
+
|
586
|
+
return Qnil;
|
587
|
+
}
|
588
|
+
|
589
|
+
/* call-seq: cdata(data)
|
590
|
+
*
|
591
|
+
* Adds a CDATA element to the XML string being formed.
|
592
|
+
* - +data+ - (String) contents of the CDATA element
|
593
|
+
*/
|
594
|
+
static VALUE
|
595
|
+
builder_cdata(VALUE self, VALUE data) {
|
596
|
+
Builder b = (Builder)DATA_PTR(self);
|
597
|
+
volatile VALUE v = data;
|
598
|
+
|
599
|
+
if (T_STRING != rb_type(v)) {
|
600
|
+
v = rb_funcall(v, ox_to_s_id, 0);
|
601
|
+
}
|
602
|
+
i_am_a_child(b, false);
|
603
|
+
append_indent(b);
|
604
|
+
buf_append_string(&b->buf, "<![CDATA[", 9);
|
605
|
+
buf_append_string(&b->buf, StringValuePtr(v), RSTRING_LEN(v));
|
606
|
+
buf_append_string(&b->buf, "]]>", 3);
|
607
|
+
|
608
|
+
return Qnil;
|
609
|
+
}
|
610
|
+
|
611
|
+
/* call-seq: raw(text)
|
612
|
+
*
|
613
|
+
* Adds the provided string directly to the XML without formatting or modifications.
|
614
|
+
*
|
615
|
+
* - +text+ - (String) contents to be added
|
616
|
+
*/
|
617
|
+
static VALUE
|
618
|
+
builder_raw(VALUE self, VALUE text) {
|
619
|
+
Builder b = (Builder)DATA_PTR(self);
|
620
|
+
volatile VALUE v = text;
|
621
|
+
|
622
|
+
if (T_STRING != rb_type(v)) {
|
623
|
+
v = rb_funcall(v, ox_to_s_id, 0);
|
624
|
+
}
|
625
|
+
i_am_a_child(b, true);
|
626
|
+
buf_append_string(&b->buf, StringValuePtr(v), RSTRING_LEN(v));
|
627
|
+
|
628
|
+
return Qnil;
|
629
|
+
}
|
630
|
+
|
631
|
+
/* call-seq: to_s()
|
632
|
+
*
|
633
|
+
* Returns the JSON document string in what ever state the construction is at.
|
634
|
+
*/
|
635
|
+
static VALUE
|
636
|
+
builder_to_s(VALUE self) {
|
637
|
+
return to_s((Builder)DATA_PTR(self));
|
638
|
+
}
|
639
|
+
|
640
|
+
/* call-seq: pop()
|
641
|
+
*
|
642
|
+
* Closes the current element.
|
643
|
+
*/
|
644
|
+
static VALUE
|
645
|
+
builder_pop(VALUE self) {
|
646
|
+
pop((Builder)DATA_PTR(self));
|
647
|
+
|
648
|
+
return Qnil;
|
649
|
+
}
|
650
|
+
|
651
|
+
/* call-seq: close()
|
652
|
+
*
|
653
|
+
* Closes the all elements and the document.
|
654
|
+
*/
|
655
|
+
static VALUE
|
656
|
+
builder_close(VALUE self) {
|
657
|
+
bclose((Builder)DATA_PTR(self));
|
658
|
+
|
659
|
+
return Qnil;
|
660
|
+
}
|
661
|
+
|
662
|
+
/*
|
663
|
+
* Document-class: Ox::Builder
|
664
|
+
*
|
665
|
+
* An XML builder.
|
666
|
+
*/
|
667
|
+
void ox_init_builder(VALUE ox) {
|
668
|
+
#if 0
|
669
|
+
ox = rb_define_module("Ox");
|
670
|
+
#endif
|
671
|
+
builder_class = rb_define_class_under(ox, "Builder", rb_cObject);
|
672
|
+
rb_define_module_function(builder_class, "new", builder_new, -1);
|
673
|
+
rb_define_module_function(builder_class, "file", builder_file, -1);
|
674
|
+
rb_define_module_function(builder_class, "io", builder_io, -1);
|
675
|
+
rb_define_method(builder_class, "instruct", builder_instruct, -1);
|
676
|
+
rb_define_method(builder_class, "comment", builder_comment, 1);
|
677
|
+
rb_define_method(builder_class, "doctype", builder_doctype, 1);
|
678
|
+
rb_define_method(builder_class, "element", builder_element, -1);
|
679
|
+
rb_define_method(builder_class, "text", builder_text, 1);
|
680
|
+
rb_define_method(builder_class, "cdata", builder_cdata, 1);
|
681
|
+
rb_define_method(builder_class, "raw", builder_raw, 1);
|
682
|
+
rb_define_method(builder_class, "pop", builder_pop, 0);
|
683
|
+
rb_define_method(builder_class, "close", builder_close, 0);
|
684
|
+
rb_define_method(builder_class, "to_s", builder_to_s, 0);
|
685
|
+
}
|