class Shax

Constants

Atts
Data
Tags

Attributes

root[RW]
xml[RW]

Public Class Methods

load(str) click to toggle source
# File lib/shax.rb, line 31
def self.load(str)
  new(str).convert
end
load!(str, deep=4) click to toggle source
# File lib/shax.rb, line 35
def self.load!(str, deep=4)
  (+load(str))[['*'] * deep * '/']
end
new(xml) click to toggle source
# File lib/shax.rb, line 76
def initialize(xml)
  @xml  = xml
  @root = parse xml
end
show(obj, lev=0) click to toggle source
# File lib/shax.rb, line 39
def self.show(obj, lev=0)
  min = '  ' * (lev + 0)
  pre = '  ' * (lev + 1)
  puts "{" if lev == 0
  case obj
  when Hash
    obj.each do |k, v|
      case v
      when Hash
        puts "#{pre}#{k.inspect} => {"
        show(v, lev + 1)
        puts "#{pre}},"
      when Array
        puts "#{pre}#{k.inspect} => ["
        show(v, lev + 2)
        puts "#{pre}],"
      else
        puts "#{pre}#{k.inspect} => #{v.inspect},"
      end
    end
  when Array
    obj.each do |v|
      case v
      when Hash
        puts "#{pre}{"
        show(v, lev + 1)
        puts "#{pre}},"
      when Array
        abort "ERROR: arrays of arrays not yet supported"
      else
        puts "#{min}#{v.inspect},"
      end
    end
  end
  puts "}" if lev == 0
end

Public Instance Methods

convert(node=@root) click to toggle source
# File lib/shax.rb, line 139
def convert(node=@root)
  return node.val if node.val
  out = {}
  for kid in node.kids
    obj = convert kid
    tag = kid.tag
    tag = skip_ns tag
    if out.key?(tag)
      val = out[tag]
      out[tag] = [val] unless val.is_a?(Array)
      out[tag].push obj
    else
      out[tag] = obj
    end
  end
  out
end
parse(str) click to toggle source
# File lib/shax.rb, line 81
def parse(str)
  node = root = Node.new "!xml"
  tags = []; str.scan(Tags).each {|hits| tags.push(hits)}
  skip = nil

  tags.each_with_index do |(tag, atts, text), i|
    next if i == skip

    # closing tag
    if tag[0] == '/'
      node = node.parent

    # create baby
    else
      node.kids.push(baby = Node.new(tag, node))
      # baby.atts = @parse_atts atts if atts? # if config.atts

      # self-closing tag
      if atts && atts[-1] == '/'
        baby.val = ''

      # text node
      elsif ((skip = i + 1) < tags.size) and tags[skip][0] == "/#{tag}"
        # baby.val = @value text?...
        # baby.val = !text ? '' : text.scan(Data).each {|hits| hits[0]}
        baby.val = text

      # starting tag
      else
        node = baby
        skip = nil
      end
    end
  end
  root
end
parse_atts(str) click to toggle source
# File lib/shax.rb, line 118
def parse_atts(str)
  if str and (str = str.strip!).length > 3
    atts = {}
    str.scan(Atts).each {|hits| atts["#{skip_ns(hits[1])}"] = value hits[3]}
    atts
  end
end
skip_ns(str) click to toggle source
# File lib/shax.rb, line 126
def skip_ns(str)
  return str unless str.include?(':')
  pre = str[0] == '/' ? '/' : ''
  pre = str.split(':', 2)[-1]
end
value(str) click to toggle source
# File lib/shax.rb, line 132
def value(str)
  return '' unless str
  return '' + str # if isNan str ???
  return str.to_f if str.include? '.'
  return str.to_i
end