use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method key_p.
/**
* Test if this node has an attribute named <code>rbkey</code>.
* Overridden in XmlElement.
*/
@JRubyMethod(name = { "key?", "has_attribute?" })
public IRubyObject key_p(ThreadContext context, IRubyObject rbkey) {
if (node instanceof Element) {
String key = rubyStringToString(rbkey);
Element element = (Element) node;
if (element.hasAttribute(key)) {
return context.getRuntime().getTrue();
} else {
NamedNodeMap namedNodeMap = element.getAttributes();
for (int i = 0; i < namedNodeMap.getLength(); i++) {
Node n = namedNodeMap.item(i);
if (key.equals(n.getLocalName())) {
return context.getRuntime().getTrue();
}
}
}
return context.getRuntime().getFalse();
} else {
return context.getRuntime().getNil();
}
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method unlink.
@JRubyMethod(name = { "unlink", "remove" })
public IRubyObject unlink(ThreadContext context) {
final Node parent = node.getParentNode();
if (parent != null) {
parent.removeChild(node);
clearXpathContext(parent);
}
return this;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method native_write_to.
/**
* @param args {IRubyObject io,
* IRubyObject encoding,
* IRubyObject indentString,
* IRubyObject options}
*/
@JRubyMethod(required = 4, visibility = Visibility.PRIVATE)
public IRubyObject native_write_to(ThreadContext context, IRubyObject[] args) {
IRubyObject io = args[0];
IRubyObject encoding = args[1];
IRubyObject indentString = args[2];
IRubyObject options = args[3];
String encString = encoding.isNil() ? null : rubyStringToString(encoding);
SaveContextVisitor visitor = new SaveContextVisitor(RubyFixnum.fix2int(options), rubyStringToString(indentString), encString, isHtmlDoc(context), isFragment(), 0);
accept(context, visitor);
final IRubyObject rubyString;
if (NokogiriHelpers.isUTF8(encString)) {
rubyString = convertString(context.getRuntime(), visitor.getInternalBuffer());
} else {
ByteBuffer bytes = convertEncoding(Charset.forName(encString), visitor.getInternalBuffer());
ByteList str = new ByteList(bytes.array(), bytes.arrayOffset(), bytes.remaining());
rubyString = RubyString.newString(context.getRuntime(), str);
}
RuntimeHelpers.invoke(context, io, "write", rubyString);
return io;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
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 nokogiri by sparklemotion.
the class XmlNode method create_internal_subset.
@JRubyMethod
public IRubyObject create_internal_subset(ThreadContext context, IRubyObject name, IRubyObject external_id, IRubyObject system_id) {
IRubyObject subset = internal_subset(context);
if (!subset.isNil()) {
throw context.getRuntime().newRuntimeError("Document already has internal subset");
}
Document document = getOwnerDocument();
if (document == null) {
return context.getRuntime().getNil();
}
XmlDocument xdoc = (XmlDocument) getCachedNodeOrCreate(context.getRuntime(), document);
IRubyObject xdtd = xdoc.createInternalSubset(context, name, external_id, system_id);
return xdtd;
}
Aggregations