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;
}
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;
}
Aggregations