Class: Nokogiri::XML::SAX::ParserContext

Inherits:
Object
  • Object
show all
Defined in:
lib/nokogiri/xml/sax/parser_context.rb,
lib/nokogiri/ffi/xml/sax/parser_context.rb,
ext/nokogiri/xml_sax_parser_context.c,
ext/nokogiri/html_sax_parser_context.c

Overview

Context for XML SAX parsers. This class is usually not instantiated by the user. Instead, you should be looking at Nokogiri::XML::SAX::Parser

Direct Known Subclasses

HTML::SAX::ParserContext

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cstructObject

Returns the value of attribute cstruct.



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

def cstruct
  @cstruct
end

#reader_callbackObject

Returns the value of attribute reader_callback.



7
8
9
# File 'lib/nokogiri/ffi/xml/sax/parser_context.rb', line 7

def reader_callback
  @reader_callback
end

Class Method Details

.parse_file(filename) ⇒ Object

Parse file given filename



44
45
46
47
48
49
# File 'ext/nokogiri/xml_sax_parser_context.c', line 44

def self.file filename
  ctx = LibXML.xmlCreateFileParserCtxt filename
  pc = allocate
  pc.cstruct = LibXML::XmlParserContext.new ctx
  pc
end

.parse_io(io, encoding) ⇒ Object

Parse io object with encoding



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'ext/nokogiri/xml_sax_parser_context.c', line 22

def self.io io, encoding
  reader_callback = IoCallbacks.reader(io) # keep a reference to prevent it from being GC'd
  sax_ctx = LibXML.xmlCreateIOParserCtxt(
    nil,
    nil,
    reader_callback,
    nil,
    nil,
    encoding
  )
  pc = allocate
  pc.cstruct = LibXML::XmlParserContext.new sax_ctx
  pc.reader_callback = reader_callback
  pc
end

.parse_memory(data) ⇒ Object

Parse the XML stored in memory in data



56
57
58
59
60
61
62
63
64
65
# File 'ext/nokogiri/xml_sax_parser_context.c', line 56

def self.memory data
  raise(ArgumentError, 'data cannot be nil') if data.nil?
  raise('data cannot be empty') if data.length == 0
  ctx = LibXML::XmlParserContext.new(
    LibXML.xmlCreateMemoryParserCtxt data, data.length
  )
  pc = allocate
  pc.cstruct = ctx
  pc
end

.new(thing, encoding = 'UTF-8') ⇒ Object



9
10
11
12
# File 'lib/nokogiri/xml/sax/parser_context.rb', line 9

def self.new thing, encoding = 'UTF-8'
  [:read, :close].all? { |x| thing.respond_to?(x) } ?
    io(thing, Parser::ENCODINGS[encoding]) : memory(thing)
end

Instance Method Details

#parse_with(sax_handler) ⇒ Object

Use sax_handler and parse the current document



78
79
80
81
82
83
84
85
86
87
88
89
# File 'ext/nokogiri/xml_sax_parser_context.c', line 78

def parse_with sax_handler, type = :xml
  raise ArgumentError unless XML::SAX::Parser === sax_handler
  sax = sax_handler.cstruct
  cstruct[:sax] = sax

  sax_handler.instance_variable_set(:@ctxt, cstruct)

  LibXML.send(:"#{type}ParseDocument", cstruct)

  cstruct[:sax] = nil
  LibXML.xmlFreeDoc cstruct[:myDoc] unless cstruct[:myDoc].null?
end

#replace_entitiesObject

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



132
133
134
# File 'ext/nokogiri/xml_sax_parser_context.c', line 132

def replace_entities
  self.cstruct[:replaceEntities] == 0 ? false : true
end

#replace_entities=(boolean) ⇒ Object

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



112
113
114
# File 'ext/nokogiri/xml_sax_parser_context.c', line 112

def replace_entities=(value)
  self.cstruct[:replaceEntities] = value ? 1 : 0
end