Sha256: 2d250aebe394de4e40ab809bccdb1d1b9b3043a714981a6006ebd198cf6b3f21

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

module LibXMLJRuby
  module XML
    class Attributes
      include Enumerable
      
      class << self
        def from_java(java_obj)
          attrs = LibXMLJRuby::XML::Attributes.new
          attrs.java_obj = java_obj
          attrs
        end
      end
      
      attr_accessor :java_obj
      
      def initialize
        @attribute_cache = {}
      end
            
      def each
        i = 0
        while(i < length)
          yield get_attribute(java_obj.attributes.item(i).name)
          i += 1
        end
      end
            
      def length
        java_obj.attributes.length
      end
      
      def [](name)
        attr = get_attribute(name)
        attr ? attr.value : nil
      end
      
      def []=(name, value)
        attr = get_attribute(name)
        if attr
          attr.value = value
        else
          java_obj.setAttribute(name.to_s, value.to_s)
        end
      end
      
      def get_attribute_ns(name, ns)
        attr = java_obj.get_attribute_node_ns(name, ns)
        attr ? nil : LibXMLJRuby::XML::Attr.from_java(attr)
      end
      
      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
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
libxml-fixed-jruby-1.0.0-jruby lib/libxml-jruby/xml/attributes.rb
libxml-jruby-fixed-1.0.0-jruby lib/libxml-jruby/xml/attributes.rb