2019-07-25 19:43:46 +03:00
|
|
|
// Copyright 2017 FoxyUtils ehf. All rights reserved.
|
2017-08-28 20:56:18 -05:00
|
|
|
//
|
2020-08-08 01:01:05 +00:00
|
|
|
// Use of this software package and source code is governed by the terms of the
|
|
|
|
// UniDoc End User License Agreement (EULA) that is available at:
|
|
|
|
// https://unidoc.io/eula/
|
|
|
|
// A trial license code for evaluation can be obtained at https://unidoc.io.
|
2017-08-28 20:56:18 -05:00
|
|
|
|
2019-05-04 13:54:29 +00:00
|
|
|
package unioffice
|
2017-08-28 20:56:18 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Any is the interface used for marshaling/unmarshaling xsd:any
|
|
|
|
type Any interface {
|
|
|
|
MarshalXML(e *xml.Encoder, start xml.StartElement) error
|
|
|
|
UnmarshalXML(d *xml.Decoder, start xml.StartElement) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var creatorFns = map[string]interface{}{}
|
|
|
|
|
|
|
|
// RegisterConstructor registers a constructor function used for unmarshaling
|
|
|
|
// xsd:any elements.
|
|
|
|
func RegisterConstructor(ns, name string, fn interface{}) {
|
|
|
|
creatorFns[ns+"/"+name] = fn
|
|
|
|
}
|
|
|
|
|
2017-08-29 23:07:54 -05:00
|
|
|
// CreateElement creates an element with the given namespace and name. It is
|
|
|
|
// used to unmarshal some xsd:any elements to the appropriate concrete type.
|
2017-08-28 20:56:18 -05:00
|
|
|
func CreateElement(start xml.StartElement) (Any, error) {
|
|
|
|
fn, ok := creatorFns[start.Name.Space+"/"+start.Name.Local]
|
|
|
|
if !ok {
|
2017-08-29 17:27:02 -05:00
|
|
|
r := &XSDAny{}
|
2017-08-28 20:56:18 -05:00
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
v := reflect.ValueOf(fn)
|
|
|
|
res := v.Call(nil)
|
|
|
|
if len(res) != 1 {
|
|
|
|
return nil, fmt.Errorf("constructor function should return one value, got %d", len(res))
|
|
|
|
}
|
|
|
|
any, ok := res[0].Interface().(Any)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("constructor function should return any 'Any'")
|
|
|
|
}
|
|
|
|
return any, nil
|
|
|
|
}
|