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()})