Search in sources :

Example 6 with NokogiriNamespaceCache

use of nokogiri.internals.NokogiriNamespaceCache in project gocd by gocd.

the class XmlNode method add_namespace_definition.

/**
 * Add a namespace definition to this node.  To the underlying
 * node, add an attribute of the form
 * <code>xmlns:prefix="uri"</code>.
 */
@JRubyMethod(name = { "add_namespace_definition", "add_namespace" })
public IRubyObject add_namespace_definition(ThreadContext context, IRubyObject prefix, IRubyObject href) {
    String prefixString = rubyStringToString(prefix);
    String hrefString;
    // try to search the namespace first
    if (href.isNil()) {
        hrefString = this.findNamespaceHref(context, rubyStringToString(prefix));
        if (hrefString == null) {
            return context.nil;
        }
        href = context.getRuntime().newString(hrefString);
    } else {
        hrefString = rubyStringToString(href);
    }
    NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(node);
    XmlNamespace cachedNamespace = nsCache.get(prefixString, hrefString);
    if (cachedNamespace != null)
        return cachedNamespace;
    Node namespaceOwner;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        namespaceOwner = node;
        Element element = (Element) node;
        // adds namespace as node's attribute
        final String uri = "http://www.w3.org/2000/xmlns/";
        String qName = prefix.isNil() ? "xmlns" : "xmlns:" + prefixString;
        element.setAttributeNS(uri, qName, hrefString);
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE)
        namespaceOwner = ((Attr) node).getOwnerElement();
    else
        namespaceOwner = node.getParentNode();
    XmlNamespace ns = XmlNamespace.createFromPrefixAndHref(namespaceOwner, prefix, href);
    if (node != namespaceOwner) {
        this.node = NokogiriHelpers.renameNode(node, ns.getHref(), ns.getPrefix() + ":" + node.getLocalName());
    }
    updateNodeNamespaceIfNecessary(context, ns);
    return ns;
}
Also used : Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) Element(org.w3c.dom.Element) NokogiriNamespaceCache(nokogiri.internals.NokogiriNamespaceCache) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 7 with NokogiriNamespaceCache

use of nokogiri.internals.NokogiriNamespaceCache in project gocd by gocd.

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) Node(org.w3c.dom.Node) NokogiriHelpers.clearCachedNode(nokogiri.internals.NokogiriHelpers.clearCachedNode) ArrayList(java.util.ArrayList) NokogiriNamespaceCache(nokogiri.internals.NokogiriNamespaceCache) NokogiriHelpers.rubyStringToString(nokogiri.internals.NokogiriHelpers.rubyStringToString) RubyString(org.jruby.RubyString) Attr(org.w3c.dom.Attr) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

NokogiriNamespaceCache (nokogiri.internals.NokogiriNamespaceCache)7 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)6 RubyString (org.jruby.RubyString)6 JRubyMethod (org.jruby.anno.JRubyMethod)6 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)4 Node (org.w3c.dom.Node)4 NokogiriHelpers.convertString (nokogiri.internals.NokogiriHelpers.convertString)3 ArrayList (java.util.ArrayList)2 NokogiriHelpers.nodeArrayToRubyArray (nokogiri.internals.NokogiriHelpers.nodeArrayToRubyArray)2 RubyArray (org.jruby.RubyArray)2 Attr (org.w3c.dom.Attr)2 Element (org.w3c.dom.Element)2 Ruby (org.jruby.Ruby)1 IRubyObject (org.jruby.runtime.builtin.IRubyObject)1