示例#1
0
文件: schema.go 项目: chreble/xsd
// Validate uses its Schema to check an xml doc.  If the doc fails to match
// the schema, an error is returned, nil otherwise.
// At the moment, the error just says that the document failed.  It doesn't
// say where and why.  It needs to - help greatly appreciated.
func (s *Schema) Validate(doc DocPtr) error {
	validCtxt := C.xmlSchemaNewValidCtxt(s.Ptr)
	if validCtxt == nil {
		// TODO find error - see below
		return errors.New("Could not build validator")
	}
	defer C.xmlSchemaFreeValidCtxt(validCtxt)

	/*
		// My plan was to register my go func to receive errors and pass it an
		// error ptr specific to this validation (useful for multiple goroutines).
		// Alas it doesn't work, help appreciated.

		var err *error
		if C.xmlSchemaGetValidErrors(validCtxt, C.schemaValidityErrorFunc, nil, unsafe.Pointer(err)) == -1 {
			return errors.New("Could not set error func.")
		}
	*/

	if C.xmlSchemaValidateDoc(validCtxt, doc) != 0 {
		//return errors.New(*err) // When the above works
		return errors.New("Document validation error")
	}
	return nil
}
示例#2
0
文件: gosaml.go 项目: wayf-dk/gosaml
// Validate - Schemavalidate the document against the the schema file given in url
func (xp *Xp) SchemaValidate(url string) (errs []string, err error) {
	cSchemaNewMemParserCtxt := C.xmlSchemaNewParserCtxt((*C.char)(unsafe.Pointer(C.CString(url))))
	if cSchemaNewMemParserCtxt == nil {
		return nil, errors.New("Could not create schema parser")
	}
	defer C.xmlSchemaFreeParserCtxt(cSchemaNewMemParserCtxt)
	cSchema := C.xmlSchemaParse(cSchemaNewMemParserCtxt)
	if cSchema == nil {
		return nil, errors.New("Could not parse schema")
	}
	defer C.xmlSchemaFree(cSchema)

	validCtxt := C.xmlSchemaNewValidCtxt(cSchema)
	if validCtxt == nil {
		return nil, errors.New("Could not build validator")
	}
	defer C.xmlSchemaFreeValidCtxt(validCtxt)

	// void_libxml_error_handler is a null function - no info collected - just the final result matters - for now
	C.xmlSchemaSetValidErrors(validCtxt, (*[0]byte)(C.void_libxml_error_handler), (*[0]byte)(C.void_libxml_error_handler), nil)

	if errno := C.xmlSchemaValidateDoc(validCtxt, xp.doc); errno != 0 {
		return nil, fmt.Errorf("Document validation error %d", errno)
	}
	return nil, nil
}