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