123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/nokogiri/xml/sax/parser_context.rb,
ext/nokogiri/xml_sax_parser_context.c,
ext/nokogiri/xml_sax_push_parser.c

Overview

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

  • #column

    Get the current column the parser context is processing.

  • #line

    Get the current line the parser context is processing.

  • #parse_with(sax_handler)

    Use sax_handler and parse the current document.

Constructor Details

.new(thing, encoding = "UTF-8") ⇒ ParserContext

[ GitHub ]

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

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

Class Method Details

.parse_file(filename)

Parse file given filename

[ GitHub ]

  
# File 'ext/nokogiri/xml_sax_parser_context.c', line 80

static VALUE
parse_file(VALUE klass, VALUE filename)
{
  xmlParserCtxtPtr ctxt = xmlCreateFileParserCtxt(StringValueCStr(filename));

  if (ctxt->sax) {
    xmlFree(ctxt->sax);
    ctxt->sax = NULL;
  }

  return noko_xml_sax_parser_context_wrap(klass, ctxt);
}

.parse_io(io, encoding)

Parse io object with encoding

[ GitHub ]

  
# File 'ext/nokogiri/xml_sax_parser_context.c', line 48

static VALUE
parse_io(VALUE klass, VALUE io, VALUE encoding)
{
  xmlParserCtxtPtr ctxt;
  xmlCharEncoding enc = (xmlCharEncoding)NUM2INT(encoding);

  if (!rb_respond_to(io, id_read)) {
    rb_raise(rb_eTypeError, "argument expected to respond to :read");
  }

  ctxt = xmlCreateIOParserCtxt(NULL, NULL,
                               (xmlInputReadCallback)noko_io_read,
                               (xmlInputCloseCallback)noko_io_close,
                               (void *)io, enc);
  if (!ctxt) {
    rb_raise(rb_eRuntimeError, "failed to create xml sax parser context");
  }

  if (ctxt->sax) {
    xmlFree(ctxt->sax);
    ctxt->sax = NULL;
  }

  return noko_xml_sax_parser_context_wrap(klass, ctxt);
}

.parse_memory(data)

Parse the ::Nokogiri::XML stored in memory in data

[ GitHub ]

  
# File 'ext/nokogiri/xml_sax_parser_context.c', line 99

static VALUE
parse_memory(VALUE klass, VALUE data)
{
  xmlParserCtxtPtr ctxt;

  Check_Type(data, T_STRING);

  if (!(int)RSTRING_LEN(data)) {
    rb_raise(rb_eRuntimeError, "data cannot be empty");
  }

  ctxt = xmlCreateMemoryParserCtxt(StringValuePtr(data),
                                   (int)RSTRING_LEN(data));
  if (ctxt->sax) {
    xmlFree(ctxt->sax);
    ctxt->sax = NULL;
  }

  return noko_xml_sax_parser_context_wrap(klass, ctxt);
}

Instance Attribute Details

#recovery (rw)

Should this parser recover from structural errors? It will not stop processing file on structural errors if set to true

[ GitHub ]

  
# File 'ext/nokogiri/xml_sax_parser_context.c', line 276

static VALUE
get_recovery(VALUE self)
{
  xmlParserCtxtPtr ctxt = noko_xml_sax_parser_context_unwrap(self);

  if (ctxt->recovery == 0) {
    return Qfalse;
  } else {
    return Qtrue;
  }
}

#recovery=(boolean) (rw)

Should this parser recover from structural errors? It will not stop processing file on structural errors if set to true

[ GitHub ]

  
# File 'ext/nokogiri/xml_sax_parser_context.c', line 255

static VALUE
set_recovery(VALUE self, VALUE value)
{
  xmlParserCtxtPtr ctxt = noko_xml_sax_parser_context_unwrap(self);

  if (value == Qfalse) {
    ctxt->recovery = 0;
  } else {
    ctxt->recovery = 1;
  }

  return value;
}

#replace_entities (rw)

#replace_entities=(boolean) (rw)

Instance Method Details

#column

Get the current column the parser context is processing.

[ GitHub ]

  
# File 'ext/nokogiri/xml_sax_parser_context.c', line 234

static VALUE
column(VALUE self)
{
  xmlParserCtxtPtr ctxt = noko_xml_sax_parser_context_unwrap(self);
  xmlParserInputPtr io;

  io = ctxt->input;
  if (io) {
    return INT2NUM(io->col);
  }

  return Qnil;
}

#line

Get the current line the parser context is processing.

[ GitHub ]

  
# File 'ext/nokogiri/xml_sax_parser_context.c', line 215

static VALUE
line(VALUE self)
{
  xmlParserInputPtr io;
  xmlParserCtxtPtr ctxt = noko_xml_sax_parser_context_unwrap(self);

  io = ctxt->input;
  if (io) {
    return INT2NUM(io->line);
  }

  return Qnil;
}

#parse_with(sax_handler)

Use sax_handler and parse the current document

[ GitHub ]

  
# File 'ext/nokogiri/xml_sax_parser_context.c', line 147

static VALUE
parse_with(VALUE self, VALUE sax_handler)
{
  xmlParserCtxtPtr ctxt;
  xmlSAXHandlerPtr sax;

  if (!rb_obj_is_kind_of(sax_handler, cNokogiriXmlSaxParser)) {
    rb_raise(rb_eArgError, "argument must be a Nokogiri::XML::SAX::Parser");
  }

  ctxt = noko_xml_sax_parser_context_unwrap(self);
  sax = noko_sax_handler_unwrap(sax_handler);

  ctxt->sax = sax;
  ctxt->userData = (void *)NOKOGIRI_SAX_TUPLE_NEW(ctxt, sax_handler);

  xmlSetStructuredErrorFunc(NULL, NULL);

  rb_ensure(parse_doc, (VALUE)ctxt, parse_doc_finalize, (VALUE)ctxt);

  return Qnil;
}