Module: REXML::Functions
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Defined in: | lib/rexml/functions.rb |
Overview
If you add a method, keep in mind two things: (1) the first argument will always be a list of nodes from which to filter. In the case of context methods (such as position), the function should return an array with a value for each child in the array. (2) all method calls from XML will have “-” replaced with “_”. Therefore, in XML, “local-name()” is identical (and actually becomes) “local_name()”
Constant Summary
-
INTERNAL_METHODS =
# File 'lib/rexml/functions.rb', line 16[ :namespace_context, :namespace_context=, :variables, :variables=, :context=, :get_namespace, :send, ]
Class Attribute Summary
- .namespace_context rw
- .namespace_context=(x) rw
- .variables rw
- .variables=(x) rw
- .context=(value) writeonly
Class Method Summary
-
.boolean(object = nil)
UNTESTED.
- .ceiling(number)
- .compare_language(lang1, lang2)
-
.concat(*objects)
UNTESTED.
-
.contains(string, test)
Fixed by Mike Stok.
-
.count(node_set)
Returns the size of the given list of nodes.
-
.false
UNTESTED.
- .floor(number)
-
.get_namespace(node_set = nil)
Helper method.
-
.id(object)
Since REXML is non-validating, this method is not implemented as it requires a DTD.
-
.lang(language)
UNTESTED.
-
.last
Returns the last node of the given list of nodes.
-
.local_name(node_set = nil)
UNTESTED.
- .name(node_set = nil)
- .namespace_uri(node_set = nil)
-
.normalize_space(string = nil)
UNTESTED.
-
.not(object)
UNTESTED.
-
.number(object = nil)
a string that consists of optional whitespace followed by an optional minus sign followed by a Number followed by whitespace is converted to the IEEE 754 number that is nearest (according to the IEEE 754 round-to-nearest rule) to the mathematical value represented by the string; any other string is converted to NaN.
- .position
- .processing_instruction(node)
- .round(number)
- .send(name, *args)
- .singleton_method_added(name)
-
.starts_with(string, test)
Fixed by Mike Stok.
-
.string(object = nil)
A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order.
-
.string_length(string)
UNTESTED.
-
.string_value(o)
A node-set is converted to a string by returning the concatenation of the string-value of each of the children of the node in the node-set that is first in document order.
-
.substring(string, start, length = nil)
Take equal portions of Mike Stok and Sean Russell; mix vigorously, and pour into a tall, chilled glass.
-
.substring_after(string, test)
Kouhei fixed this too.
-
.substring_before(string, test)
Kouhei fixed this.
- .sum(nodes)
- .text
-
.translate(string, tr1, tr2)
This is entirely Mike Stok's beast.
-
.true
UNTESTED.
Class Attribute Details
.context=(value) (writeonly)
[ GitHub ]# File 'lib/rexml/functions.rb', line 38
def Functions::context=(value); @@context = value; end
.namespace_context (rw)
[ GitHub ]# File 'lib/rexml/functions.rb', line 35
def Functions::namespace_context ; @@namespace_context ; end
.namespace_context=(x) (rw)
[ GitHub ]# File 'lib/rexml/functions.rb', line 33
def Functions::namespace_context=(x) ; @@namespace_context=x ; end
.variables (rw)
[ GitHub ]# File 'lib/rexml/functions.rb', line 36
def Functions::variables ; @@variables ; end
.variables=(x) (rw)
[ GitHub ]# File 'lib/rexml/functions.rb', line 34
def Functions::variables=(x) ; @@variables=x ; end
Class Method Details
.boolean(object = nil)
UNTESTED
# File 'lib/rexml/functions.rb', line 296
def Functions::boolean( object=nil ) if object.kind_of? String if object =~ /\d+/u return object.to_f != 0 else return object.size > 0 end elsif object.kind_of? Array object = object.find{|x| x and true} end return object ? true : false end
.ceiling(number)
[ GitHub ].compare_language(lang1, lang2)
[ GitHub ]# File 'lib/rexml/functions.rb', line 343
def Functions::compare_language lang1, lang2 lang2.downcase.index(lang1.downcase) == 0 end
.concat(*objects)
UNTESTED
# File 'lib/rexml/functions.rb', line 171
def Functions::concat( *objects ) objects.join end
.contains(string, test)
Fixed by Mike Stok
.count(node_set)
Returns the size of the given list of nodes.
# File 'lib/rexml/functions.rb', line 60
def Functions::count( node_set ) node_set.size end
.false
UNTESTED
# File 'lib/rexml/functions.rb', line 320
def Functions::false( ) false end
.floor(number)
[ GitHub ].get_namespace(node_set = nil)
Helper method.
# File 'lib/rexml/functions.rb', line 87
def Functions::get_namespace( node_set = nil ) if node_set == nil yield @@context[:node] if defined? @@context[:node].namespace else if node_set.respond_to? :each node_set.each { |node| yield node if defined? node.namespace } elsif node_set.respond_to? :namespace yield node_set end end end
.id(object)
Since REXML is non-validating, this method is not implemented as it requires a DTD
# File 'lib/rexml/functions.rb', line 66
def Functions::id( object ) end
.lang(language)
UNTESTED
# File 'lib/rexml/functions.rb', line 325
def Functions::lang( language ) lang = false node = @@context[:node] attr = nil until node.nil? if node.node_type == :element attr = node.attributes["xml:lang"] unless attr.nil? lang = compare_language(string(language), attr) break else end end node = node.parent end lang end
.last
Returns the last node of the given list of nodes.
# File 'lib/rexml/functions.rb', line 51
def Functions::last( ) @@context[:size] end
.local_name(node_set = nil)
UNTESTED
# File 'lib/rexml/functions.rb', line 70
def Functions::local_name( node_set=nil ) get_namespace( node_set ) do |node| return node.local_name end end
.name(node_set = nil)
[ GitHub ]# File 'lib/rexml/functions.rb', line 80
def Functions::name( node_set=nil ) get_namespace( node_set ) do |node| node. end end
.namespace_uri(node_set = nil)
[ GitHub ]# File 'lib/rexml/functions.rb', line 76
def Functions::namespace_uri( node_set=nil ) get_namespace( node_set ) {|node| node.namespace} end
.normalize_space(string = nil)
UNTESTED
.not(object)
UNTESTED
# File 'lib/rexml/functions.rb', line 310
def Functions::not( object ) not boolean( object ) end
.number(object = nil)
a string that consists of optional whitespace followed by an optional minus sign followed by a Number followed by whitespace is converted to the IEEE 754 number that is nearest (according to the IEEE 754 round-to-nearest rule) to the mathematical value represented by the string; any other string is converted to NaN
boolean true is converted to 1; boolean false is converted to 0
a node-set is first converted to a string as if by a call to the string function and then converted in the same way as a string argument
an object of a type other than the four basic types is converted to a number in a way that is dependent on that type
# File 'lib/rexml/functions.rb', line 360
def Functions::number( object=nil ) object = @@context[:node] unless object case object when true Float(1) when false Float(0) when Array number(string( object )) when Numeric object.to_f else str = string( object ) # If XPath ever gets scientific notation... #if str =~ /^\s*-?(\d*\.?\d|\d\.)([Ee]\d*)?\s*$/ if str =~ /^\s*-?(\d*\.?\d|\d\.)\s*$/ str.to_f else (0.0 / 0.0) end end end
.position
[ GitHub ]# File 'lib/rexml/functions.rb', line 55
def Functions::position( ) @@context[:index] end
.processing_instruction(node)
[ GitHub ]# File 'lib/rexml/functions.rb', line 407
def Functions::processing_instruction( node ) node.node_type == :processing_instruction end
.round(number)
[ GitHub ].send(name, *args)
[ GitHub ].singleton_method_added(name)
[ GitHub ]# File 'lib/rexml/functions.rb', line 26
def singleton_method_added(name) unless INTERNAL_METHODS.include?(name) @@available_functions[name] = true end end
.starts_with(string, test)
Fixed by Mike Stok
.string(object = nil)
A node-set is converted to a string by returning the string-value of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.
A number is converted to a string as follows
NaN is converted to the string NaN
positive zero is converted to the string 0
negative zero is converted to the string 0
positive infinity is converted to the string Infinity
negative infinity is converted to the string -Infinity
if the number is an integer, the number is represented in decimal form as a Number with no decimal point and no leading zeros, preceded by a minus sign (-) if the number is negative
otherwise, the number is represented in decimal form as a Number including a decimal point with at least one digit before the decimal point and at least one digit after the decimal point, preceded by a minus sign (-) if the number is negative; there must be no leading zeros before the decimal point apart possibly from the one required digit immediately before the decimal point; beyond the one required digit after the decimal point there must be as many, but only as many, more digits as are needed to uniquely distinguish the number from all other IEEE 754 numeric values.
The boolean false value is converted to the string false. The boolean true value is converted to the string true.
An object of a type other than the four basic types is converted to a string in a way that is dependent on that type.
# File 'lib/rexml/functions.rb', line 134
def Functions::string( object=nil ) #object = @context unless object if object.instance_of? Array string( object[0] ) elsif defined? object.node_type if object.node_type == :attribute object.value elsif object.node_type == :element || object.node_type == :document string_value(object) else object.to_s end elsif object.nil? return "" else object.to_s end end
.string_length(string)
UNTESTED
.string_value(o)
A node-set is converted to a string by returning the concatenation of the string-value of each of the children of the node in the node-set that is first in document order. If the node-set is empty, an empty string is returned.
# File 'lib/rexml/functions.rb', line 158
def Functions::string_value( o ) rv = "" o.children.each { |e| if e.node_type == :text rv << e.to_s elsif e.node_type == :element rv << string_value( e ) end } rv end
.substring(string, start, length = nil)
Take equal portions of Mike Stok and Sean Russell; mix vigorously, and pour into a tall, chilled glass. Serves 10,000.
# File 'lib/rexml/functions.rb', line 205
def Functions::substring( string, start, length=nil ) ruby_string = string(string) ruby_length = if length.nil? ruby_string.length.to_f else number(length) end ruby_start = number(start) # Handle the special cases return '' if ( ruby_length.nan? or ruby_start.nan? or ruby_start.infinite? ) infinite_length = ruby_length.infinite? == 1 ruby_length = ruby_string.length if infinite_length # Now, get the bounds. The XPath bounds are 1..length; the ruby bounds # are 0..length. Therefore, we have to offset the bounds by one. ruby_start = round(ruby_start) - 1 ruby_length = round(ruby_length) if ruby_start < 0 ruby_length += ruby_start unless infinite_length ruby_start = 0 end return '' if ruby_length <= 0 ruby_string[ruby_start,ruby_length] end
.substring_after(string, test)
Kouhei fixed this too
.substring_before(string, test)
Kouhei fixed this
.sum(nodes)
[ GitHub ].text
[ GitHub ]# File 'lib/rexml/functions.rb', line 40
def Functions::text( ) if @@context[:node].node_type == :element return @@context[:node].find_all{|n| n.node_type == :text}.collect{|n| n.value} elsif @@context[:node].node_type == :text return @@context[:node].value else return false end end
.translate(string, tr1, tr2)
This is entirely Mike Stok's beast
# File 'lib/rexml/functions.rb', line 253
def Functions::translate( string, tr1, tr2 ) from = string(tr1) to = string(tr2) # the map is our translation table. # # if a character occurs more than once in the # from string then we ignore the second & # subsequent mappings # # if a character maps to nil then we delete it # in the output. This happens if the from # string is longer than the to string # # there's nothing about - or ^ being special in # http://www.w3.org/TR/xpath#function-translate # so we don't build ranges or negated classes map = Hash.new 0.upto(from.length - 1) { |pos| from_char = from[pos] unless map.has_key? from_char map[from_char] = if pos < to.length to[pos] else nil end end } if ''.respond_to? :chars string(string).chars.collect { |c| if map.has_key? c then map[c] else c end }.compact.join else string(string).unpack('U*').collect { |c| if map.has_key? c then map[c] else c end }.compact.pack('U*') end end
.true
UNTESTED
# File 'lib/rexml/functions.rb', line 315
def Functions::true( ) true end