class Rexle
Attributes
doctype[R]
instructions[RW]
prefixes[R]
Public Class Methods
new(x=nil, rexle: self, debug: false)
click to toggle source
Calls superclass method
# File lib/rexle.rb, line 217 def initialize(x=nil, rexle: self, debug: false) @rexle, @debug = rexle, debug $debug = @debug puts 'inside Rexle'.debug if debug 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
Public Instance Methods
add_attribute(x)
click to toggle source
# File lib/rexle.rb, line 1446 def add_attribute(x) @doc.attribute(x) end
add_element(element)
click to toggle source
# File lib/rexle.rb, line 1450 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
Also aliased as: add
add_text(s)
click to toggle source
# File lib/rexle.rb, line 1462 def add_text(s) end
at_css(selector)
click to toggle source
# File lib/rexle.rb, line 260 def at_css(selector) @doc.root.element RexleCSS.new(selector).to_xpath end
attribute(key)
click to toggle source
# File lib/rexle.rb, line 1447 def attribute(key) @doc.attribute(key) end
attributes()
click to toggle source
# File lib/rexle.rb, line 1448 def attributes() @doc.attributes end
clone()
click to toggle source
# File lib/rexle.rb, line 256 def clone() Rexle.new self.to_a end
content(options={})
click to toggle source
# File lib/rexle.rb, line 1512 def content(options={}) CGI.unescapeHTML(xml(options)) end
css(selector)
click to toggle source
# File lib/rexle.rb, line 264 def css(selector) a = selector.split(',').flat_map do |x| @doc.root.xpath RexleCSS.new(x).to_xpath end return a end
delete(xpath)
click to toggle source
# File lib/rexle.rb, line 1466 def delete(xpath) @doc.xpath(xpath).each {|e| e.delete; e = nil } end
Also aliased as: remove
element(xpath)
click to toggle source
# File lib/rexle.rb, line 1474 def element(xpath) self.xpath(xpath).first end
elements(s=nil)
click to toggle source
# File lib/rexle.rb, line 1475 def elements(s=nil) @doc.elements(s) end
name()
click to toggle source
# File lib/rexle.rb, line 1476 def name() @doc.root.name end
parse(x=nil) { || ... }
click to toggle source
# File lib/rexle.rb, line 1425 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
root()
click to toggle source
# File lib/rexle.rb, line 1485 def root() @doc.elements.first end
text(xpath)
click to toggle source
# File lib/rexle.rb, line 1484 def text(xpath) @doc.text(xpath) end
to_a()
click to toggle source
# File lib/rexle.rb, line 1477 def to_a() @a end
to_s(options={})
click to toggle source
# File lib/rexle.rb, line 1479 def to_s(options={}) return '<UNDEFINED/>' unless @doc self.xml options end
write(f)
click to toggle source
# File lib/rexle.rb, line 1489 def write(f) f.write xml end
xml(options={})
click to toggle source
# File lib/rexle.rb, line 1493 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 and @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)
click to toggle source
# File lib/rexle.rb, line 273 def xpath(path, &blk) @doc.xpath(path, &blk) end
Private Instance Methods
parse_rexle(x)
click to toggle source
# File lib/rexle.rb, line 1518 def parse_rexle(x) rp = RexleParser.new(x) a = rp.to_a @instructions = rp.instructions return a end
parse_string(x)
click to toggle source
# File lib/rexle.rb, line 1527 def parse_string(x) # check if the XML string is a dynarex document if x[/<summary>/] then recordx_type = x[/<recordx_type>(\w+)/m,1] if recordx_type then procs = { #'dynarex' => proc {|x| DynarexParser.new(x).to_a}, 'dynarex' => proc {|x| parse_rexle(x)}, #'polyrex' => proc {|x| PolyrexParser.new(x).to_a}, 'polyrex' => proc {|x| parse_rexle(x)} } other_parser = procs[recordx_type] if other_parser then begin other_parser.call(x) rescue parse_rexle x end else parse_rexle x end else parse_rexle x end else parse_rexle x end end
scan_doc(node)
click to toggle source
scan a rexml doc
# File lib/rexle.rb, line 1612 def scan_doc(node) children = node.elements.map {|child| scan_doc child} attributes = node.attributes.inject({}){|r,x| r.merge(Hash[*x])} [node.name, node.text.to_s, attributes, *children] end
scan_element(name=nil, attributes=nil, *children)
click to toggle source
# File lib/rexle.rb, line 1570 def scan_element(name=nil, attributes=nil, *children) return unless name return Rexle::CData.new(children.first) if name == '![' return Rexle::Comment.new(children.first) if name == '!-' element = Rexle::Element.new(name, attributes: attributes, rexle: @rexle) if children then children.each do |x4| if x4.is_a? Array then element.add_element scan_element(*x4) elsif x4.is_a? String then e = if x4.is_a? String then x4 elsif x4.name == '![' then Rexle::CData.new(x4) elsif x4.name == '!-' then Rexle::Comment.new(x4) end element.add_element e end end end return element end