use of org.jruby.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlDocument method wrapJavaDocument.
@JRubyMethod(meta = true)
public static IRubyObject wrapJavaDocument(ThreadContext context, IRubyObject klazz, IRubyObject arg) {
XmlDocument xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::Document"));
RuntimeHelpers.invoke(context, xmlDocument, "initialize");
Document document = (Document) arg.toJava(Document.class);
xmlDocument.setDocumentNode(context, document);
return xmlDocument;
}
use of org.jruby.anno.JRubyMethod 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.anno.JRubyMethod in project nokogiri by sparklemotion.
the class HtmlSaxParserContext method parse_memory.
@JRubyMethod(name = "memory", meta = true)
public static IRubyObject parse_memory(ThreadContext context, IRubyObject klazz, IRubyObject data, IRubyObject encoding) {
HtmlSaxParserContext ctx = (HtmlSaxParserContext) NokogiriService.HTML_SAXPARSER_CONTEXT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
ctx.initialize(context.getRuntime());
String javaEncoding = findEncoding(context, encoding);
if (javaEncoding != null) {
String input = applyEncoding(rubyStringToString(data), javaEncoding);
ByteArrayInputStream istream = new ByteArrayInputStream(input.getBytes());
ctx.setInputSource(istream);
ctx.getInputSource().setEncoding(javaEncoding);
}
return ctx;
}
use of org.jruby.anno.JRubyMethod 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.anno.JRubyMethod in project nokogiri by sparklemotion.
the class XmlNode method element_children.
@JRubyMethod(name = { "element_children", "elements" })
public IRubyObject element_children(ThreadContext context) {
List<Node> elementNodes = new ArrayList<Node>();
addElements(node, elementNodes, false);
if (elementNodes.size() == 0)
return XmlNodeSet.newEmptyNodeSet(context);
RubyArray array = NokogiriHelpers.nodeArrayToRubyArray(context.getRuntime(), elementNodes.toArray(new Node[0]));
XmlNodeSet xmlNodeSet = XmlNodeSet.newXmlNodeSet(context, array);
return xmlNodeSet;
}
Aggregations