Class: XmlNodeStream::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/xml_node_stream/node.rb

Overview

Representation of an XML node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent = nil, attributes = nil, value = nil) ⇒ Node

Returns a new instance of Node.



8
9
10
11
12
13
14
# File 'lib/xml_node_stream/node.rb', line 8

def initialize (name, parent = nil, attributes = nil, value = nil)
  @name = name
  @attributes = attributes
  @parent = parent
  @parent.add_child(self) if @parent
  @value = value
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/xml_node_stream/node.rb', line 5

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



5
6
7
# File 'lib/xml_node_stream/node.rb', line 5

def parent
  @parent
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/xml_node_stream/node.rb', line 6

def value
  @value
end

Instance Method Details

#[](name) ⇒ Object

Get the value of the node attribute with the given name.



69
70
71
# File 'lib/xml_node_stream/node.rb', line 69

def [] (name)
  return @attributes[name] if @attributes
end

#[]=(name, val) ⇒ Object

Set the value of the node attribute with the given name.



74
75
76
# File 'lib/xml_node_stream/node.rb', line 74

def []= (name, val)
  attributes[name] = val
end

#add_child(node) ⇒ Object

Add a child node.



79
80
81
82
# File 'lib/xml_node_stream/node.rb', line 79

def add_child (node)
  children << node
  node.instance_variable_set(:@parent, self)
end

#ancestorsObject

Array of all ancestors of the node.



38
39
40
41
42
43
44
# File 'lib/xml_node_stream/node.rb', line 38

def ancestors
  if @parent
    return [@parent] + @parent.ancestors
  else
    return []
  end
end

#append(text, strip_whitespace = true) ⇒ Object

Append text to the node value. If strip_whitespace is true, whitespace at the beginning and end of the node value will be removed.



116
117
118
119
120
121
122
123
# File 'lib/xml_node_stream/node.rb', line 116

def append (text, strip_whitespace = true)
  if text
    @value ||= ''
    @last_strip_whitespace = strip_whitespace
    text = text.lstrip if @value.length == 0 and strip_whitespace
    @value << text if text.length > 0
  end
end

#append_cdata(text) ⇒ Object

Append CDATA to the node value.



110
111
112
# File 'lib/xml_node_stream/node.rb', line 110

def append_cdata (text)
  append(text, false)
end

#attributesObject

Get the attributes of the node as a hash.



47
48
49
# File 'lib/xml_node_stream/node.rb', line 47

def attributes
  @attributes ||= {}
end

#childrenObject

Array of the child nodes of the node.



24
25
26
# File 'lib/xml_node_stream/node.rb', line 24

def children
  @children ||= []
end

#descendantsObject

Array of all descendants of the node.



29
30
31
32
33
34
35
# File 'lib/xml_node_stream/node.rb', line 29

def descendants
  if children.empty?
    return children
  else
    return (children + children.collect{|child| child.descendants}).flatten
  end
end

#find(selector) ⇒ Object

Find the first node that matches the given XPath. See Selector for details.



99
100
101
# File 'lib/xml_node_stream/node.rb', line 99

def find (selector)
  select(selector).first
end

#finish!Object

Called after end tag to ensure that whitespace at the end of the string is properly stripped.



126
127
128
# File 'lib/xml_node_stream/node.rb', line 126

def finish! #:nodoc
  @value.rstrip! if @value and @last_strip_whitespace
end

#first_childObject

Get the first child node.



94
95
96
# File 'lib/xml_node_stream/node.rb', line 94

def first_child
  @children.first if @children
end

#pathObject

Get the full XPath of the node.



57
58
59
60
61
62
63
64
65
66
# File 'lib/xml_node_stream/node.rb', line 57

def path
  unless @path
    if @parent
      @path = "#{@parent.path}/#{@name}"
    else
      @path = "/#{@name}"
    end
  end
  return @path
end

#release!Object

Release a node by removing it from the tree structure so that the Ruby garbage collector can reclaim the memory. This method should be called after you are done with a node. After it is called, the node will be removed from its parent’s children and will no longer be accessible.



19
20
21
# File 'lib/xml_node_stream/node.rb', line 19

def release!
  @parent.remove_child(self) if @parent
end

#remove_child(node) ⇒ Object

Remove a child node.



85
86
87
88
89
90
91
# File 'lib/xml_node_stream/node.rb', line 85

def remove_child (node)
  if @children
    if @children.delete(node)
      node.instance_variable_set(:@parent, nil)
    end
  end
end

#rootObject

Get the root element of the node tree.



52
53
54
# File 'lib/xml_node_stream/node.rb', line 52

def root
  @parent ? @parent.root : self
end

#select(selector) ⇒ Object

Find all nodes that match the given XPath. See Selector for details.



104
105
106
107
# File 'lib/xml_node_stream/node.rb', line 104

def select (selector)
  selector = selector.is_a?(Selector) ? selector : Selector.new(selector)
  return selector.find(self)
end