Search in sources :

Example 41 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject 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 42 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject 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 43 with IRubyObject

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

the class XmlComment 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 text = args[1];
    XmlDocument xmlDoc;
    if (doc instanceof XmlDocument) {
        xmlDoc = (XmlDocument) doc;
    } else if (doc instanceof XmlNode) {
        XmlNode xmlNode = (XmlNode) doc;
        xmlDoc = (XmlDocument) xmlNode.document(context);
    } else {
        throw getRuntime().newArgumentError("first argument must be a XML::Document or XML::Node");
    }
    if (xmlDoc != null) {
        Document document = xmlDoc.getDocument();
        Node node = document.createComment(rubyStringToString(text));
        setNode(context, node);
    }
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document)

Example 44 with IRubyObject

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

the class HtmlDocument method getInternalSubset.

public IRubyObject getInternalSubset(ThreadContext context) {
    IRubyObject internalSubset = super.getInternalSubset(context);
    if (internalSubset.isNil()) {
        internalSubset = XmlDtd.newEmpty(context.getRuntime(), getDocument(), context.getRuntime().newString(DEFAULT_CONTENT_TYPE), context.getRuntime().newString(DEFAULT_PUBLIC_ID), context.getRuntime().newString(DEFAULT_SYTEM_ID));
        setInternalSubset(internalSubset);
    }
    return internalSubset;
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 45 with IRubyObject

use of org.jruby.runtime.builtin.IRubyObject in project gocd by gocd.

the class XmlNode method adoptAs.

/**
 * Adopt XmlNode <code>other</code> into the document of
 * <code>this</code> using the specified scheme.
 */
protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme, IRubyObject other_) {
    XmlNode other = asXmlNode(context, other_);
    // this.doc might be null since this node can be empty node.
    if (this.doc != null) {
        other.setDocument(context, this.doc);
    }
    IRubyObject nodeOrTags = other;
    Node thisNode = node;
    Node otherNode = other.node;
    try {
        Document prev = otherNode.getOwnerDocument();
        Document doc = thisNode.getOwnerDocument();
        clearXpathContext(prev);
        clearXpathContext(doc);
        if (doc != null && doc != otherNode.getOwnerDocument()) {
            Node ret = doc.adoptNode(otherNode);
            // FIXME: this is really a hack, see documentation of fixUserData() for more details.
            fixUserData(prev, ret);
            if (ret == null) {
                throw context.getRuntime().newRuntimeError("Failed to take ownership of node");
            }
            otherNode = ret;
        }
        Node parent = thisNode.getParentNode();
        switch(scheme) {
            case CHILD:
                Node[] children = adoptAsChild(context, thisNode, otherNode);
                if (children.length == 1 && otherNode == children[0]) {
                    break;
                } else {
                    nodeOrTags = nodeArrayToRubyArray(context.getRuntime(), children);
                }
                break;
            case PREV_SIBLING:
                adoptAsPrevSibling(context, parent, thisNode, otherNode);
                break;
            case NEXT_SIBLING:
                adoptAsNextSibling(context, parent, thisNode, otherNode);
                break;
            case REPLACEMENT:
                adoptAsReplacement(context, parent, thisNode, otherNode);
                break;
        }
    } catch (Exception e) {
        throw context.getRuntime().newRuntimeError(e.toString());
    }
    if (otherNode.getNodeType() == Node.TEXT_NODE) {
        coalesceTextNodes(context, other, scheme);
    }
    relink_namespace(context);
    return nodeOrTags;
}
Also used : Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document) RaiseException(org.jruby.exceptions.RaiseException) CharacterCodingException(java.nio.charset.CharacterCodingException)

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