-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxco-debug.xqm
81 lines (73 loc) · 2.99 KB
/
xco-debug.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
(:
: xco-debug - functions providing debug functionality
:)
module namespace dg="http://www.parsqube.de/ns/xco/debug";
import module namespace u="http://www.parsqube.de/ns/xco/util"
at "xco-util.xqm";
declare namespace z="http://www.parsqube.de/ns/xco/structure";
declare variable $dg:DIR_DEBUG := '/projects/sbb/work';
(:~
: Inserts into the options map a 'debug' entry, which is a map
: containing two keys:
: 'dir' - the output folder into which debug files are written
: 'filter' - a map associating debug ids with filter strings
:
: The 'debug' entry is evaluated by function db:WRITE_FILE. The
: entry controls where and if a file is written.
:
: Example:
: debugFilter: 5=PriceType, 7, 8=OrderElem
: debugDir: /projects/tmp
:
: 'debug' entry of options:
: debug:
: dir: /projects/tmp
: filter:
: 5: 'PriceType'
: 7: '*'
: 8: 'OrderElem
:)
declare function dg:SET_DEBUG_OPTIONS($debugDir as xs:string?,
$debugFilter as xs:string?,
$options as map(xs:string, item()*))
as map(xs:string, item()*) {
if (not($debugFilter) and not($debugFilter)) then $options else
let $filterItems := $debugFilter ! tokenize(., ',\s*')
let $filterMap := map:merge(
for $item in $filterItems
return
if (not(contains($item, '='))) then map:entry($item, '*')
else
let $id := replace($item, '\s*=.*', '')
let $cond := replace($item, '^.*?=\s*', '')
return map:entry($id, $cond))
let $outputDir := ($debugDir, '.')[1]
let $debugMap := map{'dir': $outputDir, 'filter': $filterMap}
(: let $_DEBUG := trace($debugMap, '___DEBUG_MAP: ') :)
return map:put($options, 'debug', $debugMap)
};
declare function dg:WRITE_FILE($item as item()*,
$fname as xs:string,
$debugId as xs:string,
$debugCond as xs:string?,
$options as map(xs:string, item()*))
as empty-sequence() {
if (empty($item) or empty($options?debug)) then () else
let $debugPass := $options?debug?filter($debugId)
return
if (not($debugPass = ('*', $debugCond))) then () else
let $debugDir := ($options?debug?dir, '.')[1]
let $path := $debugDir||'/'||$fname
return
if ($item[1] instance of node()) then
file:write($path, $item ! u:prettyNode(.), map{'indent': 'yes'})
else file:write($path, string-join($item, '
'))
};
declare function dg:writeNode($fname as xs:string,
$node as node(),
$options as map(xs:string, item()*))
as empty-sequence() {
let $path := $dg:DIR_DEBUG||'/'||$fname
return
file:write($path, $node, $options)
};