package smile.xml; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jruby.Ruby; import org.jruby.RubyClass; import org.jruby.RubyFixnum; import org.jruby.RubyHash; import org.jruby.RubyString; import org.jruby.RubySymbol; import org.jruby.anno.JRubyClass; import org.jruby.anno.JRubyMethod; import org.jruby.runtime.Block; import org.jruby.runtime.ObjectAllocator; import org.jruby.runtime.ThreadContext; import org.jruby.runtime.builtin.IRubyObject; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import smile.xml.util.UtilJ; @JRubyClass(name = "LibXML::XML::Attributes", include = "Enumerable") public class AttributesJ extends BaseJ { private static final long serialVersionUID = -2519312417704775509L; private static final ObjectAllocator ALLOCATOR = new ObjectAllocator() { public IRubyObject allocate(Ruby runtime, RubyClass klass) { return new AttributesJ(runtime, klass); } }; public static RubyClass define(Ruby runtime) { return UtilJ.defineClass(runtime, AttributesJ.class, ALLOCATOR); } private NodeJ parent; public void setParent(NodeJ node) { this.parent = node; } @JRubyMethod(name = { "initialize" }) public IRubyObject initialize(ThreadContext context) { return this; } private static RubyClass getRubyClass(Ruby runtime) { return UtilJ.getClass(runtime, new String[] { "LibXML", "XML", "Attributes" }); } public static AttributesJ newInstance(ThreadContext context) { return (AttributesJ) getRubyClass(context.getRuntime()).newInstance( context, new IRubyObject[0], null); } public AttributesJ(Ruby ruby, RubyClass clazz) { super(ruby, clazz); } @JRubyMethod(name = "[]") public IRubyObject getValue(ThreadContext context, IRubyObject pName) { String name = toJavaString(context, pName ); NamedNodeMap aa = getJavaObject().getAttributes(); if ( aa == null ) { return context.getRuntime().getNil(); } Node node = aa.getNamedItem( name ); if (node == null) { for( int i=0;i attributesAsList(ThreadContext context) { NamedNodeMap list = ((Node) getJavaObject()).getAttributes(); List array = new ArrayList(list.getLength()); for (int i = 0; i < list.getLength(); i++) { AttrJ node = AttrJ.newInstance(context); node.setJavaObject(list.item(i)); node.setParent(parent); array.add(node); } return array; } }