use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class XmlDocument method create_entity.
@JRubyMethod(required = 1, optional = 4)
public IRubyObject create_entity(ThreadContext context, IRubyObject[] argv) {
// would cause validation failure.
if (argv.length == 0)
throw context.getRuntime().newRuntimeError("Could not create entity");
String tagName = rubyStringToString(argv[0]);
Node n = this.getOwnerDocument().createElement(tagName);
return XmlEntityDecl.create(context, n, argv);
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class XmlDocument method rbNew.
/*
* call-seq:
* new(version = default)
*
* Create a new document with +version+ (defaults to "1.0")
*/
@JRubyMethod(name = "new", meta = true, rest = true, required = 0)
public static IRubyObject rbNew(ThreadContext context, IRubyObject klazz, IRubyObject[] args) {
XmlDocument xmlDocument = null;
try {
Document docNode = createNewDocument();
if ("Nokogiri::HTML::Document".equals(((RubyClass) klazz).getName())) {
xmlDocument = (XmlDocument) NokogiriService.HTML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
xmlDocument.setDocumentNode(context, docNode);
} else {
// XML::Document and sublass
xmlDocument = (XmlDocument) NokogiriService.XML_DOCUMENT_ALLOCATOR.allocate(context.getRuntime(), (RubyClass) klazz);
xmlDocument.setDocumentNode(context, docNode);
}
} catch (Exception ex) {
throw context.getRuntime().newRuntimeError("couldn't create document: " + ex.toString());
}
RuntimeHelpers.invoke(context, xmlDocument, "initialize", args);
return xmlDocument;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
the class XmlSaxParserContext method parse_with.
@JRubyMethod
public IRubyObject parse_with(ThreadContext context, IRubyObject handlerRuby) {
Ruby ruby = context.getRuntime();
if (!invoke(context, handlerRuby, "respond_to?", ruby.newSymbol("document")).isTrue()) {
String msg = "argument must respond_to document";
throw ruby.newArgumentError(msg);
}
handler = new NokogiriHandler(ruby, handlerRuby);
preParse(context, handlerRuby, handler);
setContentHandler(handler);
setErrorHandler(handler);
try {
setProperty("http://xml.org/sax/properties/lexical-handler", handler);
} catch (Exception ex) {
throw ruby.newRuntimeError("Problem while creating XML SAX Parser: " + ex.toString());
}
try {
try {
do_parse();
} catch (SAXParseException spe) {
// A bad document (<foo><bar></foo>) should call the
// error handler instead of raising a SAX exception.
// However, an EMPTY document should raise a
// RuntimeError. This is a bit kludgy, but AFAIK SAX
// doesn't distinguish between empty and bad whereas
// Nokogiri does.
String message = spe.getMessage();
if ("Premature end of file.".matches(message) && stringDataSize < 1) {
throw ruby.newRuntimeError("couldn't parse document: " + message);
} else {
handler.error(spe);
}
}
} catch (SAXException se) {
throw RaiseException.createNativeRaiseException(ruby, se);
} catch (IOException ioe) {
throw ruby.newIOErrorFromException(ioe);
}
postParse(context, handlerRuby, handler);
return ruby.getNil();
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
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;
}
use of org.jruby.anno.JRubyMethod in project gocd by gocd.
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);
}
Aggregations