Search in sources :

Example 1 with NokogiriHandler

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();
}
Also used : SAXParseException(org.xml.sax.SAXParseException) NokogiriHandler(nokogiri.internals.NokogiriHandler) IOException(java.io.IOException) Ruby(org.jruby.Ruby) RaiseException(org.jruby.exceptions.RaiseException) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

IOException (java.io.IOException)1 NokogiriHandler (nokogiri.internals.NokogiriHandler)1 Ruby (org.jruby.Ruby)1 JRubyMethod (org.jruby.anno.JRubyMethod)1 RaiseException (org.jruby.exceptions.RaiseException)1 SAXException (org.xml.sax.SAXException)1 SAXNotRecognizedException (org.xml.sax.SAXNotRecognizedException)1 SAXNotSupportedException (org.xml.sax.SAXNotSupportedException)1 SAXParseException (org.xml.sax.SAXParseException)1