libxml-ruby 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- /* $Id: ruby_xml_parser.c 585 2008-11-18 17:40:45Z cfis $ */
1
+ /* $Id: ruby_xml_parser.c 604 2008-11-19 18:01:55Z cfis $ */
2
2
 
3
3
  /* Please see the LICENSE file for copyright and distribution information */
4
4
 
@@ -6,8 +6,8 @@
6
6
  #include "ruby_libxml.h"
7
7
 
8
8
  VALUE cXMLParser;
9
- ID INPUT_ATTR;
10
- ID CONTEXT_ATTR;
9
+ static ID INPUT_ATTR;
10
+ static ID CONTEXT_ATTR;
11
11
 
12
12
  /*
13
13
  * Document-class: LibXML::XML::Parser
@@ -1,228 +1,228 @@
1
- /* $Id: ruby_xml_sax_parser.c 578 2008-11-18 09:23:26Z cfis $ */
2
-
3
- /* Please see the LICENSE file for copyright and distribution information */
4
-
5
- #include "ruby_libxml.h"
6
- #include "ruby_xml_sax_parser.h"
7
-
8
- /*
9
- * Document-class: LibXML::XML::SaxParser
10
- *
11
- * XML::SaxParser provides a callback based API for parsing documents,
12
- * in contrast to XML::Parser's tree based API and XML::Reader's stream
13
- * based API.
14
- *
15
- * Note that the XML::SaxParser API is fairly complex, not well standardized,
16
- * and does not directly support validation making entity, namespace and
17
- * base processing relatively hard.
18
- *
19
- * To use the XML::SaxParser, register a callback class via the
20
- * XML::SaxParser#callbacks=. It is easiest to include the
21
- * XML::SaxParser::Callbacks module in your class and override
22
- * the methods as needed.
23
- *
24
- * Basic example:
25
- *
26
- * class MyCallbacks
27
- * include XML::SaxParser::Callbacks
28
- * def on_start_element(element, attributes)
29
- * puts #Element started: #{element}"
30
- * end
31
- * end
32
- *
33
- * parser = XML::SaxParser.new
34
- * parser.callbacks = MyCallbacks.new
35
- * parser.parse
36
- */
37
-
38
- VALUE cXMLSaxParser;
39
- VALUE mXMLSaxParserCallbacks;
40
-
41
- ID INPUT_ATTR;
42
-
43
- VALUE cbidOnInternalSubset;
44
- VALUE cbidOnIsStandalone;
45
- VALUE cbidOnHasInternalSubset;
46
- VALUE cbidOnHasExternalSubset;
47
- VALUE cbidOnStartDocument;
48
- VALUE cbidOnEndDocument;
49
- VALUE cbidOnStartElement;
50
- VALUE cbidOnEndElement;
51
- VALUE cbidOnReference;
52
- VALUE cbidOnCharacters;
53
- VALUE cbidOnProcessingInstruction;
54
- VALUE cbidOnComment;
55
- VALUE cbidOnXmlParserWarning;
56
- VALUE cbidOnXmlParserError;
57
- VALUE cbidOnXmlParserFatalError;
58
- VALUE cbidOnCdataBlock;
59
- VALUE cbidOnExternalSubset;
60
-
61
- #include "sax_parser_callbacks.inc"
62
-
63
- void
64
- ruby_xml_sax_parser_free(ruby_xml_sax_parser *rxsp) {
65
- xfree(rxsp);
66
- }
67
-
68
- void
69
- ruby_xml_sax_parser_mark(ruby_xml_sax_parser *rxsp) {
70
- if (rxsp->callbackHandler != Qnil) {
71
- rb_gc_mark(rxsp->callbackHandler);
72
- }
73
-
74
- if (rxsp->filename != Qnil) {
75
- rb_gc_mark(rxsp->filename);
76
- }
77
-
78
- if (rxsp->str != Qnil) {
79
- rb_gc_mark(rxsp->str);
80
- }
81
- }
82
-
83
- VALUE
84
- ruby_xml_sax_parser_alloc(VALUE klass) {
85
- ruby_xml_sax_parser *rxsp = ALLOC(ruby_xml_sax_parser);
86
- rxsp->xsh = &rubySAXHandlerStruct;
87
- rxsp->callbackHandler = Qnil;
88
- rxsp->xpc = NULL;
89
- rxsp->filename = Qnil;
90
- rxsp->str = Qnil;
91
-
92
- return Data_Wrap_Struct(cXMLSaxParser,
93
- ruby_xml_sax_parser_mark, ruby_xml_sax_parser_free,
94
- rxsp);
95
- }
96
-
97
-
98
- /*
99
- * call-seq:
100
- * sax_parser.initialize -> sax_parser
101
- *
102
- * Initiliazes instance of parser.
103
- */
104
- VALUE
105
- ruby_xml_sax_parser_initialize(VALUE self) {
106
- VALUE input = rb_class_new_instance(0, NULL, cXMLInput);
107
- rb_iv_set(self, "@input", input);
108
- return self;
109
- }
110
-
111
- /*
112
- * call-seq:
113
- * sax_parser.callbacks -> #<XML::SaxParser::Callbacks subclass>
114
- *
115
- * Obtain the callbacks used by this parser.
116
- */
117
- VALUE
118
- ruby_xml_sax_parser_callbacks_get(VALUE self) {
119
- ruby_xml_sax_parser *rxsp;
120
- Data_Get_Struct(self, ruby_xml_sax_parser, rxsp);
121
- return(rxsp->callbackHandler);
122
- }
123
-
124
-
125
- /*
126
- * call-seq:
127
- * sax_parser.callbacks = #<XML::SaxParser::Callbacks subclass>
128
- *
129
- * Set the callbacks used by this parser. The value assigned to
130
- * this attributesibute will usually be an object that extends the the
131
- * XML::SaxParser::Callbacks module, overriding the callbacks it
132
- * wishes to process.
133
- */
134
- VALUE
135
- ruby_xml_sax_parser_callbacks_set(VALUE self, VALUE callbacks) {
136
- ruby_xml_sax_parser *rxsp;
137
- Data_Get_Struct(self, ruby_xml_sax_parser, rxsp);
138
- rxsp->callbackHandler = callbacks;
139
- return(rxsp->callbackHandler);
140
- }
141
-
142
- /*
143
- * call-seq:
144
- * parser.parse -> (true|false)
145
- *
146
- * Parse the input XML, generating callbacks to the object
147
- * registered via the +callbacks+ attributesibute.
148
- */
149
- VALUE
150
- ruby_xml_sax_parser_parse(VALUE self) {
151
- char *str;
152
- int status = 1;
153
- ruby_xml_sax_parser *rxsp;
154
- VALUE source;
155
- VALUE input = rb_ivar_get(self, INPUT_ATTR);
156
-
157
- Data_Get_Struct(self, ruby_xml_sax_parser, rxsp);
158
-
159
- if (rb_ivar_get(input, FILE_ATTR) != Qnil)
160
- {
161
- source = rb_ivar_get(input, FILE_ATTR);
162
- status = xmlSAXUserParseFile(rxsp->xsh, rxsp, StringValuePtr(source));
163
- }
164
- else if (rb_ivar_get(input, STRING_ATTR) != Qnil)
165
- {
166
- source = rb_ivar_get(input, STRING_ATTR);
167
- str = StringValueCStr(source);
168
- status = xmlSAXUserParseMemory(rxsp->xsh, rxsp, str, strlen(str));
169
- }
170
- else
171
- {
172
- rb_raise(rb_eArgError, "You must specify a parser data source");
173
- }
174
-
175
- if (status)
176
- {
177
- ruby_xml_raise(&xmlLastError);
178
- return Qfalse;
179
- }
180
- else
181
- {
182
- return(Qtrue);
183
- }
184
- }
185
-
186
- // Rdoc needs to know
187
- #ifdef RDOC_NEVER_DEFINED
188
- mLibXML = rb_define_module("LibXML");
189
- mXML = rb_define_module_under(mLibXML, "XML");
190
- #endif
191
-
192
- void
193
- ruby_init_xml_sax_parser(void) {
194
- /* SaxParser */
195
- cXMLSaxParser = rb_define_class_under(mXML, "SaxParser", rb_cObject);
196
- rb_define_alloc_func(cXMLSaxParser, ruby_xml_sax_parser_alloc);
197
- rb_define_method(cXMLSaxParser, "callbacks", ruby_xml_sax_parser_callbacks_get, 0);
198
- rb_define_method(cXMLSaxParser, "callbacks=", ruby_xml_sax_parser_callbacks_set, 1);
199
- rb_define_method(cXMLSaxParser, "parse", ruby_xml_sax_parser_parse, 0);
200
-
201
- /* Atributes */
202
- rb_define_attr(cXMLSaxParser, "input", 1, 0);
203
-
204
- /* Instance Methods */
205
- rb_define_method(cXMLSaxParser, "initialize", ruby_xml_sax_parser_initialize, 0);
206
-
207
-
208
- INPUT_ATTR = rb_intern("@input");
209
-
210
- /* SaxCallbacks */
211
- cbidOnInternalSubset = rb_intern("on_internal_subset");
212
- cbidOnIsStandalone = rb_intern("on_is_standalone");
213
- cbidOnHasInternalSubset = rb_intern("on_has_internal_subset");
214
- cbidOnHasExternalSubset = rb_intern("on_has_external_subset");
215
- cbidOnStartDocument = rb_intern("on_start_document");
216
- cbidOnEndDocument = rb_intern("on_end_document");
217
- cbidOnStartElement = rb_intern("on_start_element");
218
- cbidOnEndElement = rb_intern("on_end_element");
219
- cbidOnReference = rb_intern("on_reference");
220
- cbidOnCharacters = rb_intern("on_characters");
221
- cbidOnProcessingInstruction = rb_intern("on_processing_instruction");
222
- cbidOnComment = rb_intern("on_comment");
223
- cbidOnXmlParserWarning = rb_intern("on_parser_warning");
224
- cbidOnXmlParserError = rb_intern("on_parser_error");
225
- cbidOnXmlParserFatalError = rb_intern("on_parser_fatal_error");
226
- cbidOnCdataBlock = rb_intern("on_cdata_block");
227
- cbidOnExternalSubset = rb_intern("on_external_subset");
228
- }
1
+ /* $Id: ruby_xml_sax_parser.c 604 2008-11-19 18:01:55Z cfis $ */
2
+
3
+ /* Please see the LICENSE file for copyright and distribution information */
4
+
5
+ #include "ruby_libxml.h"
6
+ #include "ruby_xml_sax_parser.h"
7
+
8
+ /*
9
+ * Document-class: LibXML::XML::SaxParser
10
+ *
11
+ * XML::SaxParser provides a callback based API for parsing documents,
12
+ * in contrast to XML::Parser's tree based API and XML::Reader's stream
13
+ * based API.
14
+ *
15
+ * Note that the XML::SaxParser API is fairly complex, not well standardized,
16
+ * and does not directly support validation making entity, namespace and
17
+ * base processing relatively hard.
18
+ *
19
+ * To use the XML::SaxParser, register a callback class via the
20
+ * XML::SaxParser#callbacks=. It is easiest to include the
21
+ * XML::SaxParser::Callbacks module in your class and override
22
+ * the methods as needed.
23
+ *
24
+ * Basic example:
25
+ *
26
+ * class MyCallbacks
27
+ * include XML::SaxParser::Callbacks
28
+ * def on_start_element(element, attributes)
29
+ * puts #Element started: #{element}"
30
+ * end
31
+ * end
32
+ *
33
+ * parser = XML::SaxParser.new
34
+ * parser.callbacks = MyCallbacks.new
35
+ * parser.parse
36
+ */
37
+
38
+ VALUE cXMLSaxParser;
39
+ VALUE mXMLSaxParserCallbacks;
40
+
41
+ static ID INPUT_ATTR;
42
+
43
+ VALUE cbidOnInternalSubset;
44
+ VALUE cbidOnIsStandalone;
45
+ VALUE cbidOnHasInternalSubset;
46
+ VALUE cbidOnHasExternalSubset;
47
+ VALUE cbidOnStartDocument;
48
+ VALUE cbidOnEndDocument;
49
+ VALUE cbidOnStartElement;
50
+ VALUE cbidOnEndElement;
51
+ VALUE cbidOnReference;
52
+ VALUE cbidOnCharacters;
53
+ VALUE cbidOnProcessingInstruction;
54
+ VALUE cbidOnComment;
55
+ VALUE cbidOnXmlParserWarning;
56
+ VALUE cbidOnXmlParserError;
57
+ VALUE cbidOnXmlParserFatalError;
58
+ VALUE cbidOnCdataBlock;
59
+ VALUE cbidOnExternalSubset;
60
+
61
+ #include "sax_parser_callbacks.inc"
62
+
63
+ void
64
+ ruby_xml_sax_parser_free(ruby_xml_sax_parser *rxsp) {
65
+ xfree(rxsp);
66
+ }
67
+
68
+ void
69
+ ruby_xml_sax_parser_mark(ruby_xml_sax_parser *rxsp) {
70
+ if (rxsp->callbackHandler != Qnil) {
71
+ rb_gc_mark(rxsp->callbackHandler);
72
+ }
73
+
74
+ if (rxsp->filename != Qnil) {
75
+ rb_gc_mark(rxsp->filename);
76
+ }
77
+
78
+ if (rxsp->str != Qnil) {
79
+ rb_gc_mark(rxsp->str);
80
+ }
81
+ }
82
+
83
+ VALUE
84
+ ruby_xml_sax_parser_alloc(VALUE klass) {
85
+ ruby_xml_sax_parser *rxsp = ALLOC(ruby_xml_sax_parser);
86
+ rxsp->xsh = &rubySAXHandlerStruct;
87
+ rxsp->callbackHandler = Qnil;
88
+ rxsp->xpc = NULL;
89
+ rxsp->filename = Qnil;
90
+ rxsp->str = Qnil;
91
+
92
+ return Data_Wrap_Struct(cXMLSaxParser,
93
+ ruby_xml_sax_parser_mark, ruby_xml_sax_parser_free,
94
+ rxsp);
95
+ }
96
+
97
+
98
+ /*
99
+ * call-seq:
100
+ * sax_parser.initialize -> sax_parser
101
+ *
102
+ * Initiliazes instance of parser.
103
+ */
104
+ VALUE
105
+ ruby_xml_sax_parser_initialize(VALUE self) {
106
+ VALUE input = rb_class_new_instance(0, NULL, cXMLInput);
107
+ rb_iv_set(self, "@input", input);
108
+ return self;
109
+ }
110
+
111
+ /*
112
+ * call-seq:
113
+ * sax_parser.callbacks -> #<XML::SaxParser::Callbacks subclass>
114
+ *
115
+ * Obtain the callbacks used by this parser.
116
+ */
117
+ VALUE
118
+ ruby_xml_sax_parser_callbacks_get(VALUE self) {
119
+ ruby_xml_sax_parser *rxsp;
120
+ Data_Get_Struct(self, ruby_xml_sax_parser, rxsp);
121
+ return(rxsp->callbackHandler);
122
+ }
123
+
124
+
125
+ /*
126
+ * call-seq:
127
+ * sax_parser.callbacks = #<XML::SaxParser::Callbacks subclass>
128
+ *
129
+ * Set the callbacks used by this parser. The value assigned to
130
+ * this attributesibute will usually be an object that extends the the
131
+ * XML::SaxParser::Callbacks module, overriding the callbacks it
132
+ * wishes to process.
133
+ */
134
+ VALUE
135
+ ruby_xml_sax_parser_callbacks_set(VALUE self, VALUE callbacks) {
136
+ ruby_xml_sax_parser *rxsp;
137
+ Data_Get_Struct(self, ruby_xml_sax_parser, rxsp);
138
+ rxsp->callbackHandler = callbacks;
139
+ return(rxsp->callbackHandler);
140
+ }
141
+
142
+ /*
143
+ * call-seq:
144
+ * parser.parse -> (true|false)
145
+ *
146
+ * Parse the input XML, generating callbacks to the object
147
+ * registered via the +callbacks+ attributesibute.
148
+ */
149
+ VALUE
150
+ ruby_xml_sax_parser_parse(VALUE self) {
151
+ char *str;
152
+ int status = 1;
153
+ ruby_xml_sax_parser *rxsp;
154
+ VALUE source;
155
+ VALUE input = rb_ivar_get(self, INPUT_ATTR);
156
+
157
+ Data_Get_Struct(self, ruby_xml_sax_parser, rxsp);
158
+
159
+ if (rb_ivar_get(input, FILE_ATTR) != Qnil)
160
+ {
161
+ source = rb_ivar_get(input, FILE_ATTR);
162
+ status = xmlSAXUserParseFile(rxsp->xsh, rxsp, StringValuePtr(source));
163
+ }
164
+ else if (rb_ivar_get(input, STRING_ATTR) != Qnil)
165
+ {
166
+ source = rb_ivar_get(input, STRING_ATTR);
167
+ str = StringValueCStr(source);
168
+ status = xmlSAXUserParseMemory(rxsp->xsh, rxsp, str, strlen(str));
169
+ }
170
+ else
171
+ {
172
+ rb_raise(rb_eArgError, "You must specify a parser data source");
173
+ }
174
+
175
+ if (status)
176
+ {
177
+ ruby_xml_raise(&xmlLastError);
178
+ return Qfalse;
179
+ }
180
+ else
181
+ {
182
+ return(Qtrue);
183
+ }
184
+ }
185
+
186
+ // Rdoc needs to know
187
+ #ifdef RDOC_NEVER_DEFINED
188
+ mLibXML = rb_define_module("LibXML");
189
+ mXML = rb_define_module_under(mLibXML, "XML");
190
+ #endif
191
+
192
+ void
193
+ ruby_init_xml_sax_parser(void) {
194
+ /* SaxParser */
195
+ cXMLSaxParser = rb_define_class_under(mXML, "SaxParser", rb_cObject);
196
+ rb_define_alloc_func(cXMLSaxParser, ruby_xml_sax_parser_alloc);
197
+ rb_define_method(cXMLSaxParser, "callbacks", ruby_xml_sax_parser_callbacks_get, 0);
198
+ rb_define_method(cXMLSaxParser, "callbacks=", ruby_xml_sax_parser_callbacks_set, 1);
199
+ rb_define_method(cXMLSaxParser, "parse", ruby_xml_sax_parser_parse, 0);
200
+
201
+ /* Atributes */
202
+ rb_define_attr(cXMLSaxParser, "input", 1, 0);
203
+
204
+ /* Instance Methods */
205
+ rb_define_method(cXMLSaxParser, "initialize", ruby_xml_sax_parser_initialize, 0);
206
+
207
+
208
+ INPUT_ATTR = rb_intern("@input");
209
+
210
+ /* SaxCallbacks */
211
+ cbidOnInternalSubset = rb_intern("on_internal_subset");
212
+ cbidOnIsStandalone = rb_intern("on_is_standalone");
213
+ cbidOnHasInternalSubset = rb_intern("on_has_internal_subset");
214
+ cbidOnHasExternalSubset = rb_intern("on_has_external_subset");
215
+ cbidOnStartDocument = rb_intern("on_start_document");
216
+ cbidOnEndDocument = rb_intern("on_end_document");
217
+ cbidOnStartElement = rb_intern("on_start_element");
218
+ cbidOnEndElement = rb_intern("on_end_element");
219
+ cbidOnReference = rb_intern("on_reference");
220
+ cbidOnCharacters = rb_intern("on_characters");
221
+ cbidOnProcessingInstruction = rb_intern("on_processing_instruction");
222
+ cbidOnComment = rb_intern("on_comment");
223
+ cbidOnXmlParserWarning = rb_intern("on_parser_warning");
224
+ cbidOnXmlParserError = rb_intern("on_parser_error");
225
+ cbidOnXmlParserFatalError = rb_intern("on_parser_fatal_error");
226
+ cbidOnCdataBlock = rb_intern("on_cdata_block");
227
+ cbidOnExternalSubset = rb_intern("on_external_subset");
228
+ }