Search in sources :

Example 16 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlNode method previous_element.

@JRubyMethod
public IRubyObject previous_element(ThreadContext context) {
    Node prevNode = node.getPreviousSibling();
    Ruby ruby = context.getRuntime();
    if (prevNode == null)
        return ruby.getNil();
    if (prevNode instanceof Element) {
        return getCachedNodeOrCreate(context.getRuntime(), prevNode);
    }
    Node shallower = prevNode.getPreviousSibling();
    if (shallower == null)
        return ruby.getNil();
    return getCachedNodeOrCreate(context.getRuntime(), shallower);
}
Also used : Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Element(org.w3c.dom.Element) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 17 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlReader method from_io.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
    // Only to pass the  source test.
    Ruby runtime = context.getRuntime();
    // Not nil allowed!
    if (args[0].isNil())
        throw runtime.newArgumentError("io cannot be nil");
    XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
    reader.init(runtime);
    reader.setInstanceVariable("@source", args[0]);
    reader.setInstanceVariable("@errors", runtime.newArray());
    IRubyObject url = context.nil;
    if (args.length > 1)
        url = args[1];
    if (args.length > 2)
        reader.setInstanceVariable("@encoding", args[2]);
    Options options;
    if (args.length > 3) {
        options = new ParserContext.Options((Long) args[3].toJava(Long.class));
    } else {
        // use the default options RECOVER | NONET
        options = new ParserContext.Options(2048 | 1);
    }
    InputStream in = new UncloseableInputStream(new IOInputStream(args[0]));
    reader.setInput(context, in, url, options);
    return reader;
}
Also used : Options(nokogiri.internals.ParserContext.Options) Options(nokogiri.internals.ParserContext.Options) UncloseableInputStream(nokogiri.internals.UncloseableInputStream) IOInputStream(org.jruby.util.IOInputStream) InputStream(java.io.InputStream) UncloseableInputStream(nokogiri.internals.UncloseableInputStream) IRubyObject(org.jruby.runtime.builtin.IRubyObject) ParserContext(nokogiri.internals.ParserContext) Ruby(org.jruby.Ruby) IOInputStream(org.jruby.util.IOInputStream) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 18 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlNode method node_name_set.

@JRubyMethod(name = { "node_name=", "name=" })
public IRubyObject node_name_set(ThreadContext context, IRubyObject nodeName) {
    String newName = rubyStringToString(nodeName);
    this.node = NokogiriHelpers.renameNode(node, null, newName);
    setName(nodeName);
    return this;
}
Also used : NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 19 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlNode method set_namespace.

@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject set_namespace(ThreadContext context, IRubyObject namespace) {
    if (namespace.isNil()) {
        if (doc != null) {
            Node n = node;
            String prefix = n.getPrefix();
            String href = n.getNamespaceURI();
            ((XmlDocument) doc).getNamespaceCache().remove(prefix == null ? "" : prefix, href);
            this.node = NokogiriHelpers.renameNode(n, null, NokogiriHelpers.getLocalPart(n.getNodeName()));
        }
    } else {
        XmlNamespace ns = (XmlNamespace) namespace;
        String prefix = rubyStringToString(ns.prefix(context));
        String href = rubyStringToString(ns.href(context));
        // Assigning node = ...renameNode() or not seems to make no
        // difference.  Why not? -pmahoney
        // It actually makes a great deal of difference. renameNode()
        // will operate in place if it can, but sometimes it can't.
        // The node you passed in *might* come back as you expect, but
        // it might not. It's much safer to throw away the original
        // and keep the return value. -mbklein
        String new_name = NokogiriHelpers.newQName(prefix, node);
        this.node = NokogiriHelpers.renameNode(node, href, new_name);
    }
    clearXpathContext(getNode());
    return this;
}
Also used : Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 20 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project gocd by gocd.

the class XmlNode method lang.

@JRubyMethod
public IRubyObject lang(ThreadContext context) {
    IRubyObject currentObj = this;
    while (!currentObj.isNil()) {
        XmlNode currentNode = asXmlNode(context, currentObj);
        IRubyObject lang = currentNode.getAttribute(context.getRuntime(), "xml:lang");
        if (!lang.isNil()) {
            return lang;
        }
        currentObj = currentNode.parent(context);
    }
    return context.nil;
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)304 Ruby (org.jruby.Ruby)157 RubyString (org.jruby.RubyString)83 IRubyObject (org.jruby.runtime.builtin.IRubyObject)77 IOException (java.io.IOException)49 RubyArray (org.jruby.RubyArray)34 Node (org.w3c.dom.Node)30 RaiseException (org.jruby.exceptions.RaiseException)27 BigInteger (java.math.BigInteger)24 GeneralSecurityException (java.security.GeneralSecurityException)24 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)24 Document (org.w3c.dom.Document)20 ByteList (org.jruby.util.ByteList)19 RubyClass (org.jruby.RubyClass)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 RubyFloat (org.jruby.RubyFloat)13 JRubyClass (org.jruby.anno.JRubyClass)13 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)12 RubyFixnum (org.jruby.RubyFixnum)12 Element (org.w3c.dom.Element)12