use of nokogiri.internals.NokogiriHandler 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();
}
Aggregations