Search in sources :

Example 81 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlDocument method create_entity.

@JRubyMethod(required = 1, optional = 4)
public IRubyObject create_entity(ThreadContext context, IRubyObject[] argv) {
    // would cause validation failure.
    if (argv.length == 0)
        throw context.getRuntime().newRuntimeError("Could not create entity");
    String tagName = rubyStringToString(argv[0]);
    Node n = this.getOwnerDocument().createElement(tagName);
    return XmlEntityDecl.create(context, n, argv);
}
Also used : Node(org.w3c.dom.Node) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 82 with JRubyMethod

use of org.jruby.anno.JRubyMethod 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)

Example 83 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlNode method attribute_with_ns.

@JRubyMethod
public IRubyObject attribute_with_ns(ThreadContext context, IRubyObject name, IRubyObject namespace) {
    String namej = rubyStringToString(name);
    String nsj = (namespace.isNil()) ? null : rubyStringToString(namespace);
    Node el = this.node.getAttributes().getNamedItemNS(nsj, namej);
    if (el == null) {
        return context.getRuntime().getNil();
    }
    return NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), el);
}
Also used : NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 84 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlNode method get.

/**
     * Get the attribute at the given key, <code>key</code>.
     * Assumes that this node has attributes (i.e. that key? returned
     * true).
     */
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject get(ThreadContext context, IRubyObject rbkey) {
    if (node instanceof Element) {
        if (rbkey == null || rbkey.isNil())
            context.getRuntime().getNil();
        String key = rubyStringToString(rbkey);
        Element element = (Element) node;
        if (!element.hasAttribute(key))
            return context.getRuntime().getNil();
        String value = element.getAttribute(key);
        return stringOrNil(context.getRuntime(), value);
    }
    return context.getRuntime().getNil();
}
Also used : Element(org.w3c.dom.Element) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 85 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.

the class XmlNode method attribute_nodes.

@JRubyMethod
public IRubyObject attribute_nodes(ThreadContext context) {
    NamedNodeMap nodeMap = this.node.getAttributes();
    Ruby ruby = context.getRuntime();
    if (nodeMap == null) {
        return ruby.newEmptyArray();
    }
    RubyArray attr = ruby.newArray();
    for (int i = 0; i < nodeMap.getLength(); i++) {
        if ((doc instanceof HtmlDocument) || !NokogiriHelpers.isNamespace(nodeMap.item(i))) {
            attr.append(getCachedNodeOrCreate(context.getRuntime(), nodeMap.item(i)));
        }
    }
    return attr;
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)103 IRubyObject (org.jruby.runtime.builtin.IRubyObject)32 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)31 Ruby (org.jruby.Ruby)31 Node (org.w3c.dom.Node)29 RubyString (org.jruby.RubyString)28 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)23 Document (org.w3c.dom.Document)19 RubyArray (org.jruby.RubyArray)17 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)12 Element (org.w3c.dom.Element)12 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)10 JRubyClass (org.jruby.anno.JRubyClass)10 RaiseException (org.jruby.exceptions.RaiseException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 RubyClass (org.jruby.RubyClass)7 InputStream (java.io.InputStream)6 NokogiriNamespaceCache (nokogiri.internals.NokogiriNamespaceCache)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4