1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 71 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 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 355 356 357 358 359 360 361 362 363 364 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 408 409 410 411 412 413 414 415 416 417 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
|
/* $Id: ruby_xml_html_parser.c 189 2007-09-26 15:04:47Z danj $ */
/* Please see the LICENSE file for copyright and distribution information */
#include "libxml.h"
VALUE cXMLHTMLParser;
//static int
//ctxtRead(FILE *f, char * buf, int len) {
// return(fread(buf, 1, len, f));
//}
/*
* call-seq:
* parser.filename => "filename"
*
* Obtain the filename this parser will read from.
*/
/*
VALUE
ruby_xml_html_parser_filename_get(VALUE self) {
ruby_xml_html_parser *rxp;
rx_file_data *data;
Data_Get_Struct(self, ruby_xml_html_parser, rxp);
if (rxp->data == NULL)
return(Qnil);
if (rxp->data_type != RUBY_LIBXML_SRC_TYPE_FILE)
return(Qnil);
data = (rx_file_data *)rxp->data;
return(data->filename);
}
*/
/*
* call-seq:
* parser.filename = "filename"
*
* Set the filename this parser will read from.
*/
/*
VALUE
ruby_xml_html_parser_filename_set(VALUE self, VALUE filename) {
ruby_xml_html_parser *rxp;
ruby_xml_parser_context *rxpc;
rx_file_data *data;
Check_Type(filename, T_STRING);
Data_Get_Struct(self, ruby_xml_html_parser, rxp);
if (rxp->data_type == RUBY_LIBXML_SRC_TYPE_NULL) {
if (rxp->data != NULL)
rb_fatal("crap, this should be null");
rxp->data_type = RUBY_LIBXML_SRC_TYPE_FILE;
data = ALLOC(rx_file_data);
rxp->data = data;
} else if (rxp->data_type != RUBY_LIBXML_SRC_TYPE_FILE) {
return(Qnil);
}
rxp->ctxt = ruby_xml_parser_context_new3();
data = (rx_file_data *)rxp->data;
data->filename = filename;
Data_Get_Struct(rxp->ctxt, ruby_xml_parser_context, rxpc);
rxpc->ctxt = htmlCreateFileParserCtxt(StringValuePtr(filename));
if (rxpc->ctxt == NULL)
rb_sys_fail(StringValuePtr(filename));
return(data->filename);
}
*/
void
ruby_xml_html_parser_free(ruby_xml_html_parser *rxp) {
void *data;
switch(rxp->data_type) {
case RUBY_LIBXML_SRC_TYPE_NULL:
break;
case RUBY_LIBXML_SRC_TYPE_FILE:
data = (void *)(rx_file_data *)rxp->data;
free((rx_file_data *)data);
break;
case RUBY_LIBXML_SRC_TYPE_STRING:
data = (void *)(rx_string_data *)rxp->data;
free((rx_string_data *)data);
break;
case RUBY_LIBXML_SRC_TYPE_IO:
data = (void *)(rx_io_data *)rxp->data;
free((rx_io_data *)data);
break;
default:
rb_fatal("Unknown data type, %d", rxp->data_type);
}
free(rxp);
}
/*
* call-seq:
* parser.io => IO
*
* Obtain the IO instance this parser works with.
*/
/*
VALUE
ruby_xml_html_parser_io_get(VALUE self, VALUE io) {
ruby_xml_html_parser *rxp;
rx_io_data *data;
Data_Get_Struct(self, ruby_xml_html_parser, rxp);
if (rxp->data_type == RUBY_LIBXML_SRC_TYPE_NULL ||
rxp->data_type != RUBY_LIBXML_SRC_TYPE_IO ||
rxp->data == NULL)
return(Qnil);
data = (rx_io_data *)rxp->data;
return(data->io);
}
*/
/*
* call-seq:
* parser.io = IO
*
* Set the IO instance this parser works with.
*/
/*
VALUE
ruby_xml_html_parser_io_set(VALUE self, VALUE io) {
ruby_xml_html_parser *rxp;
ruby_xml_parser_context *rxpc;
rx_io_data *data;
OpenFile *fptr;
FILE *f;
if (!rb_obj_is_kind_of(io, rb_cIO))
rb_raise(rb_eTypeError, "need an IO object");
Data_Get_Struct(self, ruby_xml_html_parser, rxp);
if (rxp->data_type == RUBY_LIBXML_SRC_TYPE_NULL) {
if (rxp->data != NULL)
rb_fatal("crap, this should be null");
rxp->data_type = RUBY_LIBXML_SRC_TYPE_IO;
data = ALLOC(rx_io_data);
rxp->data = data;
} else if (rxp->data_type != RUBY_LIBXML_SRC_TYPE_IO) {
return(Qnil);
}
rxp->ctxt = ruby_xml_parser_context_new3();
data = (rx_io_data *)rxp->data;
data->io = io;
GetOpenFile(io, fptr);
rb_io_check_readable(fptr);
f = GetWriteFile(fptr);
Data_Get_Struct(rxp->ctxt, ruby_xml_parser_context, rxpc);
rxpc->ctxt = htmlCreateIOParserCtxt(NULL, NULL,
(xmlInputReadCallback) ctxtRead,
NULL, f, XML_CHAR_ENCODING_NONE);
if (NIL_P(rxpc->ctxt))
rb_sys_fail(0);
return(data->io);
}
*/
void
ruby_xml_html_parser_mark(ruby_xml_html_parser *rxp) {
if (rxp == NULL) return;
if (!NIL_P(rxp->ctxt)) rb_gc_mark(rxp->ctxt);
ruby_xml_state_marker();
switch(rxp->data_type) {
case RUBY_LIBXML_SRC_TYPE_NULL:
break;
case RUBY_LIBXML_SRC_TYPE_FILE:
if (!NIL_P(((rx_file_data *)rxp->data)->filename))
rb_gc_mark(((rx_file_data *)rxp->data)->filename);
break;
case RUBY_LIBXML_SRC_TYPE_STRING:
if (!NIL_P(((rx_string_data *)rxp->data)->str))
rb_gc_mark(((rx_string_data *)rxp->data)->str);
break;
case RUBY_LIBXML_SRC_TYPE_IO:
if (!NIL_P(((rx_io_data *)rxp->data)->io))
rb_gc_mark(((rx_io_data *)rxp->data)->io);
break;
default:
rb_fatal("unknown datatype: %d", rxp->data_type);
}
}
/*
* call-seq:
* XML::HTMLParser.new => parser
*
* Create a new parser instance with no pre-determined source.
*/
VALUE
ruby_xml_html_parser_new(VALUE class) {
ruby_xml_html_parser *rxp;
rxp = ALLOC(ruby_xml_html_parser);
rxp->ctxt = Qnil;
rxp->data_type = RUBY_LIBXML_SRC_TYPE_NULL;
rxp->data = NULL;
rxp->parsed = 0;
return(Data_Wrap_Struct(class, ruby_xml_html_parser_mark,
ruby_xml_html_parser_free, rxp));
}
/*
* call-seq:
* XML::HTMLParser.file => parser
*
* Create a new parser instance that will read the specified file.
*/
/*
VALUE
ruby_xml_html_parser_new_file(VALUE class, VALUE filename) {
VALUE obj;
ruby_xml_html_parser *rxp;
rx_file_data *data;
obj = ruby_xml_html_parser_new(class);
Data_Get_Struct(obj, ruby_xml_html_parser, rxp);
data = ALLOC(rx_file_data);
rxp->data_type = RUBY_LIBXML_SRC_TYPE_FILE;
rxp->data = data;
ruby_xml_html_parser_filename_set(obj, filename);
return(obj);
}
*/
/*
* call-seq:
* XML::HTMLParser.io => parser
*
* Create a new parser instance that will read from the
* specified IO object.
*/
/*
VALUE
ruby_xml_html_parser_new_io(VALUE class, VALUE io) {
VALUE obj;
ruby_xml_html_parser *rxp;
rx_io_data *data;
obj = ruby_xml_html_parser_new(class);
Data_Get_Struct(obj, ruby_xml_html_parser, rxp);
data = ALLOC(rx_io_data);
rxp->data_type = RUBY_LIBXML_SRC_TYPE_IO;
rxp->data = data;
ruby_xml_html_parser_io_set(obj, io);
return(obj);
}
*/
/*
* call-seq:
* XML::HTMLParser.string => parser
*
* Create a new parser instance that will parse the given
* string.
*/
VALUE
ruby_xml_html_parser_new_string(VALUE class, VALUE str) {
VALUE obj;
ruby_xml_html_parser *rxp;
rx_string_data *data;
obj = ruby_xml_html_parser_new(class);
Data_Get_Struct(obj, ruby_xml_html_parser, rxp);
data = ALLOC(rx_string_data);
rxp->data_type = RUBY_LIBXML_SRC_TYPE_STRING;
rxp->data = data;
ruby_xml_html_parser_str_set(obj, str);
return(obj);
}
/*
* call-seq:
* parser.parse => document
*
* Parse the input XML and create an XML::Document with
* it's content. If an error occurs, XML::Parser::ParseError
* is thrown.
*/
VALUE
ruby_xml_html_parser_parse(VALUE self) {
ruby_xml_document_t *rxd;
ruby_xml_html_parser *rxp;
ruby_xml_parser_context *rxpc;
htmlDocPtr xdp;
VALUE doc;
Data_Get_Struct(self, ruby_xml_html_parser, rxp);
switch (rxp->data_type) {
case RUBY_LIBXML_SRC_TYPE_NULL:
return(Qnil);
case RUBY_LIBXML_SRC_TYPE_STRING:
//case RUBY_LIBXML_SRC_TYPE_FILE:
//case RUBY_LIBXML_SRC_TYPE_IO:
Data_Get_Struct(rxp->ctxt, ruby_xml_parser_context, rxpc);
/* don't check return values here, the HTML parser returns errors
* but still allows the resulting tree to be used.
*/
htmlParseDocument(rxpc->ctxt);
xdp = rxpc->ctxt->myDoc;
rxp->parsed = 1;
doc = ruby_xml_document_wrap(cXMLDocument, xdp);
break;
default:
rb_fatal("Unknown data type, %d", rxp->data_type);
}
return(doc);
}
/*
* call-seq:
* parser.context => context
*
* Obtain the XML::Parser::Context associated with this
* parser.
*/
VALUE
ruby_xml_html_parser_context_get(VALUE self) {
ruby_xml_html_parser *rxp;
Data_Get_Struct(self, ruby_xml_html_parser, rxp);
if (rxp->ctxt == Qnil)
return(Qnil);
else
return(rxp->ctxt);
}
/*
* call-seq:
* parser.string => "string"
*
* Obtain the string this parser works with.
*/
VALUE
ruby_xml_html_parser_str_get(VALUE self) {
ruby_xml_html_parser *rxp;
rx_string_data *data;
Data_Get_Struct(self, ruby_xml_html_parser, rxp);
if (rxp->data == NULL || rxp->data_type != RUBY_LIBXML_SRC_TYPE_STRING)
return(Qnil);
data = (rx_string_data *)rxp->data;
return(data->str);
}
/*
* call-seq:
* parser.string = "string"
*
* Set the string this parser works with.
*/
VALUE
ruby_xml_html_parser_str_set(VALUE self, VALUE str) {
ruby_xml_html_parser *rxp;
ruby_xml_parser_context *rxpc;
rx_string_data *data;
Check_Type(str, T_STRING);
Data_Get_Struct(self, ruby_xml_html_parser, rxp);
if (rxp->data_type == RUBY_LIBXML_SRC_TYPE_NULL) {
rxp->data_type = RUBY_LIBXML_SRC_TYPE_STRING;
data = ALLOC(rx_string_data);
rxp->data = data;
} else if (rxp->data_type != RUBY_LIBXML_SRC_TYPE_STRING) {
return(Qnil);
}
rxp->ctxt = ruby_xml_parser_context_new3();
data = (rx_string_data *)rxp->data;
data->str = str;
Data_Get_Struct(rxp->ctxt, ruby_xml_parser_context, rxpc);
rxpc->ctxt = htmlCreateMemoryParserCtxt(StringValuePtr(data->str), RSTRING_LEN(data->str));
return(data->str);
}
// Rdoc needs to know
#ifdef RDOC_NEVER_DEFINED
mXML = rb_define_module("XML");
#endif
void
ruby_init_html_parser(void) {
cXMLHTMLParser = rb_define_class_under(mXML, "HTMLParser", rb_cObject);
/*
rb_define_singleton_method(cXMLHTMLParser, "file", ruby_xml_html_parser_new_file, 1);
rb_define_singleton_method(cXMLHTMLParser, "io", ruby_xml_html_parser_new_io, 1);
*/
rb_define_singleton_method(cXMLHTMLParser, "new", ruby_xml_html_parser_new, 0);
rb_define_singleton_method(cXMLHTMLParser, "string", ruby_xml_html_parser_new_string, 1);
/*
rb_define_method(cXMLHTMLParser, "filename", ruby_xml_html_parser_filename_get, 0);
rb_define_method(cXMLHTMLParser, "filename=", ruby_xml_html_parser_filename_set, 1);
rb_define_method(cXMLHTMLParser, "io", ruby_xml_html_parser_io_get, 0);
rb_define_method(cXMLHTMLParser, "io=", ruby_xml_html_parser_io_set, 1);
*/
rb_define_method(cXMLHTMLParser, "parse", ruby_xml_html_parser_parse, 0);
rb_define_method(cXMLHTMLParser, "parser_context", ruby_xml_html_parser_context_get, 0);
rb_define_method(cXMLHTMLParser, "string", ruby_xml_html_parser_str_get, 0);
rb_define_method(cXMLHTMLParser, "string=", ruby_xml_html_parser_str_set, 1);
}
|