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.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/rexle.rb', line 288

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, or REXML doc?
  if x then
    procs = {
      String: proc {|x| parse_string(x)},
      Array: proc {|x| x}
    }
    
    doc_node = ['doc',{}]

    @a = procs[x.class.to_s.to_sym].call(x)
    #@log.debug 'rexle: before scan_element a: ' + @a.inspect
    @doc = scan_element(*(doc_node << @a))
    #@log.debug 'rexle: after scan_element' + self.to_a.inspect
    
    # 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.



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

def doctype
  @doctype
end

#instructionsObject

Returns the value of attribute instructions.



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

def instructions
  @instructions
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



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

def prefixes
  @prefixes
end

Instance Method Details

#add_attribute(x) ⇒ Object



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

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

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



1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
# File 'lib/rexle.rb', line 1197

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', {}, element.to_a]  
    @doc = scan_element(*doc_node)      
  end
  element
end

#add_text(s) ⇒ Object



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

def add_text(s) end

#at_css(selector) ⇒ Object



326
327
328
# File 'lib/rexle.rb', line 326

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

#attribute(key) ⇒ Object



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

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

#attributesObject



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

def attributes() @doc.attributes end

#cloneObject



322
323
324
# File 'lib/rexle.rb', line 322

def clone()
  Rexle.new self.to_a
end

#content(options = {}) ⇒ Object



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

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

#css(selector) ⇒ Object



330
331
332
# File 'lib/rexle.rb', line 330

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

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



1213
1214
1215
1216
1217
# File 'lib/rexle.rb', line 1213

def delete(xpath)

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

#element(xpath) ⇒ Object



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

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

#elements(s = nil) ⇒ Object



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

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

#nameObject



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

def name() @doc.root.name end

#parse(x = nil) ⇒ Object



1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
# File 'lib/rexle.rb', line 1172

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',{}]
  @a = procs[x.class.to_s.to_sym].call(x)
  @doc = scan_element(*(doc_node << @a))
  
  self
end

#rootObject



1232
1233
1234
# File 'lib/rexle.rb', line 1232

def root() 
  @doc.elements.first 
end

#text(xpath) ⇒ Object



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

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

#to_aObject



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

def to_a() @a end

#to_s(options = {}) ⇒ Object



1226
1227
1228
1229
# File 'lib/rexle.rb', line 1226

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

#write(f) ⇒ Object



1236
1237
1238
# File 'lib/rexle.rb', line 1236

def write(f) 
  f.write xml 
end

#xml(options = {}) ⇒ Object



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

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



334
335
336
# File 'lib/rexle.rb', line 334

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