Search in sources :

Example 76 with JRubyMethod

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

the class XmlNode method line.

@JRubyMethod
public IRubyObject line(ThreadContext context) {
    Node root = getOwnerDocument();
    int[] counter = new int[1];
    count(root, counter);
    return RubyFixnum.newFixnum(context.getRuntime(), counter[0] + 1);
}
Also used : NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 77 with JRubyMethod

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

the class XmlNode method namespace_scopes.

/**
     * Return an array of XmlNamespace nodes defined on this node and
     * on any ancestor node.
     */
@JRubyMethod
public IRubyObject namespace_scopes(ThreadContext context) {
    RubyArray scoped_namespaces = context.getRuntime().newArray();
    if (doc == null)
        return scoped_namespaces;
    if (doc instanceof HtmlDocument)
        return scoped_namespaces;
    Node previousNode;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        previousNode = node;
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        previousNode = ((Attr) node).getOwnerElement();
    } else {
        previousNode = findPreviousElement(node);
    }
    if (previousNode == null)
        return scoped_namespaces;
    List<String> prefixes_in_scope = new ArrayList<String>();
    NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(previousNode);
    for (Node previous = previousNode; previous != null; ) {
        List<XmlNamespace> namespaces = nsCache.get(previous);
        for (XmlNamespace namespace : namespaces) {
            if (prefixes_in_scope.contains(namespace.getPrefix()))
                continue;
            scoped_namespaces.append(namespace);
            prefixes_in_scope.add(namespace.getPrefix());
        }
        previous = findPreviousElement(previous);
    }
    return scoped_namespaces;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) NokogiriNamespaceCache(nokogiri.internals.NokogiriNamespaceCache) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) NokogiriHelpers.convertString(nokogiri.internals.NokogiriHelpers.convertString) Attr(org.w3c.dom.Attr) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 78 with JRubyMethod

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

the class XmlNode method process_xincludes.

/**
     * call-seq:
     *   process_xincludes(options)
     *
     * Loads and substitutes all xinclude elements below the node. The
     * parser context will be initialized with +options+.
     * 
     */
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject process_xincludes(ThreadContext context, IRubyObject options) {
    XmlDocument xmlDocument = (XmlDocument) document(context);
    RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
    while (errors.getLength() > 0) {
        XmlSyntaxError error = (XmlSyntaxError) errors.shift(context);
        if (error.toString().contains("Include operation failed")) {
            throw new RaiseException(error);
        }
    }
    return this;
}
Also used : RubyArray(org.jruby.RubyArray) NokogiriHelpers.nodeArrayToRubyArray(nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray) RaiseException(org.jruby.exceptions.RaiseException) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 79 with JRubyMethod

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

the class XmlNode method set_namespace.

@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject set_namespace(ThreadContext context, IRubyObject namespace) {
    if (namespace.isNil()) {
        if (doc != null) {
            Node n = node;
            String prefix = n.getPrefix();
            String href = n.getNamespaceURI();
            ((XmlDocument) doc).getNamespaceCache().remove(prefix == null ? "" : prefix, href);
            this.node = NokogiriHelpers.renameNode(n, null, NokogiriHelpers.getLocalPart(n.getNodeName()));
        }
    } else {
        XmlNamespace ns = (XmlNamespace) namespace;
        String prefix = rubyStringToString(ns.prefix(context));
        String href = rubyStringToString(ns.href(context));
        // Assigning node = ...renameNode() or not seems to make no
        // difference.  Why not? -pmahoney
        // It actually makes a great deal of difference. renameNode()
        // will operate in place if it can, but sometimes it can't.
        // The node you passed in *might* come back as you expect, but
        // it might not. It's much safer to throw away the original
        // and keep the return value. -mbklein
        String new_name = NokogiriHelpers.newQName(prefix, node);
        this.node = NokogiriHelpers.renameNode(node, href, new_name);
    }
    clearXpathContext(getNode());
    return this;
}
Also used : 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 80 with JRubyMethod

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

the class XmlNode method rbNew.

/**
     * Allocate a new object, perform initialization, call that
     * object's initialize method, and call any block passing the
     * object as the only argument.  If <code>cls</code> is
     * Nokogiri::XML::Node, creates a new Nokogiri::XML::Element
     * instead.
     *
     * This static method seems to be inherited, strangely enough.
     * E.g. creating a new XmlAttr from Ruby code calls this method if
     * XmlAttr does not define its own 'new' method.
     *
     * Since there is some Java bookkeeping that always needs to
     * happen, we don't define the 'initialize' method in Java because
     * we'd have to count on subclasses calling 'super'.
     *
     * The main consequence of this is that every subclass needs to
     * define its own 'new' method.
     *
     * As a convenience, this method does the following:
     *
     * <ul>
     *
     * <li>allocates a new object using the allocator assigned to
     * <code>cls</code></li>
     *
     * <li>calls the Java method init(); subclasses can override this,
     * otherwise they should implement a specific 'new' method</li>
     *
     * <li>invokes the Ruby initializer</li>
     *
     * <li>if a block is given, calls the block with the new node as
     * the argument</li>
     *
     * </ul>
     *
     * -pmahoney
     */
@JRubyMethod(name = "new", meta = true, rest = true)
public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject[] args, Block block) {
    Ruby ruby = context.getRuntime();
    RubyClass klazz = (RubyClass) cls;
    if ("Nokogiri::XML::Node".equals(klazz.getName())) {
        klazz = getNokogiriClass(ruby, "Nokogiri::XML::Element");
    }
    XmlNode xmlNode = (XmlNode) klazz.allocate();
    xmlNode.init(context, args);
    xmlNode.callInit(args, block);
    assert xmlNode.node != null;
    if (block.isGiven())
        block.call(context, xmlNode);
    return xmlNode;
}
Also used : JRubyClass(org.jruby.anno.JRubyClass) RubyClass(org.jruby.RubyClass) 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