Class: XPath::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/xml/xpath.rb

Constant Summary collapse

PredefinedNamespace =
{
  'xml' => 'http://www.w3.org/XML/1998/namespace',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, namespace = nil, variable = nil, visitor = nil) ⇒ Context

Returns a new instance of Context.



3015
3016
3017
3018
3019
3020
3021
3022
3023
# 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

Instance Attribute Details

#context_positionObject (readonly)

Returns the value of attribute context_position.



3025
3026
3027
# File 'lib/xml/xpath.rb', line 3025

def context_position
  @context_position
end

#context_sizeObject (readonly)

Returns the value of attribute context_size.



3025
3026
3027
# File 'lib/xml/xpath.rb', line 3025

def context_size
  @context_size
end

#nodeObject (readonly)

Returns the value of attribute node.



3025
3026
3027
# File 'lib/xml/xpath.rb', line 3025

def node
  @node
end

#visitorObject (readonly)

Returns the value of attribute visitor.



3025
3026
3027
# File 'lib/xml/xpath.rb', line 3025

def visitor
  @visitor
end

Instance Method Details

#funcall(name, *args) ⇒ Object



3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
# 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) ⇒ Object

Raises:



3044
3045
3046
3047
3048
# 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) ⇒ Object

Raises:



3033
3034
3035
3036
3037
# 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) ⇒ Object



3059
3060
3061
3062
3063
3064
3065
# File 'lib/xml/xpath.rb', line 3059

def make_boolean(f)
  if f then
    XPathTrue
  else
    XPathFalse
  end
end

#make_nodeset(*nodes) ⇒ Object



3067
3068
3069
# File 'lib/xml/xpath.rb', line 3067

def make_nodeset(*nodes)
  XPathNodeSet.new(self, *nodes)
end

#make_number(num) ⇒ Object



3055
3056
3057
# File 'lib/xml/xpath.rb', line 3055

def make_number(num)
  XPathNumber.new num
end

#make_string(str) ⇒ Object



3051
3052
3053
# File 'lib/xml/xpath.rb', line 3051

def make_string(str)
  XPathString.new str
end

#reuse(node, pos = 1, size = 1) ⇒ Object



3027
3028
3029
3030
# 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_nodesetObject



3076
3077
3078
# File 'lib/xml/xpath.rb', line 3076

def root_nodeset
  make_nodeset @visitor.visit(@node).root
end

#to_nodesetObject



3072
3073
3074
# File 'lib/xml/xpath.rb', line 3072

def to_nodeset
  make_nodeset @node
end