Search in sources :

Example 56 with JRubyMethod

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

the class XmlNode method namespace.

@JRubyMethod
public IRubyObject namespace(ThreadContext context) {
    if (doc instanceof HtmlDocument)
        return context.getRuntime().getNil();
    NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(node);
    String prefix = node.getPrefix();
    XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, node.getNamespaceURI());
    if (namespace == null || namespace.isEmpty()) {
        return context.getRuntime().getNil();
    }
    return namespace;
}
Also used : NokogiriNamespaceCache(nokogiri.internals.NokogiriNamespaceCache) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 57 with JRubyMethod

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

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 58 with JRubyMethod

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

the class XmlNode method content.

@JRubyMethod(name = { "content", "text", "inner_text" })
public IRubyObject content(ThreadContext context) {
    if (!node.hasChildNodes() && node.getNodeValue() == null && (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE))
        return context.nil;
    String textContent;
    if (this instanceof XmlDocument) {
        Node node = ((Document) this.node).getDocumentElement();
        if (node == null) {
            textContent = "";
        } else {
            Node documentElement = ((Document) this.node).getDocumentElement();
            StringBuffer buffer = new StringBuffer();
            getTextContentRecursively(context, buffer, documentElement);
            textContent = buffer.toString();
        }
    } else {
        StringBuffer buffer = new StringBuffer();
        getTextContentRecursively(context, buffer, node);
        textContent = buffer.toString();
    }
    NokogiriHelpers.convertEncodingByNKFIfNecessary(context.getRuntime(), (XmlDocument) document(context), textContent);
    return stringOrNil(context.getRuntime(), textContent);
}
Also used : Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) Document(org.w3c.dom.Document) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 59 with JRubyMethod

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

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 60 with JRubyMethod

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

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)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)103 IRubyObject (org.jruby.runtime.builtin.IRubyObject)32 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)31 Ruby (org.jruby.Ruby)31 Node (org.w3c.dom.Node)29 RubyString (org.jruby.RubyString)28 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)23 Document (org.w3c.dom.Document)19 RubyArray (org.jruby.RubyArray)17 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)12 Element (org.w3c.dom.Element)12 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)10 JRubyClass (org.jruby.anno.JRubyClass)10 RaiseException (org.jruby.exceptions.RaiseException)10 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 RubyClass (org.jruby.RubyClass)7 InputStream (java.io.InputStream)6 NokogiriNamespaceCache (nokogiri.internals.NokogiriNamespaceCache)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4