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

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/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

Direct Known Subclasses

HTML::SAX::PushParser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

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



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

def initialize(doc = XML::SAX::Document.new, file_name = nil, encoding = 'UTF-8')
  @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

#documentObject

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



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

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.



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

def finish
  write '', true
end

#optionsObject



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

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

  return INT2NUM(ctx->options);
}

#options=(options) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'ext/nokogiri/xml_sax_push_parser.c', line 99

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;
}

#replace_entitiesObject

Should this parser replace entities? &amp; will get converted to ‘&’ if set to true



119
120
121
122
123
124
125
126
127
128
129
130
# File 'ext/nokogiri/xml_sax_push_parser.c', line 119

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

  if (0 == ctx->replaceEntities) {
    return Qfalse;
  } else {
    return Qtrue;
  }
}

#replace_entities=(boolean) ⇒ Object

Should this parser replace entities? &amp; will get converted to ‘&’ if set to true



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'ext/nokogiri/xml_sax_push_parser.c', line 139

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

  if (Qfalse == value) {
    ctx->replaceEntities = 0;
  } else {
    ctx->replaceEntities = 1;
  }

  return value;
}

#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 immediately.



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

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