Module: REXML::Functions
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 collapse
- INTERNAL_METHODS =
[ :namespace_context, :namespace_context=, :variables, :variables=, :context=, :get_namespace, :send, ]
- @@available_functions =
{}
- @@context =
nil
- @@namespace_context =
{}
- @@variables =
{}
Class Method Summary collapse
- .boolean(object = ) ⇒ Object
- .ceiling(number) ⇒ Object
- .compare_language(lang1, lang2) ⇒ Object
- .concat(*objects) ⇒ Object
-
.contains(string, test) ⇒ Object
Fixed by Mike Stok.
- .context=(value) ⇒ Object
-
.count(node_set) ⇒ Object
Returns the size of the given list of nodes.
-
.false ⇒ Object
UNTESTED.
- .floor(number) ⇒ Object
-
.get_namespace(node_set = nil) ⇒ Object
Helper method.
-
.id(object) ⇒ Object
Since REXML is non-validating, this method is not implemented as it requires a DTD.
-
.lang(language) ⇒ Object
UNTESTED.
-
.last ⇒ Object
Returns the last node of the given list of nodes.
- .local_name(node_set = nil) ⇒ Object
- .name(node_set = nil) ⇒ Object
- .namespace_context ⇒ Object
- .namespace_context=(x) ⇒ Object
- .namespace_uri(node_set = nil) ⇒ Object
- .normalize_space(string = nil) ⇒ Object
-
.not(object) ⇒ Object
UNTESTED.
-
.number(object = ) ⇒ Object
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 ⇒ Object
- .processing_instruction(node) ⇒ Object
- .round(number) ⇒ Object
- .send(name, *args) ⇒ Object
- .singleton_method_added(name) ⇒ Object
-
.starts_with(string, test) ⇒ Object
Fixed by Mike Stok.
-
.string(object = ) ⇒ Object
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) ⇒ Object
UNTESTED.
-
.string_value(o) ⇒ Object
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) ⇒ Object
Take equal portions of Mike Stok and Sean Russell; mix vigorously, and pour into a tall, chilled glass.
-
.substring_after(string, test) ⇒ Object
Kouhei fixed this too.
-
.substring_before(string, test) ⇒ Object
Kouhei fixed this.
- .sum(nodes) ⇒ Object
- .text ⇒ Object
-
.translate(string, tr1, tr2) ⇒ Object
This is entirely Mike Stok’s beast.
-
.true ⇒ Object
UNTESTED.
- .variables ⇒ Object
- .variables=(x) ⇒ Object
Class Method Details
.boolean(object = ) ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/rexml/functions.rb', line 317 def Functions::boolean(object=@@context[:node]) case object when true, false object when Float return false if object.zero? return false if object.nan? true when Numeric not object.zero? when String not object.empty? when Array not object.empty? else object ? true : false end end |
.ceiling(number) ⇒ Object
417 418 419 |
# File 'lib/rexml/functions.rb', line 417 def Functions::ceiling( number ) number(number).ceil end |
.compare_language(lang1, lang2) ⇒ Object
370 371 372 |
# File 'lib/rexml/functions.rb', line 370 def Functions::compare_language lang1, lang2 lang2.downcase.index(lang1.downcase) == 0 end |
.concat(*objects) ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/rexml/functions.rb', line 190 def Functions::concat( *objects ) concatenated = "" objects.each do |object| concatenated << string(object) end concatenated end |
.contains(string, test) ⇒ Object
Fixed by Mike Stok
204 205 206 |
# File 'lib/rexml/functions.rb', line 204 def Functions::contains( string, test ) string(string).include?(string(test)) end |
.context=(value) ⇒ Object
38 |
# File 'lib/rexml/functions.rb', line 38 def Functions::context=(value); @@context = value; end |
.count(node_set) ⇒ Object
Returns the size of the given list of nodes.
60 61 62 |
# File 'lib/rexml/functions.rb', line 60 def Functions::count( node_set ) node_set.size end |
.false ⇒ Object
UNTESTED
347 348 349 |
# File 'lib/rexml/functions.rb', line 347 def Functions::false( ) false end |
.floor(number) ⇒ Object
413 414 415 |
# File 'lib/rexml/functions.rb', line 413 def Functions::floor( number ) number(number).floor end |
.get_namespace(node_set = nil) ⇒ Object
Helper method.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rexml/functions.rb', line 87 def Functions::get_namespace( node_set = nil ) if node_set == nil yield @@context[:node] if @@context[:node].respond_to?(:namespace) else if node_set.respond_to? :each result = [] node_set.each do |node| result << yield(node) if node.respond_to?(:namespace) end result elsif node_set.respond_to? :namespace yield node_set end end end |
.id(object) ⇒ Object
Since REXML is non-validating, this method is not implemented as it requires a DTD
66 67 |
# File 'lib/rexml/functions.rb', line 66 def Functions::id( object ) end |
.lang(language) ⇒ Object
UNTESTED
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/rexml/functions.rb', line 352 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 ⇒ Object
Returns the last node of the given list of nodes.
51 52 53 |
# File 'lib/rexml/functions.rb', line 51 def Functions::last( ) @@context[:size] end |
.local_name(node_set = nil) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/rexml/functions.rb', line 69 def Functions::local_name(node_set=nil) get_namespace(node_set) do |node| return node.local_name end "" end |
.name(node_set = nil) ⇒ Object
80 81 82 83 84 |
# File 'lib/rexml/functions.rb', line 80 def Functions::name( node_set=nil ) get_namespace( node_set ) do |node| node. end end |
.namespace_context ⇒ Object
35 |
# File 'lib/rexml/functions.rb', line 35 def Functions::namespace_context ; @@namespace_context ; end |
.namespace_context=(x) ⇒ Object
33 |
# File 'lib/rexml/functions.rb', line 33 def Functions::namespace_context=(x) ; @@namespace_context=x ; end |
.namespace_uri(node_set = nil) ⇒ Object
76 77 78 |
# 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) ⇒ Object
265 266 267 268 269 270 271 272 |
# File 'lib/rexml/functions.rb', line 265 def Functions::normalize_space( string=nil ) string = string(@@context[:node]) if string.nil? if string.kind_of? Array string.collect{|x| x.to_s.strip.gsub(/\s+/um, ' ') if x} else string.to_s.strip.gsub(/\s+/um, ' ') end end |
.not(object) ⇒ Object
UNTESTED
337 338 339 |
# File 'lib/rexml/functions.rb', line 337 def Functions::not( object ) not boolean( object ) end |
.number(object = ) ⇒ Object
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
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/rexml/functions.rb', line 387 def Functions::number(object=@@context[:node]) case object when true Float(1) when false Float(0) when Array number(string(object)) when Numeric object.to_f else str = string(object) case str.strip when /\A\s*(-?(?:\d+(?:\.\d*)?|\.\d+))\s*\z/ $1.to_f else Float::NAN end end end |
.position ⇒ Object
55 56 57 |
# File 'lib/rexml/functions.rb', line 55 def Functions::position( ) @@context[:index] end |
.processing_instruction(node) ⇒ Object
432 433 434 |
# File 'lib/rexml/functions.rb', line 432 def Functions::processing_instruction( node ) node.node_type == :processing_instruction end |
.round(number) ⇒ Object
421 422 423 424 425 426 427 428 429 430 |
# File 'lib/rexml/functions.rb', line 421 def Functions::round( number ) number = number(number) begin neg = number.negative? number = number.abs.round neg ? -number : number rescue FloatDomainError number end end |
.send(name, *args) ⇒ Object
436 437 438 439 440 441 442 443 444 |
# File 'lib/rexml/functions.rb', line 436 def Functions::send(name, *args) if @@available_functions[name.to_sym] super else # TODO: Maybe, this is not XPath spec behavior. # This behavior must be reconsidered. XPath.match(@@context[:node], name.to_s) end end |
.singleton_method_added(name) ⇒ Object
26 27 28 29 30 |
# 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) ⇒ Object
Fixed by Mike Stok
199 200 201 |
# File 'lib/rexml/functions.rb', line 199 def Functions::starts_with( string, test ) string(string).index(string(test)) == 0 end |
.string(object = ) ⇒ Object
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.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/rexml/functions.rb', line 138 def Functions::string( object=@@context[:node] ) if object.respond_to?(:node_type) case object.node_type when :attribute object.value when :element string_value(object) when :document string_value(object.root) when :processing_instruction object.content else object.to_s end else case object when Array string(object[0]) when Float if object.nan? "NaN" else integer = object.to_i if object == integer "%d" % integer else object.to_s end end else object.to_s end end end |
.string_length(string) ⇒ Object
UNTESTED
261 262 263 |
# File 'lib/rexml/functions.rb', line 261 def Functions::string_length( string ) string(string).length end |
.string_value(o) ⇒ Object
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.
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rexml/functions.rb', line 178 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) ⇒ Object
Take equal portions of Mike Stok and Sean Russell; mix vigorously, and pour into a tall, chilled glass. Serves 10,000.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/rexml/functions.rb', line 228 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) ⇒ Object
Kouhei fixed this too
220 221 222 223 224 |
# File 'lib/rexml/functions.rb', line 220 def Functions::substring_after( string, test ) ruby_string = string(string) return $1 if ruby_string =~ /#{test}(.*)/ "" end |
.substring_before(string, test) ⇒ Object
Kouhei fixed this
209 210 211 212 213 214 215 216 217 |
# File 'lib/rexml/functions.rb', line 209 def Functions::substring_before( string, test ) ruby_string = string(string) ruby_index = ruby_string.index(string(test)) if ruby_index.nil? "" else ruby_string[ 0...ruby_index ] end end |
.sum(nodes) ⇒ Object
408 409 410 411 |
# File 'lib/rexml/functions.rb', line 408 def Functions::sum( nodes ) nodes = [nodes] unless nodes.kind_of? Array nodes.inject(0) { |r,n| r + number(string(n)) } end |
.text ⇒ Object
40 41 42 43 44 45 46 47 48 |
# 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) ⇒ Object
This is entirely Mike Stok’s beast
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 |
# File 'lib/rexml/functions.rb', line 275 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 ⇒ Object
UNTESTED
342 343 344 |
# File 'lib/rexml/functions.rb', line 342 def Functions::true( ) true end |
.variables ⇒ Object
36 |
# File 'lib/rexml/functions.rb', line 36 def Functions::variables ; @@variables ; end |
.variables=(x) ⇒ Object
34 |
# File 'lib/rexml/functions.rb', line 34 def Functions::variables=(x) ; @@variables=x ; end |