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

Returns a new instance of Rexle.



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
269
270
271
272
273
274
275
# File 'lib/rexle.rb', line 243

def initialize(x=nil)

  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.



240
241
242
# File 'lib/rexle.rb', line 240

def doctype
  @doctype
end

#instructionsObject

Returns the value of attribute instructions.



241
242
243
# File 'lib/rexle.rb', line 241

def instructions
  @instructions
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



240
241
242
# File 'lib/rexle.rb', line 240

def prefixes
  @prefixes
end

Instance Method Details

#add_attribute(x) ⇒ Object



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

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

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



1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
# File 'lib/rexle.rb', line 1395

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



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

def add_text(s) end

#at_css(selector) ⇒ Object



281
282
283
# File 'lib/rexle.rb', line 281

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

#attribute(key) ⇒ Object



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

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

#attributesObject



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

def attributes() @doc.attributes end

#cloneObject



277
278
279
# File 'lib/rexle.rb', line 277

def clone()
  Rexle.new self.to_a
end

#content(options = {}) ⇒ Object



1457
1458
1459
# File 'lib/rexle.rb', line 1457

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

#css(selector) ⇒ Object



285
286
287
288
289
290
291
292
293
294
# File 'lib/rexle.rb', line 285

def css(selector)
  
  a = selector.split(',').flat_map do |x| 
    @doc.root.xpath RexleCSS.new(x).to_xpath
  end
  
  a.shift if self.kind_of? Rexle
  
  return a
end

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



1411
1412
1413
1414
1415
# File 'lib/rexle.rb', line 1411

def delete(xpath)

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

end

#element(xpath) ⇒ Object



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

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

#elements(s = nil) ⇒ Object



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

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

#nameObject



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

def name() @doc.root.name end

#parse(x = nil) ⇒ Object



1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
# File 'lib/rexle.rb', line 1370

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



1430
1431
1432
# File 'lib/rexle.rb', line 1430

def root() 
  @doc.elements.first 
end

#text(xpath) ⇒ Object



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

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

#to_aObject



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

def to_a() @a end

#to_s(options = {}) ⇒ Object



1424
1425
1426
1427
# File 'lib/rexle.rb', line 1424

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

#write(f) ⇒ Object



1434
1435
1436
# File 'lib/rexle.rb', line 1434

def write(f) 
  f.write xml 
end

#xml(options = {}) ⇒ Object



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

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



296
297
298
# File 'lib/rexle.rb', line 296

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