Class: LibXMLJRuby::XML::Attributes

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttributes

Returns a new instance of Attributes.



16
17
18
# File 'lib/libxml-jruby/xml/attributes.rb', line 16

def initialize
  @attribute_cache = {}
end

Instance Attribute Details

#java_objObject

Returns the value of attribute java_obj.



14
15
16
# File 'lib/libxml-jruby/xml/attributes.rb', line 14

def java_obj
  @java_obj
end

Class Method Details

.from_java(java_obj) ⇒ Object



7
8
9
10
11
# File 'lib/libxml-jruby/xml/attributes.rb', line 7

def from_java(java_obj)
  attrs = LibXMLJRuby::XML::Attributes.new
  attrs.java_obj = java_obj
  attrs
end

Instance Method Details

#[](name) ⇒ Object



32
33
34
35
# File 'lib/libxml-jruby/xml/attributes.rb', line 32

def [](name)
  attr = get_attribute(name)
  attr ? attr.value : nil
end

#[]=(name, value) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/libxml-jruby/xml/attributes.rb', line 37

def []=(name, value)
  attr = get_attribute(name)
  if attr
    attr.value = value
  else
    java_obj.setAttribute(name.to_s, value.to_s)
  end
end

#eachObject



20
21
22
23
24
25
26
# File 'lib/libxml-jruby/xml/attributes.rb', line 20

def each
  i = 0
  while(i < length)
    yield get_attribute(java_obj.attributes.item(i).name)
    i += 1
  end
end

#get_attribute(name) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/libxml-jruby/xml/attributes.rb', line 51

def get_attribute(name)
  if @attribute_cache[name.to_s]
    @attribute_cache[name.to_s]
  elsif java_obj && java_obj.attributes
    attr = java_obj.attributes.get_named_item(name.to_s)
    if attr
      @attribute_cache[name.to_s] = LibXMLJRuby::XML::Attr.from_java(attr)
      @attribute_cache[name.to_s]
    else
      nil
    end
  else
    nil
  end
end

#get_attribute_ns(name, ns) ⇒ Object



46
47
48
49
# File 'lib/libxml-jruby/xml/attributes.rb', line 46

def get_attribute_ns(name, ns)
  attr = java_obj.get_attribute_node_ns(name, ns)
  attr ? nil : LibXMLJRuby::XML::Attr.from_java(attr)
end

#lengthObject



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

def length
  java_obj.attributes.length
end