-
Notifications
You must be signed in to change notification settings - Fork 17
/
util.xqm
289 lines (245 loc) · 9 KB
/
util.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
xquery version "3.1";
(:
Edirom Online
Copyright (C) 2011 The Edirom Project
http://www.edirom.de
Edirom Online is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Edirom Online is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Edirom Online. If not, see <http://www.gnu.org/licenses/>.
ID: $Id: util.xqm 1334 2012-06-14 12:40:33Z daniel $
:)
(:~
: This module provides library utility functions
:
: @author <a href="mailto:roewenstrunk@edirom.de">Daniel Röwenstrunk</a>
: @author <a href="mailto:roewenstrunk@edirom.de">Nikolaos Beer</a>
: @author <a href="mailto:bohl@edirom.de">Benjamin W. Bohl</a>
:)
module namespace eutil = "http://www.edirom.de/xquery/util";
import module namespace work="http://www.edirom.de/xquery/work" at "work.xqm";
import module namespace source="http://www.edirom.de/xquery/source" at "source.xqm";
import module namespace teitext="http://www.edirom.de/xquery/teitext" at "teitext.xqm";
import module namespace edition="http://www.edirom.de/xquery/edition" at "../xqm/edition.xqm";
import module namespace functx = "http://www.functx.com" at "../xqm/functx-1.0-nodoc-2007-01.xq";
import module namespace annotation="http://www.edirom.de/xquery/annotation" at "../xqm/annotation.xqm";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace system="http://exist-db.org/xquery/system";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace mei="http://www.music-encoding.org/ns/mei";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace edirom="http://www.edirom.de/ns/1.3";
(:~
: Returns the namespace (standardized prefix)
:
: @param $node The node to be processed
: @return The namespace (prefix)
:)
declare function eutil:getNamespace($node as node()) as xs:string {
switch (namespace-uri($node))
case 'http://www.music-encoding.org/ns/mei' return 'mei'
case 'http://www.tei-c.org/ns/1.0' return 'tei'
case 'http://www.edirom.de/ns/1.3' return 'edirom'
default return 'unknown'
};
(:~
: Returns a localized string
:
: @param $node The node to be processed
: @return The string
:)
declare function eutil:getLocalizedName($node, $lang) {
let $name :=
if ($node/mei:title)
then (
if ($lang = $node/mei:title/@xml:lang)
then $node/mei:title[@xml:lang = $lang]/text()
else $node/mei:title[1]/text()
)
else if ($node/mei:name)
then (
if ($lang = $node/mei:name/@xml:lang)
then $node/mei:name[@xml:lang = $lang]/text()
else $node/mei:name[1]/text()
)
else if ($node/edirom:names)
then (
if ($lang = $node/edirom:names/edirom:name/@xml:lang)
then $node/edirom:names/edirom:name[@xml:lang = $lang]/node()
else $node/edirom:names/edirom:name[1]/node()
)
else if (local-name($node) = 'annot' and $node/@type = 'editorialComment')
then(annotation:generateTitle($node))
else (normalize-space($node))
return
if($node/edirom:names)
then($name)
else($name => string-join(' ') => normalize-space())
};
(:~
: Returns a localized string
:
: @param $node The node to be processed
: @param $lang Optional parameter for lang selection
: @return The string (normalized space)
:)
declare function eutil:getLocalizedTitle($node as node(), $lang as xs:string?) as xs:string {
let $namespace := eutil:getNamespace($node)
let $titleMEI := if ($lang != '' and $lang = $node/mei:title/@xml:lang)
then ($node/mei:title[@xml:lang = $lang]//text() => string-join() => normalize-space())
else (($node//mei:title)[1]//text() => string-join() => normalize-space())
let $titleTEI := if ($lang != '' and $lang = $node/tei:title/@xml:lang)
then $node/tei:title[@xml:lang = $lang]/text()
else $node/tei:title[1]/text()
return
if ($namespace = 'mei')
then($titleMEI)
else if ($namespace = 'tei')
then($titleTEI)
else('unknown')
};
(:~
: Returns a document
:
: @param $uri The URIs of the documents to process
: @return The document
:)
declare function eutil:getDoc($uri) {
if(starts-with($uri, 'textgrid:'))
then(
let $session := request:get-cookie-value('edirom_online_textgrid_sessionId')
return
doc('http://textgridlab.org/1.0/tgcrud/rest/' || $uri || '/data?sessionId=' || $session)
)
else(
doc($uri)
)
};
(:~
: Returns a comma separated list of document labels
:
: @param $docs The URIs of the documents to process
: @return The labels
:)
declare function eutil:getDocumentsLabels($docs as xs:string*, $edition as xs:string) as xs:string {
string-join(
eutil:getDocumentsLabelsAsArray($docs, $edition)
, ', ')
};
(:~
: Returns an array of document labels
:
: @param $docs The URIs of the documents to process
: @return The labels
:)
declare function eutil:getDocumentsLabelsAsArray($docs as xs:string*, $edition as xs:string) as xs:string* {
for $doc in $docs
return
eutil:getDocumentLabel($doc, $edition)
};
(:~
: Returns a document's label
:
: @param $doc The URIs of the document to process
: @return The label
:)
declare function eutil:getDocumentLabel($doc as xs:string, $edition as xs:string) as xs:string {
if(work:isWork($doc))
then(work:getLabel($doc, $edition))
else if(source:isSource($doc))
then(source:getLabel($doc, $edition))
else if(teitext:isText($doc))
then(teitext:getLabel($doc, $edition))
else('')
};
(:~
: Returns a language specific string
:
: @param $key The key to search for
: @param $values The values to include into the string
: @return The string
:)
declare function eutil:getLanguageString($key as xs:string, $values as xs:string*) as xs:string {
eutil:getLanguageString($key, $values, eutil:getLanguage(''))
};
(:~
: Returns a language specific string from the locale/edirom-lang files
:
: @param $key The key to search for
: @param $values The values to include into the string
: @param $lang The language
: @return The string
:)
declare function eutil:getLanguageString($key as xs:string, $values as xs:string*, $lang as xs:string) as xs:string {
let $base := system:get-module-load-path()
let $file := doc(concat($base, '/../locale/edirom-lang-', $lang, '.xml'))
let $string := $file//entry[@key = $key]/string(@value)
let $string := functx:replace-multi($string, for $i in (0 to (count($values) - 1)) return concat('\{',$i,'\}'), $values)
return
$string
};
(:~
: Return a value of preference to key
:
: @param $key The key to search for
: @return The string
:)
declare function eutil:getPreference($key as xs:string, $edition as xs:string?) as xs:string {
let $file := doc('../prefs/edirom-prefs.xml')
let $projectFile := doc(edition:getPreferencesURI($edition))
return
if($projectFile != 'null' and $projectFile//entry[@key = $key]) then ($projectFile//entry[@key = $key]/string(@value))
else ($file//entry[@key = $key]/string(@value))
};
(:~
: Return the application and content language
:
: @param $edition The edition's path
: @return The language key
:)
declare function eutil:getLanguage($edition as xs:string?) as xs:string {
if(request:get-cookie-names() = 'edirom-language')
then(
request:get-cookie-value('edirom-language')
)
else(
eutil:getPreference('application_language', $edition)
)
};
(:~
: Returns the application base URL as seen from the client
:
: NB, this is a relative path on the server, missing the scheme,
: as well as the server address and port.
: This function simply concats the current context path with the
: eXist variables `$exist:prefix` and `$exist:controller`
: (see https://exist-db.org/exist/apps/doc/urlrewrite)
:
: @return a relative path on the server
:)
declare function eutil:get-app-base-url() as xs:string? {
if(request:exists())
then request:get-context-path() || request:get-attribute("exist:prefix") || request:get-attribute('exist:controller')
else util:log-system-out('request object does not exist; failing to compute base url')
};
(:~
: Sorts a sequence of numeric-alpha values or nodes (e.g. 1, 1a, 1b, 2)
: This is an adaption of functx:sort-as-numeric()
:
: @author Dennis Ried
: @see http://www.xqueryfunctions.com/xq/functx_sort-as-numeric.html
: @param $seq the sequence to sort
:)
declare function eutil:sort-as-numeric-alpha($seq as item()* ) as item()* {
for $item in $seq
let $itemPart1 := functx:get-matches($item, '\d+')
let $itemPart2 := substring-after($item, $itemPart1)
order by number($itemPart1), $itemPart2
return $item
} ;