Search in sources :

Example 16 with IRubyObject

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

the class XmlNode method create_external_subset.

@JRubyMethod
public IRubyObject create_external_subset(ThreadContext context, IRubyObject name, IRubyObject external_id, IRubyObject system_id) {
    IRubyObject subset = external_subset(context);
    if (!subset.isNil()) {
        throw context.getRuntime().newRuntimeError("Document already has external subset");
    }
    Document document = getOwnerDocument();
    if (document == null) {
        return context.getRuntime().getNil();
    }
    XmlDocument xdoc = (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
    IRubyObject xdtd = xdoc.createExternalSubset(context, name, external_id, system_id);
    return xdtd;
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject) Document(org.w3c.dom.Document) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 17 with IRubyObject

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

the class XmlNode method external_subset.

@JRubyMethod
public IRubyObject external_subset(ThreadContext context) {
    Document document = getOwnerDocument();
    if (document == null) {
        return context.getRuntime().getNil();
    }
    XmlDocument xdoc = (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
    IRubyObject xdtd = xdoc.getExternalSubset(context);
    return xdtd;
}
Also used : Document(org.w3c.dom.Document) IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 18 with IRubyObject

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

the class XmlNode method lang.

@JRubyMethod
public IRubyObject lang(ThreadContext context) {
    IRubyObject currentObj = this;
    while (!currentObj.isNil()) {
        XmlNode currentNode = asXmlNode(context, currentObj);
        IRubyObject lang = currentNode.getAttribute(context.getRuntime(), "xml:lang");
        if (!lang.isNil()) {
            return lang;
        }
        currentObj = currentNode.parent(context);
    }
    return context.nil;
}
Also used : IRubyObject(org.jruby.runtime.builtin.IRubyObject) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 19 with IRubyObject

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

the class XmlNamespace method createFromAttr.

public static XmlNamespace createFromAttr(Ruby runtime, Attr attr) {
    String prefixValue = getLocalNameForNamespace(attr.getName());
    IRubyObject prefix_value;
    if (prefixValue == null) {
        prefix_value = runtime.getNil();
        prefixValue = "";
    } else {
        prefix_value = RubyString.newString(runtime, prefixValue);
    }
    String hrefValue = attr.getValue();
    IRubyObject href_value = RubyString.newString(runtime, hrefValue);
    // check namespace cache
    XmlDocument xmlDocument = (XmlDocument) getCachedNodeOrCreate(runtime, attr.getOwnerDocument());
    xmlDocument.initializeNamespaceCacheIfNecessary();
    XmlNamespace xmlNamespace = xmlDocument.getNamespaceCache().get(prefixValue, hrefValue);
    if (xmlNamespace != null)
        return xmlNamespace;
    // creating XmlNamespace instance
    XmlNamespace namespace = (XmlNamespace) NokogiriService.XML_NAMESPACE_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
    namespace.init(attr, prefix_value, href_value, prefixValue, hrefValue, xmlDocument);
    // updateing namespace cache
    xmlDocument.getNamespaceCache().put(namespace, attr.getOwnerElement());
    return namespace;
}
Also used : RubyString(org.jruby.RubyString) IRubyObject(org.jruby.runtime.builtin.IRubyObject)

Example 20 with IRubyObject

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

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);
    }
    if (this instanceof XmlDocument) {
        ((XmlDocument) this).resetNamespaceCache(context);
    }
    other.relink_namespace(context);
    return nodeOrTags;
}
Also used : NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) 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