2018-11-29 05:40:11 +09:00
|
|
|
package xsd
|
|
|
|
|
|
|
|
import (
|
2018-12-03 07:48:54 +09:00
|
|
|
"fmt"
|
2018-11-29 09:22:56 +09:00
|
|
|
"github.com/cjslep/activity/tools/exp/rdf"
|
2018-12-09 01:50:26 +09:00
|
|
|
"net/url"
|
2018-12-03 07:48:54 +09:00
|
|
|
"strings"
|
2018-11-29 05:40:11 +09:00
|
|
|
)
|
|
|
|
|
2018-12-09 01:50:26 +09:00
|
|
|
const (
|
2018-12-09 02:57:40 +09:00
|
|
|
xmlSpec = "http://www.w3.org/2001/XMLSchema#"
|
|
|
|
anyURISpec = "anyURI"
|
|
|
|
dateTimeSpec = "dateTime"
|
|
|
|
floatSpec = "float"
|
|
|
|
stringSpec = "string"
|
|
|
|
booleanSpec = "boolean"
|
|
|
|
nonNegativeIntegerSpec = "nonNegativeInteger"
|
|
|
|
durationSpec = "duration"
|
2018-12-09 01:50:26 +09:00
|
|
|
)
|
|
|
|
|
2018-11-29 05:40:11 +09:00
|
|
|
type XMLOntology struct{}
|
|
|
|
|
|
|
|
func (o *XMLOntology) SpecURI() string {
|
2018-12-09 01:50:26 +09:00
|
|
|
return xmlSpec
|
2018-11-29 05:40:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *XMLOntology) Load() ([]rdf.RDFNode, error) {
|
2018-12-09 01:50:26 +09:00
|
|
|
return o.LoadAsAlias("")
|
2018-11-29 05:40:11 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
func (o *XMLOntology) LoadAsAlias(s string) ([]rdf.RDFNode, error) {
|
2018-12-09 01:50:26 +09:00
|
|
|
return []rdf.RDFNode{
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: xmlSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: anyURISpec,
|
|
|
|
Delegate: &anyURI{},
|
|
|
|
},
|
2018-12-09 02:57:40 +09:00
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: xmlSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: dateTimeSpec,
|
|
|
|
Delegate: &dateTime{},
|
|
|
|
},
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: xmlSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: floatSpec,
|
|
|
|
Delegate: &float{},
|
|
|
|
},
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: xmlSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: stringSpec,
|
|
|
|
Delegate: &xmlString{},
|
|
|
|
},
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: xmlSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: booleanSpec,
|
|
|
|
Delegate: &boolean{},
|
|
|
|
},
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: xmlSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: nonNegativeIntegerSpec,
|
|
|
|
Delegate: &nonNegativeInteger{},
|
|
|
|
},
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: xmlSpec,
|
|
|
|
Alias: s,
|
|
|
|
Name: durationSpec,
|
|
|
|
Delegate: &duration{},
|
|
|
|
},
|
2018-12-09 01:50:26 +09:00
|
|
|
}, nil
|
2018-11-29 05:40:11 +09:00
|
|
|
}
|
|
|
|
|
2018-12-03 04:04:56 +09:00
|
|
|
func (o *XMLOntology) LoadSpecificAsAlias(alias, name string) ([]rdf.RDFNode, error) {
|
2018-12-09 01:50:26 +09:00
|
|
|
switch name {
|
|
|
|
case anyURISpec:
|
|
|
|
return []rdf.RDFNode{
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
|
|
|
Delegate: &anyURI{},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-12-09 02:57:40 +09:00
|
|
|
case dateTimeSpec:
|
|
|
|
return []rdf.RDFNode{
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
|
|
|
Delegate: &dateTime{},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case floatSpec:
|
|
|
|
return []rdf.RDFNode{
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
|
|
|
Delegate: &float{},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case stringSpec:
|
|
|
|
return []rdf.RDFNode{
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
|
|
|
Delegate: &xmlString{},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case booleanSpec:
|
|
|
|
return []rdf.RDFNode{
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
|
|
|
Delegate: &boolean{},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case nonNegativeIntegerSpec:
|
|
|
|
return []rdf.RDFNode{
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
|
|
|
Delegate: &nonNegativeInteger{},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case durationSpec:
|
|
|
|
return []rdf.RDFNode{
|
|
|
|
&rdf.AliasedDelegate{
|
|
|
|
Spec: "",
|
|
|
|
Alias: "",
|
|
|
|
Name: alias,
|
|
|
|
Delegate: &duration{},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-12-09 01:50:26 +09:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("xml ontology cannot find %q to alias to %q", name, alias)
|
2018-12-03 04:04:56 +09:00
|
|
|
}
|
|
|
|
|
2018-11-29 05:40:11 +09:00
|
|
|
func (o *XMLOntology) LoadElement(name string, payload map[string]interface{}) ([]rdf.RDFNode, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2018-12-03 07:48:54 +09:00
|
|
|
|
|
|
|
func (o *XMLOntology) GetByName(name string) (rdf.RDFNode, error) {
|
|
|
|
name = strings.TrimPrefix(name, o.SpecURI())
|
|
|
|
switch name {
|
2018-12-09 01:50:26 +09:00
|
|
|
case anyURISpec:
|
|
|
|
return &anyURI{}, nil
|
2018-12-09 02:57:40 +09:00
|
|
|
case dateTimeSpec:
|
|
|
|
return &dateTime{}, nil
|
|
|
|
case floatSpec:
|
|
|
|
return &float{}, nil
|
|
|
|
case stringSpec:
|
|
|
|
return &xmlString{}, nil
|
|
|
|
case booleanSpec:
|
|
|
|
return &boolean{}, nil
|
|
|
|
case nonNegativeIntegerSpec:
|
|
|
|
return &nonNegativeInteger{}, nil
|
|
|
|
case durationSpec:
|
|
|
|
return &duration{}, nil
|
2018-12-03 07:48:54 +09:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("xsd ontology could not find node for name %s", name)
|
|
|
|
}
|
2018-12-09 01:50:26 +09:00
|
|
|
|
|
|
|
var _ rdf.RDFNode = &anyURI{}
|
|
|
|
|
|
|
|
type anyURI struct{}
|
|
|
|
|
|
|
|
func (a *anyURI) Enter(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd anyURI cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *anyURI) Exit(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd anyURI cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *anyURI) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
v := ctx.Result.GetReference(xmlSpec)
|
|
|
|
if len(v.Values[anyURISpec].Name) == 0 {
|
|
|
|
u, err := url.Parse(xmlSpec + anyURISpec)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
val := &rdf.VocabularyValue{
|
2018-12-09 02:57:40 +09:00
|
|
|
Name: anyURISpec,
|
2018-12-09 01:50:26 +09:00
|
|
|
URI: u,
|
|
|
|
DefinitionType: "*url.URL",
|
|
|
|
Zero: "&url.URL{}",
|
|
|
|
}
|
|
|
|
if err = v.SetValue(anyURISpec, val); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2018-12-09 02:57:40 +09:00
|
|
|
|
|
|
|
var _ rdf.RDFNode = &dateTime{}
|
|
|
|
|
|
|
|
type dateTime struct{}
|
|
|
|
|
|
|
|
func (d *dateTime) Enter(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd dateTime cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dateTime) Exit(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd dateTime cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dateTime) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
v := ctx.Result.GetReference(xmlSpec)
|
|
|
|
if len(v.Values[dateTimeSpec].Name) == 0 {
|
|
|
|
u, err := url.Parse(xmlSpec + dateTimeSpec)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
val := &rdf.VocabularyValue{
|
|
|
|
Name: dateTimeSpec,
|
|
|
|
URI: u,
|
|
|
|
DefinitionType: "time.Time",
|
|
|
|
Zero: "&time.Time{}",
|
|
|
|
}
|
|
|
|
if err = v.SetValue(dateTimeSpec, val); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ rdf.RDFNode = &float{}
|
|
|
|
|
|
|
|
type float struct{}
|
|
|
|
|
|
|
|
func (f *float) Enter(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd float cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *float) Exit(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd float cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *float) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
v := ctx.Result.GetReference(xmlSpec)
|
|
|
|
if len(v.Values[floatSpec].Name) == 0 {
|
|
|
|
u, err := url.Parse(xmlSpec + floatSpec)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
val := &rdf.VocabularyValue{
|
|
|
|
Name: floatSpec,
|
|
|
|
URI: u,
|
|
|
|
DefinitionType: "float32",
|
|
|
|
Zero: "0.0",
|
|
|
|
}
|
|
|
|
if err = v.SetValue(floatSpec, val); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ rdf.RDFNode = &xmlString{}
|
|
|
|
|
|
|
|
type xmlString struct{}
|
|
|
|
|
|
|
|
func (*xmlString) Enter(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd string cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*xmlString) Exit(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd string cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*xmlString) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
v := ctx.Result.GetReference(xmlSpec)
|
|
|
|
if len(v.Values[stringSpec].Name) == 0 {
|
|
|
|
u, err := url.Parse(xmlSpec + stringSpec)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
val := &rdf.VocabularyValue{
|
|
|
|
Name: stringSpec,
|
|
|
|
URI: u,
|
|
|
|
DefinitionType: "string",
|
|
|
|
Zero: "\"\"",
|
|
|
|
}
|
|
|
|
if err = v.SetValue(stringSpec, val); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ rdf.RDFNode = &boolean{}
|
|
|
|
|
|
|
|
type boolean struct{}
|
|
|
|
|
|
|
|
func (*boolean) Enter(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd boolean cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*boolean) Exit(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd boolean cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*boolean) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
v := ctx.Result.GetReference(xmlSpec)
|
|
|
|
if len(v.Values[booleanSpec].Name) == 0 {
|
|
|
|
u, err := url.Parse(xmlSpec + booleanSpec)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
val := &rdf.VocabularyValue{
|
|
|
|
Name: booleanSpec,
|
|
|
|
URI: u,
|
|
|
|
DefinitionType: "string",
|
|
|
|
Zero: "\"\"",
|
|
|
|
}
|
|
|
|
if err = v.SetValue(booleanSpec, val); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ rdf.RDFNode = &nonNegativeInteger{}
|
|
|
|
|
|
|
|
type nonNegativeInteger struct{}
|
|
|
|
|
|
|
|
func (*nonNegativeInteger) Enter(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd nonNegativeInteger cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*nonNegativeInteger) Exit(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd nonNegativeInteger cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*nonNegativeInteger) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
v := ctx.Result.GetReference(xmlSpec)
|
|
|
|
if len(v.Values[nonNegativeIntegerSpec].Name) == 0 {
|
|
|
|
u, err := url.Parse(xmlSpec + nonNegativeIntegerSpec)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
val := &rdf.VocabularyValue{
|
|
|
|
Name: nonNegativeIntegerSpec,
|
|
|
|
URI: u,
|
|
|
|
DefinitionType: "int",
|
|
|
|
Zero: "0",
|
|
|
|
}
|
|
|
|
if err = v.SetValue(nonNegativeIntegerSpec, val); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ rdf.RDFNode = &duration{}
|
|
|
|
|
|
|
|
type duration struct{}
|
|
|
|
|
|
|
|
func (*duration) Enter(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd duration cannot be entered")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*duration) Exit(key string, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
return true, fmt.Errorf("xsd duration cannot be exited")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*duration) Apply(key string, value interface{}, ctx *rdf.ParsingContext) (bool, error) {
|
|
|
|
v := ctx.Result.GetReference(xmlSpec)
|
|
|
|
if len(v.Values[durationSpec].Name) == 0 {
|
|
|
|
u, err := url.Parse(xmlSpec + durationSpec)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
val := &rdf.VocabularyValue{
|
|
|
|
Name: durationSpec,
|
|
|
|
URI: u,
|
|
|
|
DefinitionType: "time.Duration",
|
|
|
|
Zero: "time.Duration(0)",
|
|
|
|
}
|
|
|
|
if err = v.SetValue(durationSpec, val); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|