bool TasksetWriter::isValid(const xmlDocPtr doc) const { xmlDocPtr schema_doc = xmlReadFile(this->_schemafilename.c_str(), NULL, XML_PARSE_PEDANTIC); if (schema_doc == NULL) { // the schema cannot be loaded or is not well-formed return -1; } xmlSchemaParserCtxtPtr parser_ctxt = xmlSchemaNewDocParserCtxt(schema_doc); if (parser_ctxt == NULL) { // unable to create a parser context for the schema xmlFreeDoc(schema_doc); return -2; } xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt); if (schema == NULL) { // the schema itself is not valid xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(schema_doc); return -3; } xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema); if (valid_ctxt == NULL) { // unable to create a validation context for the schema xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(schema_doc); return -4; } int is_valid = (xmlSchemaValidateDoc(valid_ctxt, doc) == 0); xmlSchemaFreeValidCtxt(valid_ctxt); xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(schema_doc); // force the return value to be non-negative on success return is_valid ? 1 : 0; }
validity schema::validate(document& doc) const { // Check if the schema has been loaded. if (raw_ == 0) { throw bad_dom_operation("failed to validate XML document: schema not loaded"); } // Create a schema validation context from the schema. schema_valid_context valid_ctxt(raw_); if (valid_ctxt.get() == 0) { throw internal_dom_error("failed to create xmlSchemaValidCtxt from xmlSchema"); } // Set validation callback functions, and validate the XML document. validity result; xmlSchemaSetValidErrors( valid_ctxt.get(), &detail::on_validity_error, &detail::on_validity_warning, static_cast<void*>(&result) ); int ret_code = xmlSchemaValidateDoc(valid_ctxt.get(), doc.raw_doc()); // Check the return code: 0 if the document is schemas valid, a positive error code number // otherwise, -1 in case of internal or API error. if (ret_code >= 0) { return result; } else { throw internal_dom_error("fail to validate: xmlSchemaValidateDoc returned -1"); } }
osync_bool osync_xml_validate_document(xmlDocPtr doc, char *schemafilepath) { int rc = 0; xmlSchemaParserCtxtPtr xmlSchemaParserCtxt = NULL; xmlSchemaPtr xmlSchema = NULL; xmlSchemaValidCtxtPtr xmlSchemaValidCtxt = NULL; osync_assert(doc); osync_assert(schemafilepath); xmlSchemaParserCtxt = xmlSchemaNewParserCtxt(schemafilepath); xmlSchema = xmlSchemaParse(xmlSchemaParserCtxt); xmlSchemaFreeParserCtxt(xmlSchemaParserCtxt); xmlSchemaValidCtxt = xmlSchemaNewValidCtxt(xmlSchema); if (xmlSchemaValidCtxt == NULL) { xmlSchemaFree(xmlSchema); rc = 1; }else{ /* Validate the document */ rc = xmlSchemaValidateDoc(xmlSchemaValidCtxt, doc); xmlSchemaFree(xmlSchema); xmlSchemaFreeValidCtxt(xmlSchemaValidCtxt); } if(rc != 0) return FALSE; return TRUE; }
bool PluginXmlOptions::validateXml(xmlDocPtr doc, const char *schemaFile) { char *pluginDir = ADM_getPluginPath(); char schemaPath[strlen(pluginDir) + strlen(PLUGIN_SCHEMA_DIR) + 1 + strlen(schemaFile) + 1]; bool success = false; strcpy(schemaPath, pluginDir); strcat(schemaPath, PLUGIN_SCHEMA_DIR); strcat(schemaPath, "/"); strcat(schemaPath, schemaFile); delete [] pluginDir; xmlSchemaParserCtxtPtr xmlSchemaParserCtxt = xmlSchemaNewParserCtxt(schemaPath); xmlSchemaPtr xmlSchema = xmlSchemaParse(xmlSchemaParserCtxt); xmlSchemaFreeParserCtxt(xmlSchemaParserCtxt); xmlSchemaValidCtxtPtr xmlSchemaValidCtxt = xmlSchemaNewValidCtxt(xmlSchema); if (xmlSchemaValidCtxt) { success = !xmlSchemaValidateDoc(xmlSchemaValidCtxt, doc); xmlSchemaFree(xmlSchema); xmlSchemaFreeValidCtxt(xmlSchemaValidCtxt); } else xmlSchemaFree(xmlSchema); return success; }
/* * call-seq: * validate_document(document) * * Validate a Nokogiri::XML::Document against this Schema. */ static VALUE validate_document(VALUE self, VALUE document) { xmlDocPtr doc; xmlSchemaPtr schema; Data_Get_Struct(self, xmlSchema, schema); Data_Get_Struct(document, xmlDoc, doc); VALUE errors = rb_ary_new(); xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema); if(NULL == valid_ctxt) { // we have a problem rb_raise(rb_eRuntimeError, "Could not create a validation context"); } #ifdef HAVE_XMLSCHEMASETVALIDSTRUCTUREDERRORS xmlSchemaSetValidStructuredErrors( valid_ctxt, Nokogiri_error_array_pusher, (void *)errors ); #endif xmlSchemaValidateDoc(valid_ctxt, doc); xmlSchemaFreeValidCtxt(valid_ctxt); return errors; }
SEXP RS_XML_xmlSchemaValidateDoc(SEXP r_schema, SEXP r_doc, SEXP r_options, SEXP r_errorHandlers) { xmlSchemaValidCtxtPtr ctxt; xmlDocPtr doc = (xmlDocPtr) R_ExternalPtrAddr(r_doc); xmlSchemaPtr schema = (xmlSchemaPtr) R_ExternalPtrAddr(r_schema); // ctxt = (xmlSchemaValidCtxtPtr) R_ExternalPtrAddr(r_ctxt); int status; int numErrHandlers; ctxt = xmlSchemaNewValidCtxt(schema); if(LENGTH(r_options)) xmlSchemaSetValidOptions(ctxt, INTEGER(r_options)[0]); numErrHandlers = Rf_length(r_errorHandlers); if(numErrHandlers > 0) { R_SchemaValidCallback cbinfo; PROTECT(cbinfo.fun = allocVector(LANGSXP, 2)); SETCAR(cbinfo.fun, VECTOR_ELT(r_errorHandlers, 0)); xmlSchemaSetValidErrors(ctxt, (xmlSchemaValidityErrorFunc) R_schemaValidityErrorFunc, (xmlSchemaValidityWarningFunc) R_schemaValidityWarningFunc, &cbinfo); } status = xmlSchemaValidateDoc(ctxt, doc); xmlSchemaFreeValidCtxt(ctxt); /* R_alloc this if possible. */ if(numErrHandlers > 0) UNPROTECT(1); return(ScalarInteger(status)); }
//this schema validation code is taken from http://wiki.njh.eu/XML-Schema_validation_with_libxml2 this code int Parser::validateAgainstSchema(const xmlDocPtr &_doc,const xmlDocPtr &_schema ) { xmlSchemaParserCtxtPtr parser_ctxt = xmlSchemaNewDocParserCtxt(_schema); if (parser_ctxt == NULL) { /* unable to create a parser context for the schema */ xmlFreeDoc(_schema); return -1; } xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt); if (schema == NULL) { /* the schema itself is not valid */ xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(_schema); return -2; } xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema); if (valid_ctxt == NULL) { /* unable to create a validation context for the schema */ xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(_schema); return -3; } int is_valid = (xmlSchemaValidateDoc(valid_ctxt, _doc) == 0); xmlSchemaFreeValidCtxt(valid_ctxt); xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); /* force the return value to be non-negative on success */ return is_valid ? 1 : 0; }
/* * call-seq: * document.validate_schema(schema) -> (true|false) * * Validate this document against the specified XML::Schema. * * If a block is provided it is used as an error handler for validaten errors. * The block is called with two argument, the message and a flag indication * if the message is an error (true) or a warning (false). */ static VALUE rxml_document_validate_schema(VALUE self, VALUE schema) { xmlSchemaValidCtxtPtr vptr; xmlDocPtr xdoc; xmlSchemaPtr xschema; int is_invalid; Data_Get_Struct(self, xmlDoc, xdoc); Data_Get_Struct(schema, xmlSchema, xschema); vptr = xmlSchemaNewValidCtxt(xschema); xmlSchemaSetValidErrors(vptr, (xmlSchemaValidityErrorFunc) LibXML_validity_error, (xmlSchemaValidityWarningFunc) LibXML_validity_warning, NULL); is_invalid = xmlSchemaValidateDoc(vptr, xdoc); xmlSchemaFreeValidCtxt(vptr); if (is_invalid) { rxml_raise(&xmlLastError); return Qfalse; } else { return Qtrue; } }
int xml_validate(struct xml_node_ctx *ctx, xml_node_t *node, const char *xml_schema_fname, char **ret_err) { xmlDocPtr doc; xmlNodePtr n; xmlSchemaParserCtxtPtr pctx; xmlSchemaValidCtxtPtr vctx; xmlSchemaPtr schema; int ret; struct str_buf errors; if (ret_err) *ret_err = NULL; doc = xmlNewDoc((xmlChar *) "1.0"); if (doc == NULL) return -1; n = xmlDocCopyNode((xmlNodePtr) node, doc, 1); if (n == NULL) { xmlFreeDoc(doc); return -1; } xmlDocSetRootElement(doc, n); os_memset(&errors, 0, sizeof(errors)); pctx = xmlSchemaNewParserCtxt(xml_schema_fname); xmlSchemaSetParserErrors(pctx, (xmlSchemaValidityErrorFunc) add_str, (xmlSchemaValidityWarningFunc) add_str, &errors); schema = xmlSchemaParse(pctx); xmlSchemaFreeParserCtxt(pctx); vctx = xmlSchemaNewValidCtxt(schema); xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc) add_str, (xmlSchemaValidityWarningFunc) add_str, &errors); ret = xmlSchemaValidateDoc(vctx, doc); xmlSchemaFreeValidCtxt(vctx); xmlFreeDoc(doc); xmlSchemaFree(schema); if (ret == 0) { os_free(errors.buf); return 0; } else if (ret > 0) { if (ret_err) *ret_err = errors.buf; else os_free(errors.buf); return -1; } else { if (ret_err) *ret_err = errors.buf; else os_free(errors.buf); return -1; } }
valid_status validate_doc(void *pool, xmlSchemaPtr schema, xmlDocPtr doc, qhead *err_list) { xmlSchemaValidCtxtPtr svctx; /* schema validator context */ valerr_ctx ctx; /* context used for validator's error hook */ int rc; /* return code from xmllib's validator */ svctx = xmlSchemaNewValidCtxt(schema); if (svctx == NULL) { return VAL_EINTERNAL; } /* initialize error hook's context */ ctx.err_list = err_list; ctx.doc = doc; ctx.pool = pool; xmlSchemaSetValidStructuredErrors(svctx, validerr_callback, &ctx); /* validate request against schema */ rc = xmlSchemaValidateDoc(svctx, doc); if (rc < 0) { /* -1 is validator's internal error */ xmlSchemaFreeValidCtxt(svctx); return VAL_EINTERNAL; } if (rc > 0) { /* the doc does not validate */ xmlSchemaFreeValidCtxt(svctx); return VAL_NOT_VALID; } xmlSchemaFreeValidCtxt(svctx); return VAL_OK; }
/* * Valid an xml string against an XML schema * Inpired from: http://xml.developpez.com/sources/?page=validation#validate_XSD_CppCLI_2 * taken from tinyows.org */ int msOWSSchemaValidation(const char* xml_schema, const char* xml) { xmlSchemaPtr schema; xmlSchemaParserCtxtPtr ctxt; xmlSchemaValidCtxtPtr validctxt; int ret; xmlDocPtr doc; if (!xml_schema || !xml) return MS_FAILURE; xmlInitParser(); schema = NULL; ret = -1; /* Open XML Schema File */ ctxt = xmlSchemaNewParserCtxt(xml_schema); /* else ctxt = xmlSchemaNewMemParserCtxt(xml_schema); */ /* xmlSchemaSetParserErrors(ctxt, (xmlSchemaValidityErrorFunc) libxml2_callback, (xmlSchemaValidityWarningFunc) libxml2_callback, stderr); */ schema = xmlSchemaParse(ctxt); xmlSchemaFreeParserCtxt(ctxt); /* If XML Schema hasn't been rightly loaded */ if (schema == NULL) { xmlSchemaCleanupTypes(); xmlMemoryDump(); xmlCleanupParser(); return ret; } doc = xmlParseDoc((xmlChar *)xml); if (doc != NULL) { /* Loading XML Schema content */ validctxt = xmlSchemaNewValidCtxt(schema); /* xmlSchemaSetValidErrors(validctxt, (xmlSchemaValidityErrorFunc) libxml2_callback, (xmlSchemaValidityWarningFunc) libxml2_callback, stderr); */ /* validation */ ret = xmlSchemaValidateDoc(validctxt, doc); xmlSchemaFreeValidCtxt(validctxt); } xmlSchemaFree(schema); xmlFreeDoc(doc); xmlCleanupParser(); return ret; }
HRESULT SchemaCache_validate_tree(IXMLDOMSchemaCollection2* iface, xmlNodePtr tree) { schema_cache* This = impl_from_IXMLDOMSchemaCollection2(iface); cache_entry* entry; xmlChar const* ns = NULL; TRACE("(%p, %p)\n", This, tree); if (!tree) return E_POINTER; if ((xmlNodePtr)tree->doc == tree) { xmlNodePtr root = xmlDocGetRootElement(tree->doc); if (root && root->ns) ns = root->ns->href; } else if (tree->ns) { ns = tree->ns->href; } entry = xmlHashLookup(This->cache, ns); /* TODO: if the ns is not in the cache, and it's a URL, * do we try to load from that? */ if (entry) { if (entry->type == SCHEMA_TYPE_XDR) { FIXME("partial stub: XDR schema support not implemented\n"); return S_OK; } else if (entry->type == SCHEMA_TYPE_XSD) { xmlSchemaValidCtxtPtr svctx; int err; /* TODO: if validateOnLoad property is false, * we probably need to validate the schema here. */ svctx = xmlSchemaNewValidCtxt(entry->schema); xmlSchemaSetValidErrors(svctx, validate_error, validate_warning, NULL); #ifdef HAVE_XMLSCHEMASSETVALIDSTRUCTUREDERRORS xmlSchemaSetValidStructuredErrors(svctx, validate_serror, NULL); #endif if ((xmlNodePtr)tree->doc == tree) err = xmlSchemaValidateDoc(svctx, (xmlDocPtr)tree); else err = xmlSchemaValidateOneElement(svctx, tree); xmlSchemaFreeValidCtxt(svctx); return err? S_FALSE : S_OK; } } return E_FAIL; }
void MainWindow::on_actionCheckSchema_triggered() { if ((0 == schema_valid_ctxt_) || (0 == xml_doc_ptr_)) return; if (0 == xmlSchemaValidateDoc(schema_valid_ctxt_, xml_doc_ptr_)) { QMessageBox::information(this, tr("Проверка схемы XML"), tr("Структура документа корректна"), QMessageBox::Ok); } else { QMessageBox::critical(this, tr("Проверка схемы XML"), tr("Структура документа не соответствует схеме"), QMessageBox::Ok); } }
/** * Parses the user data XML and copies data into a new ims_subscription structure. * @param xml - the input xml * @returns the ims_subscription* on success or NULL on error */ ims_subscription *parse_user_data(str xml) { xmlDocPtr doc=0; xmlNodePtr root=0; char c; ims_subscription *s; if (!ctxtInit) parser_init(scscf_user_data_dtd,scscf_user_data_xsd); doc=0; c = xml.s[xml.len]; xml.s[xml.len]=0; doc = xmlParseDoc((unsigned char *)xml.s); if (!doc){ LOG(L_ERR,"ERR:"M_NAME":parse_user_data: This is not a valid XML <%.*s>\n", xml.len,xml.s); goto error; } if (dtdCtxt){ if (xmlValidateDtd(dtdCtxt,doc,dtd)!=1){ LOG(L_ERR,"ERR:"M_NAME":parse_user_data: Verification of XML against DTD failed <%.*s>\n", xml.len,xml.s); goto error; } } if (xsdCtxt){ if (xmlSchemaValidateDoc(xsdCtxt,doc)!=0){ LOG(L_ERR,"ERR:"M_NAME":parse_user_data: Verification of XML against XSD failed <%.*s>\n", xml.len,xml.s); goto error; } } root = xmlDocGetRootElement(doc); if (!root){ LOG(L_ERR,"ERR:"M_NAME":parse_user_data: Empty XML <%.*s>\n", xml.len,xml.s); goto error; } s = parse_ims_subscription(doc,root); if (!s){ LOG(L_ERR,"ERR:"M_NAME":parse_user_data: Error while loading into ims subscription structure\n"); goto error; } xmlFreeDoc(doc); // print_user_data(L_CRIT,s); return s; error: if (doc) xmlFreeDoc(doc); xml.s[xml.len]=c; return 0; }
bool xml_validate(bool *pvalid, xmlDocPtr doc, const char *schema_path) { bool result = false; xmlDocPtr schema_doc = NULL; xmlSchemaParserCtxtPtr schema_parser_ctxt = NULL; xmlSchemaPtr schema = NULL; xmlSchemaValidCtxtPtr schema_valid_ctxt = NULL; int valid_rc; schema_doc = xmlReadFile(schema_path, NULL, XML_PARSE_NONET); if (schema_doc == NULL) goto cleanup; schema_parser_ctxt = xmlSchemaNewDocParserCtxt(schema_doc); if (schema_parser_ctxt == NULL) goto cleanup; schema = xmlSchemaParse(schema_parser_ctxt); if (schema == NULL) goto cleanup; schema_valid_ctxt = xmlSchemaNewValidCtxt(schema); if (schema_valid_ctxt == NULL) goto cleanup; valid_rc = xmlSchemaValidateDoc(schema_valid_ctxt, doc); if (valid_rc < 0) goto cleanup; if (pvalid != NULL) *pvalid = (valid_rc == 0); result = true; cleanup: xmlSchemaFreeValidCtxt(schema_valid_ctxt); xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(schema_parser_ctxt); if (schema_doc != NULL) xmlFreeDoc(schema_doc); return result; }
/******************* FUNCTION *********************/ void CMRXmlDoc::validateWithSchema ( const string& xsltFile ) { /* vars */ bool status = true; xmlSchemaPtr schema; xmlSchemaValidCtxtPtr vctxt; xmlSchemaParserCtxtPtr pctxt; /* errors */ assert(doc != NULL); assert(rootNode != NULL); assert(xsltFile.empty() == false); /* Nothing to do */ if (rootNode == NULL || xsltFile.empty()) return; /* Open XML schema file */ pctxt = xmlSchemaNewParserCtxt(xsltFile.c_str()); assert(pctxt != NULL); //assume_m(pctxt != NULL,"Fail to open XML schema file to validate config : %s.",xml_shema_path); /* Parse the schema */ schema = xmlSchemaParse(pctxt); xmlSchemaFreeParserCtxt(pctxt); assert(schema != NULL); //assume_m(schema != NULL,"Fail to parse the XML schema file to validate config : %s.",xml_shema_path); /* Create validation context */ if ((vctxt = xmlSchemaNewValidCtxt(schema)) == NULL) { assert(vctxt != NULL); //sctk_fatal("Fail to create validation context from XML schema file : %s.",xml_shema_path); } /* Create validation output system */ xmlSchemaSetValidErrors(vctxt, (xmlSchemaValidityErrorFunc) fprintf, (xmlSchemaValidityWarningFunc) fprintf, stderr); /* Validation */ status = (xmlSchemaValidateDoc(vctxt, doc) == 0); /* Free the schema */ xmlSchemaFree(schema); xmlSchemaFreeValidCtxt(vctxt); if (!status) throw LatexException("XML file is invalid."); }
void validateDoc(xmlDocPtr doc, std::string baseUri, std::string schemaFile) { xmlSchemaParserCtxtPtr ctx = xmlSchemaNewParserCtxt(schemaFile.c_str()); if (ctx) { xmlSchemaSetParserErrors(ctx,(xmlSchemaValidityErrorFunc)errorHandler,(xmlSchemaValidityWarningFunc)warningHandler,nullptr); xmlSchemaPtr schema = xmlSchemaParse(ctx); xmlSchemaFreeParserCtxt(ctx); // validate xmlSchemaValidCtxtPtr vctx = xmlSchemaNewValidCtxt(schema); xmlSchemaSetValidErrors(vctx, (xmlSchemaValidityErrorFunc)errorHandler, (xmlSchemaValidityErrorFunc)warningHandler,nullptr); int ret = xmlSchemaValidateDoc(vctx, doc); xmlSchemaFreeValidCtxt(vctx); xmlSchemaFree(schema); if (ret != 0) { THROW_EXC("xml file '" << baseUri << "' is not valid"); } } }
ReturnCode validateSchema(const TixiDocumentHandle handle, xmlDocPtr* schema_doc) { TixiDocument* document = getDocument(handle); xmlSchemaParserCtxtPtr parser_ctxt; xmlSchemaPtr schema; xmlSchemaValidCtxtPtr valid_ctxt; int is_valid; if (*schema_doc == NULL) { /* the schema cannot be loaded or is not well-formed */ return OPEN_SCHEMA_FAILED; } parser_ctxt = xmlSchemaNewDocParserCtxt(*schema_doc); if (parser_ctxt == NULL) { printMsg(MESSAGETYPE_ERROR, "Error: validateSchema: unable to create a parser context for the schema.\n"); xmlFreeDoc(*schema_doc); return FAILED; } schema = xmlSchemaParse(parser_ctxt); if (schema == NULL) { printMsg(MESSAGETYPE_ERROR, "Error: validateSchema: the schema itself is not valid.\n"); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(*schema_doc); return FAILED; } valid_ctxt = xmlSchemaNewValidCtxt(schema); if (valid_ctxt == NULL) { printMsg(MESSAGETYPE_ERROR, "Error: validateSchema: unable to create a validation context for the schema.\n"); xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(*schema_doc); return FAILED; } is_valid = (xmlSchemaValidateDoc(valid_ctxt, document->docPtr) == 0); xmlSchemaFreeValidCtxt(valid_ctxt); xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(*schema_doc); if (is_valid) { return SUCCESS; } else { return NOT_SCHEMA_COMPLIANT; } }
/* validate manifest file */ int ermXmlValidateManifest(erManifest *pCtx) { int retVal = RET_ERR; xmlSchemaParserCtxtPtr parserCtxtPtr = NULL; xmlSchemaValidCtxtPtr validCtxPtr = NULL; xmlSchemaPtr schema = NULL; int result; if (NULL == pCtx->pDoc) { TRACE("pCtx->pDoc pointer is empty!\n"); return retVal; } // get XML schema parserCtxtPtr = xmlSchemaNewParserCtxt(MANIFEST_SCHEMA_FILE); if (parserCtxtPtr) { schema = xmlSchemaParse(parserCtxtPtr); if (schema) { validCtxPtr = xmlSchemaNewValidCtxt(schema); if (validCtxPtr) { if (xmlSchemaIsValid(validCtxPtr)) { // validate XML document result = xmlSchemaValidateDoc(validCtxPtr, pCtx->pDoc); TRACE("ValidateDoc returns [%d] (0 == OK)\n", result); if (result == 0) { retVal = RET_OK; } } xmlSchemaFreeValidCtxt(validCtxPtr); } xmlSchemaFree(schema); } } xmlSchemaFreeParserCtxt(parserCtxtPtr); return retVal; }
/*-----------------------------------------------------------------------------------------------------------------------*/ static FIXErrCode xml_validate(xmlDoc* doc, FIXError** error) { xmlSchemaParserCtxtPtr pctx = xmlSchemaNewMemParserCtxt(fix_xsd, strlen(fix_xsd)); xmlSchemaPtr schema = xmlSchemaParse(pctx); if (!schema) { return FIX_FAILED; } xmlSchemaValidCtxtPtr validCtx = xmlSchemaNewValidCtxt(schema); xmlSchemaSetValidErrors(validCtx, &xmlErrorHandler, &xmlErrorHandler, error); int32_t res = xmlSchemaValidateDoc(validCtx, doc); xmlSchemaFreeValidCtxt(validCtx); xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(pctx); return res ? FIX_FAILED : FIX_SUCCESS; }
int xml2lpc_validate(xml2lpc_context *xmlCtx) { xmlSchemaValidCtxtPtr validCtx; xmlSchemaParserCtxtPtr parserCtx; int ret; xml2lpc_context_clear_logs(xmlCtx); parserCtx = xmlSchemaNewDocParserCtxt(xmlCtx->xsd); validCtx = xmlSchemaNewValidCtxt(xmlSchemaParse(parserCtx)); xmlSchemaSetValidErrors(validCtx, xml2lpc_genericxml_error, xml2lpc_genericxml_warning, xmlCtx); ret = xmlSchemaValidateDoc(validCtx, xmlCtx->doc); if(ret > 0) { if(strlen(xmlCtx->warningBuffer) > 0) xml2lpc_log(xmlCtx, XML2LPC_WARNING, "%s", xmlCtx->warningBuffer); if(strlen(xmlCtx->errorBuffer) > 0) xml2lpc_log(xmlCtx, XML2LPC_ERROR, "%s", xmlCtx->errorBuffer); } else if(ret < 0) { xml2lpc_log(xmlCtx, XML2LPC_ERROR, "Internal error"); } xmlSchemaFreeValidCtxt(validCtx); return ret; }
/* This code is from http://wiki.njh.eu/XML-Schema_validation_with_libxml2 */ static int is_valid(const xmlDocPtr doc, const char *schema_filename) { xmlDocPtr schema_doc = xmlReadFile(schema_filename, NULL, XML_PARSE_NONET); if (schema_doc == NULL) { /* the schema cannot be loaded or is not well-formed */ fprintf(stderr, "Error: Unable to load the schema file \"%s\"\n", schema_filename); exit(1); } xmlSchemaParserCtxtPtr parser_ctxt = xmlSchemaNewDocParserCtxt( schema_doc); if (parser_ctxt == NULL) { /* unable to create a parser context for the schema */ xmlFreeDoc(schema_doc); return -2; } xmlSchemaPtr schema = xmlSchemaParse(parser_ctxt); if (schema == NULL) { /* the schema itself is not valid */ xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(schema_doc); return -3; } xmlSchemaValidCtxtPtr valid_ctxt = xmlSchemaNewValidCtxt(schema); if (valid_ctxt == NULL) { /* unable to create a validation context for the schema */ xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(schema_doc); return -4; } int is_valid = (xmlSchemaValidateDoc(valid_ctxt, doc) == 0); xmlSchemaFreeValidCtxt(valid_ctxt); xmlSchemaFree(schema); xmlSchemaFreeParserCtxt(parser_ctxt); xmlFreeDoc(schema_doc); /* force the return value to be non-negative on success */ return is_valid ? 1 : 0; }
static inline HRESULT Schema_validate_tree(xmlSchemaPtr schema, xmlNodePtr tree) { xmlSchemaValidCtxtPtr svctx; int err; TRACE("(%p, %p)\n", schema, tree); /* TODO: if validateOnLoad property is false, * we probably need to validate the schema here. */ svctx = xmlSchemaNewValidCtxt(schema); xmlSchemaSetValidErrors(svctx, validate_error, validate_warning, NULL); #ifdef HAVE_XMLSCHEMASSETVALIDSTRUCTUREDERRORS xmlSchemaSetValidStructuredErrors(svctx, validate_serror, NULL); #endif if (tree->type == XML_DOCUMENT_NODE) err = xmlSchemaValidateDoc(svctx, (xmlDocPtr)tree); else err = xmlSchemaValidateOneElement(svctx, tree); xmlSchemaFreeValidCtxt(svctx); return err? S_FALSE : S_OK; }
bool XMLSchema::validate(xmlDocPtr doc, const std::string& xsd) { xmlParserOption parserOption(XML_PARSE_NONET); xmlDocPtr schemaDoc = xmlReadMemory(xsd.c_str(), xsd.size(), NULL, NULL, parserOption); xmlSchemaParserCtxtPtr parserCtxt = xmlSchemaNewDocParserCtxt(schemaDoc); xmlSchemaSetParserStructuredErrors(parserCtxt, &OCISchemaParserStructuredErrorHandler, m_global_context); xmlSchemaPtr schema = xmlSchemaParse(parserCtxt); xmlSchemaValidCtxtPtr validCtxt = xmlSchemaNewValidCtxt(schema); xmlSchemaSetValidErrors(validCtxt, &OCISchemaValidityError, &OCISchemaValidityDebug, m_global_context); bool valid = (xmlSchemaValidateDoc(validCtxt, doc) == 0); xmlFreeDoc(schemaDoc); xmlSchemaFreeParserCtxt(parserCtxt); xmlSchemaFree(schema); xmlSchemaFreeValidCtxt(validCtxt); return valid; }
/* * call-seq: * document.validate_schema(schema) * * Validate this document against the specified XML::Schema. * If the document is valid the method returns true. Otherwise an * exception is raised with validation information. */ static VALUE rxml_document_validate_schema(VALUE self, VALUE schema) { xmlSchemaValidCtxtPtr vptr; xmlDocPtr xdoc; xmlSchemaPtr xschema; int is_invalid; Data_Get_Struct(self, xmlDoc, xdoc); Data_Get_Struct(schema, xmlSchema, xschema); vptr = xmlSchemaNewValidCtxt(xschema); is_invalid = xmlSchemaValidateDoc(vptr, xdoc); xmlSchemaFreeValidCtxt(vptr); if (is_invalid) { rxml_raise(&xmlLastError); return Qfalse; } else { return Qtrue; } }
void XMLParser::parse(const string& sXML, const string& sXMLName) { if (m_Doc) { xmlFreeDoc(m_Doc); } m_Doc = xmlParseMemory(sXML.c_str(), int(sXML.length())); checkError(!m_Doc, sXMLName); bool bOK = true; if (m_SchemaValidCtxt) { int err = xmlSchemaValidateDoc(m_SchemaValidCtxt, m_Doc); AVG_ASSERT(err != -1); bOK = (err == 0); } if (m_DTD) { int err = xmlValidateDtd(m_DTDValidCtxt, m_Doc, m_DTD); bOK = (err != 0); } if (!bOK) { xmlFreeDoc(m_Doc); m_Doc = 0; checkError(true, sXMLName); } }
void XMLParser::validateDoc (xmlDocPtr xmlDoc) throw (::fwTools::Failed) { SLM_ASSERT("xmlDoc not instanced", xmlDoc); SLM_DEBUG( "checking schema ...." ); xmlSchemaValidCtxtPtr validationCtxt = xmlSchemaNewValidCtxt(NULL); if (validationCtxt == NULL) { throw ::fwTools::Failed (std::string ("Failed to create the validation context")); } //validationCtxt ::boost::uint32_t ui32Res = xmlSchemaValidateDoc (validationCtxt, xmlDoc); xmlSchemaFreeValidCtxt(validationCtxt); if (ui32Res > 0) { throw ::fwTools::Failed (std::string ("XML file not valid against the XML schema")); } else if (ui32Res == std::numeric_limits<boost::uint32_t> ::max() ) { throw ::fwTools::Failed (std::string ("XML validation internal error")); } }
static inline int oscap_validate_xml(struct oscap_source *source, const char *schemafile, xml_reporter reporter, void *arg) { int result = -1; xmlSchemaParserCtxtPtr parser_ctxt = NULL; xmlSchemaPtr schema = NULL; xmlSchemaValidCtxtPtr ctxt = NULL; xmlDocPtr doc = NULL; struct ctxt context = { reporter, arg, (void*) oscap_source_readable_origin(source)}; if (schemafile == NULL) { oscap_seterr(OSCAP_EFAMILY_OSCAP, "'schemafile' == NULL"); return -1; } char * schemapath = oscap_sprintf("%s%s%s", oscap_path_to_schemas(), "/", schemafile); if (access(schemapath, R_OK)) { oscap_seterr(OSCAP_EFAMILY_OSCAP, "Schema file '%s' not found in path '%s' when trying to validate '%s'", schemafile, oscap_path_to_schemas(), oscap_source_readable_origin(source)); goto cleanup; } parser_ctxt = xmlSchemaNewParserCtxt(schemapath); if (parser_ctxt == NULL) { oscap_seterr(OSCAP_EFAMILY_XML, "Could not create parser context for validation"); goto cleanup; } xmlSchemaSetParserStructuredErrors(parser_ctxt, oscap_xml_validity_handler, &context); schema = xmlSchemaParse(parser_ctxt); if (schema == NULL) { oscap_seterr(OSCAP_EFAMILY_XML, "Could not parse XML schema"); goto cleanup; } ctxt = xmlSchemaNewValidCtxt(schema); if (ctxt == NULL) { oscap_seterr(OSCAP_EFAMILY_XML, "Could not create validation context"); goto cleanup; } xmlSchemaSetValidStructuredErrors(ctxt, oscap_xml_validity_handler, &context); doc = oscap_source_get_xmlDoc(source); if (!doc) goto cleanup; result = xmlSchemaValidateDoc(ctxt, doc); /* * xmlSchemaValidateFile() returns "-1" if document is not well formed * thefore we ignore libxml internal errors here and map return code to * either pass or fail. */ if (result != 0) result = 1; /* This would be nicer * if (result == -1) * oscap_setxmlerr(xmlGetLastError()); */ cleanup: if (ctxt) xmlSchemaFreeValidCtxt(ctxt); if (schema) xmlSchemaFree(schema); if (parser_ctxt) xmlSchemaFreeParserCtxt(parser_ctxt); oscap_free(schemapath); return result; }
/* ************************************************** */ int do_configuration(void) { xmlSchemaValidCtxtPtr sv_ctxt = NULL; xmlSchemaParserCtxtPtr sp_ctxt = NULL; xmlSchemaPtr schema = NULL; xmlParserCtxtPtr p_ctxt = NULL; xmlDocPtr doc = NULL; xmlXPathContextPtr xp_ctx = NULL; xmlXPathObjectPtr simul_xobj = NULL; xmlXPathObjectPtr entity_xobj = NULL; xmlXPathObjectPtr environment_xobj = NULL; xmlXPathObjectPtr bundle_xobj = NULL; xmlXPathObjectPtr node_xobj = NULL; xmlNodeSetPtr nodeset; xmlpathobj_t xpathobj[] = {{&simul_xobj, (xmlChar *) XML_X_SIMULATION}, {&entity_xobj, (xmlChar *) XML_X_ENTITY}, {&environment_xobj, (xmlChar *) XML_X_ENVIRONMENT}, {&bundle_xobj, (xmlChar *) XML_X_BUNDLE}, {&node_xobj, (xmlChar *) XML_X_NODE}}; int ok = 0, i; /* Check XML version */ LIBXML_TEST_VERSION; /* Initialise and parse schema */ sp_ctxt = xmlSchemaNewParserCtxt(schemafile); if (sp_ctxt == NULL) { fprintf(stderr, "config: XML schema parser initialisation failure (do_configuration())\n"); ok = -1; goto cleanup; } xmlSchemaSetParserErrors(sp_ctxt, (xmlSchemaValidityErrorFunc) xml_error, (xmlSchemaValidityWarningFunc) xml_warning, NULL); schema = xmlSchemaParse(sp_ctxt); if (schema == NULL) { fprintf(stderr, "config: error in schema %s (do_configuration())\n", schemafile); ok = -1; goto cleanup; } xmlSchemaSetValidErrors(sv_ctxt, (xmlSchemaValidityErrorFunc) xml_error, (xmlSchemaValidityWarningFunc) xml_warning, NULL); sv_ctxt = xmlSchemaNewValidCtxt(schema); if (sv_ctxt == NULL) { fprintf(stderr, "config: XML schema validator initialisation failure (do_configuration())\n"); ok = -1; goto cleanup; } /* Initialise and parse document */ p_ctxt = xmlNewParserCtxt(); if (p_ctxt == NULL) { fprintf(stderr, "config: XML parser initialisation failure (do_configuration())\n"); ok = -1; goto cleanup; } doc = xmlCtxtReadFile(p_ctxt, configfile, NULL, XML_PARSE_NONET | XML_PARSE_NOBLANKS | XML_PARSE_NSCLEAN); if (doc == NULL) { fprintf(stderr, "config: failed to parse %s (do_configuration())\n", configfile); ok = -1; goto cleanup; } /* Validate document */ if (xmlSchemaValidateDoc(sv_ctxt, doc)) { fprintf(stderr, "config: error in configuration file %s (do_configuration())\n", configfile); ok = -1; goto cleanup; } /* Create xpath context */ xp_ctx = xmlXPathNewContext(doc); if (xp_ctx == NULL) { fprintf(stderr, "config: XPath initialisation failure (do_configuration())\n"); ok = -1; goto cleanup; } xmlXPathRegisterNs(xp_ctx, (xmlChar *) XML_NS_ID, (xmlChar *) XML_NS_URL); /* Get xpath obj */ for (i = 0 ; i < (int) (sizeof(xpathobj) / sizeof(xpathobj[0])); i++) { *xpathobj[i].ptr = xmlXPathEvalExpression(xpathobj[i].expr, xp_ctx); if (*xpathobj[i].ptr == NULL) { fprintf(stderr, "config: unable to evaluate xpath \"%s\" (do_configuration())\n", xpathobj[i].expr); ok = -1; goto cleanup; } } /***************/ /* Counting... */ /***************/ nodeset = entity_xobj->nodesetval; if ((entities.size = (nodeset) ? nodeset->nodeNr : 0) == 0) { fprintf(stderr, "config: no entity defined (do_configuration())\n"); ok = -1; goto cleanup; } fprintf(stderr, "\nFound %d entities...\n", entities.size); nodeset = environment_xobj->nodesetval; if (((nodeset) ? nodeset->nodeNr : 0) == 0) { fprintf(stderr, "config: no environment defined (do_configuration())\n"); ok = -1; goto cleanup; } fprintf(stderr, "Found 1 environment...\n"); nodeset = bundle_xobj->nodesetval; if ((bundles.size = (nodeset) ? nodeset->nodeNr : 0) == 0) { fprintf(stderr, "config: no bundle defined (do_configuration())\n"); ok = -1; goto cleanup; } fprintf(stderr, "Found %d bundles...\n", bundles.size); if ((dflt_params = das_create()) == NULL) { ok = -1; goto cleanup; } /**************/ /* Simulation */ /**************/ if (parse_simulation(simul_xobj->nodesetval)) { ok = -1; goto cleanup; } /**********/ /* Entity */ /**********/ /* initialize library paths */ config_set_usr_modulesdir(); user_path_list = g_strsplit(user_modulesdir, ":", 0); /* TOCLEAN */ sys_path_list = g_strsplit(sys_modulesdir, ":", 0); /* TOCLEAN */ /* parse */ if (parse_entities(entity_xobj->nodesetval)) { ok = -1; goto cleanup; } /**************/ /* Measure */ /**************/ if (parse_measure()) { ok = -1; goto cleanup; } /***************/ /* Environment */ /***************/ if (parse_environment(environment_xobj->nodesetval)) { ok = -1; goto cleanup; } /***************/ /* Bundle */ /***************/ if (parse_bundles(bundle_xobj->nodesetval)) { ok = -1; goto cleanup; } /***************/ /* Nodes */ /***************/ if (parse_nodes(node_xobj->nodesetval)) { ok = -1; goto cleanup; } /* edit by Quentin Lampin <*****@*****.**> */ gchar **path = NULL; for (path = user_path_list ; *path ; path++) { g_free(*path); } path = NULL; for (path = sys_path_list ; *path ; path++) { g_free(*path); } /* end of edition */ cleanup: clean_params(); for (i = 0 ; i < (int) (sizeof(xpathobj) / sizeof(xpathobj[0])); i++) { xmlXPathFreeObject(*xpathobj[i].ptr); } if (xp_ctx) { xmlXPathFreeContext(xp_ctx); } if (sp_ctxt) { xmlSchemaFreeParserCtxt(sp_ctxt); } if (schema) { xmlSchemaFree(schema); } if (sv_ctxt) { xmlSchemaFreeValidCtxt(sv_ctxt); } if (doc) { xmlFreeDoc(doc); } if (p_ctxt) { xmlFreeParserCtxt(p_ctxt); } xmlCleanupParser(); return ok; }
static xmlDocPtr parse_arguments(const char *data, gsize len, GError **err) { xmlDocPtr schema_doc = NULL; xmlSchemaParserCtxtPtr schema_parser = NULL; xmlSchemaPtr schema = NULL; xmlSchemaValidCtxtPtr validator = NULL; xmlDocPtr doc = NULL; xmlDocPtr ret = NULL; /* Read schema */ schema_doc = xmlReadFile(VMNETFS_SCHEMA_PATH, NULL, 0); if (schema_doc == NULL) { g_set_error(err, VMNETFS_CONFIG_ERROR, VMNETFS_CONFIG_ERROR_INVALID_SCHEMA, "Couldn't parse XML schema document"); goto out; } /* Load schema */ schema_parser = xmlSchemaNewDocParserCtxt(schema_doc); g_assert(schema_parser); schema = xmlSchemaParse(schema_parser); if (schema == NULL) { g_set_error(err, VMNETFS_CONFIG_ERROR, VMNETFS_CONFIG_ERROR_INVALID_SCHEMA, "Couldn't parse XML schema"); goto out; } validator = xmlSchemaNewValidCtxt(schema); g_assert(validator); /* Parse XML document */ doc = xmlReadMemory(data, len, NULL, NULL, 0); if (doc == NULL) { g_set_error(err, VMNETFS_CONFIG_ERROR, VMNETFS_CONFIG_ERROR_INVALID_CONFIG, "Couldn't parse XML document"); goto out; } /* Validate XML document */ if (xmlSchemaValidateDoc(validator, doc)) { g_set_error(err, VMNETFS_CONFIG_ERROR, VMNETFS_CONFIG_ERROR_INVALID_CONFIG, "Config XML did not validate"); goto out; } ret = doc; doc = NULL; out: if (doc) { xmlFreeDoc(doc); } if (validator) { xmlSchemaFreeValidCtxt(validator); } if (schema) { xmlSchemaFree(schema); } if (schema_parser) { xmlSchemaFreeParserCtxt(schema_parser); } if (schema_doc) { xmlFreeDoc(schema_doc); } return ret; }