class XPath::Context

Constants

PredefinedNamespace

Attributes

context_position[R]
context_size[R]
node[R]
visitor[R]

Public Class Methods

new(node, namespace = nil, variable = nil, visitor = nil) click to toggle source
# File lib/xml/xpath.rb, line 3015
def initialize(node, namespace = nil, variable = nil, visitor = nil)
  visitor = TransparentNodeVisitor.new unless visitor
  @visitor = visitor
  @node = node
  @context_position = 1
  @context_size = 1
  @variables = variable
  @namespaces = namespace || {}
end

Public Instance Methods

funcall(name, *args) click to toggle source
# File lib/xml/xpath.rb, line 3081
def funcall(name, *args)
  begin
    send('f_' + name.tr('-', '_'), *args)
  rescue Object::NameError
    if $@[0] == "#{__FILE__}:#{__LINE__-2}:in `send'" then
      raise XPath::NameError, "undefined function `#{name}'"
    end
    raise
  rescue Object::ArgumentError
    if $@[1] == "#{__FILE__}:#{__LINE__-7}:in `send'" then
      raise XPath::ArgumentError, "#{$!} for `#{name}'"
    end
    raise
  end
end
get_namespace(prefix) click to toggle source
# File lib/xml/xpath.rb, line 3044
def get_namespace(prefix)
  ret = @namespaces[prefix] || PredefinedNamespace[prefix]
  raise XPath::Error, "undeclared namespace `#{prefix}'" unless ret
  ret
end
get_variable(name) click to toggle source
# File lib/xml/xpath.rb, line 3033
def get_variable(name)
  value = @variables && @variables[name]  # value should be a XPathObjcect.
  raise XPath::NameError, "undefined variable `#{name}'" unless value
  value
end
make_boolean(f) click to toggle source
# File lib/xml/xpath.rb, line 3059
def make_boolean(f)
  if f then
    XPathTrue
  else
    XPathFalse
  end
end
make_nodeset(*nodes) click to toggle source
# File lib/xml/xpath.rb, line 3067
def make_nodeset(*nodes)
  XPathNodeSet.new(self, *nodes)
end
make_number(num) click to toggle source
# File lib/xml/xpath.rb, line 3055
def make_number(num)
  XPathNumber.new num
end
make_string(str) click to toggle source
# File lib/xml/xpath.rb, line 3051
def make_string(str)
  XPathString.new str
end
reuse(node, pos = 1, size = 1) click to toggle source
# File lib/xml/xpath.rb, line 3027
def reuse(node, pos = 1, size = 1)
  @variables = nil
  @node, @context_position, @context_size = node, pos, size
end
root_nodeset() click to toggle source
# File lib/xml/xpath.rb, line 3076
def root_nodeset
  make_nodeset @visitor.visit(@node).root
end
to_nodeset() click to toggle source
# File lib/xml/xpath.rb, line 3072
def to_nodeset
  make_nodeset @node
end

Private Instance Methods

f_boolean(obj) click to toggle source
# File lib/xml/xpath.rb, line 3233
def f_boolean(obj)
  obj.to_boolean self
end
f_ceiling(num) click to toggle source
# File lib/xml/xpath.rb, line 3273
def f_ceiling(num)
  num.to_number(self).ceil
end
f_concat(str, str2, *strs) click to toggle source
# File lib/xml/xpath.rb, line 3183
def f_concat(str, str2, *strs)
  s = str2.to_str.dup
  strs.each { |i| s << i.to_str }
  str.to_string(self).concat(s)
end
f_contains(str, sub) click to toggle source
# File lib/xml/xpath.rb, line 3193
def f_contains(str, sub)
  make_boolean str.to_string(self).contain?(sub.to_str)
end
f_count(nodeset) click to toggle source
# File lib/xml/xpath.rb, line 3122
def f_count(nodeset)
  must_be_nodeset nodeset
  make_number nodeset.count.to_f
end
f_false() click to toggle source
# File lib/xml/xpath.rb, line 3245
def f_false
  make_boolean false
end
f_floor(num) click to toggle source
# File lib/xml/xpath.rb, line 3269
def f_floor(num)
  num.to_number(self).floor
end
f_id(obj) click to toggle source
# File lib/xml/xpath.rb, line 3127
def f_id(obj)
  unless obj.is_a? XPathNodeSet then
    ids = obj.to_str.strip.split(/\s+/)
  else
    ids = []
    obj.each { |node| ids.push @visitor.visit(node).string_value }
  end
  root = @visitor.visit(@node).root
  make_nodeset(*@visitor.visit(root).select_id(*ids))
end
f_lang(str) click to toggle source
# File lib/xml/xpath.rb, line 3249
def f_lang(str)
  lang = @visitor.visit(@node).lang
  make_boolean(lang && /\A#{Regexp.quote(str.to_str)}(?:-|\z)/i =~ lang)
end
f_last() click to toggle source
# File lib/xml/xpath.rb, line 3114
def f_last
  make_number @context_size.to_f
end
f_local_name(nodeset = nil) click to toggle source
# File lib/xml/xpath.rb, line 3138
def f_local_name(nodeset = nil)
  unless nodeset then
    n = @node
  else
    must_be_nodeset nodeset
    n = nodeset.first
  end
  n = @visitor.visit(n) if n
  n = n.name_localpart if n
  n = '' unless n
  make_string n
end
f_name(nodeset = nil) click to toggle source
# File lib/xml/xpath.rb, line 3164
def f_name(nodeset = nil)
  unless nodeset then
    n = @node
  else
    must_be_nodeset nodeset
    n = nodeset.first
  end
  n = @visitor.visit(n) if n
  n = n.qualified_name if n
  n = '' unless n
  make_string n
end
f_namespace_uri(nodeset = nil) click to toggle source
# File lib/xml/xpath.rb, line 3151
def f_namespace_uri(nodeset = nil)
  unless nodeset then
    n = @node
  else
    must_be_nodeset nodeset
    n = nodeset.first
  end
  n = @visitor.visit(n) if n
  n = n.namespace_uri if n
  n = '' unless n
  make_string n
end
f_normalize_space(str = nil) click to toggle source
# File lib/xml/xpath.rb, line 3219
def f_normalize_space(str = nil)
  if str then
    str = str.to_string(self)
  else
    str = make_string(@node.string_value)
  end
  str.normalize_space
end
f_not(bool) click to toggle source
# File lib/xml/xpath.rb, line 3237
def f_not(bool)
  make_boolean(!bool.true?)
end
f_number(obj = nil) click to toggle source
# File lib/xml/xpath.rb, line 3255
def f_number(obj = nil)
  obj = to_nodeset unless obj
  obj.to_number self
end
f_position() click to toggle source
# File lib/xml/xpath.rb, line 3118
def f_position
  make_number @context_position.to_f
end
f_round(num) click to toggle source
# File lib/xml/xpath.rb, line 3277
def f_round(num)
  num.to_number(self).round
end
f_starts_with(str, sub) click to toggle source
# File lib/xml/xpath.rb, line 3189
def f_starts_with(str, sub)
  make_boolean str.to_string(self).start_with?(sub.to_str)
end
f_string(obj = nil) click to toggle source
# File lib/xml/xpath.rb, line 3178
def f_string(obj = nil)
  obj = to_nodeset unless obj
  obj.to_string self
end
f_string_length(str = nil) click to toggle source
# File lib/xml/xpath.rb, line 3210
def f_string_length(str = nil)
  if str then
    str = str.to_string(self)
  else
    str = make_string(@node.string_value)
  end
  make_number str.size.to_f
end
f_substring(str, start, len = nil) click to toggle source
# File lib/xml/xpath.rb, line 3205
def f_substring(str, start, len = nil)
  len = len.to_number(self) if len
  str.to_string(self).substring start.to_number(self), len
end
f_substring_after(str, sub) click to toggle source
# File lib/xml/xpath.rb, line 3201
def f_substring_after(str, sub)
  str.to_string(self).substring_after sub.to_str
end
f_substring_before(str, sub) click to toggle source
# File lib/xml/xpath.rb, line 3197
def f_substring_before(str, sub)
  str.to_string(self).substring_before sub.to_str
end
f_sum(nodeset) click to toggle source
# File lib/xml/xpath.rb, line 3260
def f_sum(nodeset)
  must_be_nodeset nodeset
  sum = 0.0
  nodeset.each { |node|
    sum += make_string(@visitor.visit(node).string_value).to_f
  }
  make_number sum
end
f_translate(str, from, to) click to toggle source
# File lib/xml/xpath.rb, line 3228
def f_translate(str, from, to)
  str.to_string(self).translate from.to_str, to.to_str
end
f_true() click to toggle source
# File lib/xml/xpath.rb, line 3241
def f_true
  make_boolean true
end
must(type, *args) click to toggle source
# File lib/xml/xpath.rb, line 3100
def must(type, *args)
  args.each { |i|
    unless i.is_a? type then
      s = type.name.sub(/\A.*::(?:XPath)?(?=[^:]+\z)/, '')
      raise XPath::TypeError, "argument must be #{s}"
    end
  }
end
must_be_nodeset(*args) click to toggle source
# File lib/xml/xpath.rb, line 3109
def must_be_nodeset(*args)
  must XPathNodeSet, *args
end