forked from inspire-eu-validation/ets-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testquery-md.xq
273 lines (253 loc) · 11.2 KB
/
testquery-md.xq
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
declare namespace csw='http://www.opengis.net/cat/csw/2.0.2';
declare namespace gsr='http://www.isotc211.org/2005/gsr';
declare namespace gss='http://www.isotc211.org/2005/gss';
declare namespace gts='http://www.isotc211.org/2005/gts';
declare namespace gmx='http://www.isotc211.org/2005/gmx';
declare namespace srv='http://www.isotc211.org/2005/srv';
declare namespace gco='http://www.isotc211.org/2005/gco';
declare namespace gmd='http://www.isotc211.org/2005/gmd';
declare namespace gml='http://www.opengis.net/gml/3.2';
declare namespace gml31='http://www.opengis.net/gml';
declare namespace xsi='http://www.w3.org/2001/XMLSchema-instance';
declare namespace xlink='http://www.w3.org/1999/xlink';
declare namespace etf='http://www.interactive-instruments.de/etf/2.0';
declare namespace atom='http://www.w3.org/2005/Atom';
declare namespace wfs='http://www.opengis.net/wfs/2.0';
declare namespace wcs='http://www.opengis.net/wcs/2.0';
declare namespace sos='http://www.opengis.net/sos/2.0';
declare namespace wms='http://www.opengis.net/wms';
declare namespace uuid='java.util.UUID';
import module namespace functx = 'http://www.functx.com';
import module namespace http = 'http://expath.org/ns/http-client';
declare variable $limitErrors external := 1000;
declare variable $validationErrors external := '';
declare variable $db external;
declare variable $records external;
declare variable $idMap external;
declare variable $testObjectId external;
declare variable $logFile external;
declare variable $statFile external;
declare variable $encoding external;
declare variable $schemapath external;
declare function local:strippath($path as xs:string) as xs:string
{
let $sep := file:dir-separator()
return
if (contains($path,$sep)) then
local:strippath(substring-after($path,$sep))
else
replace($path,'\.[gGxX][mM][lL]','')
};
declare function local:filename($element as node()) as xs:string
{
db:path($element)
};
declare function local:log($text as xs:string) as empty-sequence()
{
let $dummy := file:append($logFile, $text || file:line-separator(), map { "method": "text", "media-type": "text/plain" })
return prof:dump($text)
};
declare function local:start($id as xs:string) as empty-sequence()
{
()
};
declare function local:end($id as xs:string, $status as xs:string) as empty-sequence()
{
()
};
declare function local:addMessage($templateId as xs:string, $map as map(*)) as element()
{
<message xmlns='http://www.interactive-instruments.de/etf/2.0' ref='{$templateId}'>
<translationArguments>
{ for $key in map:keys($map) return <argument token='{$key}'>{map:get($map,$key)}</argument> }
</translationArguments>
</message>
};
declare function local:passed($id as xs:string) as xs:boolean
{
true() (: TODO :)
};
declare function local:error-statistics($template as xs:string, $count as xs:integer) as element()*
{
(if ($count>=$limitErrors) then local:addMessage('TR.tooManyErrors', map { 'count': string($limitErrors) }) else (),
if ($count>0) then local:addMessage($template, map { 'count': string($count) }) else ())
};
declare function local:status($stati as xs:string*) as xs:string
{
if ($stati='FAILED') then 'FAILED' else if ($stati='SKIPPED') then 'SKIPPED' else if ($stati='WARNING') then 'WARNING' else if ($stati='INFO') then 'INFO' else if ($stati='PASSED_MANUAL') then 'PASSED_MANUAL' else if ($stati='PASSED') then 'PASSED' else if ($stati='NOT_APPLICABLE') then 'NOT_APPLICABLE' else 'UNDEFINED'
};
(: 'notHTTP' = not a HTTP(S) URL
'idNotFound' = a bare name Xpointer was provided, but not found
'EXCEPTION ...' = an exception occurred
HTTP status code = resource not available, the status code may point to the reason
media type = accessible :)
declare function local:check-resource-uri($uri as xs:string, $timeoutInS as xs:integer, $redirect as xs:boolean) as xs:string
{
if ($uri=('http://','https://')) then
'notHTTP'
else if (starts-with($uri,'http://') or starts-with($uri,'https://')) then
try {
let $loginfo := local:log('Checking URL: ''' || $uri || '''')
let $query := "import module namespace http = 'http://expath.org/ns/http-client'; declare variable $timeoutInS external; declare variable $redirect external; declare variable $uri external; http:send-request(<http:request method='get' timeout='{$timeoutInS}' status-only='true' follow-redirect='{$redirect}'/>, $uri)"
let $response := xquery:eval($query, map{ 'timeoutInS' : $timeoutInS, 'uri': $uri, 'redirect': $redirect }, map{ 'timeout': $timeoutInS })
return
if ($response/@status=('200','204')) then
let $contenttype := $response/http:header[lower-case(@name)='content-type']/@value
return
if ($contenttype) then $contenttype else 'application/octet-stream'
else if ($redirect and $response/@status=('301','302','303','307')) then
local:check-resource-uri($response/http:header[@name='Location']/@value, $timeoutInS, $redirect)
else
$response/@status
} catch *
{
let $logerror := local:log('Exception: ' || $err:description || ' URL: ' || $uri)
return 'EXCEPTION ' || $err:description
}
else if (starts-with($uri,'#')) then
let $response := $records[@id=substring($uri,2)]
return
if ($response) then
'application/xml'
else
'idNotFound'
else
'notHTTP'
};
declare function local:check-http-uris($uris as xs:string*, $timeoutInS as xs:integer, $max as xs:integer, $errors as xs:integer, $map as map(*), $redirect as xs:boolean) as map(*)
{
if (count($uris)=0) then $map
else if ($errors ge $limitErrors) then map:merge(( $map, for $uri in $uris return map{ $uri: 'SKIPPED' } )) (: too many errors already identified, skipping the remaining URIs :)
else
let $result := local:check-resource-uri($uris[1], $timeoutInS, $redirect)
let $newmap := map:merge( ($map, map{ $uris[1] : $result }) )
let $newerrors := if (matches($result,'(EXCEPTION|\d{3})')) then $errors+1 else $errors
return local:check-http-uris($uris[position() gt 1], $timeoutInS, $max, $newerrors, $newmap, $redirect)
};
declare function local:check-resource-uris($uris as xs:string*, $timeoutInS as xs:integer, $redirect as xs:boolean) as map(*)
{
let $remote := $uris[starts-with(.,'http://') or starts-with(.,'https://')]
let $local := $uris[starts-with(.,'#')]
return
map:merge((
local:check-http-uris($remote, $timeoutInS, count($remote), 0, map{}, $redirect),
for $uri in $local[map:contains($idMap,substring(.,2))] return map { $uri : 'application/gml+xml' },
for $uri in $local[not(map:contains($idMap,substring(.,2)))] return map { $uri : 'idNotFound' },
for $uri in $uris[not(starts-with(.,'#') or starts-with(.,'http://') or starts-with(.,'https://'))] return map { $uri : 'notHTTP' }
))
};
(:
@throws: an error that explains why the code list could not be accessed
:)
declare function local:get-code-list-values($url as xs:string) as xs:string*
{
let $clname := functx:substring-after-last-match($url, 'http(s)?://inspire.ec.europa.eu/((metadata-)?codelist/)?')
let $clurl := $url || '/' || $clname || '.en.atom'
let $valid_clurl := try { local:check-resource-uri($clurl, 30, false()) } catch * { false() }
return
if ($valid_clurl = ('notHTTP','idNotFound') or matches($valid_clurl,'\d{3}')) then
error((),'Code list ' || $url || ' cannot be accessed.')
else if (starts-with($valid_clurl, 'EXCEPTION')) then
error((),'Access to code list ' || $url || ' failed or timed out.')
else if (not(starts-with($valid_clurl,'text/xml') or starts-with($valid_clurl,'application/xml') or starts-with($valid_clurl,'application/atom+xml'))) then
error((),'Unknown resource type encountered when accessing the atom representation of code list ' || $url || ' at URL ' || $clurl || '.')
else
try {
let $normalizedUrl := functx:substring-after-last-match($url, 'http(s)?:')
let $clfeed := fn:doc($clurl)
let $codeUris := $clfeed//atom:entry/atom:id/text()
let $codes := for $codeUri in $codeUris
let $normalizedCodeUri := functx:substring-after-last-match($codeUri, 'http(s)?:')
return fn:substring-after($normalizedCodeUri, $normalizedUrl || '/')
return
$codes
} catch * {
error((),'Code list ' || $url || ' cannot be accessed.')
}
};
(:
@throws: an error that explains why the code list could not be accessed
:)
declare function local:get-codes-in-atom-format($url as xs:string, $langId as xs:string) as element()*
{
let $clname := if ($url = 'http://inspire.ec.europa.eu/theme') then 'theme' else functx:substring-after-last-match($url, 'http(s)?://inspire.ec.europa.eu/((metadata\-)?codelist/)?')
let $clurl := $url || '/' || $clname || '.' || $langId || '.atom'
let $valid_clurl := try { local:check-resource-uri($clurl, 30, false()) } catch * { false() }
return
if ($valid_clurl = ('notHTTP','idNotFound') or matches($valid_clurl,'\d{3}')) then
error((),'Code list ' || $url || ' cannot be accessed.')
else if (starts-with($valid_clurl, 'EXCEPTION')) then
error((),'Access to code list ' || $url || ' failed or timed out.')
else if (not(starts-with($valid_clurl,'text/xml') or starts-with($valid_clurl,'application/xml') or starts-with($valid_clurl,'application/atom+xml'))) then
error((),'Unknown resource type encountered when accessing the atom representation of code list ' || $url || ' at URL ' || $clurl || '.')
else
try {
let $root := fn:doc($clurl)/element()
return
$root//atom:entry
} catch * {
error((),'Code list ' || $url || ' cannot be accessed.')
}
};
(:
@throws: an error that explains why the invocation failed
:)
declare function local:get-code-titles($url as xs:string, $langIds as xs:string*) as xs:string*
{
let $codesAsAtomEntries := (
for $lang in $langIds
return
local:get-codes-in-atom-format($url,$lang)
)
return $codesAsAtomEntries/atom:title/text()
};
declare function local:is-valid-date-or-dateTime($dateString as xs:string?) as xs:boolean
{
if (not($dateString)) then
false()
else
let $date :=
try {
let $tmp := xs:date($dateString)
return
(: NOTE: apparently, the value of the xs:date must be evaluated to be parsed by BaseX :)
'DATE ' || $tmp
} catch * {
'INVALID'
}
let $year :=
try {
let $tmp := xs:gYear($dateString)
return
(: NOTE: apparently, the value of the xs:date must be evaluated to be parsed by BaseX :)
'DATE ' || $tmp
} catch * {
'INVALID'
}
let $yearMonth :=
try {
let $tmp := xs:gYearMonth($dateString)
return
(: NOTE: apparently, the value of the xs:date must be evaluated to be parsed by BaseX :)
'DATE ' || $tmp
} catch * {
'INVALID'
}
let $dateTime :=
try {
let $tmp := xs:dateTime($dateString)
return
(: NOTE: apparently, the value of the xs:date must be evaluated to be parsed by BaseX :)
'DATETIME ' || $tmp
} catch * {
'INVALID'
}
return
if(starts-with($date,'DATE') or starts-with($year,'DATE') or starts-with($yearMonth,'DATE') or starts-with($dateTime,'DATETIME')) then
true()
else
false()
};
(: Start logging :)
let $logentry := local:log('Testing ' || count($records) || ' records')
(: Statistics and assertions follow below :)