rdf.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package gold
  2. import (
  3. "strings"
  4. )
  5. var (
  6. ns = struct {
  7. rdf, rdfs, acl, cert, foaf, stat, ldp, dct, space, st NS
  8. }{
  9. rdf: NewNS("http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
  10. rdfs: NewNS("http://www.w3.org/2000/01/rdf-schema#"),
  11. acl: NewNS("http://www.w3.org/ns/auth/acl#"),
  12. cert: NewNS("http://www.w3.org/ns/auth/cert#"),
  13. foaf: NewNS("http://xmlns.com/foaf/0.1/"),
  14. stat: NewNS("http://www.w3.org/ns/posix/stat#"),
  15. ldp: NewNS("http://www.w3.org/ns/ldp#"),
  16. dct: NewNS("http://purl.org/dc/terms/"),
  17. space: NewNS("http://www.w3.org/ns/pim/space#"),
  18. st: NewNS("http://www.w3.org/ns/solid/terms#"),
  19. }
  20. )
  21. // NS is a generic namespace type
  22. type NS string
  23. // NewNS is used to set a new namespace
  24. func NewNS(base string) (ns NS) {
  25. return NS(base)
  26. }
  27. // Get is used to return the prefix for a namespace
  28. func (ns NS) Get(name string) (term Term) {
  29. return NewResource(string(ns) + name)
  30. }
  31. func brack(s string) string {
  32. if len(s) > 0 && s[0] == '<' {
  33. return s
  34. }
  35. if len(s) > 0 && s[len(s)-1] == '>' {
  36. return s
  37. }
  38. return "<" + s + ">"
  39. }
  40. func debrack(s string) string {
  41. if len(s) < 2 {
  42. return s
  43. }
  44. if s[0] != '<' {
  45. return s
  46. }
  47. if s[len(s)-1] != '>' {
  48. return s
  49. }
  50. return s[1 : len(s)-1]
  51. }
  52. func defrag(s string) string {
  53. lst := strings.Split(s, "#")
  54. if len(lst) != 2 {
  55. return s
  56. }
  57. return lst[0]
  58. }
  59. func unquote(s string) string {
  60. if len(s) < 2 {
  61. return s
  62. }
  63. if s[0] != '"' {
  64. return s
  65. }
  66. if s[len(s)-1] != '"' {
  67. return s
  68. }
  69. return s[1 : len(s)-1]
  70. }
  71. // frag = lambda x: x[x.find('#')==-1 and len(x) or x.find('#'):len(x)-(x[-1]=='>')]
  72. // unfrag = lambda x: '#' in x and (x[:x.find('#')==-1 and len(x) or x.find('#')] + (x[0]=='<' and '>' or '')) or x
  73. // cpfrag = lambda x,y: unfrag(y)[-1] == '>' and unfrag(y)[:-1]+frag(x)+'>' or unfrag(y)+frag(x)