Search in sources :

Example 1 with SchemaErrorHandler

use of nokogiri.internals.SchemaErrorHandler in project gocd by gocd.

the class XmlSchema method validate_document_or_file.

IRubyObject validate_document_or_file(ThreadContext context, XmlDocument xmlDocument) {
    RubyArray errors = (RubyArray) this.getInstanceVariable("@errors");
    ErrorHandler errorHandler = new SchemaErrorHandler(context.getRuntime(), errors);
    setErrorHandler(errorHandler);
    try {
        validate(xmlDocument.getDocument());
    } catch (SAXException ex) {
        XmlSyntaxError xmlSyntaxError = (XmlSyntaxError) NokogiriService.XML_SYNTAXERROR_ALLOCATOR.allocate(context.getRuntime(), getNokogiriClass(context.getRuntime(), "Nokogiri::XML::SyntaxError"));
        xmlSyntaxError.setException(ex);
        errors.append(xmlSyntaxError);
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
    return errors;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IgnoreSchemaErrorsErrorHandler(nokogiri.internals.IgnoreSchemaErrorsErrorHandler) RubyArray(org.jruby.RubyArray) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 2 with SchemaErrorHandler

use of nokogiri.internals.SchemaErrorHandler in project nokogiri by sparklemotion.

the class XmlSchema method validate_document_or_file.

IRubyObject validate_document_or_file(ThreadContext context, XmlDocument xmlDocument) {
    RubyArray<?> errors = (RubyArray) this.getInstanceVariable("@errors");
    ErrorHandler errorHandler = new SchemaErrorHandler(context.runtime, errors);
    setErrorHandler(errorHandler);
    try {
        validate(xmlDocument.getDocument());
    } catch (SAXException ex) {
        XmlSyntaxError xmlSyntaxError = XmlSyntaxError.createXMLSyntaxError(context.runtime);
        xmlSyntaxError.setException(ex);
        errors.append(xmlSyntaxError);
    } catch (IOException ex) {
        throw context.runtime.newIOError(ex.getMessage());
    }
    return errors;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IgnoreSchemaErrorsErrorHandler(nokogiri.internals.IgnoreSchemaErrorsErrorHandler) RubyArray(org.jruby.RubyArray) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 3 with SchemaErrorHandler

use of nokogiri.internals.SchemaErrorHandler in project nokogiri by sparklemotion.

the class XmlSchema method createSchemaInstance.

static XmlSchema createSchemaInstance(ThreadContext context, RubyClass klazz, Source source, IRubyObject parseOptions) {
    Ruby runtime = context.getRuntime();
    XmlSchema xmlSchema = (XmlSchema) NokogiriService.XML_SCHEMA_ALLOCATOR.allocate(runtime, klazz);
    if (parseOptions == null) {
        parseOptions = defaultParseOptions(context.getRuntime());
    }
    long intParseOptions = RubyFixnum.fix2long(Helpers.invoke(context, parseOptions, "to_i"));
    xmlSchema.setInstanceVariable("@errors", runtime.newEmptyArray());
    xmlSchema.setInstanceVariable("@parse_options", parseOptions);
    try {
        SchemaErrorHandler errorHandler = new SchemaErrorHandler(context.getRuntime(), (RubyArray) xmlSchema.getInstanceVariable("@errors"));
        Schema schema = xmlSchema.getSchema(source, context.getRuntime().getCurrentDirectory(), context.getRuntime().getInstanceConfig().getScriptFileName(), errorHandler, intParseOptions);
        xmlSchema.setValidator(schema.newValidator());
        return xmlSchema;
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    }
}
Also used : Schema(javax.xml.validation.Schema) SchemaErrorHandler(nokogiri.internals.SchemaErrorHandler) Ruby(org.jruby.Ruby) SAXException(org.xml.sax.SAXException)

Aggregations

SchemaErrorHandler (nokogiri.internals.SchemaErrorHandler)3 SAXException (org.xml.sax.SAXException)3 IOException (java.io.IOException)2 IgnoreSchemaErrorsErrorHandler (nokogiri.internals.IgnoreSchemaErrorsErrorHandler)2 RubyArray (org.jruby.RubyArray)2 ErrorHandler (org.xml.sax.ErrorHandler)2 Schema (javax.xml.validation.Schema)1 Ruby (org.jruby.Ruby)1