Class: Rexle

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

Defined Under Namespace

Classes: CData, Comment, Element, Elements

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.



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/rexle.rb', line 304

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



301
302
303
# File 'lib/rexle.rb', line 301

def doctype
  @doctype
end

#instructionsObject

Returns the value of attribute instructions.



302
303
304
# File 'lib/rexle.rb', line 302

def instructions
  @instructions
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



301
302
303
# File 'lib/rexle.rb', line 301

def prefixes
  @prefixes
end

Instance Method Details

#add_attribute(x) ⇒ Object



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

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

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



1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
# File 'lib/rexle.rb', line 1261

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



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

def add_text(s) end

#at_css(selector) ⇒ Object



340
341
342
# File 'lib/rexle.rb', line 340

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

#attribute(key) ⇒ Object



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

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

#attributesObject



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

def attributes() @doc.attributes end

#cloneObject



336
337
338
# File 'lib/rexle.rb', line 336

def clone()
  Rexle.new self.to_a
end

#content(options = {}) ⇒ Object



1323
1324
1325
# File 'lib/rexle.rb', line 1323

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

#css(selector) ⇒ Object



344
345
346
# File 'lib/rexle.rb', line 344

def css(selector)
  selector.split(',').flat_map{|x| @doc.root.xpath RexleCSS.new(x).to_xpath}
end

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



1277
1278
1279
1280
1281
# File 'lib/rexle.rb', line 1277

def delete(xpath)

  e = @doc.element(xpath)
  e.delete if e
end

#element(xpath) ⇒ Object



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

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

#elements(s = nil) ⇒ Object



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

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

#nameObject



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

def name() @doc.root.name end

#parse(x = nil) ⇒ Object



1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
# File 'lib/rexle.rb', line 1236

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



1296
1297
1298
# File 'lib/rexle.rb', line 1296

def root() 
  @doc.elements.first 
end

#text(xpath) ⇒ Object



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

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

#to_aObject



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

def to_a() @a end

#to_s(options = {}) ⇒ Object



1290
1291
1292
1293
# File 'lib/rexle.rb', line 1290

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

#write(f) ⇒ Object



1300
1301
1302
# File 'lib/rexle.rb', line 1300

def write(f) 
  f.write xml 
end

#xml(options = {}) ⇒ Object



1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
# File 'lib/rexle.rb', line 1304

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



348
349
350
# File 'lib/rexle.rb', line 348

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