use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlDocument method create_entity.
@JRubyMethod(required = 1, optional = 4)
public IRubyObject create_entity(ThreadContext context, IRubyObject[] argv) {
// would cause validation failure.
if (argv.length == 0)
throw context.getRuntime().newRuntimeError("Could not create entity");
String tagName = rubyStringToString(argv[0]);
Node n = this.getOwnerDocument().createElement(tagName);
return XmlEntityDecl.create(context, n, argv);
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlDocument method root_set.
@JRubyMethod(name = "root=")
public IRubyObject root_set(ThreadContext context, IRubyObject newRoot_) {
// should be nil.
if (newRoot_ instanceof RubyNil) {
getDocument().getDocumentElement().setUserData(NokogiriHelpers.VALID_ROOT_NODE, false, null);
return newRoot_;
}
XmlNode newRoot = asXmlNode(context, newRoot_);
IRubyObject root = root(context);
if (root.isNil()) {
Node newRootNode;
if (getDocument() == newRoot.getOwnerDocument()) {
newRootNode = newRoot.node;
} else {
// must copy otherwise newRoot may exist in two places
// with different owner document.
newRootNode = getDocument().importNode(newRoot.node, true);
}
add_child_node(context, getCachedNodeOrCreate(context.getRuntime(), newRootNode));
} else {
Node rootNode = asXmlNode(context, root).node;
((XmlNode) getCachedNodeOrCreate(context.getRuntime(), rootNode)).replace_node(context, newRoot);
}
return newRoot;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method attribute_with_ns.
@JRubyMethod
public IRubyObject attribute_with_ns(ThreadContext context, IRubyObject name, IRubyObject namespace) {
String namej = rubyStringToString(name);
String nsj = (namespace.isNil()) ? null : rubyStringToString(namespace);
Node el = this.node.getAttributes().getNamedItemNS(nsj, namej);
if (el == null) {
return context.getRuntime().getNil();
}
return NokogiriHelpers.getCachedNodeOrCreate(context.getRuntime(), el);
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method get.
/**
* Get the attribute at the given key, <code>key</code>.
* Assumes that this node has attributes (i.e. that key? returned
* true).
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject get(ThreadContext context, IRubyObject rbkey) {
if (node instanceof Element) {
if (rbkey == null || rbkey.isNil())
context.getRuntime().getNil();
String key = rubyStringToString(rbkey);
Element element = (Element) node;
if (!element.hasAttribute(key))
return context.getRuntime().getNil();
String value = element.getAttribute(key);
return stringOrNil(context.getRuntime(), value);
}
return context.getRuntime().getNil();
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method attribute_nodes.
@JRubyMethod
public IRubyObject attribute_nodes(ThreadContext context) {
NamedNodeMap nodeMap = this.node.getAttributes();
Ruby ruby = context.getRuntime();
if (nodeMap == null) {
return ruby.newEmptyArray();
}
RubyArray attr = ruby.newArray();
for (int i = 0; i < nodeMap.getLength(); i++) {
if ((doc instanceof HtmlDocument) || !NokogiriHelpers.isNamespace(nodeMap.item(i))) {
attr.append(getCachedNodeOrCreate(context.getRuntime(), nodeMap.item(i)));
}
}
return attr;
}
Aggregations