Parent

Class Index [+]

Quicksearch

Nokogiri::XML::SAX::PushParser

 

PushParser can parse a document that is fed to it manually. It must be given a SAX::Document object which will be called with SAX events as the document is being parsed.

Calling PushParser#<< writes XML to the parser, calling any SAX callbacks it can.

PushParser#finish tells the parser that the document is finished and calls the end_document SAX method.

Example:

  parser = PushParser.new(Class.new(XML::SAX::Document) {
    def start_document
      puts "start document called"
    end
  }.new)
  parser << "<div>hello<"
  parser << "/div>"
  parser.finish

Attributes

document[RW]

The Nokogiri::XML::SAX::Document on which the PushParser will be operating

Public Class Methods

new(doc = XML::SAX::Document.new, file_name = nil, encoding = 'UTF-8') click to toggle source
 

Create a new PushParser with doc as the SAX Document, providing an optional file_name and encoding

    # File lib/nokogiri/xml/sax/push_parser.rb, line 34
34:         def initialize(doc = XML::SAX::Document.new, file_name = nil, encoding = 'UTF-8')
35:           @document = doc
36:           @encoding = encoding
37:           @sax_parser = XML::SAX::Parser.new(doc)
38: 
39:           ## Create our push parser context
40:           initialize_native(@sax_parser, file_name)
41:         end

Public Instance Methods

<<(chunk, last_chunk = false) click to toggle source
Alias for: write
finish() click to toggle source
 

Finish the parsing. This method is only necessary for Nokogiri::XML::SAX::Document#end_document to be called.

    # File lib/nokogiri/xml/sax/push_parser.rb, line 54
54:         def finish
55:           write '', true
56:         end
options() click to toggle source
    # File lib/nokogiri/ffi/xml/sax/push_parser.rb, line 8
 8:         def options
 9:           cstruct[:options]
10:         end
options=(user_options) click to toggle source
    # File lib/nokogiri/ffi/xml/sax/push_parser.rb, line 12
12:         def options=(user_options)
13:           if LibXML.xmlCtxtUseOptions(cstruct, user_options) != 0
14:             raise RuntimeError, "Cannot set XML parser context options"
15:           end
16:           nil
17:         end
write(chunk, last_chunk = false) click to toggle source
 

Write a chunk of XML to the PushParser. Any callback methods that can be called will be called immediately.

    # File lib/nokogiri/xml/sax/push_parser.rb, line 46
46:         def write chunk, last_chunk = false
47:           native_write(chunk, last_chunk)
48:         end
Also aliased as: <<

Private Instance Methods

initialize_native(xml_sax, filename) click to toggle source

Initialize the push parser with xml_sax using filename

static VALUE initialize_native(VALUE self, VALUE _xml_sax, VALUE _filename)
{
  xmlSAXHandlerPtr sax;
  const char * filename = NULL;
  xmlParserCtxtPtr ctx;

  Data_Get_Struct(_xml_sax, xmlSAXHandler, sax);

  if(_filename != Qnil) filename = StringValuePtr(_filename);

  ctx = xmlCreatePushParserCtxt(
      sax,
      NULL,
      NULL,
      0,
      filename
  );
  if(ctx == NULL)
    rb_raise(rb_eRuntimeError, "Could not create a parser context");

  ctx->userData = NOKOGIRI_SAX_TUPLE_NEW(ctx, self);

  ctx->sax2 = 1;
  DATA_PTR(self) = ctx;
  return self;
}
native_write(chunk, last_chunk) click to toggle source

Write chunk to PushParser. last_chunk triggers the end_document handle

static VALUE native_write(VALUE self, VALUE _chunk, VALUE _last_chunk)
{
  xmlParserCtxtPtr ctx;
  const char * chunk  = NULL;
  int size            = 0;


  Data_Get_Struct(self, xmlParserCtxt, ctx);

  if(Qnil != _chunk) {
    chunk = StringValuePtr(_chunk);
    size = (int)RSTRING_LEN(_chunk);
  }

  if(xmlParseChunk(ctx, chunk, size, Qtrue == _last_chunk ? 1 : 0)) {
    if (!(ctx->options & XML_PARSE_RECOVER)) {
      xmlErrorPtr e = xmlCtxtGetLastError(ctx);
      Nokogiri_error_raise(NULL, e);
    }
  }

  return self;
}

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.