def Test(tester): tester.startGroup('RELAX NG WXS type facets') factory = InputSource.DefaultFactory for title, rng, doc in g_valid_cases: #doc = NonvalidatingReader.parseString(DOC1, __name__) tester.startTest(title) rng_isrc = factory.fromString(rng, __name__) xml_isrc = factory.fromString(doc, __name__) validator = RelaxNgValidator(rng_isrc) result = not not validator.isValid(xml_isrc) tester.compare(True, result) tester.testDone() for title, rng, doc in g_invalid_cases: #doc = NonvalidatingReader.parseString(DOC1, __name__) tester.startTest(title) rng_isrc = factory.fromString(rng, __name__) xml_isrc = factory.fromString(doc, __name__) validator = RelaxNgValidator(rng_isrc) result = not not validator.isValid(xml_isrc) tester.compare(False, result) tester.testDone() tester.groupDone() return
def loadFromFile(self, filename): #rngParser = libxml2.relaxNGNewMemParserCtxt(VALIDATION_SCHEMA, len(VALIDATION_SCHEMA)) #rngContext = rngParser.relaxNGParse().relaxNGNewValidCtxt() #file = fileRead(filename, 'r') #doc = libxml2.parseDoc(file) #if doc.relaxNGValidateDoc(rngContext) != 0: # raise InvalidLibraryException(filename) #for item in doc.xpathEval('/library/item'): # self.addItem({'authors': map(lambda x: x.content, item.xpathEval('authors/author')), # 'title': item.xpathEval('title')[0].content, # 'type': item.xpathEval('type')[0].content, # 'date': item.xpathEval('date')[0].content, # 'language': item.xpathEval('language')[0].content}) factory = InputSource.DefaultFactory schema = factory.fromString(VALIDATION_SCHEMA) validator = RelaxNgValidator(schema) file = Uri.OsPathToUri(filename, attemptAbsolute=1) # validate file if not(validator.isValid(factory.fromUri(file))): raise InvalidLibraryException(filename) doc = NonvalidatingReader.parse(factory.fromUri(file)) # read items from document for item in doc.xpath('/library/item'): self.addItem({'authors': map(lambda x: x.childNodes[0].nodeValue.strip(), item.xpath('authors/author')), 'title': item.xpath('title')[0].childNodes[0].nodeValue.strip(), 'type': item.xpath('type')[0].childNodes[0].nodeValue.strip(), 'date': item.xpath('date')[0].childNodes[0].nodeValue.strip(), 'language': item.xpath('language')[0].childNodes[0].nodeValue.strip()})
except ImportError: raise SystemExit("Missing RELAX-NG support library. It is " "available in the Ft.Xml.ThirdParty package.") from Ft.Xml import Domlette if validate_flag: reader = Domlette.ValidatingReader else: reader = Domlette.NonvalidatingReader try: doc = reader.parse(source_isrc) CloseStream(source_isrc, quiet=True) if rng_isrc is not None: validator = RelaxNgValidator(rng_isrc) CloseStream(rng_isrc, quiet=True) result = validator.validateNode(doc) if not result.nullable(): raise RngInvalid(result) if not noserialize: from Ft.Xml.Domlette import Print, PrettyPrint if pretty: PrettyPrint(doc, out_file, encoding, as_html) else: Print(doc, out_file, encoding, as_html) except Exception, e: import traceback traceback.print_exc(1000, sys.stderr)
def process_test(test, tester): description = get_description(test) tester.startGroup(description) incorrect_schema = test.xpath('incorrect/*') correct_schema = test.xpath('correct/*') valid_xml = test.xpath('valid/*') invalid_xml = test.xpath('invalid/*') if incorrect_schema: tester.startTest('Parse invalid schema') schema_doc = doc_from_fragment(incorrect_schema[0]) try: validator = RelaxNgValidator(schema_doc) except RngSchemaInvalidException: pass else: errmsg = 'Invalid schema parse fails to raise RngSchemaInvalidException!\n' errmsg += 'This is the schema:\n%s' % serialize_doc(schema_doc) tester.error(errmsg) tester.testDone() elif correct_schema: schema_doc = doc_from_fragment(correct_schema[0]) valid_docs = [doc_from_fragment(node) for node in valid_xml] invalid_docs = [doc_from_fragment(node) for node in invalid_xml] i = 0 for valid_doc in valid_docs: i += 1 desc = 'valid doc %d of %d' % (i, len(valid_docs)) tester.startTest(desc) try: validator = RelaxNgValidator(schema_doc) except RngSchemaInvalidException, e: tester.warning('Could not complete test; schema was not valid: %s' % e) tester.message('This is the schema:\n%s' % serialize_doc(schema_doc)) tester.message('This is the valid doc:\n%s' % serialize_doc(valid_doc)) tester.testDone() break result = validator.validateNode(valid_doc) if not result.nullable(): if hasattr(result, 'msg'): tester.error('Valid doc fails to validate: %r' % result.msg) else: tester.error('Valid doc fails to validate (no reason given)') tester.message('Result was %r' % result) tester.message('This is the schema:\n%s' % serialize_doc(schema_doc)) tester.message('This is the valid doc:\n%s' % serialize_doc(valid_doc)) tester.testDone() for invalid_doc in invalid_docs: desc = 'invalid doc %d of %d' % (i, len(valid_docs)) tester.startTest(desc) try: validator = RelaxNgValidator(schema_doc) except RngSchemaInvalidException, e: tester.warning('Could not complete test; schema was not valid: %s' % e) tester.message('This is the schema:\n%s' % serialize_doc(schema_doc)) tester.testDone() break result = validator.validateNode(invalid_doc) if result.nullable(): tester.error('Invalid doc validates as valid!') tester.message('This is the schema:\n%s' % serialize_doc(schema_doc)) tester.message('This is the invalid doc:\n%s' % serialize_doc(invalid_doc)) tester.testDone()
def process_test(test, tester): description = get_description(test) tester.startGroup(description) incorrect_schema = test.xpath('incorrect/*') correct_schema = test.xpath('correct/*') valid_xml = test.xpath('valid/*') invalid_xml = test.xpath('invalid/*') if incorrect_schema: tester.startTest('Parse invalid schema') schema_doc = doc_from_fragment(incorrect_schema[0]) try: validator = RelaxNgValidator(schema_doc) except RngSchemaInvalidException: pass else: errmsg = 'Invalid schema parse fails to raise RngSchemaInvalidException!\n' errmsg += 'This is the schema:\n%s' % serialize_doc(schema_doc) tester.error(errmsg) tester.testDone() elif correct_schema: schema_doc = doc_from_fragment(correct_schema[0]) valid_docs = [doc_from_fragment(node) for node in valid_xml] invalid_docs = [doc_from_fragment(node) for node in invalid_xml] i = 0 for valid_doc in valid_docs: i += 1 desc = 'valid doc %d of %d' % (i, len(valid_docs)) tester.startTest(desc) try: validator = RelaxNgValidator(schema_doc) except RngSchemaInvalidException, e: tester.warning( 'Could not complete test; schema was not valid: %s' % e) tester.message('This is the schema:\n%s' % serialize_doc(schema_doc)) tester.message('This is the valid doc:\n%s' % serialize_doc(valid_doc)) tester.testDone() break result = validator.validateNode(valid_doc) if not result.nullable(): if hasattr(result, 'msg'): tester.error('Valid doc fails to validate: %r' % result.msg) else: tester.error( 'Valid doc fails to validate (no reason given)') tester.message('Result was %r' % result) tester.message('This is the schema:\n%s' % serialize_doc(schema_doc)) tester.message('This is the valid doc:\n%s' % serialize_doc(valid_doc)) tester.testDone() for invalid_doc in invalid_docs: desc = 'invalid doc %d of %d' % (i, len(valid_docs)) tester.startTest(desc) try: validator = RelaxNgValidator(schema_doc) except RngSchemaInvalidException, e: tester.warning( 'Could not complete test; schema was not valid: %s' % e) tester.message('This is the schema:\n%s' % serialize_doc(schema_doc)) tester.testDone() break result = validator.validateNode(invalid_doc) if result.nullable(): tester.error('Invalid doc validates as valid!') tester.message('This is the schema:\n%s' % serialize_doc(schema_doc)) tester.message('This is the invalid doc:\n%s' % serialize_doc(invalid_doc)) tester.testDone()