Search in sources :

Example 96 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 97 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 98 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 99 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)

Example 100 with JRubyMethod

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

the class XmlNode method create_internal_subset.

@JRubyMethod
public IRubyObject create_internal_subset(ThreadContext context, IRubyObject name, IRubyObject external_id, IRubyObject system_id) {
    IRubyObject subset = internal_subset(context);
    if (!subset.isNil()) {
        throw context.getRuntime().newRuntimeError("Document already has internal subset");
    }
    Document document = getOwnerDocument();
    if (document == null) {
        return context.getRuntime().getNil();
    }
    XmlDocument xdoc = (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
    IRubyObject xdtd = xdoc.createInternalSubset(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)

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