Class: JEXML::Node
Overview
Wraps an underlying org.w3c.dom.Node (java.sun.com/javase/6/docs/api/org/w3c/dom/Node.html). Generic Ruby representation of an underlying Java node, if no special handling provided.
Instance Attribute Summary collapse
-
#java_node ⇒ Object
readonly
Allow read access to the underlying Java Node object.
Class Method Summary collapse
-
.create(java_node) ⇒ Object
Instantiate the correct JEXML object depending on the type of the Node object.
Instance Method Summary collapse
-
#<<(node) ⇒ Object
Append the specified node to the children of this node.
-
#add_child_node(node) ⇒ Object
Add the provided node as a child of this node.
-
#child_elements(name = nil) ⇒ Object
Get the child elements of this node with the specified name (if provided).
-
#child_nodes(name = nil) ⇒ Object
Get all the child nodes of this node.
-
#document ⇒ Object
Get the owner document for this node.
-
#element(xpath) ⇒ Object
Get the first element of this node according to the provided XPath expression.
-
#elements(xpath) ⇒ Object
Get the elements of this node according to the provided XPath expression.
-
#first_child_element(name = nil) ⇒ Object
Get the first child elements of this node with the specified name (if provided).
-
#first_child_node(name = nil) ⇒ Object
Get the first child node with the specified name.
-
#initialize(java_node) ⇒ Node
constructor
Initialize the Ruby Node object with the corresponding Java Node object.
-
#name ⇒ Object
Get the name of the node.
-
#node(xpath) ⇒ Object
Return the first matching node, if any, for the provided XPath expression.
-
#nodes(xpath) ⇒ Object
Search for nodes within the context of this node according to the XPath expression.
-
#normalize ⇒ Object
Normalize the node.
-
#remove_child_nodes(name) ⇒ Object
Remove all the child nodes for this node with the specified name.
-
#replace_child_node(old_node, new_node) ⇒ Object
Replace the specified node with the new one provided.
-
#text ⇒ Object
Get the text content for the node.
-
#text=(text) ⇒ Object
Set the text content for the node.
-
#type_code ⇒ Object
Get the type code for the node.
-
#value ⇒ Object
Get the value of the node.
Constructor Details
#initialize(java_node) ⇒ Node
Initialize the Ruby Node object with the corresponding Java Node object.
26 27 28 29 30 |
# File 'lib/jexml/node.rb', line 26 def initialize(java_node) @java_node = java_node @nodeset_constant = javax.xml.xpath.XPathConstants.java_class.declared_field('NODESET').static_value @xpath_factory = javax.xml.xpath.XPathFactory.newInstance end |
Instance Attribute Details
#java_node ⇒ Object (readonly)
Allow read access to the underlying Java Node object.
6 7 8 |
# File 'lib/jexml/node.rb', line 6 def java_node @java_node end |
Class Method Details
.create(java_node) ⇒ Object
Instantiate the correct JEXML object depending on the type of the Node object.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/jexml/node.rb', line 9 def self.create(java_node) # Comparing to the code values rather than the constants in Node can be dangerous. # Possible that the actual values can change. However, unlikely. # Difficult to get to the Interface constants using JRuby. if java_node.getNodeType == 1 # Element Element.new(java_node) elsif java_node.getNodeType == 9 # Document Document.new(java_node) else # No special handling. Treat as a generic node. Node.new(java_node) end end |
Instance Method Details
#<<(node) ⇒ Object
Append the specified node to the children of this node.
142 143 144 |
# File 'lib/jexml/node.rb', line 142 def <<(node) @java_node.appendChild(node.java_node) end |
#add_child_node(node) ⇒ Object
Add the provided node as a child of this node.
85 86 87 |
# File 'lib/jexml/node.rb', line 85 def add_child_node(node) @java_node.appendChild(node.java_node) end |
#child_elements(name = nil) ⇒ Object
Get the child elements of this node with the specified name (if provided).
101 102 103 |
# File 'lib/jexml/node.rb', line 101 def child_elements(name = nil) extract_elements(child_nodes(name)) end |
#child_nodes(name = nil) ⇒ Object
Get all the child nodes of this node. If a name is specified, only the child nodes with the same name are returned.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/jexml/node.rb', line 52 def child_nodes(name = nil) nodes = [] child_nodes = @java_node.getChildNodes (0..child_nodes.getLength - 1).each do |index| child_node = child_nodes.item(index) if name != nil nodes << Node.create(child_node) if child_node.getNodeName == name else nodes << Node.create(child_node) end end nodes end |
#document ⇒ Object
Get the owner document for this node.
147 148 149 |
# File 'lib/jexml/node.rb', line 147 def document Node.create(@java_node.getOwnerDocument) end |
#element(xpath) ⇒ Object
Get the first element of this node according to the provided XPath expression.
95 96 97 98 |
# File 'lib/jexml/node.rb', line 95 def element(xpath) elements = elements(xpath) elements.size > 0 ? elements[0] : nil end |
#elements(xpath) ⇒ Object
Get the elements of this node according to the provided XPath expression.
90 91 92 |
# File 'lib/jexml/node.rb', line 90 def elements(xpath) extract_elements(nodes(xpath)) end |
#first_child_element(name = nil) ⇒ Object
Get the first child elements of this node with the specified name (if provided).
106 107 108 109 |
# File 'lib/jexml/node.rb', line 106 def first_child_element(name = nil) child_elements = child_elements(name) child_elements.size > 0 ? child_elements[0] : nil end |
#first_child_node(name = nil) ⇒ Object
Get the first child node with the specified name.
67 68 69 70 |
# File 'lib/jexml/node.rb', line 67 def first_child_node(name = nil) child_nodes = child_nodes(name) child_nodes.size > 0 ? child_nodes[0] : nil end |
#name ⇒ Object
Get the name of the node.
112 113 114 |
# File 'lib/jexml/node.rb', line 112 def name @java_node.getNodeName end |
#node(xpath) ⇒ Object
Return the first matching node, if any, for the provided XPath expression.
45 46 47 48 |
# File 'lib/jexml/node.rb', line 45 def node(xpath) nodes = nodes(xpath) nodes.size > 0 ? nodes[0] : nil end |
#nodes(xpath) ⇒ Object
Search for nodes within the context of this node according to the XPath expression.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jexml/node.rb', line 33 def nodes(xpath) nodes = [] xpath_expr = @xpath_factory.newXPath.compile(xpath) xpath_nodes = xpath_expr.evaluate(@java_node, @nodeset_constant) (0..xpath_nodes.getLength - 1).each do |index| xpath_node = xpath_nodes.item(index) nodes << Node.create(xpath_node) end nodes end |
#normalize ⇒ Object
Normalize the node.
137 138 139 |
# File 'lib/jexml/node.rb', line 137 def normalize @java_node.normalize end |
#remove_child_nodes(name) ⇒ Object
Remove all the child nodes for this node with the specified name.
73 74 75 76 77 |
# File 'lib/jexml/node.rb', line 73 def remove_child_nodes(name) @java_node.getChildNodes.each do |child_node| @java_node.removeChild(child_node) if child_node.getNodeName == name end end |
#replace_child_node(old_node, new_node) ⇒ Object
Replace the specified node with the new one provided.
80 81 82 |
# File 'lib/jexml/node.rb', line 80 def replace_child_node(old_node, new_node) @java_node.replaceChild(new_node.java_node, old_node.java_node) end |
#text ⇒ Object
Get the text content for the node.
127 128 129 |
# File 'lib/jexml/node.rb', line 127 def text @java_node.getTextContent end |
#text=(text) ⇒ Object
Set the text content for the node.
132 133 134 |
# File 'lib/jexml/node.rb', line 132 def text=(text) @java_node.setTextContent(text) end |
#type_code ⇒ Object
Get the type code for the node.
122 123 124 |
# File 'lib/jexml/node.rb', line 122 def type_code @java_node.getNodeType end |
#value ⇒ Object
Get the value of the node.
117 118 119 |
# File 'lib/jexml/node.rb', line 117 def value @java_node.getNodeValue end |