Class: Nokogiri::XML::SAX::PushParser

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xml/sax/push_parser.rb,
lib/nokogiri/ffi/xml/sax/push_parser.rb,
ext/nokogiri/xml_sax_push_parser.c

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc = XML::SAX::Document.new, file_name = nil, encoding = 'ASCII') ⇒ PushParser

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



34
35
36
37
38
39
40
41
# File 'lib/nokogiri/xml/sax/push_parser.rb', line 34

def initialize(doc = XML::SAX::Document.new, file_name = nil, encoding = 'ASCII')
  @document = doc
  @encoding = encoding
  @sax_parser = XML::SAX::Parser.new(doc)

  ## Create our push parser context
  initialize_native(@sax_parser, file_name)
end

Instance Attribute Details

#cstructObject

:nodoc:



6
7
8
# File 'lib/nokogiri/ffi/xml/sax/push_parser.rb', line 6

def cstruct
  @cstruct
end

#documentObject

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



29
30
31
# File 'lib/nokogiri/xml/sax/push_parser.rb', line 29

def document
  @document
end

Instance Method Details

#finishObject

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



54
55
56
# File 'lib/nokogiri/xml/sax/push_parser.rb', line 54

def finish
  write '', true
end

#optionsObject



80
81
82
83
84
85
86
# File 'ext/nokogiri/xml_sax_push_parser.c', line 80

static VALUE get_options(VALUE self)
{
  xmlParserCtxtPtr ctx;
  Data_Get_Struct(self, xmlParserCtxt, ctx);

  return INT2NUM(ctx->options);
}

#options=(options) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'ext/nokogiri/xml_sax_push_parser.c', line 88

static VALUE set_options(VALUE self, VALUE options)
{
  xmlParserCtxtPtr ctx;
  Data_Get_Struct(self, xmlParserCtxt, ctx);

  if (xmlCtxtUseOptions(ctx, (int)NUM2INT(options)) != 0)
    rb_raise(rb_eRuntimeError, "Cannot set XML parser context options");

  return Qnil;
}

#write(chunk, last_chunk = false) ⇒ Object Also known as: <<

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



46
47
48
# File 'lib/nokogiri/xml/sax/push_parser.rb', line 46

def write chunk, last_chunk = false
  native_write(chunk, last_chunk)
end