/* * 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; }
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; }
/* * call-seq: * validate_file(filename) * * Validate a file against this Schema. */ static VALUE validate_file(VALUE self, VALUE rb_filename) { xmlSchemaPtr schema; const char *filename ; Data_Get_Struct(self, xmlSchema, schema); filename = (const char*)StringValuePtr(rb_filename) ; 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 xmlSchemaValidateFile(valid_ctxt, filename, 0); xmlSchemaFreeValidCtxt(valid_ctxt); return errors; }
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; }
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)); }
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; }
/** * 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; }
//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; } }
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); }
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; }
/******************* 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"); } } }
void XMLParser::setSchema(const string& sSchema, const string& sSchemaName) { AVG_ASSERT(!m_SchemaParserCtxt); AVG_ASSERT(!m_Schema); AVG_ASSERT(!m_SchemaValidCtxt); AVG_ASSERT(!m_DTD); AVG_ASSERT(!m_DTDValidCtxt); m_SchemaParserCtxt = xmlSchemaNewMemParserCtxt(sSchema.c_str(), sSchema.length()); checkError(!m_SchemaParserCtxt, sSchemaName); m_Schema = xmlSchemaParse(m_SchemaParserCtxt); checkError(!m_Schema, sSchemaName); m_SchemaValidCtxt = xmlSchemaNewValidCtxt(m_Schema); checkError(!m_SchemaValidCtxt, sSchemaName); }
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; }
SEXP RS_XML_xmlSchemaNewValidCtxt(SEXP r_schema, SEXP r_options, SEXP r_errorHandlers) { xmlSchemaPtr schema = (xmlSchemaPtr) R_ExternalPtrAddr(r_schema); xmlSchemaValidCtxtPtr ctxt; int numErrHandlers; ctxt = xmlSchemaNewValidCtxt(schema); if(LENGTH(r_options)) xmlSchemaSetValidOptions(ctxt, INTEGER(r_options)[0]); numErrHandlers = LENGTH(r_errorHandlers); if(numErrHandlers > 0) { R_SchemaValidCallback *cbinfo = (R_SchemaValidCallback*) malloc(sizeof(R_SchemaValidCallback)); cbinfo->fun = VECTOR_ELT(r_errorHandlers); xmlSchemaSetValidErrors(routine); } return(); }
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); }
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; }
/* 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 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::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")); } }