use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method namespace.
@JRubyMethod
public IRubyObject namespace(ThreadContext context) {
Ruby runtime = context.getRuntime();
if (doc instanceof HtmlDocument)
return runtime.getNil();
NokogiriNamespaceCache nsCache = NokogiriHelpers.getNamespaceCacheFormNode(node);
String namespaceURI = node.getNamespaceURI();
if (namespaceURI == null || namespaceURI == "") {
return runtime.getNil();
}
String prefix = node.getPrefix();
XmlNamespace namespace = nsCache.get(prefix == null ? "" : prefix, namespaceURI);
if (namespace == null || namespace.isEmpty()) {
// if it's not in the cache, create an unowned, uncached namespace and
// return that. XmlReader can't insert namespaces into the cache, so
// this is necessary for XmlReader to work correctly.
namespace = new XmlNamespace(runtime, getNokogiriClass(runtime, "Nokogiri::XML::Namespace"));
IRubyObject rubyPrefix = NokogiriHelpers.stringOrNil(runtime, prefix);
IRubyObject rubyUri = NokogiriHelpers.stringOrNil(runtime, namespaceURI);
namespace.init(null, rubyPrefix, rubyUri, doc);
}
return namespace;
}
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);
}
Aggregations