use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method line.
@JRubyMethod
public IRubyObject line(ThreadContext context) {
Node root = getOwnerDocument();
int[] counter = new int[1];
count(root, counter);
return RubyFixnum.newFixnum(context.getRuntime(), counter[0] + 1);
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
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;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method process_xincludes.
/**
* call-seq:
* process_xincludes(options)
*
* Loads and substitutes all xinclude elements below the node. The
* parser context will be initialized with +options+.
*
*/
@JRubyMethod(visibility = Visibility.PRIVATE)
public IRubyObject process_xincludes(ThreadContext context, IRubyObject options) {
XmlDocument xmlDocument = (XmlDocument) document(context);
RubyArray errors = (RubyArray) xmlDocument.getInstanceVariable("@errors");
while (errors.getLength() > 0) {
XmlSyntaxError error = (XmlSyntaxError) errors.shift(context);
if (error.toString().contains("Include operation failed")) {
throw new RaiseException(error);
}
}
return this;
}
use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
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 nokogiri by sparklemotion.
the class XmlNode method rbNew.
/**
* Allocate a new object, perform initialization, call that
* object's initialize method, and call any block passing the
* object as the only argument. If <code>cls</code> is
* Nokogiri::XML::Node, creates a new Nokogiri::XML::Element
* instead.
*
* This static method seems to be inherited, strangely enough.
* E.g. creating a new XmlAttr from Ruby code calls this method if
* XmlAttr does not define its own 'new' method.
*
* Since there is some Java bookkeeping that always needs to
* happen, we don't define the 'initialize' method in Java because
* we'd have to count on subclasses calling 'super'.
*
* The main consequence of this is that every subclass needs to
* define its own 'new' method.
*
* As a convenience, this method does the following:
*
* <ul>
*
* <li>allocates a new object using the allocator assigned to
* <code>cls</code></li>
*
* <li>calls the Java method init(); subclasses can override this,
* otherwise they should implement a specific 'new' method</li>
*
* <li>invokes the Ruby initializer</li>
*
* <li>if a block is given, calls the block with the new node as
* the argument</li>
*
* </ul>
*
* -pmahoney
*/
@JRubyMethod(name = "new", meta = true, rest = true)
public static IRubyObject rbNew(ThreadContext context, IRubyObject cls, IRubyObject[] args, Block block) {
Ruby ruby = context.getRuntime();
RubyClass klazz = (RubyClass) cls;
if ("Nokogiri::XML::Node".equals(klazz.getName())) {
klazz = getNokogiriClass(ruby, "Nokogiri::XML::Element");
}
XmlNode xmlNode = (XmlNode) klazz.allocate();
xmlNode.init(context, args);
xmlNode.callInit(args, block);
assert xmlNode.node != null;
if (block.isGiven())
block.call(context, xmlNode);
return xmlNode;
}
Aggregations