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

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse_file(filename) ⇒ Object

Parse file given filename



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

static VALUE parse_file(VALUE klass, VALUE filename)
{
  xmlParserCtxtPtr ctxt = xmlCreateFileParserCtxt(StringValuePtr(filename));
  return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}

.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

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

  xmlParserCtxtPtr ctxt = xmlCreateIOParserCtxt(
      NULL,
      NULL,
      (xmlInputReadCallback)io_read_callback,
      (xmlInputCloseCallback)io_close_callback,
      (void *)io,
      enc
  );

  return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}

.parse_memory(data) ⇒ Object

Parse the XML stored in memory in data



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

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

  if(NIL_P(data)) rb_raise(rb_eArgError, "data cannot be nil");
  if(!(int)RSTRING_LEN(data))
    rb_raise(rb_eRuntimeError, "data cannot be empty");

  ctxt = xmlCreateMemoryParserCtxt(
      StringValuePtr(data),
      (int)RSTRING_LEN(data)
  );

  return Data_Wrap_Struct(klass, NULL, deallocate, ctxt);
}

.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

#columnObject

Get the current column the parser context is processing.



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'ext/nokogiri/xml_sax_parser_context.c', line 167

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

  Data_Get_Struct(self, xmlParserCtxt, ctxt);

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

  return Qnil;
}

#lineObject

Get the current line the parser context is processing.



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/nokogiri/xml_sax_parser_context.c', line 148

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

  Data_Get_Struct(self, xmlParserCtxt, ctxt);

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

  return Qnil;
}

#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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'ext/nokogiri/xml_sax_parser_context.c', line 78

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");

  Data_Get_Struct(self, xmlParserCtxt, ctxt);
  Data_Get_Struct(sax_handler, xmlSAXHandler, sax);

  /* Free the sax handler since we'll assign our own */
  if(ctxt->sax && ctxt->sax != (xmlSAXHandlerPtr)&xmlDefaultSAXHandler)
    xmlFree(ctxt->sax);

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

  xmlParseDocument(ctxt);

  if(NULL != ctxt->myDoc) xmlFreeDoc(ctxt->myDoc);

  NOKOGIRI_SAX_TUPLE_DESTROY(ctxt->userData);

  return Qnil ;
}

#replace_entitiesObject

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



132
133
134
135
136
137
138
139
140
141
# File 'ext/nokogiri/xml_sax_parser_context.c', line 132

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

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

#replace_entities=(boolean) ⇒ Object

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



112
113
114
115
116
117
118
119
120
121
122
123
# File 'ext/nokogiri/xml_sax_parser_context.c', line 112

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

  if(Qfalse == value)
    ctxt->replaceEntities = 0;
  else
    ctxt->replaceEntities = 1;

  return value;
}