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; }
/* * Release ows struct */ void ows_free(ows * o) { assert(o); if (o->config_file) buffer_free(o->config_file); if (o->schema_dir) buffer_free(o->schema_dir); if (o->online_resource) buffer_free(o->online_resource); if (o->pg) PQfinish(o->pg); if (o->log_file) buffer_free(o->log_file); if (o->log) fclose(o->log); if (o->pg_dsn) buffer_free(o->pg_dsn); if (o->cgi) array_free(o->cgi); if (o->psql_requests) list_free(o->psql_requests); if (o->layers) ows_layer_list_free(o->layers); if (o->request) ows_request_free(o->request); if (o->max_geobbox) ows_geobbox_free(o->max_geobbox); if (o->metadata) ows_metadata_free(o->metadata); if (o->contact) ows_contact_free(o->contact); if (o->encoding) buffer_free(o->encoding); if (o->db_encoding) buffer_free(o->db_encoding); if (o->wfs_default_version) ows_version_free(o->wfs_default_version); if (o->postgis_version) ows_version_free(o->postgis_version); if (o->schema_wfs_100) xmlSchemaFree(o->schema_wfs_100); if (o->schema_wfs_110) xmlSchemaFree(o->schema_wfs_110); free(o); o = NULL; }
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; }
static LONG cache_entry_release(cache_entry* entry) { LONG ref = InterlockedDecrement(&entry->ref); TRACE("(%p)->(%d)\n", entry, ref); if (ref == 0) { if (entry->type == CacheEntryType_XSD) { xmldoc_release(entry->doc); entry->schema->doc = NULL; xmlSchemaFree(entry->schema); } else if (entry->type == CacheEntryType_XDR) { xmldoc_release(entry->doc); xmldoc_release(entry->schema->doc); entry->schema->doc = NULL; xmlSchemaFree(entry->schema); } heap_free(entry); } return ref; }
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 LONG cache_entry_release(cache_entry* entry) { LONG ref = InterlockedDecrement(&entry->ref); TRACE("%p new ref %d\n", entry, ref); if (ref == 0) { if (entry->type == SCHEMA_TYPE_XSD) { xmldoc_release(entry->doc); entry->schema->doc = NULL; xmlSchemaFree(entry->schema); heap_free(entry); } else /* SCHEMA_TYPE_XDR */ { xmldoc_release(entry->doc); xmldoc_release(entry->schema->doc); entry->schema->doc = NULL; xmlSchemaFree(entry->schema); heap_free(entry); } } return ref; }
//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; }
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; } }
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); }
void schemasCleanup(void) { if (datatypes_handle) FreeResource(datatypes_handle); if (datatypes_schema) xmlSchemaFree(datatypes_schema); xmlSetExternalEntityLoader(_external_entity_loader); }
void schema::free_() { if (raw_ != 0) { xmlSchemaFree(raw_); raw_ = 0; } }
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; }
void ruby_xml_schema_free(ruby_xml_schema *rxschema) { if (rxschema->schema != NULL) { xmlSchemaFree(rxschema->schema); rxschema->schema = NULL; } free(rxschema); }
/* 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); }
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"); } } }
/* 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; }
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); }
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); } }
static void rxml_schema_free(xmlSchemaPtr xschema) { xmlSchemaFree(xschema); }
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; }
static void dealloc(xmlSchemaPtr schema) { NOKOGIRI_DEBUG_START(schema); xmlSchemaFree(schema); NOKOGIRI_DEBUG_END(schema); }
void schemasCleanup(void) { xmlSchemaFree(datatypes_schema); HeapFree(GetProcessHeap(), 0, datatypes_src); xmlSetExternalEntityLoader(_external_entity_loader); }
/* ************************************************** */ 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; }