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, #pretty_print, #processing_instructions, #scan_print, #scan_to_a

Constructor Details

#initialize(x = nil) ⇒ Rexle

Returns a new instance of Rexle.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/rexle.rb', line 275

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



272
273
274
# File 'lib/rexle.rb', line 272

def doctype
  @doctype
end

#instructionsObject

Returns the value of attribute instructions.



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

def instructions
  @instructions
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



272
273
274
# File 'lib/rexle.rb', line 272

def prefixes
  @prefixes
end

Instance Method Details

#add_attribute(x) ⇒ Object



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

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

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



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

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



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

def add_text(s) end

#at_css(selector) ⇒ Object



311
312
313
# File 'lib/rexle.rb', line 311

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

#attribute(key) ⇒ Object



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

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

#attributesObject



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

def attributes() @doc.attributes end

#cloneObject



307
308
309
# File 'lib/rexle.rb', line 307

def clone()
  Rexle.new self.to_a
end

#content(options = {}) ⇒ Object



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

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

#css(selector) ⇒ Object



315
316
317
# File 'lib/rexle.rb', line 315

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

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



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

def delete(xpath)

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

#element(xpath) ⇒ Object



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

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

#elements(s = nil) ⇒ Object



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

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

#nameObject



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

def name() @doc.root.name end

#parse(x = nil) ⇒ Object



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

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



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

def root() @doc.elements.first end

#text(xpath) ⇒ Object



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

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

#to_aObject



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

def to_a() @a end

#to_s(options = {}) ⇒ Object



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

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

#write(f) ⇒ Object



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

def write(f) 
  f.write xml 
end

#xml(options = {}) ⇒ Object



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

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



319
320
321
# File 'lib/rexle.rb', line 319

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