use of org.jruby.RubyArray in project enumerable by hraberg.
the class JRubyTestBase method javaInRubyToRuby.
protected static IRubyObject javaInRubyToRuby(Ruby ruby, Object value) {
if (value instanceof List<?> && !(value instanceof RubyArray)) {
RubyArray array = RubyArray.newArray(ruby);
array.addAll((Collection<?>) value);
return array;
}
if (value instanceof Map<?, ?> && !(value instanceof RubyHash)) {
RubyHash hash = RubyHash.newHash(ruby);
hash.putAll((Map<?, ?>) value);
return hash;
}
return javaToRuby(ruby, value);
}
use of org.jruby.RubyArray in project nokogiri by sparklemotion.
the class XmlElement method accept.
@Override
public void accept(ThreadContext context, SaveContextVisitor visitor) {
visitor.enter((Element) node);
XmlNodeSet xmlNodeSet = (XmlNodeSet) children(context);
if (xmlNodeSet.length() > 0) {
RubyArray nodes = xmlNodeSet.nodes;
for (int i = 0; i < nodes.size(); i++) {
Object item = nodes.eltInternal(i);
if (item instanceof XmlNode) {
((XmlNode) item).accept(context, visitor);
} else if (item instanceof XmlNamespace) {
((XmlNamespace) item).accept(context, visitor);
}
}
}
visitor.leave((Element) node);
}
use of org.jruby.RubyArray in project nokogiri by sparklemotion.
the class XmlNode method element_children.
@JRubyMethod(name = { "element_children", "elements" })
public IRubyObject element_children(ThreadContext context) {
List<Node> elementNodes = new ArrayList<Node>();
addElements(node, elementNodes, false);
if (elementNodes.size() == 0)
return XmlNodeSet.newEmptyNodeSet(context);
RubyArray array = NokogiriHelpers.nodeArrayToRubyArray(context.getRuntime(), elementNodes.toArray(new Node[0]));
XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, array);
return xmlNodeSet;
}
use of org.jruby.RubyArray in project nokogiri by sparklemotion.
the class XmlNode method accept.
// Users might extend XmlNode. This method works for such a case.
public void accept(ThreadContext context, SaveContextVisitor visitor) {
visitor.enter(node);
XmlNodeSet xmlNodeSet = (XmlNodeSet) children(context);
if (xmlNodeSet.length() > 0) {
RubyArray array = (RubyArray) xmlNodeSet.to_a(context);
for (int i = 0; i < array.getLength(); i++) {
Object item = array.get(i);
if (item instanceof XmlNode) {
XmlNode cur = (XmlNode) item;
cur.accept(context, visitor);
} else if (item instanceof XmlNamespace) {
XmlNamespace cur = (XmlNamespace) item;
cur.accept(context, visitor);
}
}
}
visitor.leave(node);
}
use of org.jruby.RubyArray in project nokogiri by sparklemotion.
the class XmlNode method namespace_definitions.
/**
* Return an array of XmlNamespace nodes based on the attributes
* of this node.
*/
@JRubyMethod
public IRubyObject namespace_definitions(ThreadContext context) {
// don't use namespace_definitions cache anymore since
// namespaces might be deleted. Reflecting the result of
// namesapce removals is complicated, so the cache might not be
// updated.
Ruby ruby = context.getRuntime();
RubyArray namespace_definitions = ruby.newArray();
if (doc == null)
return namespace_definitions;
if (doc instanceof HtmlDocument)
return namespace_definitions;
List<XmlNamespace> namespaces = ((XmlDocument) doc).getNamespaceCache().get(node);
for (XmlNamespace namespace : namespaces) {
namespace_definitions.append(namespace);
}
return namespace_definitions;
}
Aggregations