Search in sources :

Example 86 with IRubyObject

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

the class XmlNode method in_context.

/**
     * TODO: this is a stub implementation.  It's not clear what
     * 'in_context' is supposed to do.  Also should take
     * <code>options</code> into account.
     */
@JRubyMethod(required = 2, visibility = Visibility.PRIVATE)
public IRubyObject in_context(ThreadContext context, IRubyObject str, IRubyObject options) {
    RubyModule klass;
    XmlDomParserContext ctx;
    InputStream istream;
    XmlDocument document;
    IRubyObject d = document(context);
    Ruby runtime = context.getRuntime();
    if (d != null && d instanceof XmlDocument) {
        document = (XmlDocument) d;
    } else {
        return runtime.getNil();
    }
    if (document instanceof HtmlDocument) {
        klass = getNokogiriClass(runtime, "Nokogiri::HTML::Document");
        ctx = new HtmlDomParserContext(runtime, options);
        ((HtmlDomParserContext) ctx).enableDocumentFragment();
        istream = new ByteArrayInputStream((rubyStringToString(str)).getBytes());
    } else {
        klass = getNokogiriClass(runtime, "Nokogiri::XML::Document");
        ctx = new XmlDomParserContext(runtime, options);
        String input = rubyStringToString(str);
        istream = new ByteArrayInputStream(input.getBytes());
    }
    ctx.setInputSource(istream);
    // run `test_parse_with_unparented_html_text_context_node' few times to see this happen
    if (document instanceof HtmlDocument && !(document.getEncoding() == null || document.getEncoding().isNil())) {
        HtmlDomParserContext htmlCtx = (HtmlDomParserContext) ctx;
        htmlCtx.setEncoding(document.getEncoding().asJavaString());
    }
    XmlDocument doc = ctx.parse(context, klass, runtime.getNil());
    RubyArray documentErrors = getErrorArray(document);
    RubyArray docErrors = getErrorArray(doc);
    if (isErrorIncreased(documentErrors, docErrors)) {
        for (int i = 0; i < docErrors.getLength(); i++) {
            documentErrors.add(docErrors.get(i));
        }
        document.setInstanceVariable("@errors", documentErrors);
        XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, RubyArray.newArray(runtime));
        return xmlNodeSet;
    }
    // The first child might be document type node (dtd declaration).
    // XmlNodeSet to be return should not have dtd decl in its list.
    Node first;
    if (doc.node.getFirstChild().getNodeType() == Node.DOCUMENT_TYPE_NODE) {
        first = doc.node.getFirstChild().getNextSibling();
    } else {
        first = doc.node.getFirstChild();
    }
    RubyArray nodeArray = RubyArray.newArray(runtime);
    nodeArray.add(NokogiriHelpers.getCachedNodeOrCreate(runtime, first));
    XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, nodeArray);
    return xmlNodeSet;
}
Also used : RubyModule(org.jruby.RubyModule) HtmlDomParserContext(nokogiri.internals.HtmlDomParserContext) RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) XmlDomParserContext(nokogiri.internals.XmlDomParserContext) ByteArrayInputStream(java.io.ByteArrayInputStream) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 87 with IRubyObject

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

the class XmlNodeSet method setReference.

private void setReference(XmlNodeSet reference) {
    this.nodes = null;
    IRubyObject first = reference.nodes.first();
    initialize(reference.getRuntime(), first);
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 88 with IRubyObject

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

the class XmlDocument method root_set.

@JRubyMethod(name = "root=")
public IRubyObject root_set(ThreadContext context, IRubyObject newRoot_) {
    // should be nil.
    if (newRoot_ instanceof RubyNil) {
        getDocument().getDocumentElement().setUserData(NokogiriHelpers.VALID_ROOT_NODE, false, null);
        return newRoot_;
    }
    XmlNode newRoot = asXmlNode(context, newRoot_);
    IRubyObject root = root(context);
    if (root.isNil()) {
        Node newRootNode;
        if (getDocument() == newRoot.getOwnerDocument()) {
            newRootNode = newRoot.node;
        } else {
            // must copy otherwise newRoot may exist in two places
            // with different owner document.
            newRootNode = getDocument().importNode(newRoot.node, true);
        }
        add_child_node(context, getCachedNodeOrCreate(context.getRuntime(), newRootNode));
    } else {
        Node rootNode = asXmlNode(context, root).node;
        ((XmlNode) getCachedNodeOrCreate(context.getRuntime(), rootNode)).replace_node(context, newRoot);
    }
    return newRoot;
}
Also used : RubyNil(org.jruby.RubyNil) Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 89 with IRubyObject

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

the class XmlDtd method newFromExternalSubset.

public static IRubyObject newFromExternalSubset(Ruby runtime, Document doc) {
    Object dtdTree_ = doc.getUserData(XmlDocument.DTD_RAW_DOCUMENT);
    if (dtdTree_ == null) {
        return runtime.getNil();
    }
    Node dtdTree = (Node) dtdTree_;
    Node dtd = getExternalSubset(dtdTree);
    if (dtd == null) {
        return runtime.getNil();
    } else if (!dtd.hasChildNodes()) {
        return runtime.getNil();
    } else {
        // Import the node into doc so it has the correct owner document.
        dtd = doc.importNode(dtd, true);
        XmlDtd xmlDtd = (XmlDtd) NokogiriService.XML_DTD_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::DTD"));
        xmlDtd.setNode(runtime, dtd);
        return xmlDtd;
    }
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 90 with IRubyObject

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

the class XmlEntityReference method accept.

@Override
public void accept(ThreadContext context, SaveContextVisitor visitor) {
    visitor.enter(node);
    Node child = node.getFirstChild();
    while (child != null) {
        IRubyObject nokoNode = getCachedNodeOrCreate(context.getRuntime(), child);
        if (nokoNode instanceof XmlNode) {
            XmlNode cur = (XmlNode) nokoNode;
            cur.accept(context, visitor);
        } else if (nokoNode instanceof XmlNamespace) {
            XmlNamespace cur = (XmlNamespace) nokoNode;
            cur.accept(context, visitor);
        }
        child = child.getNextSibling();
    }
    visitor.leave(node);
}
Also used : Node(org.w3c.dom.Node) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

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