Class: Rexle

Inherits:
Object
  • Object
show all
Includes:
XMLhelper
Defined in:
lib/rexle.rb

Defined Under Namespace

Classes: CData, Comment, Element, Elements, Recordset

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from XMLhelper

#doc_pretty_print, #doc_print, #inspect, #pretty_print, #processing_instructions, #scan_print, #scan_to_a

Constructor Details

#initialize(x = nil, rexle: self, debug: false) ⇒ Rexle

Returns a new instance of Rexle.



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/rexle.rb', line 231

def initialize(x=nil, rexle: self, debug: false)

  @rexle, @debug = rexle, debug
  $debug = @debug

  puts 'inside Rexle'.debug if debug

  super()

  @instructions = [["xml", "version='1.0' encoding='UTF-8'"]]
  @doctype = :xml

  # what type of input is it? Is it a string, array
  if x then
    procs = {
      String: proc {|x| parse_string(x)},
      Array: proc {|x| x},
      RexleParser: ->(x){ parse_rexle(x)}
    }

    doc_node = ['doc', Attributes.new]

    @a = procs[x.class.to_s.to_sym].call(x)

    @doc = scan_element(*(doc_node << @a))

    # fetch the namespaces
    @prefixes = []

    if @doc.root.attributes then

      xmlns = @doc.root.attributes.select {|k,v| k[/^xmlns:/]}
      @prefixes = xmlns.keys.map{|x| x[/\w+$/]}
    end

  end

end

Instance Attribute Details

#doctypeObject (readonly)

Returns the value of attribute doctype.



228
229
230
# File 'lib/rexle.rb', line 228

def doctype
  @doctype
end

#instructionsObject

Returns the value of attribute instructions.



229
230
231
# File 'lib/rexle.rb', line 229

def instructions
  @instructions
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



228
229
230
# File 'lib/rexle.rb', line 228

def prefixes
  @prefixes
end

Instance Method Details

#add_attribute(x) ⇒ Object



1460
# File 'lib/rexle.rb', line 1460

def add_attribute(x) @doc.attribute(x) end

#add_element(element) ⇒ Object Also known as: add



1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
# File 'lib/rexle.rb', line 1464

def add_element(element)

  if @doc then
    raise 'attempted adding second root element to document' if @doc.root
    @doc.root.add_element(element)
  else
    doc_node = ['doc', Attributes.new, element.to_a]
    @doc = scan_element(*doc_node)
  end
  element
end

#add_text(s) ⇒ Object



1476
# File 'lib/rexle.rb', line 1476

def add_text(s) end

#at_css(selector) ⇒ Object



274
275
276
# File 'lib/rexle.rb', line 274

def at_css(selector)
  @doc.root.element RexleCSS.new(selector).to_xpath
end

#attribute(key) ⇒ Object



1461
# File 'lib/rexle.rb', line 1461

def attribute(key) @doc.attribute(key) end

#attributesObject



1462
# File 'lib/rexle.rb', line 1462

def attributes() @doc.attributes end

#cloneObject



270
271
272
# File 'lib/rexle.rb', line 270

def clone()
  Rexle.new self.to_a
end

#content(options = {}) ⇒ Object



1526
1527
1528
# File 'lib/rexle.rb', line 1526

def content(options={})
  CGI.unescapeHTML(xml(options))
end

#css(selector) ⇒ Object



278
279
280
281
282
283
284
285
# File 'lib/rexle.rb', line 278

def css(selector)

  a = selector.split(',').flat_map do |x|
    @doc.root.xpath RexleCSS.new(x).to_xpath
  end

  return a
end

#delete(xpath) ⇒ Object Also known as: remove



1480
1481
1482
1483
1484
# File 'lib/rexle.rb', line 1480

def delete(xpath)

  @doc.xpath(xpath).each {|e| e.delete; e = nil }

end

#element(xpath) ⇒ Object



1488
# File 'lib/rexle.rb', line 1488

def element(xpath) self.xpath(xpath).first end

#elements(s = nil) ⇒ Object



1489
# File 'lib/rexle.rb', line 1489

def elements(s=nil) @doc.elements(s) end

#nameObject



1490
# File 'lib/rexle.rb', line 1490

def name() @doc.root.name end

#parse(x = nil) ⇒ Object



1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
# File 'lib/rexle.rb', line 1439

def parse(x=nil)

  a = []

  if x then
    procs = {
      String: proc {|x| parse_string(x)},
      Array: proc {|x| x}
    }
    a = procs[x.class.to_s.to_sym].call(x)
  else
    a = yield
  end

  doc_node = ['doc',Attributes.new]
  @a = procs[x.class.to_s.to_sym].call(x)
  @doc = scan_element(*(doc_node << @a))

  self
end

#rootObject



1499
1500
1501
# File 'lib/rexle.rb', line 1499

def root()
  @doc.elements.first
end

#text(xpath) ⇒ Object



1498
# File 'lib/rexle.rb', line 1498

def text(xpath) @doc.text(xpath) end

#to_aObject



1491
# File 'lib/rexle.rb', line 1491

def to_a() @a end

#to_s(options = {}) ⇒ Object



1493
1494
1495
1496
# File 'lib/rexle.rb', line 1493

def to_s(options={})
  return '<UNDEFINED/>' unless @doc
  self.xml options
end

#write(f) ⇒ Object



1503
1504
1505
# File 'lib/rexle.rb', line 1503

def write(f)
  f.write xml
end

#xml(options = {}) ⇒ Object



1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
# File 'lib/rexle.rb', line 1507

def xml(options={})

  return '' unless @doc
  o = {pretty: false, declaration: true}.merge(options)
  msg = o[:pretty] == false ? :doc_print : :doc_pretty_print

  r = ''

  if o[:declaration] == true then

    unless @instructions and @instructions.assoc 'xml' then
      @instructions.unshift ["xml","version='1.0' encoding='UTF-8'"]
    end
  end

  r << method(msg).call(self.root.children, o[:declaration]).strip
  r
end

#xpath(path, &blk) ⇒ Object



287
288
289
# File 'lib/rexle.rb', line 287

def xpath(path,  &blk)
  @doc.xpath(path,  &blk)
end