Search in sources :

Example 86 with JRubyMethod

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

the class XmlNode method namespace.

@JRubyMethod
public IRubyObject namespace(ThreadContext context) {
    Ruby runtime = context.getRuntime();
    if (doc instanceof HtmlDocument)
        return runtime.getNil();
    NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(node);
    String namespaceURI = node.getNamespaceURI();
    if (namespaceURI == null || namespaceURI == "") {
        return runtime.getNil();
    }
    String prefix = node.getPrefix();
    XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, namespaceURI);
    if (namespace == null || namespace.isEmpty()) {
        // if it's not in the cache, create an unowned, uncached namespace and
        // return that. XmlReader can't insert namespaces into the cache, so
        // this is necessary for XmlReader to work correctly.
        namespace = new XmlNamespace(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
        IRubyObject rubyPrefix = NokogiriHelpers.stringOrNil(runtime, prefix);
        IRubyObject rubyUri = NokogiriHelpers.stringOrNil(runtime, namespaceURI);
        namespace.init(null, rubyPrefix, rubyUri, doc);
    }
    return namespace;
}
Also used : NokogiriNamespaceCache(nokogiri.internals.NokogiriNamespaceCache) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 87 with JRubyMethod

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

the class XmlNode method key_p.

/**
     * Test if this node has an attribute named <code>rbkey</code>.
     * Overridden in XmlElement.
     */
@JRubyMethod(name = { "key?", "has_attribute?" })
public IRubyObject key_p(ThreadContext context, IRubyObject rbkey) {
    if (node instanceof Element) {
        String key = rubyStringToString(rbkey);
        Element element = (Element) node;
        if (element.hasAttribute(key)) {
            return context.getRuntime().getTrue();
        } else {
            NamedNodeMap namedNodeMap = element.getAttributes();
            for (int i = 0; i < namedNodeMap.getLength(); i++) {
                Node n = namedNodeMap.item(i);
                if (key.equals(n.getLocalName())) {
                    return context.getRuntime().getTrue();
                }
            }
        }
        return context.getRuntime().getFalse();
    } else {
        return context.getRuntime().getNil();
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) 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) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 88 with JRubyMethod

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

the class XmlNode method unlink.

@JRubyMethod(name = { "unlink", "remove" })
public IRubyObject unlink(ThreadContext context) {
    final Node parent = node.getParentNode();
    if (parent != null) {
        parent.removeChild(node);
        clearXpathContext(parent);
    }
    return this;
}
Also used : NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 89 with JRubyMethod

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

the class XmlNode method native_write_to.

/**
     * @param args {IRubyObject io,
     *              IRubyObject encoding,
     *              IRubyObject indentString,
     *              IRubyObject options}
     */
@JRubyMethod(required = 4, visibility = Visibility.PRIVATE)
public IRubyObject native_write_to(ThreadContext context, IRubyObject[] args) {
    IRubyObject io = args[0];
    IRubyObject encoding = args[1];
    IRubyObject indentString = args[2];
    IRubyObject options = args[3];
    String encString = encoding.isNil() ? null : rubyStringToString(encoding);
    SaveContextVisitor visitor = new SaveContextVisitor(RubyFixnum.fix2int(options), rubyStringToString(indentString), encString, isHtmlDoc(context), isFragment(), 0);
    accept(context, visitor);
    final IRubyObject rubyString;
    if (NokogiriHelpers.isUTF8(encString)) {
        rubyString = convertString(context.getRuntime(), visitor.getInternalBuffer());
    } else {
        ByteBuffer bytes = convertEncoding(Charset.forName(encString), visitor.getInternalBuffer());
        ByteList str = new ByteList(bytes.array(), bytes.arrayOffset(), bytes.remaining());
        rubyString = RubyString.newString(context.getRuntime(), str);
    }
    RuntimeHelpers.invoke(context, io, "write", rubyString);
    return io;
}
Also used : ByteList(org.jruby.util.ByteList) SaveContextVisitor(nokogiri.internals.SaveContextVisitor) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) IRubyObject(org.jruby.runtime.builtin.IRubyObject) ByteBuffer(java.nio.ByteBuffer) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 90 with JRubyMethod

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

the class XmlNode method previous_element.

@JRubyMethod
public IRubyObject previous_element(ThreadContext context) {
    Node prevNode = node.getPreviousSibling();
    Ruby ruby = context.getRuntime();
    if (prevNode == null)
        return ruby.getNil();
    if (prevNode instanceof Element) {
        return getCachedNodeOrCreate(context.getRuntime(), prevNode);
    }
    Node shallower = prevNode.getPreviousSibling();
    if (shallower == null)
        return ruby.getNil();
    return getCachedNodeOrCreate(context.getRuntime(), shallower);
}
Also used : NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Ruby(org.jruby.Ruby) 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