Search in sources :

Example 1 with RubyNil

use of org.jruby.RubyNil in project gocd by gocd.

the class XmlDocument method root_set.

@JRubyMethod(name = "root=")
public IRubyObject root_set(ThreadContext context, IRubyObject newRoot_) {
    // should be nil.
    if (newRoot_ instanceof RubyNil) {
        getDocument().getDocumentElement().setUserData(NokogiriHelpers.VALID_ROOT_NODE, false, null);
        return newRoot_;
    }
    XmlNode newRoot = asXmlNode(context, newRoot_);
    IRubyObject root = root(context);
    if (root.isNil()) {
        Node newRootNode;
        if (getDocument() == newRoot.getOwnerDocument()) {
            newRootNode = newRoot.node;
        } else {
            // must copy otherwise newRoot may exist in two places
            // with different owner document.
            newRootNode = getDocument().importNode(newRoot.node, true);
        }
        add_child_node(context, getCachedNodeOrCreate(context.getRuntime(), newRootNode));
    } else {
        Node rootNode = asXmlNode(context, root).node;
        ((XmlNode) getCachedNodeOrCreate(context.getRuntime(), rootNode)).replace_node(context, newRoot);
    }
    return newRoot;
}
Also used : RubyNil(org.jruby.RubyNil) Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 2 with RubyNil

use of org.jruby.RubyNil in project nokogiri by sparklemotion.

the class XmlDocument method root_set.

@JRubyMethod(name = "root=")
public IRubyObject root_set(ThreadContext context, IRubyObject newRoot_) {
    // should be nil.
    if (newRoot_ instanceof RubyNil) {
        getDocument().getDocumentElement().setUserData(NokogiriHelpers.VALID_ROOT_NODE, false, null);
        return newRoot_;
    }
    XmlNode newRoot = asXmlNode(context, newRoot_);
    IRubyObject root = root(context);
    if (root.isNil()) {
        Node newRootNode;
        if (getDocument() == newRoot.getOwnerDocument()) {
            newRootNode = newRoot.node;
        } else {
            // must copy otherwise newRoot may exist in two places
            // with different owner document.
            newRootNode = getDocument().importNode(newRoot.node, true);
        }
        add_child_node(context, getCachedNodeOrCreate(context.getRuntime(), newRootNode));
    } else {
        Node rootNode = asXmlNode(context, root).node;
        ((XmlNode) getCachedNodeOrCreate(context.getRuntime(), rootNode)).replace_node(context, newRoot);
    }
    return newRoot;
}
Also used : RubyNil(org.jruby.RubyNil) Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

RubyNil (org.jruby.RubyNil)2 JRubyMethod (org.jruby.anno.JRubyMethod)2 IRubyObject (org.jruby.runtime.builtin.IRubyObject)2 Node (org.w3c.dom.Node)2