use of org.jruby.anno.JRubyMethod in project gocd by gocd.
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);
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class XmlReader method from_io.
@JRubyMethod(meta = true, rest = true)
public static IRubyObject from_io(ThreadContext context, IRubyObject cls, IRubyObject[] args) {
// Only to pass the source test.
Ruby runtime = context.getRuntime();
// Not nil allowed!
if (args[0].isNil())
throw runtime.newArgumentError("io cannot be nil");
XmlReader reader = (XmlReader) NokogiriService.XML_READER_ALLOCATOR.allocate(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Reader"));
reader.init(runtime);
reader.setInstanceVariable("@source", args[0]);
reader.setInstanceVariable("@errors", runtime.newArray());
IRubyObject url = context.nil;
if (args.length > 1)
url = args[1];
if (args.length > 2)
reader.setInstanceVariable("@encoding", args[2]);
Options options;
if (args.length > 3) {
options = new ParserContext.Options((Long) args[3].toJava(Long.class));
} else {
// use the default options RECOVER | NONET
options = new ParserContext.Options(2048 | 1);
}
InputStream in = new UncloseableInputStream(new IOInputStream(args[0]));
reader.setInput(context, in, url, options);
return reader;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class XmlNode method node_name_set.
@JRubyMethod(name = { "node_name=", "name=" })
public IRubyObject node_name_set(ThreadContext context, IRubyObject nodeName) {
String newName = rubyStringToString(nodeName);
this.node = NokogiriHelpers.renameNode(node, null, newName);
setName(nodeName);
return this;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
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;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class XmlNode method lang.
@JRubyMethod
public IRubyObject lang(ThreadContext context) {
IRubyObject currentObj = this;
while (!currentObj.isNil()) {
XmlNode currentNode = asXmlNode(context, currentObj);
IRubyObject lang = currentNode.getAttribute(context.getRuntime(), "xml:lang");
if (!lang.isNil()) {
return lang;
}
currentObj = currentNode.parent(context);
}
return context.nil;
}
Aggregations