-
Notifications
You must be signed in to change notification settings - Fork 14
/
schema.go
83 lines (68 loc) · 2.09 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package xsd
/*
#cgo pkg-config: libxml-2.0
#include <libxml/xmlschemas.h>
void xmlErrorFunc_cgo(void *, const char *); // Forward declaration.
*/
import "C"
import (
"errors"
"runtime"
"strings"
"unsafe"
)
type Schema struct {
Ptr C.xmlSchemaPtr
}
type DocPtr C.xmlDocPtr
//export xmlErrorFunc
func xmlErrorFunc(id unsafe.Pointer, msg *C.char) {
errs := (*SchemaErrors)(id)
*errs = append(*errs, C.GoString(msg))
}
// ParseSchema creates new Schema from []byte containing xml schema data.
// Will probably change []byte to DocPtr.
func ParseSchema(buffer []byte) (*Schema, error) {
cSchemaNewMemParserCtxt := C.xmlSchemaNewMemParserCtxt((*C.char)(unsafe.Pointer(&buffer[0])), C.int(len(buffer)))
if cSchemaNewMemParserCtxt == nil {
return nil, errors.New("Could not create schema parser") // TODO extract error - see Validate func below
}
defer C.xmlSchemaFreeParserCtxt(cSchemaNewMemParserCtxt)
cSchema := C.xmlSchemaParse(cSchemaNewMemParserCtxt)
if cSchema == nil {
return nil, errors.New("Could not parse schema") // TODO extract error - see Validate func below
}
return makeSchema(cSchema), nil
}
func finaliseSchema(s *Schema) {
C.xmlSchemaFree(s.Ptr)
}
func makeSchema(cSchema C.xmlSchemaPtr) *Schema {
s := &Schema{cSchema}
runtime.SetFinalizer(s, finaliseSchema)
return s
}
type SchemaErrors []string
func (e SchemaErrors) Error() string {
return strings.Join(e, "")
}
// Validate uses its Schema to check an xml doc. If the doc fails to match
// the schema, a list of errors is returned, nil otherwise.
func (s *Schema) Validate(doc DocPtr) SchemaErrors {
validCtxt := C.xmlSchemaNewValidCtxt(s.Ptr)
if validCtxt == nil {
// TODO find error
return SchemaErrors{"Could not build validator"}
}
defer C.xmlSchemaFreeValidCtxt(validCtxt)
validationErrors := SchemaErrors{}
C.xmlSchemaSetValidErrors(validCtxt,
(C.xmlSchemaValidityErrorFunc)(unsafe.Pointer(C.xmlErrorFunc_cgo)),
(C.xmlSchemaValidityErrorFunc)(unsafe.Pointer(C.xmlErrorFunc_cgo)),
unsafe.Pointer(&validationErrors),
)
if C.xmlSchemaValidateDoc(validCtxt, doc) != 0 {
return validationErrors
}
return nil
}