Example #1
0
    def validate(self, str):
        """
        Validate the given string 
        """
        if not self.schema:
            return False

        file = open(self.schema)
        try:
            schema = file.read()
        finally:
            file.close()

        rngParser = libxml2.relaxNGNewMemParserCtxt(schema, len(schema))
        rngSchema = rngParser.relaxNGParse()
        ctxt = rngSchema.relaxNGNewValidCtxt()
        doc = libxml2.parseDoc(str)
        is_valid = doc.relaxNGValidateDoc(ctxt)

        # Clean up
        doc.freeDoc()
        del rngParser, rngSchema, ctxt
        libxml2.relaxNGCleanupTypes()
        libxml2.cleanupParser()
        return is_valid == 0
libxml2.registerErrorHandler(callback, "")

libxml2.setEntityLoader(resolver)
root = testsuite.getRootElement()
if root.name != 'testSuite':
    print "%s doesn't start with a testSuite element, aborting" % (CONF)
    sys.exit(1)
if quiet == 0:
    print "Running Relax NG testsuite"
handle_testSuite(root)

if quiet == 0 or nb_schemas_failed != 0:
    print "\nTOTAL:\nfound %d test schemas: %d success %d failures" % (
        nb_schemas_tests, nb_schemas_success, nb_schemas_failed)
if quiet == 0 or nb_instances_failed != 0:
    print "found %d test instances: %d success %d failures" % (
        nb_instances_tests, nb_instances_success, nb_instances_failed)

testsuite.freeDoc()

# Memory debug specific
libxml2.relaxNGCleanupTypes()
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
    if quiet == 0:
        print "OK"
else:
    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
    libxml2.dumpMemory()
Example #3
0
if ret != 0:
    print "Error parsing the document"
    sys.exit(1)

if reader.IsValid() != 0:
    print "Document failed to detect the validation error"
    sys.exit(1)

if err != expect:
    print "Did not get the expected error message:"
    print err
    sys.exit(1)

#
# cleanup
#
del f
del input
del reader
del rngs
libxml2.relaxNGCleanupTypes()

# Memory debug specific
libxml2.cleanupParser()
if libxml2.debugMemory(1) == 0:
    print "OK"
else:
    print "Memory leak %d bytes" % (libxml2.debugMemory(1))
    libxml2.dumpMemory()