use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class HtmlElementDescription method sub_elements.
@JRubyMethod()
public IRubyObject sub_elements(ThreadContext context) {
Ruby ruby = context.getRuntime();
List<String> subs = findSubElements(element);
IRubyObject[] ary = new IRubyObject[subs.size()];
for (int i = 0; i < subs.size(); ++i) {
ary[i] = ruby.newString(subs.get(i));
}
return ruby.newArray(ary);
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class HtmlSaxPushParser method native_write.
@JRubyMethod
public IRubyObject native_write(ThreadContext context, IRubyObject chunk, IRubyObject isLast) {
try {
initialize_task(context);
} catch (IOException e) {
throw context.getRuntime().newRuntimeError(e.getMessage());
}
final ByteArrayInputStream data = NokogiriHelpers.stringBytesToStream(chunk);
if (data == null) {
terminateTask(context);
// Nokogiri::HTML::SyntaxError
throw new RaiseException(XmlSyntaxError.createHTMLSyntaxError(context.runtime));
}
int errorCount0 = parserTask.getErrorCount();
if (isLast.isTrue()) {
IRubyObject document = invoke(context, this, "document");
invoke(context, document, "end_document");
terminateTask(context);
} else {
try {
Future<Void> task = stream.addChunk(data);
task.get();
} catch (ClosedStreamException ex) {
// this means the stream is closed, ignore this exception
} catch (Exception e) {
throw context.getRuntime().newRuntimeError(e.getMessage());
}
}
if (!options.recover && parserTask.getErrorCount() > errorCount0) {
terminateTask(context);
throw new RaiseException(parserTask.getLastError(), true);
}
return this;
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class XmlComment method init.
@Override
protected void init(ThreadContext context, IRubyObject[] args) {
if (args.length < 2)
throw getRuntime().newArgumentError(args.length, 2);
IRubyObject doc = args[0];
IRubyObject text = args[1];
XmlDocument xmlDoc;
if (doc instanceof XmlDocument) {
xmlDoc = (XmlDocument) doc;
} else if (doc instanceof XmlNode) {
XmlNode xmlNode = (XmlNode) doc;
xmlDoc = (XmlDocument) xmlNode.document(context);
} else {
throw getRuntime().newArgumentError("first argument must be a XML::Document or XML::Node");
}
if (xmlDoc != null) {
Document document = xmlDoc.getDocument();
Node node = document.createComment(rubyStringToString(text));
setNode(context, node);
}
}
use of org.jruby.runtime.builtin.IRubyObject in project nokogiri by sparklemotion.
the class HtmlDocument method getInternalSubset.
public IRubyObject getInternalSubset(ThreadContext context) {
IRubyObject internalSubset = super.getInternalSubset(context);
if (internalSubset.isNil()) {
internalSubset = XmlDtd.newEmpty(context.getRuntime(), getDocument(), context.getRuntime().newString(DEFAULT_CONTENT_TYPE), context.getRuntime().newString(DEFAULT_PUBLIC_ID), context.getRuntime().newString(DEFAULT_SYTEM_ID));
setInternalSubset(internalSubset);
}
return internalSubset;
}
use of org.jruby.runtime.builtin.IRubyObject in project gocd by gocd.
the class XmlNode method adoptAs.
/**
* Adopt XmlNode <code>other</code> into the document of
* <code>this</code> using the specified scheme.
*/
protected IRubyObject adoptAs(ThreadContext context, AdoptScheme scheme, IRubyObject other_) {
XmlNode other = asXmlNode(context, other_);
// this.doc might be null since this node can be empty node.
if (this.doc != null) {
other.setDocument(context, this.doc);
}
IRubyObject nodeOrTags = other;
Node thisNode = node;
Node otherNode = other.node;
try {
Document prev = otherNode.getOwnerDocument();
Document doc = thisNode.getOwnerDocument();
clearXpathContext(prev);
clearXpathContext(doc);
if (doc != null && doc != otherNode.getOwnerDocument()) {
Node ret = doc.adoptNode(otherNode);
// FIXME: this is really a hack, see documentation of fixUserData() for more details.
fixUserData(prev, ret);
if (ret == null) {
throw context.getRuntime().newRuntimeError("Failed to take ownership of node");
}
otherNode = ret;
}
Node parent = thisNode.getParentNode();
switch(scheme) {
case CHILD:
Node[] children = adoptAsChild(context, thisNode, otherNode);
if (children.length == 1 && otherNode == children[0]) {
break;
} else {
nodeOrTags = nodeArrayToRubyArray(context.getRuntime(), children);
}
break;
case PREV_SIBLING:
adoptAsPrevSibling(context, parent, thisNode, otherNode);
break;
case NEXT_SIBLING:
adoptAsNextSibling(context, parent, thisNode, otherNode);
break;
case REPLACEMENT:
adoptAsReplacement(context, parent, thisNode, otherNode);
break;
}
} catch (Exception e) {
throw context.getRuntime().newRuntimeError(e.toString());
}
if (otherNode.getNodeType() == Node.TEXT_NODE) {
coalesceTextNodes(context, other, scheme);
}
relink_namespace(context);
return nodeOrTags;
}
Aggregations