def processSchema(self, filePath): global msgSchemaNotValidButShould, msgSchemaValidButShouldNot schema = None # # Parse the schema. # self.debugMsg("loading schema: %s" % filePath) schema_ParserCtxt = libxml2.schemaNewParserCtxt(filePath) try: try: schema = schema_ParserCtxt.schemaParse() except: pass finally: self.debugMsg("after loading schema") del schema_ParserCtxt if schema is None: self.debugMsg("schema is None") self.debugMsg("checking for IO errors...") if self.isIOError(file, "schema"): return None self.debugMsg("checking schema result") if (schema is None and self.schema_Val) or (schema is not None and self.schema_Val == 0): self.debugMsg("schema result is BAD") if schema == None: self.fail(msgSchemaNotValidButShould) else: self.fail(msgSchemaValidButShouldNot) else: self.debugMsg("schema result is OK") return schema
def validateProcessDefinition(self): '''Validates document and returns it's language id on success. Exception is raised in any other case''' schemas = { 'jpdl-3.1': 'jpdl-3.1.xsd', 'jpdl-3.2': 'jpdl-3.2.xsd', 'bpmn-2.0': 'BPMN20.xsd' } # iterate through all schemas and try to validate XML definitionLang = None for lang in schemas: try: # load schema schema_parser_ctx = libxml2.schemaNewParserCtxt( os.path.join(self.scriptPath, 'schemas', schemas[lang])) schema = schema_parser_ctx.schemaParse() valid_schema = schema.schemaNewValidCtxt() except libxml2.libxmlError, e: raise InvalidSchemaException('Schema for ' + lang + ' is invalid.') # validate if not self.xml.schemaValidateDoc(valid_schema): # xml validate, store result definitionLang = lang
def processSchema(self, filePath): global msgSchemaNotValidButShould, msgSchemaValidButShouldNot schema = None # # Parse the schema. # self.debugMsg("loading schema: %s" % filePath) schema_ParserCtxt = libxml2.schemaNewParserCtxt(filePath) try: try: schema = schema_ParserCtxt.schemaParse() except: pass finally: self.debugMsg("after loading schema") del schema_ParserCtxt if schema is None: self.debugMsg("schema is None") self.debugMsg("checking for IO errors...") if self.isIOError(file, "schema"): return None self.debugMsg("checking schema result") if (schema is None and self.schema_Val) or (schema is not None and self.schema_Val == 0): self.debugMsg("schema result is BAD") if (schema == None): self.fail(msgSchemaNotValidButShould) else: self.fail(msgSchemaValidButShouldNot) else: self.debugMsg("schema result is OK") return schema
def validateXML(xmlDoc=None, xmlSchema=None): """ Validate XML document against given schema (.xsd) """ if xmlDoc == None or xmlSchema == None: return None try: import libxml2 except ImportError: return None try: ctxt = libxml2.schemaNewParserCtxt(xmlSchema) schema = ctxt.schemaParse() del ctxt validationCtxt = schema.schemaNewValidCtxt() doc = libxml2.parseFile(xmlDoc) instance_Err = validationCtxt.schemaValidateDoc(doc) del validationCtxt del schema doc.freeDoc() except Exception, err: return None
def validate_doc(doc): """Validate the document""" path = platform.get_platform().find_resource("chirp.xsd") try: ctx = libxml2.schemaNewParserCtxt(path) schema = ctx.schemaParse() except libxml2.parserError as e: LOG.error("Unable to load schema: %s" % e) LOG.error("Path: %s" % path) raise errors.RadioError("Unable to load schema") del ctx errs = [] warnings = [] def _err(msg, *_args): errs.append("ERROR: %s" % msg) def _wrn(msg, *_args): warnings.append("WARNING: %s" % msg) validctx = schema.schemaNewValidCtxt() validctx.setValidityErrorHandler(_err, _wrn) err = validctx.schemaValidateDoc(doc) for w in warnings: LOG.warn(w) if err: for l in [ "--- DOC ---", doc.serialize(format=1).split("\n"), "-----------", errs ]: LOG.error(l) raise errors.RadioError("Schema error")
def validateOVF(schema_file, ovf_file, handler=None): """ OVF schema validation. @type schema_file: string @param schema_file: OVF Schema path. @type ovf_file: string @param ovf_file: OVF file path. @type handler: L{ErrorHandler} @param handler: object used to return validation error and warning messages. @rtype: int @return: Error Code. 0 (zero) is OK. """ ctxt_parser = libxml2.schemaNewParserCtxt(schema_file) ctxt_schema = ctxt_parser.schemaParse() del ctxt_parser validator = ctxt_schema.schemaNewValidCtxt() if not handler: handler = ErrorHandler() validator.setValidityErrorHandler(handler.error, handler.warning) # Test valid document ret = validator.schemaValidateFile(ovf_file, 0) del ctxt_schema del validator return ret
def validateTaskModel(self): '''Validates task model XML and returns default namespace on success''' try: # load schema schema_parser_ctx = libxml2.schemaNewParserCtxt(os.path.join(self.scriptPath, 'schemas', 'modelSchema.xsd')) schema = schema_parser_ctx.schemaParse() valid_schema = schema.schemaNewValidCtxt() except libxml2.libxmlError, e: raise InvalidSchemaException('Task model schema is invalid')
def validateContentModel(self): """Validates task model XML and returns default namespace on success""" try: # load schema schema_parser_ctx = libxml2.schemaNewParserCtxt(os.path.join(self.scriptPath, "schemas", "modelSchema.xsd")) schema = schema_parser_ctx.schemaParse() valid_schema = schema.schemaNewValidCtxt() except libxml2.libxmlError, e: raise InvalidSchemaException("Task model schema is invalid")
def schema_validation(xml_file, xsd_file): ctxt = libxml2.schemaNewParserCtxt(xsd_file) schema = ctxt.schemaParse() validationCtxt = schema.schemaNewValidCtxt() res = validationCtxt.schemaValidateFile(xml_file, 0) if res != 0: print "VALIDATION FAILED" else: print "VALIDATED" return res
def schema_validate(xml_file, xsd_file): ctxt = libxml2.schemaNewParserCtxt(xsd_file) schema = ctxt.schemaParse() validationCtxt = schema.schemaNewValidCtxt() res = validationCtxt.schemaValidateFile(xml_file, 0) if res != 0: print "VALIDATION FAILED" else: print "VALIDATED" return res
def _init_xsd(self, xsd_file): """XSD Schema initialization""" try: schema = libxml2.schemaNewParserCtxt( xsd_file).schemaParse().schemaNewValidCtxt() schema.setValidityErrorHandler(self._xsd_validation_err, self._xsd_validation_wrn) return schema except Exception as ex: log.error('XSD initialization failed') raise MyXmlParserXSDInitExc(ex)
def validate_doc(doc): """Validate the document""" path = platform.get_platform().find_resource("chirp.xsd") try: ctx = libxml2.schemaNewParserCtxt(path) schema = ctx.schemaParse() except libxml2.parserError, e: LOG.error("Unable to load schema: %s" % e) LOG.error("Path: %s" % path) raise errors.RadioError("Unable to load schema")
def validateProcessDefinition(self): '''Validates process definition and return default namespace on success.''' # try to validate XML as jpdl-3.1 ns = 'urn:jbpm.org:jpdl-3.1' try: # load 3.1 schema schema_parser_ctx = libxml2.schemaNewParserCtxt(os.path.join(self.scriptPath, 'schemas', 'jpdl-3.1.xsd')) schema = schema_parser_ctx.schemaParse() valid_schema = schema.schemaNewValidCtxt() except libxml2.libxmlError, e: raise InvalidSchemaException('jpdl-3.1 schema is invalid')
def parseSchema(fileName): schema = None ctxt = libxml2.schemaNewParserCtxt(fileName) try: try: schema = ctxt.schemaParse() except: pass finally: del ctxt return schema
def validateContentModel(self): '''Validates task model XML and returns default namespace on success''' try: # load schema schema_parser_ctx = libxml2.schemaNewParserCtxt( os.path.join(self.scriptPath, 'schemas', 'modelSchema.xsd')) schema = schema_parser_ctx.schemaParse() valid_schema = schema.schemaNewValidCtxt() except libxml2.libxmlError, e: raise InvalidSchemaException('Task model schema is invalid')
def validate_doc(doc): """Validate the document""" basepath = platform.get_platform().executable_path() path = os.path.abspath(os.path.join(basepath, "chirp.xsd")) if not os.path.exists(path): path = "/usr/share/chirp/chirp.xsd" try: ctx = libxml2.schemaNewParserCtxt(path) schema = ctx.schemaParse() except libxml2.parserError, e: print "Unable to load schema: %s" % e print "Path: %s" % path raise errors.RadioError("Unable to load schema")
def validate_doc(doc): """Validate the document""" basepath = platform.get_platform().executable_path() path = os.path.abspath(os.path.join(basepath, "chirp.xsd")) if not os.path.exists(path): path = "/usr/share/chirp/chirp.xsd" try: ctx = libxml2.schemaNewParserCtxt(path) schema = ctx.schemaParse() except libxml2.parserError, e: LOG.error("Unable to load schema: %s" % e) LOG.error("Path: %s" % path) raise errors.RadioError("Unable to load schema")
def __init__(self): # set up the W3C schema parser self.error_handler = ErrorHandler() filepath = os.path.join(os.path.dirname(__file__), 'data', 'isotc211', 'gmx', 'gmx.xsd') ctxt_parser = libxml2.schemaNewParserCtxt(filepath) self.isotc211_schema = ctxt_parser.schemaParse() # set up the ISO TS 19139 A1 constraints schematron filepath = os.path.join(os.path.dirname(__file__), 'data', 'ISOTS19139A1Constraints_v1.3.xsl') doc = libxml2.parseFile(filepath) self.isots19139_xslt = libxslt.parseStylesheetDoc(doc) # set up the MEDIN Metadata Profile schematron filepath = os.path.join(os.path.dirname(__file__), 'data', 'MedinMetadataProfile_v1.8.xsl') doc = libxml2.parseFile(filepath) self.medin_xslt = libxslt.parseStylesheetDoc(doc)
def validate_xsd(xml_file, xsd_file): """Проверка xml файла на соответствие XML схеме""" doc = libxml2.parseFile(xml_file) ctxt = libxml2.schemaNewParserCtxt(xsd_file) schema = ctxt.schemaParse() del ctxt validation_ctxt = schema.schemaNewValidCtxt() validation_err = validation_ctxt.schemaValidateDoc(doc) del validation_ctxt del schema del doc if validation_err == 0: print "Документ " + xml_file + " соответствует " + xsd_file open(xml_file) else: print "Документ " + xml_file + " НЕ соответствует " + xsd_file
def validateXML(self, xmlToProcess): ctxtParser = libxml2.schemaNewParserCtxt(XSD) ctxtSchema = ctxtParser.schemaParse() ctxtValid = ctxtSchema.schemaNewValidCtxt() doc = libxml2.parseDoc(xmlToProcess) retVal = doc.schemaValidateDoc(ctxtValid) if( retVal != 0): self.logger.error("Error validating against XML Schema - "+XSD) sys.exit(RET_CRITICAL) doc.freeDoc() del ctxtParser del ctxtSchema del ctxtValid libxml2.schemaCleanupTypes() libxml2.cleanupParser()
def validateXML(self, xmlToProcess): ctxtParser = libxml2.schemaNewParserCtxt(XSD) ctxtSchema = ctxtParser.schemaParse() ctxtValid = ctxtSchema.schemaNewValidCtxt() doc = libxml2.parseDoc(xmlToProcess) retVal = doc.schemaValidateDoc(ctxtValid) if (retVal != 0): self.logger.error("Error validating against XML Schema - " + XSD) sys.exit(RET_CRITICAL) doc.freeDoc() del ctxtParser del ctxtSchema del ctxtValid libxml2.schemaCleanupTypes() libxml2.cleanupParser()
def validate_all_xml(dpaths, xsdfile=default_xsd): xmlschema_context = None if not has_libxml2: # Use lxml xmlschema_context = etree.parse(xsdfile) else: # Use libxml2 and prepare the schema validation context ctxt = libxml2.schemaNewParserCtxt(xsdfile) xmlschema_context = ctxt.schemaParse() del ctxt fpaths = [] for dp in dpaths: if dp.endswith('.xml') and isSConsXml(dp): path = '.' fpaths.append(dp) else: for path, dirs, files in os.walk(dp): for f in files: if f.endswith('.xml'): fp = os.path.join(path, f) if isSConsXml(fp): fpaths.append(fp) fails = [] fpaths = sorted(fpaths) for idx, fp in enumerate(fpaths): fpath = os.path.join(path, fp) print("%.2f%s (%d/%d) %s" % (float(idx + 1) * 100.0 / float(len(fpaths)), perc, idx + 1, len(fpaths), fp)) if not tf.validateXml(fp, xmlschema_context): fails.append(fp) continue if has_libxml2: # Cleanup del xmlschema_context if fails: return False return True
def validateProfile(profilePath=None): """ Validate profilePath against profile schema """ logger = TCSLogger.TCSLogger.getInstance() if XSD_PROFILE == "": return None try: ctxt = libxml2.schemaNewParserCtxt(XSD_PROFILE) schema = ctxt.schemaParse() validationCtxt = schema.schemaNewValidCtxt() doc = libxml2.parseFile(profilePath) instance_Err = validationCtxt.schemaValidateDoc(doc) doc.freeDoc() except Exception, err: logger.error(MODULE_NAME, str(err)) return False
def _validateStateFile(self): """Validate state file against its schema""" if not os.path.isfile(self.STATE_FILE): self._createNewStateFile() return True msg = "Using schema %s to verify state file" % (self.XSD_STATE_FILE) self.logger.debug(MODULE_NAME, msg) try: ctxt = libxml2.schemaNewParserCtxt(self.XSD_STATE_FILE) schema = ctxt.schemaParse() validationCtxt = schema.schemaNewValidCtxt() self.stateFileXmlDoc = libxml2.parseFile(self.STATE_FILE) instance_Err = validationCtxt.schemaValidateDoc(self.stateFileXmlDoc) except Exception, err: self.logger.error(MODULE_NAME, str(err)) return False
def validate_all_xml(dpaths, xsdfile=default_xsd): xmlschema_context = None if not has_libxml2: # Use lxml xmlschema_context = etree.parse(xsdfile) else: # Use libxml2 and prepare the schema validation context ctxt = libxml2.schemaNewParserCtxt(xsdfile) xmlschema_context = ctxt.schemaParse() del ctxt fpaths = [] for dp in dpaths: if dp.endswith('.xml') and isSConsXml(dp): path='.' fpaths.append(dp) else: for path, dirs, files in os.walk(dp): for f in files: if f.endswith('.xml'): fp = os.path.join(path, f) if isSConsXml(fp): fpaths.append(fp) fails = [] for idx, fp in enumerate(fpaths): fpath = os.path.join(path, fp) print "%.2f%s (%d/%d) %s" % (float(idx+1)*100.0/float(len(fpaths)), perc, idx+1, len(fpaths),fp) if not tf.validateXml(fp, xmlschema_context): fails.append(fp) continue if has_libxml2: # Cleanup del xmlschema_context if fails: return False return True
def validateProcessDefinition(self): """Validates document and returns it's language id on success. Exception is raised in any other case""" schemas = {"jpdl-3.1": "jpdl-3.1.xsd", "jpdl-3.2": "jpdl-3.2.xsd", "bpmn-2.0": "BPMN20.xsd"} # iterate through all schemas and try to validate XML definitionLang = None for lang in schemas: try: # load schema schema_parser_ctx = libxml2.schemaNewParserCtxt(os.path.join(self.scriptPath, "schemas", schemas[lang])) schema = schema_parser_ctx.schemaParse() valid_schema = schema.schemaNewValidCtxt() except libxml2.libxmlError, e: raise InvalidSchemaException("Schema for " + lang + " is invalid.") # validate if not self.xml.schemaValidateDoc(valid_schema): # xml validate, store result definitionLang = lang
def validateXSD(cls,xmlstring,target): """ Performs XSD validation on the data. @param xmlstring: XML containing the corpus @type xmlstring: string @param target: target format @type target: string @return: validity of the corpus @rtype: boolean """ printMessage(cls,inspect.stack()[0][3], "Validating against '%s' XSD.."%(target)) curdir = os.path.dirname(globals()['__file__']) if target=="new": xsd="%s/../bioinfer.xsd"%curdir elif target=="relaxed": xsd="%s/../bioinfer.relaxed.xsd"%curdir elif target=="compatible": xsd="%s/../bioinfer.relaxed.xsd"%curdir else: printError(cls,inspect.stack()[0][3],"Cannot validate '%s' format"%target) return(False) doc = L.parseDoc(xmlstring) schemaCtxt = L.schemaNewParserCtxt(xsd) schema = schemaCtxt.schemaParse() validatorCtxt = schema.schemaNewValidCtxt() exitstatus = validatorCtxt.schemaValidateDoc(doc) valid = (exitstatus==0) if valid: printMessage(cls,inspect.stack()[0][3],"Valid XML") else: printError(cls,inspect.stack()[0][3],"Invalid XML") return(valid)
schemaTag = None for tag in ['schemaLocation', 'noNamespaceSchemaLocation']: if schemaElement.hasProp(tag): schemaTag = schemaElement.prop(tag) break schemaTags = schemaTag.split(' ') spath = schemaTags[len(schemaTags) - 1] dpath = os.path.dirname(args.xml) if len(dpath) == 0: schema = spath else: schema = '%s/%s' % (dpath, spath) sys.stderr.write('schema=%s\n' % schema) ctxt = libxml2.schemaNewParserCtxt(schema) schema = ctxt.schemaParse() del ctxt validationCtxt = schema.schemaNewValidCtxt() #instance_Err = validationCtxt.schemaValidateFile(filePath, 0) instance_Err = validationCtxt.schemaValidateDoc(doc) del validationCtxt del schema doc.freeDoc() if instance_Err != 0: print "VALIDATION FAILED" else: