Class: LibXMLJRuby::XML::Node

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/libxml-jruby/xml/node.rb

Direct Known Subclasses

Attr

Defined Under Namespace

Classes: Set

Constant Summary collapse

ATTRIBUTE_NODE =
org.w3c.dom.Node.ATTRIBUTE_NODE
CDATA_SECTION_NODE =
org.w3c.dom.Node.CDATA_SECTION_NODE
COMMENT_NODE =
org.w3c.dom.Node.COMMENT_NODE
DOCUMENT_FRAG_NODE =
org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE
DOCUMENT_NODE =
org.w3c.dom.Node.DOCUMENT_NODE
DOCUMENT_TYPE_NODE =
org.w3c.dom.Node.DOCUMENT_TYPE_NODE
ELEMENT_NODE =
org.w3c.dom.Node.ELEMENT_NODE
ENTITY_NODE =
org.w3c.dom.Node.ENTITY_NODE
ENTITY_REFERENCE_NODE =
org.w3c.dom.Node.ENTITY_REFERENCE_NODE
NOTATION_NODE =
org.w3c.dom.Node.NOTATION_NODE
PROCESSING_INSTRUCTION_NODE =
org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE
TEXT_NODE =
org.w3c.dom.Node.TEXT_NODE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content = nil) ⇒ Node

Returns a new instance of Node.



30
31
32
33
34
# File 'lib/libxml-jruby/xml/node.rb', line 30

def initialize(name, content = nil)
  jdoc = DocumentBuilderFactory.new_instance.new_document_builder.new_document
  self.java_obj = jdoc.createElement(name)
  self.content = content
end

Instance Attribute Details

#java_objObject

org.w3c.dom.Element(Node?)



28
29
30
# File 'lib/libxml-jruby/xml/node.rb', line 28

def java_obj
  @java_obj
end

Class Method Details

.from_java(java_obj) ⇒ Object



18
19
20
21
22
23
# File 'lib/libxml-jruby/xml/node.rb', line 18

def from_java(java_obj)
  return nil if java_obj.nil?
  n = allocate
  n.java_obj = java_obj
  n
end

Instance Method Details

#<<(node) ⇒ Object



68
69
70
# File 'lib/libxml-jruby/xml/node.rb', line 68

def <<(node)
  
end

#==(other) ⇒ Object



209
210
211
# File 'lib/libxml-jruby/xml/node.rb', line 209

def ==(other)
  eql?(other)
end

#[](name) ⇒ Object



201
202
203
# File 'lib/libxml-jruby/xml/node.rb', line 201

def [](name)
  attributes[name]
end

#[]=(name, value) ⇒ Object



205
206
207
# File 'lib/libxml-jruby/xml/node.rb', line 205

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

#attributesObject Also known as: properties



197
198
199
# File 'lib/libxml-jruby/xml/node.rb', line 197

def attributes
  @attributes ||= LibXMLJRuby::XML::Attributes.from_java(java_obj)
end

#baseObject



49
50
51
52
# File 'lib/libxml-jruby/xml/node.rb', line 49

def base
  b = java_obj.getAttributeNS('xml', 'base')
  b.empty? ? nil : b
end

#childObject



189
190
191
# File 'lib/libxml-jruby/xml/node.rb', line 189

def child
  @child ||= LibXMLJRuby::XML::Node.from_java(java_obj.first_child)
end

#child?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/libxml-jruby/xml/node.rb', line 161

def child?
  !!child
end

#child_add(node) ⇒ Object



59
60
61
62
# File 'lib/libxml-jruby/xml/node.rb', line 59

def child_add(node)
  java_obj.getOwnerDocument.adoptNode(node.java_obj)
  java_obj.appendChild(node.java_obj) # should this be importNode?
end

#childrenObject



157
158
159
# File 'lib/libxml-jruby/xml/node.rb', line 157

def children
  @children ||= LibXMLJRuby::XML::Node::Set.from_java(java_obj.getChildNodes).to_a
end

#contentObject



40
41
42
# File 'lib/libxml-jruby/xml/node.rb', line 40

def content
  java_obj.text_content ||= @content
end

#content=(content) ⇒ Object



54
55
56
57
# File 'lib/libxml-jruby/xml/node.rb', line 54

def content=(content)
  @content = content
  java_obj.node_value = @value if @value
end

#copy(deep = false) ⇒ Object



64
65
66
# File 'lib/libxml-jruby/xml/node.rb', line 64

def copy(deep = false)
  LibXMLJRuby::XML::Node.from_java(java_obj.cloneNode(deep))
end

#docObject



223
224
225
# File 'lib/libxml-jruby/xml/node.rb', line 223

def doc
  @doc ||= LibXMLJRuby::XML::Document.from_java(java_obj.owner_document)
end

#document?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/libxml-jruby/xml/node.rb', line 235

def document?
  doc == self
end

#each(&block) ⇒ Object



72
73
74
# File 'lib/libxml-jruby/xml/node.rb', line 72

def each(&block)
  children.each(&block)
end

#each_elementObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/libxml-jruby/xml/node.rb', line 76

def each_element
  if block_given?
    each do |node|
      yield(node) if node.node_type == ELEMENT_NODE
    end
  else
    select do |node|
      node.node_type == org.w3c.dom.Node.ELEMENT_NODE
    end
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (TypeError)


213
214
215
216
217
# File 'lib/libxml-jruby/xml/node.rb', line 213

def eql?(other)
  return false if other.nil?
  raise TypeError unless self.class === other
  (java_obj.eql?(other.java_obj) || self.to_s == other.to_s)
end

#equal?(other) ⇒ Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/libxml-jruby/xml/node.rb', line 219

def equal?(other)
  self.class === other && java_obj.equal?(other.java_obj)
end

#find(expr) ⇒ Object



88
89
90
# File 'lib/libxml-jruby/xml/node.rb', line 88

def find(expr)
  LibXMLJRuby::XML::XPath::Object.new(expr, java_obj)
end

#find_first(expr) ⇒ Object

Raises:

  • (TypeError)


92
93
94
95
# File 'lib/libxml-jruby/xml/node.rb', line 92

def find_first(expr)
  raise TypeError if java_obj.nil?
  find(expr).first
end

#firstObject



153
154
155
# File 'lib/libxml-jruby/xml/node.rb', line 153

def first
  child
end

#lastObject



185
186
187
# File 'lib/libxml-jruby/xml/node.rb', line 185

def last
  @last ||= LibXMLJRuby::XML::Node.from_java(java_obj.last_child)
end

#nameObject



36
37
38
# File 'lib/libxml-jruby/xml/node.rb', line 36

def name
  java_obj.node_name ||= @name
end

#name=(name) ⇒ Object



44
45
46
47
# File 'lib/libxml-jruby/xml/node.rb', line 44

def name=(name)
  @name = name
  java_obj.node_name = @name if @name
end

#nextObject



193
194
195
# File 'lib/libxml-jruby/xml/node.rb', line 193

def next
  @next ||= LibXMLJRuby::XML::Node.from_java(java_obj.next_sibling)
end

#next?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/libxml-jruby/xml/node.rb', line 169

def next?
  !!self.next
end

#node_typeObject



97
98
99
# File 'lib/libxml-jruby/xml/node.rb', line 97

def node_type
  java_obj.node_type
end

#node_type_nameObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/libxml-jruby/xml/node.rb', line 101

def node_type_name
  case node_type
    # Most common choices first
    when ATTRIBUTE_NODE
      'attribute'
    when DOCUMENT_NODE
      'document_xml'
    when ELEMENT_NODE
      'element'
    when TEXT_NODE
      'text'
    
    # Now the rest  
    when ATTRIBUTE_DECL
      'attribute_decl'
    when CDATA_SECTION_NODE
      'cdata'
    when COMMENT_NODE
      'comment'
    when DOCB_DOCUMENT_NODE
      'document_docbook'
    when DOCUMENT_FRAG_NODE
      'fragment'
    when DOCUMENT_TYPE_NODE
      'doctype'
    when DTD_NODE
      'dtd'
    when ELEMENT_DECL
      'elem_decl'
    when ENTITY_DECL
      'entity_decl'
    when ENTITY_NODE
      'entity'
    when ENTITY_REF_NODE
      'entity_ref'
    when HTML_DOCUMENT_NODE
      'document_html'
    when NAMESPACE_DECL
      'namespace'
    when NOTATION_NODE
      'notation'
    when PI_NODE
      'pi'
    when XINCLUDE_START
      'xinclude_start'
    when XINCLUDE_END
      'xinclude_end'
    else
      raise(UnknownType, "Unknown node type: %n", node.node_type);
  end          
end

#parentObject



177
178
179
# File 'lib/libxml-jruby/xml/node.rb', line 177

def parent
  @parent ||= LibXMLJRuby::XML::Node.from_java(java_obj.parent_node)
end

#parent?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/libxml-jruby/xml/node.rb', line 173

def parent?
  !!parent
end

#prevObject



181
182
183
# File 'lib/libxml-jruby/xml/node.rb', line 181

def prev
  @prev ||= LibXMLJRuby::XML::Node.from_java(java_obj.previous_sibling)
end

#prev?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/libxml-jruby/xml/node.rb', line 165

def prev?
  !!prev
end

#to_sObject



227
228
229
230
231
232
233
# File 'lib/libxml-jruby/xml/node.rb', line 227

def to_s
  transformer = TransformerFactory.newInstance.newTransformer
  result = StreamResult.new(StringWriter.new)
  source = DOMSource.new(java_obj)
  transformer.transform(source, result)
  result.getWriter.toString
end