Search in sources :

Example 21 with JRubyMethod

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

the class XmlNode method next_element.

@JRubyMethod
public IRubyObject next_element(ThreadContext context) {
    Node nextNode = node.getNextSibling();
    Ruby ruby = context.getRuntime();
    if (nextNode == null)
        return ruby.getNil();
    if (nextNode instanceof Element) {
        return getCachedNodeOrCreate(context.getRuntime(), nextNode);
    }
    Node deeper = nextNode.getNextSibling();
    if (deeper == null)
        return ruby.getNil();
    return getCachedNodeOrCreate(context.getRuntime(), deeper);
}
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 22 with JRubyMethod

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

the class HtmlDocument method rbNew.

@JRubyMethod(name = "new", meta = true, rest = true, required = 0)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
    HtmlDocument htmlDocument = null;
    try {
        Document docNode = createNewDocument();
        htmlDocument = (HtmlDocument) NokogiriService.HTML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
        htmlDocument.setDocumentNode(context, docNode);
    } catch (Exception ex) {
        throw context.getRuntime().newRuntimeError("couldn't create document: " + ex.toString());
    }
    RuntimeHelpers.invoke(context, htmlDocument, "initialize", args);
    return htmlDocument;
}
Also used : Document(org.w3c.dom.Document) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 23 with JRubyMethod

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

the class HtmlSaxParserContext method parse_memory.

@JRubyMethod(name = "memory", meta = true)
public static IRubyObject parse_memory(ThreadContext context, IRubyObject klazz, IRubyObject data, IRubyObject encoding) {
    HtmlSaxParserContext ctx = (HtmlSaxParserContext) NokogiriService.HTML_SAXPARSER_CONTEXT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
    ctx.initialize(context.getRuntime());
    String javaEncoding = findEncoding(context, encoding);
    if (javaEncoding != null) {
        String input = applyEncoding(rubyStringToString(data), javaEncoding);
        ByteArrayInputStream istream = new ByteArrayInputStream(input.getBytes());
        ctx.setInputSource(istream);
        ctx.getInputSource().setEncoding(javaEncoding);
    }
    return ctx;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) RubyString(org.jruby.RubyString) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 24 with JRubyMethod

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

the class HtmlSaxParserContext method parse_file.

@JRubyMethod(name = "file", meta = true)
public static IRubyObject parse_file(ThreadContext context, IRubyObject klazz, IRubyObject data, IRubyObject encoding) {
    HtmlSaxParserContext ctx = (HtmlSaxParserContext) NokogiriService.HTML_SAXPARSER_CONTEXT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
    ctx.initialize(context.getRuntime());
    ctx.setInputSourceFile(context, data);
    String javaEncoding = findEncoding(context, encoding);
    if (javaEncoding != null) {
        ctx.getInputSource().setEncoding(javaEncoding);
    }
    return ctx;
}
Also used : JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) RubyString(org.jruby.RubyString) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 25 with JRubyMethod

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

the class HtmlSaxPushParser method native_write.

@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
    try {
        initialize_task(context);
    } catch (IOException e) {
        throw context.getRuntime().newRuntimeError(e.getMessage());
    }
    byte[] data = null;
    if (chunk instanceof RubyString || chunk.respondsTo("to_str")) {
        data = chunk.convertToString().getBytes();
    } else {
        terminateTask(context);
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::HTML::SyntaxError"));
        throw new RaiseException(xmlSyntaxError);
    }
    int errorCount0 = parserTask.getErrorCount();
    ;
    if (isLast.isTrue()) {
        IRubyObject document = invoke(context, this, "document");
        invoke(context, document, "end_document");
        terminateTask(context);
    } else {
        try {
            Future<Void> task = stream.addChunk(new ByteArrayInputStream(data));
            task.get();
        } catch (ClosedStreamException ex) {
        // this means the stream is closed, ignore this exception
        } catch (Exception e) {
            throw context.getRuntime().newRuntimeError(e.getMessage());
        }
    }
    if (!options.recover && parserTask.getErrorCount() > errorCount0) {
        terminateTask(context);
        throw new RaiseException(parserTask.getLastError(), true);
    }
    return this;
}
Also used : ClosedStreamException(nokogiri.internals.ClosedStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) RubyString(org.jruby.RubyString) RaiseException(org.jruby.exceptions.RaiseException) IOException(java.io.IOException) IRubyObject(org.jruby.runtime.builtin.IRubyObject) RaiseException(org.jruby.exceptions.RaiseException) IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) RubyException(org.jruby.RubyException) IOException(java.io.IOException) ClosedStreamException(nokogiri.internals.ClosedStreamException) 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