-
Notifications
You must be signed in to change notification settings - Fork 5
/
propvalue.xqm
178 lines (161 loc) · 6.27 KB
/
propvalue.xqm
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
xquery version "3.1";
(: part of Guid-O-Matic 2.0 https://github.com/baskaufs/guid-o-matic . You are welcome to reuse or hack in any way :)
module namespace propvalue = 'http://bioimages.vanderbilt.edu/xqm/propvalue';
(: Note: copied this function from http://www.xqueryfunctions.com/xq/functx_chars.html :)
declare function propvalue:chars
( $arg as xs:string? ) as xs:string* {
for $ch in string-to-codepoints($arg)
return codepoints-to-string($ch)
} ;
declare function propvalue:escape-bad-characters($string,$serialization)
{
switch ($serialization)
case "json"
case "turtle"
return fn:replace(
fn:replace($string,'\\','\\\\')
,'"','\\"')
case "xml"
return propvalue:escape-less-than(
fn:replace($string,'&','&')
)
default return $string
};
declare function propvalue:escape-less-than($string)
{
string-join(
for $char in propvalue:chars($string)
return
if ($char = '<') then
``[<]``
else
$char
)
};
declare function propvalue:expand-iri($abbreviated,$namespaces)
{
(: if the passed URI is already expanded as an HTTP IRI or a URN, the function does nothing :)
if (fn:substring($abbreviated,1,8)="https://")
then
$abbreviated
else
if (fn:substring($abbreviated,1,7)="http://")
then
$abbreviated
else
if (fn:substring($abbreviated,1,4)="urn:")
then
$abbreviated
else
let $curie := substring-before($abbreviated,":")
let $localName := substring-after($abbreviated,":")
for $namespace in $namespaces
where $namespace/curie/text()=$curie
return concat($namespace/value/text(),$localName)
};
declare function propvalue:wrap-turtle-iri($iri)
{
(: check whether an unabbreviated HTTP IRI or URN. If so, wrap in lt/gt brackets. If not, do nothing :)
if (fn:substring($iri,1,8)="https://")
then
concat('<',$iri,">")
else
if (fn:substring($iri,1,7)="http://")
then
concat('<',$iri,">")
else
if (fn:substring($iri,1,4)="urn:")
then
concat('<',$iri,">")
else
$iri
};
declare function propvalue:subject($iri,$serialization)
{
(: Note: the subject iri begins the description, so the returned string includes characters necessary to open the container. In turtle and xml, blank nodes have different formats than full URIs :)
switch ($serialization)
case "turtle" return
if (fn:substring($iri,1,2)="_:")
then concat($iri," ")
else concat("<",$iri,"> ")
case "xml" return
if (fn:substring($iri,1,2)="_:")
then concat('<rdf:Description rdf:nodeID="',concat("U",fn:substring($iri,3,fn:string-length($iri)-2)),'"> ')
else concat('<rdf:Description rdf:about="',$iri,'"> ')
case "json" return concat("{ ",'"@id": "',$iri,'", ')
default return ""
};
declare function propvalue:plain-literal($predicate,$dirtyString,$serialization)
{
let $string := propvalue:escape-bad-characters($dirtyString,$serialization)
return switch ($serialization)
case "turtle" return concat(" ",$predicate,' "',$string,'"; ')
case "xml" return concat(" <",$predicate,'>',$string,'</',$predicate,'> ')
case "json" return concat('"',$predicate,'": "',$string,'", ')
default return ""
};
declare function propvalue:datatyped-literal($predicate,$dirtyString,$datatype,$serialization,$namespaces)
{
let $string := propvalue:escape-bad-characters($dirtyString,$serialization)
return switch ($serialization)
case "turtle" return concat(" ",$predicate,' "',$string,'"^^',propvalue:wrap-turtle-iri($datatype),"; ")
case "xml" return concat(" <",$predicate,' rdf:datatype="',propvalue:expand-iri($datatype,$namespaces),'">',$string,'</',$predicate,'> ')
case "json" return concat('"',$predicate,'": {"@type": "',$datatype,'","@value": "',$string,'"}, ')
default return ""
};
declare function propvalue:language-tagged-literal($predicate,$dirtyString,$lang,$serialization)
{
let $string := propvalue:escape-bad-characters($dirtyString,$serialization)
return switch ($serialization)
case "turtle" return concat(" ",$predicate,' "',$string,'"@',$lang,"; ")
case "xml" return concat(" <",$predicate,' xml:lang="',$lang,'">',$string,'</',$predicate,'> ')
case "json" return concat('"',$predicate,'": {"@language": "',$lang,'","@value": "',$string,'"}, ')
default return ""
};
declare function propvalue:iri($predicate,$string,$serialization,$namespaces)
{
switch ($serialization)
case "turtle" return concat(" ",$predicate,' ',propvalue:wrap-turtle-iri($string),"; ")
case "xml" return
if (fn:substring($string,1,2)="_:")
then concat(" <",$predicate,' rdf:nodeID="',concat("U",fn:substring($string,3,fn:string-length($string)-2)),'"/> ')
else concat(" <",$predicate,' rdf:resource="',propvalue:expand-iri($string,$namespaces),'"/> ')
case "json" return concat('"',$predicate,'": {"@id": "',$string,'"}, ')
default return ""
};
declare function propvalue:type($type,$serialization,$namespaces)
{
(: Note: type is the last property listed, so the returned string includes characters necessary to close the container :)
(: There also is no trailing separator (if the serialization has one). :)
(: A value of "null" suppresses declaring a type and simply closes the container. :)
switch ($serialization)
case "turtle" return
if ($type = "null")
then " "
else concat(" a ",propvalue:wrap-turtle-iri($type),". ")
case "xml" return
if ($type = "null")
then '</rdf:Description> '
else concat(' <rdf:type rdf:resource="',propvalue:expand-iri($type,$namespaces),'"/> </rdf:Description> ')
case "json" return
if ($type = "null")
then "} "
else concat('"@type": "',$type,'" ',"} ")
default return ""
};
declare function propvalue:media-type($serialization)
{
switch ($serialization)
case "turtle" return "text/turtle"
case "xml" return "application/rdf+xml"
case "json" return "application/json"
default return ""
};
declare function propvalue:extension($serialization)
{
switch ($serialization)
case "turtle" return ".ttl"
case "xml" return ".rdf"
case "json" return ".json"
default return ""
};