Search in sources :

Example 81 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlNodeSet method setNodes.

void setNodes(RubyArray array) {
    this.nodes = array;
    IRubyObject first = array.first();
    initialize(array.getRuntime(), first);
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 82 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlProcessingInstruction method rbNew.

@JRubyMethod(name = "new", meta = true, rest = true, required = 3)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    IRubyObject doc = args[0];
    IRubyObject target = args[1];
    IRubyObject data = args[2];
    Document document = ((XmlNode) doc).getOwnerDocument();
    Node node = document.createProcessingInstruction(rubyStringToString(target), rubyStringToString(data));
    XmlProcessingInstruction self = new XmlProcessingInstruction(context.getRuntime(), (RubyClass) klazz, node);
    RuntimeHelpers.invoke(context, self, "initialize", args);
    return self;
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 83 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlReader method from_memory.

@JRubyMethod(meta = true, rest = true)
public static IRubyObject from_memory(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
    // args[0]: string, args[1]: url, args[2]: encoding, args[3]: options
    Ruby runtime = context.getRuntime();
    // Not nil allowed!
    if (args[0].isNil())
        throw runtime.newArgumentError("string 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);
    }
    IRubyObject stringIO = runtime.getClass("StringIO").newInstance(context, args[0], Block.NULL_BLOCK);
    InputStream in = new UncloseableInputStream(new IOInputStream(stringIO));
    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 84 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlAttr method init.

@Override
protected void init(ThreadContext context, IRubyObject[] args) {
    if (args.length < 2) {
        throw getRuntime().newArgumentError(args.length, 2);
    }
    IRubyObject doc = args[0];
    IRubyObject content = args[1];
    if (!(doc instanceof XmlDocument)) {
        final String msg = "document must be an instance of Nokogiri::XML::Document";
        throw getRuntime().newArgumentError(msg);
    }
    XmlDocument xmlDoc = (XmlDocument) doc;
    String str = rubyStringToString(content);
    Node attr = xmlDoc.getDocument().createAttribute(str);
    setNode(context, attr);
}
Also used : Node(org.w3c.dom.Node) RubyString(org.jruby.RubyString) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 85 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.

the class XmlCdata method init.

@Override
protected void init(ThreadContext context, IRubyObject[] args) {
    if (args.length < 2) {
        throw getRuntime().newArgumentError(args.length, 2);
    }
    IRubyObject doc = args[0];
    content = args[1];
    XmlDocument xmlDoc = (XmlDocument) ((XmlNode) doc).document(context);
    Document document = xmlDoc.getDocument();
    Node node = document.createCDATASection((content.isNil()) ? null : rubyStringToString(content));
    setNode(context, node);
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Aggregations

IRubyObject (org.jruby.runtime.builtin.IRubyObject)105 JRubyMethod (org.jruby.anno.JRubyMethod)32 Document (org.w3c.dom.Document)27 Ruby (org.jruby.Ruby)25 Node (org.w3c.dom.Node)24 RubyString (org.jruby.RubyString)16 RubyArray (org.jruby.RubyArray)13 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)11 RaiseException (org.jruby.exceptions.RaiseException)10 InputStream (java.io.InputStream)6 ClojureTest (org.enumerable.lambda.support.clojure.ClojureTest)6 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)6 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)6 LambdaJRuby (org.enumerable.lambda.support.jruby.LambdaJRuby)6 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)6 RubyProc (org.jruby.RubyProc)6 Test (org.junit.Test)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)4 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)4