void MainWindow::on_actionLoadSchema_triggered() { FreeXMLSchema(); ui_->actionCheckSchema->setEnabled(false); QString file_name = QFileDialog::getOpenFileName(this, tr("Выберите XML-схему"), "..", tr("XSD-файлы (*.xsd)")); //file_name.push_front("file://"); xmlSchemaParserCtxtPtr schema_parser = xmlSchemaNewParserCtxt(file_name.toUtf8().data()); if (0 == schema_parser) return; xmlSchemaPtr schema = xmlSchemaParse(schema_parser); if (0 == schema) { xmlSchemaFreeParserCtxt(schema_parser); schema_parser = 0; return; } schema_valid_ctxt_ = xmlSchemaNewValidCtxt(schema); if (0 == schema_valid_ctxt_) { xmlSchemaFreeParserCtxt(schema_parser); schema_parser = 0; xmlSchemaFree(schema); schema = 0; return; } ui_->actionCheckSchema->setEnabled(true); }
//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; }
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; }
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; } }
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; }
static cache_entry* cache_entry_from_xsd_doc(xmlDocPtr doc, xmlChar const* nsURI, MSXML_VERSION v) { cache_entry* entry = heap_alloc(sizeof(cache_entry)); xmlSchemaParserCtxtPtr spctx; xmlDocPtr new_doc = xmlCopyDoc(doc, 1); link_datatypes(new_doc); /* TODO: if the nsURI is different from the default xmlns or targetNamespace, * do we need to do something special here? */ entry->type = CacheEntryType_XSD; entry->ref = 0; spctx = xmlSchemaNewDocParserCtxt(new_doc); if ((entry->schema = Schema_parse(spctx))) { xmldoc_init(entry->schema->doc, v); entry->doc = entry->schema->doc; xmldoc_add_ref(entry->doc); } else { FIXME("failed to parse doc\n"); xmlFreeDoc(new_doc); heap_free(entry); entry = NULL; } xmlSchemaFreeParserCtxt(spctx); return entry; }
static cache_entry* cache_entry_from_xdr_doc(xmlDocPtr doc, xmlChar const* nsURI, MSXML_VERSION version) { cache_entry* entry = heap_alloc(sizeof(cache_entry)); xmlSchemaParserCtxtPtr spctx; xmlDocPtr new_doc = xmlCopyDoc(doc, 1), xsd_doc = XDR_to_XSD_doc(doc, nsURI); link_datatypes(xsd_doc); entry->type = CacheEntryType_XDR; entry->ref = 0; spctx = xmlSchemaNewDocParserCtxt(xsd_doc); if ((entry->schema = Schema_parse(spctx))) { entry->doc = new_doc; xmldoc_init(entry->schema->doc, version); xmldoc_init(entry->doc, version); xmldoc_add_ref(entry->doc); xmldoc_add_ref(entry->schema->doc); } else { FIXME("failed to parse doc\n"); xmlFreeDoc(new_doc); xmlFreeDoc(xsd_doc); heap_free(entry); entry = NULL; } xmlSchemaFreeParserCtxt(spctx); return entry; }
/** * Initializes the libxml2 parser. * @param dtd_filename - path to the DTD or NULL if none * @param xsd_filename - path to the XSD or NULL if none * @returns 1 on success or 0 on error */ int parser_init(char *dtd_filename, char *xsd_filename) { if (dtd_filename){ dtd = xmlParseDTD(NULL,(unsigned char*)dtd_filename); if (!dtd){ LM_ERR("unsuccesful DTD parsing from file <%s>\n", dtd_filename); return 0; } dtdCtxt = xmlNewValidCtxt(); dtdCtxt->userData = (void*)stderr; dtdCtxt->error = (xmlValidityErrorFunc) fprintf; dtdCtxt->warning = (xmlValidityWarningFunc) fprintf; } if (xsd_filename){ xmlSchemaParserCtxtPtr ctxt; ctxt = xmlSchemaNewParserCtxt(xsd_filename); if (!ctxt) { LM_ERR("unsuccesful XSD parsing from file <%s>\n", xsd_filename); return 0; } xmlSchemaSetParserErrors(ctxt,(xmlValidityErrorFunc) fprintf,(xmlValidityWarningFunc) fprintf,stderr); xsd = xmlSchemaParse(ctxt); xmlSchemaFreeParserCtxt(ctxt); xsdCtxt = xmlSchemaNewValidCtxt(xsd); xmlSchemaSetValidErrors(xsdCtxt,(xmlValidityErrorFunc) fprintf,(xmlValidityWarningFunc) fprintf,stderr); } ctxtInit=1; return 1; }
static cache_entry* cache_entry_from_url(char const* url) { cache_entry* entry = heap_alloc(sizeof(cache_entry)); xmlSchemaParserCtxtPtr spctx = xmlSchemaNewParserCtxt(url); entry->type = SCHEMA_TYPE_XSD; entry->ref = 0; if (spctx) { if((entry->schema = xmlSchemaParse(spctx))) { xmldoc_init(entry->schema->doc, &CLSID_DOMDocument40); entry->doc = entry->schema->doc; xmldoc_add_ref(entry->doc); } else { heap_free(entry); entry = NULL; } xmlSchemaFreeParserCtxt(spctx); } else { FIXME("schema for nsURI %s not found\n", wine_dbgstr_a(url)); heap_free(entry); entry = NULL; } return entry; }
static cache_entry* cache_entry_from_xsd_doc(xmlDocPtr doc) { cache_entry* entry = heap_alloc(sizeof(cache_entry)); xmlSchemaParserCtxtPtr spctx; xmlDocPtr new_doc = xmlCopyDoc(doc, 1); entry->type = SCHEMA_TYPE_XSD; entry->ref = 0; spctx = xmlSchemaNewDocParserCtxt(new_doc); if ((entry->schema = xmlSchemaParse(spctx))) { xmldoc_init(entry->schema->doc, &CLSID_DOMDocument40); entry->doc = entry->schema->doc; xmldoc_add_ref(entry->doc); } else { FIXME("failed to parse doc\n"); xmlFreeDoc(new_doc); heap_free(entry); entry = NULL; } xmlSchemaFreeParserCtxt(spctx); return entry; }
static CPLXMLSchemaPtr CPLLoadXMLSchema(const char* pszXSDFilename) { char* pszStr = CPLLoadSchemaStr(pszXSDFilename); if (pszStr == NULL) return NULL; xmlExternalEntityLoader pfnLibXMLOldExtranerEntityLoaderLocal = NULL; pfnLibXMLOldExtranerEntityLoaderLocal = xmlGetExternalEntityLoader(); pfnLibXMLOldExtranerEntityLoader = pfnLibXMLOldExtranerEntityLoaderLocal; xmlSetExternalEntityLoader(CPLExternalEntityLoader); xmlSchemaParserCtxtPtr pSchemaParserCtxt = xmlSchemaNewMemParserCtxt(pszStr, static_cast<int>(strlen(pszStr))); xmlSchemaSetParserErrors(pSchemaParserCtxt, CPLLibXMLWarningErrorCallback, CPLLibXMLWarningErrorCallback, NULL); xmlSchemaPtr pSchema = xmlSchemaParse(pSchemaParserCtxt); xmlSchemaFreeParserCtxt(pSchemaParserCtxt); xmlSetExternalEntityLoader(pfnLibXMLOldExtranerEntityLoaderLocal); CPLFree(pszStr); return (CPLXMLSchemaPtr) pSchema; }
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; } }
/* * 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; }
/* 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 bool CPLHasLibXMLBug() { if (bHasLibXMLBug >= 0) return CPL_TO_BOOL(bHasLibXMLBug); static const char szLibXMLBugTester[] = "<schema targetNamespace=\"http://foo\" xmlns:foo=\"http://foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\">" "<simpleType name=\"t1\">" "<list itemType=\"double\"/>" "</simpleType>" "<complexType name=\"t2\">" "<simpleContent>" "<extension base=\"foo:t1\"/>" "</simpleContent>" "</complexType>" "<complexType name=\"t3\">" "<simpleContent>" "<restriction base=\"foo:t2\">" "<length value=\"2\"/>" "</restriction>" "</simpleContent>" "</complexType>" "</schema>"; xmlSchemaParserCtxtPtr pSchemaParserCtxt; xmlSchemaPtr pSchema; pSchemaParserCtxt = xmlSchemaNewMemParserCtxt(szLibXMLBugTester, strlen(szLibXMLBugTester)); xmlSchemaSetParserErrors(pSchemaParserCtxt, CPLHasLibXMLBugWarningCallback, CPLHasLibXMLBugWarningCallback, NULL); pSchema = xmlSchemaParse(pSchemaParserCtxt); xmlSchemaFreeParserCtxt(pSchemaParserCtxt); bHasLibXMLBug = (pSchema == NULL); if (pSchema) xmlSchemaFree(pSchema); if (bHasLibXMLBug) { CPLDebug("CPL", "LibXML bug found (cf https://bugzilla.gnome.org/show_bug.cgi?id=630130). " "Will try to workaround for GML schemas."); } return CPL_TO_BOOL(bHasLibXMLBug); }
void schema::load_from_file(const std::string& file_name) { free_(); xmlSchemaParserCtxt* ctxt = xmlSchemaNewParserCtxt(file_name.c_str()); if (ctxt != 0) { raw_ = xmlSchemaParse(ctxt); xmlSchemaFreeParserCtxt(ctxt); } if (raw_ == 0) { std::string what = "failed to load schema from file " + file_name; throw dom_error(what); } }
void schema::load_from_string(const char* str) { free_(); int size = static_cast<int>(std::strlen(str)); xmlSchemaParserCtxt* ctxt = xmlSchemaNewMemParserCtxt(str, size); if (ctxt != 0) { raw_ = xmlSchemaParse(ctxt); xmlSchemaFreeParserCtxt(ctxt); } if (raw_ == 0) { throw dom_error("failed to load schema from string"); } }
/******************* 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."); }
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; }
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"); } } }
/* 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; }
/* * call-seq: * from_document(doc) * * Create a new Schema from the Nokogiri::XML::Document +doc+ */ static VALUE from_document(VALUE klass, VALUE document) { xmlDocPtr doc; Data_Get_Struct(document, xmlDoc, doc); // In case someone passes us a node. ugh. doc = doc->doc; xmlSchemaParserCtxtPtr ctx = xmlSchemaNewDocParserCtxt(doc); VALUE errors = rb_ary_new(); xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher); #ifdef HAVE_XMLSCHEMASETPARSERSTRUCTUREDERRORS xmlSchemaSetParserStructuredErrors( ctx, Nokogiri_error_array_pusher, (void *)errors ); #endif xmlSchemaPtr schema = xmlSchemaParse(ctx); xmlSetStructuredErrorFunc(NULL, NULL); xmlSchemaFreeParserCtxt(ctx); if(NULL == schema) { xmlErrorPtr error = xmlGetLastError(); if(error) Nokogiri_error_raise(NULL, error); else rb_raise(rb_eRuntimeError, "Could not parse document"); return Qnil; } VALUE rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema); rb_iv_set(rb_schema, "@errors", errors); return rb_schema; return Qnil; }
/*-----------------------------------------------------------------------------------------------------------------------*/ 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; }
void CLibXmlValidator::validate() { if (!xmlFile.length() && !xml.length()) throw MakeStringException(XMLERR_MissingSource, "Source XML not provided"); if (!xsdFile.length() && !xsd.length()) throw MakeStringException(XMLERR_MissingSource, "XML Schema not provided"); xmlParserInputBufferPtr input; if (xmlFile.length()) input = xmlParserInputBufferCreateFilename(xmlFile.get(), XML_CHAR_ENCODING_NONE); else input = xmlParserInputBufferCreateMem(xml.str(), xml.length()+1, XML_CHAR_ENCODING_NONE); if (!input) throw MakeStringException(XMLERR_InvalidXml, "Failed to create XML input stream"); xmlSchemaParserCtxtPtr xsdParser; if (xsdFile.length()) xsdParser = xmlSchemaNewParserCtxt(xsdFile.get()); else xsdParser = xmlSchemaNewMemParserCtxt(xsd.str(), xsd.length()); if (!xsdParser) throw MakeStringException(XMLERR_InvalidXsd, "Failed to load XML Schema"); xmlSchemaSetParserErrors(xsdParser, libxmlXsdErrorMsgHandler, libxmlXsdErrorMsgHandler, this); xmlSchemaPtr schema = xmlSchemaParse(xsdParser); xmlSchemaFreeParserCtxt(xsdParser); if (!schema) throw MakeStringException(XMLERR_InvalidXsd, "XSD schema parsing failed"); xmlSchemaValidCtxtPtr validator = xmlSchemaNewValidCtxt(schema); xmlSchemaSetValidErrors(validator, libxmlXsdErrorMsgHandler, libxmlXsdErrorMsgHandler, this); int ret = xmlSchemaValidateStream(validator, input, XML_CHAR_ENCODING_NONE, emptySAXHandler, (void *)this); if (ret != 0) { ensureExceptions()->append(*MakeStringException(XMLERR_XsdValidationFailed, "XML validation failed")); throw exceptions.getClear(); } xmlSchemaFreeValidCtxt(validator); }
/* * call-seq: * read_memory(string) * * Create a new Schema from the contents of +string+ */ static VALUE read_memory(VALUE klass, VALUE content) { xmlSchemaParserCtxtPtr ctx = xmlSchemaNewMemParserCtxt( (const char *)StringValuePtr(content), (int)RSTRING_LEN(content) ); VALUE errors = rb_ary_new(); xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher); #ifdef HAVE_XMLSCHEMASETPARSERSTRUCTUREDERRORS xmlSchemaSetParserStructuredErrors( ctx, Nokogiri_error_array_pusher, (void *)errors ); #endif xmlSchemaPtr schema = xmlSchemaParse(ctx); xmlSetStructuredErrorFunc(NULL, NULL); xmlSchemaFreeParserCtxt(ctx); if(NULL == schema) { xmlErrorPtr error = xmlGetLastError(); if(error) Nokogiri_error_raise(NULL, error); else rb_raise(rb_eRuntimeError, "Could not parse document"); return Qnil; } VALUE rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema); rb_iv_set(rb_schema, "@errors", errors); return rb_schema; }
/* * call-seq: * read_memory(string) * * Create a new Schema from the contents of +string+ */ static VALUE read_memory(VALUE klass, VALUE content) { xmlSchemaParserCtxtPtr ctx = xmlSchemaNewMemParserCtxt( (const char *)StringValuePtr(content), RSTRING_LEN(content) ); VALUE errors = rb_ary_new(); xmlSetStructuredErrorFunc((void *)errors, Nokogiri_error_array_pusher); xmlSchemaSetParserStructuredErrors( ctx, Nokogiri_error_array_pusher, (void *)errors ); xmlSchemaPtr schema = xmlSchemaParse(ctx); xmlSetStructuredErrorFunc(NULL, NULL); xmlSchemaFreeParserCtxt(ctx); if(NULL == schema) { xmlErrorPtr error = xmlGetLastError(); if(error) rb_funcall(rb_mKernel, rb_intern("raise"), 1, Nokogiri_wrap_xml_syntax_error((VALUE)NULL, error) ); else rb_raise(rb_eRuntimeError, "Could not parse document"); return Qnil; } VALUE rb_schema = Data_Wrap_Struct(klass, 0, dealloc, schema); rb_iv_set(rb_schema, "@errors", errors); return rb_schema; }
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; }
/* process interface.cfg file iface - name of the interface to process, or "" for all interfaces dry_run = 1 - test xml without applying anything dry_run = 0 - test + apply */ void opt_process(char *iface, int dry_run) { char cfg_file_name[FILENAME_MAX]; sprintf(cfg_file_name, "%s/%s", KB_CONFIG_DIR, KB_IFACE_CFG); xmlDocPtr document = xmlReadFile(cfg_file_name, NULL, 0); if (NULL == document) { /* can't load */ form_sys_result(KB_ERR, "Can't parse config file"); } else { /* process */ /* check schema compliance */ char schema_file_name[FILENAME_MAX]; sprintf(schema_file_name, "%s/%s", KB_CONFIG_DIR, KB_IFACE_SCHEMA); xmlSchemaParserCtxtPtr schemaParser = xmlSchemaNewParserCtxt(schema_file_name); if (NULL != schemaParser) { xmlSchemaPtr schema = xmlSchemaParse(schemaParser); if (NULL != schema) { xmlSchemaValidCtxtPtr validityContext = xmlSchemaNewValidCtxt(schema); xmlSchemaSetValidErrors(validityContext, schemaErrorCallback, schemaWarningCallback, 0); if (NULL != validityContext) { if (0 != xmlSchemaValidateFile(validityContext, cfg_file_name, 0)) { /* validation error */ schema_error_end(); } else { /* additional processing */ kb_process_xml(document, iface, dry_run); } xmlSchemaFreeValidCtxt(validityContext); } else { form_sys_result(KB_ERR, "Can't create validation context"); } xmlSchemaFree(schema); } else { form_sys_result(KB_ERR, "Can't parse schema"); } xmlSchemaFreeParserCtxt(schemaParser); } else { form_sys_result(KB_ERR, "Can't parse schema file"); } xmlFreeDoc(document); } }
XMLParser::~XMLParser() { if (m_Schema) { xmlSchemaFree(m_Schema); } if (m_SchemaParserCtxt) { xmlSchemaFreeParserCtxt(m_SchemaParserCtxt); } if (m_SchemaValidCtxt) { xmlSchemaFreeValidCtxt(m_SchemaValidCtxt); } if (m_DTD) { xmlFreeDtd(m_DTD); } if (m_DTDValidCtxt) { xmlFreeValidCtxt(m_DTDValidCtxt); } if (m_Doc) { xmlFreeDoc(m_Doc); } xmlSetGenericErrorFunc(0, 0); }