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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rexle.rb', line 252

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.



249
250
251
# File 'lib/rexle.rb', line 249

def doctype
  @doctype
end

#instructionsObject

Returns the value of attribute instructions.



250
251
252
# File 'lib/rexle.rb', line 250

def instructions
  @instructions
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



249
250
251
# File 'lib/rexle.rb', line 249

def prefixes
  @prefixes
end

Instance Method Details

#add_attribute(x) ⇒ Object



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

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

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



1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
# File 'lib/rexle.rb', line 1317

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



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

def add_text(s) end

#at_css(selector) ⇒ Object



290
291
292
# File 'lib/rexle.rb', line 290

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

#attribute(key) ⇒ Object



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

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

#attributesObject



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

def attributes() @doc.attributes end

#cloneObject



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

def clone()
  Rexle.new self.to_a
end

#content(options = {}) ⇒ Object



1379
1380
1381
# File 'lib/rexle.rb', line 1379

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

#css(selector) ⇒ Object



294
295
296
# File 'lib/rexle.rb', line 294

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

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



1333
1334
1335
1336
1337
# File 'lib/rexle.rb', line 1333

def delete(xpath)

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

end

#element(xpath) ⇒ Object



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

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

#elements(s = nil) ⇒ Object



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

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

#nameObject



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

def name() @doc.root.name end

#parse(x = nil) ⇒ Object



1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
# File 'lib/rexle.rb', line 1292

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



1352
1353
1354
# File 'lib/rexle.rb', line 1352

def root() 
  @doc.elements.first 
end

#text(xpath) ⇒ Object



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

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

#to_aObject



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

def to_a() @a end

#to_s(options = {}) ⇒ Object



1346
1347
1348
1349
# File 'lib/rexle.rb', line 1346

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

#write(f) ⇒ Object



1356
1357
1358
# File 'lib/rexle.rb', line 1356

def write(f) 
  f.write xml 
end

#xml(options = {}) ⇒ Object



1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
# File 'lib/rexle.rb', line 1360

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



298
299
300
# File 'lib/rexle.rb', line 298

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