Search in sources :

Example 1 with JRubyMethod

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

the class XmlDocument method wrapJavaDocument.

@JRubyMethod(meta = true)
public static IRubyObject wrapJavaDocument(ThreadContext context, IRubyObject klazz, IRubyObject arg) {
    XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::Document"));
    RuntimeHelpers.invoke(context, xmlDocument, "initialize");
    Document document = (Document) arg.toJava(Document.class);
    xmlDocument.setDocumentNode(context, document);
    return xmlDocument;
}
Also used : Document(org.w3c.dom.Document) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 2 with JRubyMethod

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

the class HtmlElementDescription method sub_elements.

@JRubyMethod()
public IRubyObject sub_elements(ThreadContext context) {
    Ruby ruby = context.getRuntime();
    List<String> subs = findSubElements(element);
    IRubyObject[] ary = new IRubyObject[subs.size()];
    for (int i = 0; i < subs.size(); ++i) {
        ary[i] = ruby.newString(subs.get(i));
    }
    return ruby.newArray(ary);
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 3 with JRubyMethod

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

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) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 4 with JRubyMethod

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

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());
    }
    final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
    if (data == null) {
        terminateTask(context);
        // Nokogiri::HTML::SyntaxError
        throw new RaiseException(XmlSyntaxError.createHTMLSyntaxError(context.runtime));
    }
    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(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) 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)

Example 5 with JRubyMethod

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

the class XmlNode method element_children.

@JRubyMethod(name = { "element_children", "elements" })
public IRubyObject element_children(ThreadContext context) {
    List<Node> elementNodes = new ArrayList<Node>();
    addElements(node, elementNodes, false);
    if (elementNodes.size() == 0)
        return XmlNodeSet.newEmptyNodeSet(context);
    RubyArray array = NokogiriHelpers.nodeArrayToRubyArray(context.getRuntime(), elementNodes.toArray(new Node[0]));
    XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, array);
    return xmlNodeSet;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) 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