-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.ledoux.xqm
181 lines (168 loc) · 6.32 KB
/
models.ledoux.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
xquery version "3.0";
module namespace ledoux.models.ledoux = "ledoux.models.ledoux";
(:~
: This xquery module is an application for ledouxApp
:
: @author emchateau & carolineCorbieres
: @since 2021-05
: @licence GNU http://www.gnu.org/licenses
: @version 0.2
:
: ledouxApp 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.
:
:)
import module namespace G = 'ledoux.globals' at './globals.xqm' ;
declare namespace rest = "http://exquery.org/ns/restxq" ;
declare namespace file = "http://expath.org/ns/file" ;
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization" ;
declare namespace db = "http://basex.org/modules/db" ;
declare namespace web = "http://basex.org/modules/web" ;
declare namespace update = "http://basex.org/modules/update" ;
declare namespace perm = "http://basex.org/modules/perm" ;
declare namespace user = "http://basex.org/modules/user" ;
declare namespace session = 'http://basex.org/modules/session' ;
declare namespace http = "http://expath.org/ns/http-client" ;
declare namespace fetch = "http://basex.org/modules/fetch" ;
declare namespace ev = "http://www.w3.org/2001/xml-events" ;
declare namespace map = "http://www.w3.org/2005/xpath-functions/map" ;
declare namespace xf = "http://www.w3.org/2002/xforms" ;
declare namespace xlink = "http://www.w3.org/1999/xlink" ;
declare namespace ledoux = "ledoux" ;
declare default function namespace "ledoux.models.ledoux" ;
declare default collation "http://basex.org/collation?lang=fr" ;
(:~
: this function return a mime-type for a specified file
:
: @param $name file name
: @return a mime type for the specified file
:)
declare function ledoux.models.ledoux:mime-type($name as xs:string) as xs:string {
fetch:content-type($name)
};
(:~
: this function call a wrapper
:
: @param $content the content to serialize
: @param $outputParams the output params
: @return an updated document and instantiated pattern
:)
declare function wrapper($content as map(*), $outputParams as map(*)) as node()* {
let $layout := file:base-dir() || "files/" || map:get($outputParams, 'layout')
let $wrap := fn:doc($layout)
let $regex := '\{(.+?)\}'
return
$wrap/* update (
for $node in .//*[fn:matches(text(), $regex)] | .//@*[fn:matches(., $regex)]
let $key := fn:analyze-string($node, $regex)//fn:group/text()
return switch ($key)
case 'model' return replace node $node with getModels($content)
case 'trigger' return replace node $node with getTriggers($content)
case 'form' return replace node $node with getForms($content)
case 'data' return replace node $node with $content?data
case 'content' return replace node $node with $outputParams?mapping
default return associate($content, $outputParams, $node)
)
};
(:~
: this function get the models
:
: @param $content the content params
: @return the default models or its instance version
: @bug not generic enough
: @todo add control on models and instances number
:)
declare function getModels($content as map(*)){
let $instances := map:get($content, 'instance')
let $path := map:get($content, 'path')
let $models := map:get($content, 'model')
for $model at $i in $models return
if ($instances[$i])
then (
copy $doc := fn:doc(file:base-dir() || "files/" || $model)
modify replace value of node $doc/xf:model/xf:instance[@id=fn:substring-before($model, 'Model.xml')]/@src with '/ledoux/' || $path || '/' || $instances[$i]
return $doc
)
else
fn:doc(file:base-dir() || "files/" || $model)
};
(:~
: this function get the models
:
: @param $content the content params
: @return the default models or its instance version
: @bug not generic enough
:)
declare function getTriggers($content as map(*)){
let $instance := map:get($content, 'instance')
let $path := map:get($content, 'path')
let $triggers := map:get($content, 'trigger')
return if ($triggers) then fn:doc(file:base-dir() || "files/" || $triggers) else ()
};
(:~
: this function get the forms
:
: @param $content the content params
: @return the default forms or its instance version
: @bug not generic enough
: @todo make loop if multiple forms
:)
declare function getForms($content as map(*)){
let $instance := map:get($content, 'instance')
let $path := map:get($content, 'path')
let $forms := map:get($content, 'form')
return if ($forms) then fn:doc(file:base-dir() || "files/" || $forms) else ()
};
(:~
: this function dispatch the content with the data
:
: @param $content the content to serialize
: @param $outputParams the serialization params
: @return an updated node with the data
: @bug the behavior is not complete
:)
declare
%updating
function associate($content as map(*), $outputParams as map(*), $node as node()) {
let $regex := '\{(.+?)\}'
let $keys := fn:analyze-string($node, $regex)//fn:group/text()
let $values := map:get($content, $keys)
return typeswitch ($values)
case document-node() return replace node $node with $values
case empty-sequence() return ()
case text() return replace value of node $node with $values
case xs:string return replace value of node $node with $values
case xs:string+ return
if ($node instance of attribute()) (: when key is an attribute value :)
then
replace node $node/parent::* with
element {fn:name($node/parent::*)} {
for $att in $node/parent::*/(@* except $node) return $att,
attribute {fn:name($node)} {fn:string-join($values, ' ')},
$node/parent::*/text()
}
else
replace node $node with
for $value in $values
return element {fn:name($node)} {
for $att in $node/@* return $att,
$value
}
case xs:integer return replace value of node $node with xs:string($values)
case element()+ return replace node $node with
for $value in $values
return element {fn:name($node)} {
for $att in $node/@* return $att, "todo"
}
default return replace value of node $node with 'default'
};
(:~
: this function
:)
(: declare
%output:indent('no')
function entry($node as node()*, $options as map(*)) as item()* {
for $i in $node return dispatch($i, $options)
}; :)