use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class XmlNode method next_element.
@JRubyMethod
public IRubyObject next_element(ThreadContext context) {
Node nextNode = node.getNextSibling();
Ruby ruby = context.getRuntime();
if (nextNode == null)
return ruby.getNil();
if (nextNode instanceof Element) {
return getCachedNodeOrCreate(context.getRuntime(), nextNode);
}
Node deeper = nextNode.getNextSibling();
if (deeper == null)
return ruby.getNil();
return getCachedNodeOrCreate(context.getRuntime(), deeper);
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class HtmlDocument method rbNew.
@JRubyMethod(name = "new", meta = true, rest = true, required = 0)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
HtmlDocument htmlDocument = null;
try {
Document docNode = createNewDocument();
htmlDocument = (HtmlDocument) NokogiriService.HTML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
htmlDocument.setDocumentNode(context, docNode);
} catch (Exception ex) {
throw context.getRuntime().newRuntimeError("couldn't create document: " + ex.toString());
}
RuntimeHelpers.invoke(context, htmlDocument, "initialize", args);
return htmlDocument;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
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 gocd by gocd.
the class HtmlSaxParserContext method parse_file.
@JRubyMethod(name = "file", meta = true)
public static IRubyObject parse_file(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());
ctx.setInputSourceFile(context, data);
String javaEncoding = findEncoding(context, encoding);
if (javaEncoding != null) {
ctx.getInputSource().setEncoding(javaEncoding);
}
return ctx;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
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());
}
byte[] data = null;
if (chunk instanceof RubyString || chunk.respondsTo("to_str")) {
data = chunk.convertToString().getBytes();
} else {
terminateTask(context);
XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::HTML::SyntaxError"));
throw new RaiseException(xmlSyntaxError);
}
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(new ByteArrayInputStream(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;
}
Aggregations