Permalink
Browse files

xmlpushparser: add simple reset_parser to reset the libxml2 parser co…

…ntext (TODO: figure out how to destroy the old context)
  • Loading branch information...
1 parent ef94670 commit ea21170bd9909e1f977fd30f914f28008056d592 @tmm1 committed Jun 5, 2008
Showing with 36 additions and 3 deletions.
  1. +36 −3 vendor/eventmachine_xmlpushparser/ext/rubymain.cpp
@@ -51,6 +51,7 @@ class RubyXmlPushParser_t
void SaxStartElement (const xmlChar*, const xmlChar**);
void SaxEndElement (const xmlChar*);
void SaxCharacters (const xmlChar*, int);
+ void SaxReset();
void SaxError();
private:
@@ -155,6 +156,25 @@ RubyXmlPushParser_t::RubyXmlPushParser_t (VALUE v):
throw std::runtime_error ("no push-parser context");
}
+/*****************************
+RubyXmlPushParser_t::SaxReset
+*****************************/
+
+void RubyXmlPushParser_t::SaxReset()
+{
+ if (!Context)
+ throw std::runtime_error ("no push-parser context");
+
+ // XXX: figure out how to free the existing Context
+ // xmlCtxtReset (Context); // segfault
+ // xmlClearParserCtxt (Context); // segfault
+ // xmlFreeParserCtxt (Context); // segfault
+ // xmlCtxtResetPush (Context, "", 0, "", NULL);
+
+ Context = xmlCreatePushParserCtxt (&saxHandler, (void*)this, "", 0, "");
+ if (!Context)
+ throw std::runtime_error ("no push-parser context");
+}
/********************************
RubyXmlPushParser_t::ConsumeData
@@ -329,13 +349,26 @@ t_unbind
static VALUE t_unbind (VALUE self)
{
- RubyXmlPushParser_t *pp = new RubyXmlPushParser_t (self);
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*)(NUM2INT (rb_ivar_get (self, rb_intern ("@xml__push__parser__object"))));
if (!pp)
throw std::runtime_error ("no xml push-parser object");
pp->Close();
return Qnil;
}
+/*******
+t_reset_parser
+*******/
+
+static VALUE t_reset_parser (VALUE self)
+{
+ RubyXmlPushParser_t *pp = (RubyXmlPushParser_t*)(NUM2INT (rb_ivar_get (self, rb_intern ("@xml__push__parser__object"))));
+ if (!pp)
+ throw std::runtime_error ("no xml push-parser object");
+ pp->SaxReset();
+ return Qnil;
+}
+
/****************
t_start_document
****************/
@@ -434,6 +467,6 @@ extern "C" void Init_rubyxmlpushparser()
rb_define_method (XmlModule, "start_element", (VALUE(*)(...))t_start_element, 2);
rb_define_method (XmlModule, "end_element", (VALUE(*)(...))t_end_element, 1);
rb_define_method (XmlModule, "characters", (VALUE(*)(...))t_characters, 1);
+ rb_define_method (XmlModule, "reset_parser", (VALUE(*)(...))t_reset_parser, 0);
rb_define_method (XmlModule, "error", (VALUE(*)(...))t_error, 1);
-}
-
+}

0 comments on commit ea21170

Please sign in to comment.