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 (
|
|
|
|
xmlSpec = "http://www.w3.org/2001/XMLSchema#"
|
|
|
|
anyURISpec = "anyURI"
|
|
|
|
)
|
|
|
|
|
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{},
|
|
|
|
},
|
|
|
|
}, 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
|
|
|
|
}
|
|
|
|
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-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{
|
|
|
|
Name: "anyURI",
|
|
|
|
URI: u,
|
|
|
|
DefinitionType: "*url.URL",
|
|
|
|
Zero: "&url.URL{}",
|
|
|
|
}
|
|
|
|
if err = v.SetValue(anyURISpec, val); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|