/tools/Ruby/lib/ruby/1.8/rexml/node.rb

http://github.com/agross/netopenspace · Ruby · 75 lines · 54 code · 9 blank · 12 comment · 6 complexity · c09080fd2dad04858138d64c67a722f4 MD5 · raw file

  1. require "rexml/parseexception"
  2. require "rexml/formatters/pretty"
  3. require "rexml/formatters/default"
  4. module REXML
  5. # Represents a node in the tree. Nodes are never encountered except as
  6. # superclasses of other objects. Nodes have siblings.
  7. module Node
  8. # @return the next sibling (nil if unset)
  9. def next_sibling_node
  10. return nil if @parent.nil?
  11. @parent[ @parent.index(self) + 1 ]
  12. end
  13. # @return the previous sibling (nil if unset)
  14. def previous_sibling_node
  15. return nil if @parent.nil?
  16. ind = @parent.index(self)
  17. return nil if ind == 0
  18. @parent[ ind - 1 ]
  19. end
  20. # indent::
  21. # *DEPRECATED* This parameter is now ignored. See the formatters in the
  22. # REXML::Formatters package for changing the output style.
  23. def to_s indent=nil
  24. unless indent.nil?
  25. Kernel.warn( "#{self.class.name}.to_s(indent) parameter is deprecated" )
  26. f = REXML::Formatters::Pretty.new( indent )
  27. f.write( self, rv = "" )
  28. else
  29. f = REXML::Formatters::Default.new
  30. f.write( self, rv = "" )
  31. end
  32. return rv
  33. end
  34. def indent to, ind
  35. if @parent and @parent.context and not @parent.context[:indentstyle].nil? then
  36. indentstyle = @parent.context[:indentstyle]
  37. else
  38. indentstyle = ' '
  39. end
  40. to << indentstyle*ind unless ind<1
  41. end
  42. def parent?
  43. false;
  44. end
  45. # Visit all subnodes of +self+ recursively
  46. def each_recursive(&block) # :yields: node
  47. self.elements.each {|node|
  48. block.call(node)
  49. node.each_recursive(&block)
  50. }
  51. end
  52. # Find (and return) first subnode (recursively) for which the block
  53. # evaluates to true. Returns +nil+ if none was found.
  54. def find_first_recursive(&block) # :yields: node
  55. each_recursive {|node|
  56. return node if block.call(node)
  57. }
  58. return nil
  59. end
  60. # Returns the position that +self+ holds in its parent's array, indexed
  61. # from 1.
  62. def index_in_parent
  63. parent.index(self)+1
  64. end
  65. end
  66. end