forked from jsulak/eXist-XProc-Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheXist.xpl
815 lines (684 loc) · 32.1 KB
/
eXist.xpl
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
<?xml version="1.0" encoding="iso-8859-1"?>
<p:library xmlns:p="http://www.w3.org/ns/xproc" xmlns:c="http://www.w3.org/ns/xproc-step"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:cx="http://xmlcalabash.com/ns/extensions" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:saxon="http://saxon.sf.net/" xmlns:exist="http://exist.sourceforge.net/NS/exist"
xmlns:wxp="http://www.wordsinboxes.com/xproc" xmlns:ex="http://www.wordsinboxes.com/xproc/exist" version="1.0" >
<!--
Copyright 2009 James Sulak
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<div>
<h1>eXist XProc Extension Library</h1>
<h2>Version 0.1</h2>
<p>The steps defined in this library are implemented using the eXist REST interface.</p>
<p>Contact: James Sulak</p>
<p>Public repository: http://bitbucket.org/jasulak/exist-xproc-library/</p>
</div>
</p:documentation>
<!--<p:import href="library-1.0.xpl"/>-->
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h3>ex:copy</h3>
<p>The <code>ex:copy</code> step copies a resource or collection to a new destination.</p>
<p>It returns a <code><c:result/></code> containing the absolute URI of the target.</p>
</p:documentation>
<p:declare-step type="ex:copy">
<p:output port="result" primary="true"/>
<p:option name="href" required="true"/> <!-- URI of collection -->
<p:option name="user" select="''"/> <!-- eXist user -->
<p:option name="password" select="''"/> <!-- eXist password -->
<p:option name="resource" select="''"/> <!-- name of resource to copy -->
<p:option name="collection" select="''"/> <!-- name of collection to copy -->
<p:option name="target" required="true" /> <!-- name of target collection -->
<!-- Create a uri without the trailing slash -->
<p:variable name="clean-uri" select="replace($href, '(.*)/$', '$1')" >
<p:empty />
</p:variable>
<p:variable name="base-uri" select="replace($href, '^(.*)/db/?.*$', '$1')">
<p:empty />
</p:variable>
<p:variable name="parent-collection" select="replace(replace($href, '.*(/db.*)$', '$1'), '[//]$', '')">
<p:empty/>
</p:variable>
<p:variable name="clean-target" select="replace($target, '(.*)/$', '$1')" >
<p:empty />
</p:variable>
<p:identity name="initial-request">
<p:input port="source">
<p:inline>
<c:query xmlns="http://exist.sourceforge.net/NS/exist" start="1" max="20" cache="no">
<c:text>
declare namespace c="http://www.w3.org/ns/xproc-step";
let $resource := "${resource}"
let $collection := "${collection}"
let $login := xmldb:login("xmldb:exist:///db", "${user}", "${password}")
let $response := if ($resource != '')
then xmldb:copy("${parent-collection}", "${target}", "${resource}")
else xmldb:copy("${parent-collection}/${collection}", "${target}")
return (element c:result { concat(request:get-url(), "${target}/${resource}${collection}") })
</c:text>
</c:query>
</p:inline>
</p:input>
</p:identity>
<!-- Abort step if wrong options specified -->
<p:choose>
<p:when test="($collection = '' and $resource = '') or ($collection != '' and $resource != '')">
<p:error code="invalid-options"/>
</p:when>
<p:otherwise>
<p:identity />
</p:otherwise>
</p:choose>
<wxp:resolve-placeholders>
<p:input port="parameters">
<p:empty />
</p:input>
<p:with-param name="user" select="$user" />
<p:with-param name="password" select="$password" />
<p:with-param name="parent-collection" select="$parent-collection" />
<p:with-param name="target" select="$clean-target" />
<p:with-param name="resource" select="$resource" />
<p:with-param name="collection" select="$collection" />
</wxp:resolve-placeholders>
<!--<cx:message>
<p:with-option name="message" select="concat($base-uri, $target, '/', $resource)" />
</cx:message>
<cx:message>
<p:with-option name="message" select="$base-uri" />
</cx:message>-->
<ex:xquery>
<p:with-option name="user" select="$user"/>
<p:with-option name="password" select="$password"/>
<p:with-option name="href" select="$base-uri"/>
</ex:xquery>
</p:declare-step>
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h3>ex:move</h3>
<p>The <code>ex:move</code> step move a resource or collection to a new destination.</p>
<p>It returns a <code><c:result/></code> containing the absolute URI of the target.</p>
</p:documentation>
<p:declare-step type="ex:move">
<p:output port="result" primary="true"/>
<p:option name="href" required="true"/> <!-- URI of collection -->
<p:option name="user" select="''"/> <!-- eXist user -->
<p:option name="password" select="''"/> <!-- eXist password -->
<p:option name="resource" select="''"/> <!-- name of resource to move -->
<p:option name="collection" select="''"/> <!-- name of collection to move -->
<p:option name="target" required="true" /> <!-- name of target collection -->
<!-- Create a uri without the trailing slash -->
<p:variable name="clean-uri" select="replace($href, '(.*)/$', '$1')" >
<p:empty />
</p:variable>
<p:variable name="base-uri" select="replace($href, '^(.*)/db/?.*$', '$1')">
<p:empty />
</p:variable>
<p:variable name="parent-collection" select="replace(replace($href, '.*(/db.*)$', '$1'), '[//]$', '')">
<p:empty/>
</p:variable>
<p:variable name="clean-target" select="replace($target, '(.*)/$', '$1')" >
<p:empty />
</p:variable>
<p:identity name="initial-request">
<p:input port="source">
<p:inline>
<c:query xmlns="http://exist.sourceforge.net/NS/exist" start="1" max="20" cache="no">
<c:text>
declare namespace c="http://www.w3.org/ns/xproc-step";
let $resource := "${resource}"
let $collection := "${collection}"
let $login := xmldb:login("xmldb:exist:///db", "${user}", "${password}")
let $response := if ($resource != '')
then xmldb:move("${parent-collection}", "${target}", "${resource}")
else xmldb:move("${parent-collection}/${collection}", "${target}")
return (element c:result { concat(request:get-url(), "${target}/${resource}${collection}") })
</c:text>
</c:query>
</p:inline>
</p:input>
</p:identity>
<!-- Abort step if wrong options specified -->
<p:choose>
<p:when test="($collection = '' and $resource = '') or ($collection != '' and $resource != '')">
<p:error code="invalid-options"/>
</p:when>
<p:otherwise>
<p:identity />
</p:otherwise>
</p:choose>
<wxp:resolve-placeholders>
<p:input port="parameters">
<p:empty />
</p:input>
<p:with-param name="user" select="$user" />
<p:with-param name="password" select="$password" />
<p:with-param name="parent-collection" select="$parent-collection" />
<p:with-param name="target" select="$clean-target" />
<p:with-param name="resource" select="$resource" />
<p:with-param name="collection" select="$collection" />
</wxp:resolve-placeholders>
<!--<cx:message>
<p:with-option name="message" select="concat($base-uri, $target, '/', $resource)" />
</cx:message>
<cx:message>
<p:with-option name="message" select="$base-uri" />
</cx:message>-->
<ex:xquery>
<p:with-option name="user" select="$user"/>
<p:with-option name="password" select="$password"/>
<p:with-option name="href" select="$base-uri"/>
</ex:xquery>
</p:declare-step>
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h3>ex:extract</h3>
<p>The <code>ex:extract</code> step extracts resources from an eXist database.
If <code>resource</code> is specified, then it extracts a single resource from
the collection specified by the <code>href</code> option. If <code>resource</code> is not specified,
then it extracts all the resources contained in the collection specified by <code>href</code>.
If <code>subcollections</code> is set to 'true', then it recurses through all the subcollections
and extracts those resources as well.</p>
<p>The step fails if the resource is inaccessible or does not exist.</p>
</p:documentation>
<p:declare-step type="ex:extract">
<p:output port="result" sequence="true"/>
<p:option name="href" required="true"/> <!-- URI of collection -->
<p:option name="user" select="''"/> <!-- eXist username -->
<p:option name="password" select="''"/> <!-- eXist password -->
<p:option name="resource" select="''" /> <!-- resource to extract -->
<p:option name="subcollections" select="'false'"/> <!-- extract subcollections? (boolean) -->
<p:option name="indent" select="'false'" /> <!-- indent output? -->
<!-- Create a uri without the trailing slash -->
<p:variable name="clean-uri" select="replace($href, '(.*)/$', '$1')" >
<p:empty />
</p:variable>
<!-- First, choose between extracting a single file, and looping through an entire collection -->
<p:choose>
<p:xpath-context>
<p:empty />
</p:xpath-context>
<p:when test="$resource != ''">
<wxp:smart-http-get>
<p:with-option name="href" select="concat($clean-uri, '/', $resource, if ($indent = 'true') then '' else '?_indent=no')" >
<p:empty />
</p:with-option>
<p:with-option name="password" select="$password">
<p:empty />
</p:with-option>
<p:with-option name="user" select="$user">
<p:empty />
</p:with-option>
</wxp:smart-http-get>
<!-- If it's XML, go ahead and unwrap it. -->
<!-- Otherwise, wrap it in a c:result -->
<p:choose>
<p:when test="//c:body[contains(@content-type, 'xml')]">
<p:unwrap match="//c:*"/>
</p:when>
<p:otherwise>
<p:filter select="//c:body"/>
<p:rename match="c:body" new-name="c:result" />
</p:otherwise>
</p:choose>
<p:add-attribute attribute-name="xml:base" match="/*">
<p:with-option name="attribute-value" select="concat($clean-uri, '/', $resource)" />
</p:add-attribute>
</p:when>
<p:otherwise>
<!-- Use the list step to get all the items we need.
Then loop through them and extract.
-->
<ex:list>
<p:with-option name="href" select="$clean-uri">
<p:empty/>
</p:with-option>
<p:with-option name="collections" select="$subcollections">
<p:empty/>
</p:with-option>
<p:with-option name="user" select="$user">
<p:empty />
</p:with-option>
<p:with-option name="password" select="$password">
<p:empty />
</p:with-option>
</ex:list>
<p:for-each>
<p:iteration-source select="/c:collection/*"/>
<p:output port="result" sequence="true"/>
<p:choose>
<p:when test="c:resource">
<!--<cx:message>
<p:with-option name="message" select="concat('resource: ', $clean-uri, '/', c:resource/@name)"
/>
</cx:message>-->
<ex:extract>
<p:with-option name="href" select="$clean-uri"/>
<p:with-option name="resource" select="c:resource/@name"/>
<p:with-option name="user" select="$user" />
<p:with-option name="password" select="$password" />
<p:with-option name="indent" select="$indent" />
</ex:extract>
</p:when>
<p:otherwise>
<!--<cx:message>
<p:with-option name="message"
select="concat('collection: ', $clean-uri, '/', c:collection/@name)"/>
</cx:message>-->
<ex:extract subcollections="true">
<p:with-option name="href" select="concat($clean-uri, '/', c:collection/@name)"/>
<p:with-option name="user" select="$user" />
<p:with-option name="password" select="$password" />
</ex:extract>
</p:otherwise>
</p:choose>
</p:for-each>
</p:otherwise>
</p:choose>
</p:declare-step>
<!-- TODO: Make it conditionally create collections -->
<!-- TODO: optional authentication? -->
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h3>ex:store</h3>
<p>The <code>ex:store</code> step stores the document provided on the input port in an eXist
database in the location provided by the <code>href</code> option.</p>
<p>It returns a <code><c:result/></code> containing the absolute URI of the stored file.
The step fails if the resource cannot be stored.</p>
</p:documentation>
<p:declare-step type="ex:store" name="store-def">
<p:input port="source" primary="true" sequence="false"/>
<p:output port="result"/>
<p:option name="href" required="true"/> <!-- URI of collection -->
<p:option name="user" select="''"/> <!-- eXist username -->
<p:option name="password" select="''"/> <!-- eXist password -->
<p:option name="resource" required="true"/> <!-- name of new resource -->
<!-- Create a uri without the trailing slash -->
<p:variable name="clean-uri" select="replace($href, '(.*)/$', '$1')" />
<p:wrap wrapper="c:body" match="/"/>
<p:add-attribute attribute-name="content-type" attribute-value="text/xml" match="/c:body"/>
<p:wrap wrapper="c:request" match="/"/>
<p:add-attribute attribute-name="username" match="/c:request">
<p:with-option name="attribute-value" select="$user"/>
</p:add-attribute>
<p:add-attribute attribute-name="password" match="/c:request">
<p:with-option name="attribute-value" select="$password"/>
</p:add-attribute>
<p:add-attribute attribute-name="href" match="/c:request">
<p:with-option name="attribute-value" select="concat($clean-uri, '/', $resource)"/>
</p:add-attribute>
<p:set-attributes match="c:request">
<p:input port="attributes">
<p:inline>
<c:request method="put" auth-method="Basic" send-authorization="true" detailed="true"
status-only="true"/>
</p:inline>
</p:input>
</p:set-attributes>
<p:http-request/>
<wxp:check-status success-status="201">
<p:with-option name="failonerror" select="'true'"/>
<p:with-option name="return-string" select="concat($clean-uri, '/', $resource)"/>
</wxp:check-status>
</p:declare-step>
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h3>ex:remove</h3>
<p>The <code>ex:remove</code> step removes a single resource or collection from the collection
specified in the <code>href</code> option.</p>
<p>It returns a <code><c:result/></code> containing the absolute URI of the deleted file.
The step fails if the resource does not exist or cannot be deleted. </p>
</p:documentation>
<p:declare-step type="ex:remove" name="remove-def">
<p:output port="result" primary="true"/>
<p:option name="href"/> <!-- URI of collection -->
<p:option name="user" select="''"/> <!-- eXist password -->
<p:option name="password" select="''"/> <!-- eXist username -->
<p:option name="collection" select="''"/> <!-- name of subcollection to remove -->
<p:option name="resource" select="''"/> <!-- name of resource to remove -->
<p:variable name="parent-collection" select="replace(replace($href, '.*(/db.*)$', '$1'), '[//]$', '')">
<p:empty/>
</p:variable>
<!-- Create a uri without the trailing slash -->
<p:variable name="clean-uri" select="replace($href, '(.*)/$', '$1')" >
<p:empty />
</p:variable>
<p:variable name="base-uri" select="replace($clean-uri, '^(.*)/db/?.*$', '$1')">
<p:empty />
</p:variable>
<p:identity name="initial-request">
<p:input port="source">
<p:inline>
<c:request method="delete" status-only="true" detailed="true" auth-method="Basic"
send-authorization="true" />
</p:inline>
</p:input>
</p:identity>
<!-- Abort step if wrong options specified -->
<p:choose>
<p:when test="($collection = '' and $resource = '') or ($collection != '' and $resource != '')">
<p:error code="invalid-options"/>
</p:when>
<p:otherwise>
<p:identity />
</p:otherwise>
</p:choose>
<p:add-attribute match="/c:request" attribute-name="username" >
<p:with-option name="attribute-value" select="$user" />
</p:add-attribute>
<p:add-attribute match="/c:request" attribute-name="password" >
<p:with-option name="attribute-value" select="$password" />
</p:add-attribute>
<p:add-attribute match="/c:request" attribute-name="href">
<p:with-option name="attribute-value" select="concat($clean-uri, '/', $resource, $collection)" />
</p:add-attribute>
<p:http-request />
<wxp:check-status success-status="200">
<p:with-option name="failonerror" select="'true'"/>
<p:with-option name="return-string" select="concat($clean-uri, '/', $resource, $collection)"/>
</wxp:check-status>
</p:declare-step>
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h3>ex:xquery</h3>
<p>The <code>ex:xquery</code> step executes an XQuery expression.</p>
<p>It expects the query to be wrapped in a <code><c:query></code>. Any elements
that appear in the result sequence will be treated as documents with the element as
their document element (same as p:xquery).</p>
</p:documentation>
<!-- TODO: handle sequence of documents? -->
<!-- TODO: check result wrapper element -->
<!-- TODO: Make authentication optional? -->
<p:declare-step type="ex:xquery" name="xquery-def">
<p:input port="source" primary="true"/>
<p:output port="result" primary="true" sequence="true"/>
<p:option name="href" required="true"/> <!-- URI of eXist database -->
<p:option name="user" select="''"/> <!-- eXist username -->
<p:option name="password" select="''"/> <!-- eXist password -->
<!-- Change the xproc document namespace to the exist namespace -->
<p:namespace-rename from="http://www.w3.org/ns/xproc-step"
to="http://exist.sourceforge.net/NS/exist" name="rename"/>
<!-- Insert actual query -->
<p:replace match="exist:query">
<p:input port="source">
<p:inline>
<c:request method="post" auth-method="Basic" send-authorization="true" detailed="true">
<c:body content-type="text/xml">
<exist:query/>
</c:body>
</c:request>
</p:inline>
</p:input>
<p:input port="replacement">
<p:pipe port="result" step="rename"/>
</p:input>
</p:replace>
<p:add-attribute match="c:request" attribute-name="password">
<p:with-option name="attribute-value" select="$password"/>
</p:add-attribute>
<p:add-attribute match="c:request" attribute-name="username">
<p:with-option name="attribute-value" select="$user"/>
</p:add-attribute>
<p:add-attribute match="c:request" attribute-name="href">
<p:with-option name="attribute-value" select="$href"/>
</p:add-attribute>
<p:http-request/>
<!-- Make every top-level document it's own document -->
<p:for-each>
<p:iteration-source select="/c:response/c:body/exist:result/*" />
<p:identity />
</p:for-each>
</p:declare-step>
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h3>ex:create</h3>
<p>The <code>ex:create</code> step creates a single empty collection. The name is specified in the <code>collection option</code>, and its
location is specified in the <code>href</code> option.</p>
<p>It returns a <code><c:result/></code> containing the absolute URI of the created collection.
The step fails if the collection cannot be created.</p>
</p:documentation>
<p:declare-step type="ex:create" name="create-def">
<p:output port="result" primary="true" sequence="true"/>
<p:option name="href" required="true"/> <!-- URI of collection -->
<p:option name="user" select="''"/> <!-- eXist username -->
<p:option name="password" select="''"/> <!-- eXist password -->
<p:option name="collection" required="true"/> <!-- subcollection to create -->
<p:variable name="parent-collection" select="replace(replace($href, '.*(/db.*)$', '$1'), '[//]$', '')">
<p:empty/>
</p:variable>
<p:variable name="base-uri" select="replace($href, '^(.*)/db/?.*$', '$1')">
<p:empty />
</p:variable>
<!-- Create a uri without the trailing slash -->
<p:variable name="clean-uri" select="replace($href, '(.*)/$', '$1')" >
<p:empty />
</p:variable>
<p:identity>
<p:input port="source">
<p:inline>
<c:query xmlns="http://exist.sourceforge.net/NS/exist" start="1" max="20" cache="no">
<c:text>
declare namespace c="http://www.w3.org/ns/xproc-step";
let $login := xmldb:login("xmldb:exist:///db", "${user}", "${password}")
let $response := xmldb:create-collection("${parent-collection}", "${collection}")
return (element c:result { concat(request:get-url(), $response) })
</c:text>
</c:query>
</p:inline>
</p:input>
</p:identity>
<wxp:resolve-placeholders>
<p:input port="parameters">
<p:empty />
</p:input>
<p:with-param name="user" select="$user" />
<p:with-param name="password" select="$password" />
<p:with-param name="parent-collection" select="$parent-collection" />
<p:with-param name="collection" select="$collection" />
</wxp:resolve-placeholders>
<ex:xquery>
<p:with-option name="user" select="$user"/>
<p:with-option name="password" select="$password"/>
<p:with-option name="href" select="$base-uri"/>
</ex:xquery>
</p:declare-step>
<p:documentation xmlns="http://www.w3.org/1999/xhtml">
<h3>ex:list</h3>
<p>The <code>ex:list</code> step returns a list of the resources and/or collections contained in
the collection specified in the <code>href</code> option.</p>
<p>It's behavior is modeled after <code><p:directory-list</code>. It returns a
<code><c:collection></code> containing a sequence of <code><c:resource</code>
and <code><c:collection</code>s. The step fails if the resource does not exist or cannot be deleted.</p>
</p:documentation>
<p:declare-step type="ex:list" name="list-def">
<p:output port="result" primary="true"/>
<p:option name="href" required="true"/> <!-- URI of collection -->
<p:option name="user" select="''" /> <!-- eXist username -->
<p:option name="password" select="''" /> <!-- eXist password -->
<p:option name="resources" select="'true'"/> <!-- list resources? (boolean) -->
<p:option name="collections" select="'true'"/> <!-- list subcollections? (boolean) -->
<!-- Create a uri without the trailing slash -->
<p:variable name="clean-uri" select="replace($href, '(.*)/$', '$1')" >
<p:empty />
</p:variable>
<wxp:smart-http-get>
<p:with-option name="href" select="$clean-uri" >
<p:empty />
</p:with-option>
<p:with-option name="password" select="$password">
<p:empty />
</p:with-option>
<p:with-option name="user" select="$user">
<p:empty />
</p:with-option>
</wxp:smart-http-get>
<p:filter select="//exist:result" />
<p:unwrap match="exist:result"/>
<p:rename match="exist:collection" new-name="c:collection"/>
<p:rename match="exist:resource" new-name="c:resource"/>
<p:choose>
<p:when test="$resources = 'false'">
<p:delete match="c:resource"/>
</p:when>
<p:otherwise>
<p:identity/>
</p:otherwise>
</p:choose>
<p:choose>
<p:when test="$collections = 'false'">
<p:delete match="*/c:collection"/>
</p:when>
<p:otherwise>
<p:identity/>
</p:otherwise>
</p:choose>
</p:declare-step>
<!-- ================================= -->
<!-- Utility Steps -->
<!-- ================================= -->
<p:declare-step type="wxp:resolve-placeholders" name="resolver">
<p:input port="source" primary="true" sequence="false"/>
<p:input port="parameters" kind="parameter"/>
<p:output port="result" />
<p:parameters name="parameters">
<p:input port="parameters">
<p:pipe port="parameters" step="resolver"/>
</p:input>
</p:parameters>
<p:xslt>
<p:input port="source">
<p:pipe port="source" step="resolver"/>
<p:pipe port="result" step="parameters"/>
</p:input>
<p:input port="stylesheet">
<p:inline>
<xsl:stylesheet version="2.0">
<xsl:variable name="values" select="collection()[2]/c:param-set/c:param" as="element()*" />
<xsl:template match="text()" name="text" priority="1">
<xsl:variable name="regex" as="xs:string">
<xsl:text>\$\{([a-zA-Z0-9-]{1,20})\}</xsl:text>
</xsl:variable>
<xsl:analyze-string select="." regex="{$regex}">
<xsl:matching-substring>
<xsl:variable name="placeholder" select="regex-group(1)" />
<xsl:value-of select="$values[@name = $placeholder]/@value" />
</xsl:matching-substring>
<xsl:non-matching-substring>
<xsl:value-of select="." />
</xsl:non-matching-substring>
</xsl:analyze-string>
</xsl:template>
<xsl:template match="@*">
<xsl:attribute name="{name(.)}">
<xsl:call-template name="text" />
</xsl:attribute>
</xsl:template>
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
</p:inline>
</p:input>
</p:xslt>
</p:declare-step>
<!-- Shorthand for a common http-get.
Intelligently chooses whether to use authentication, based on
the presence or absense of $user and $password.
(That may not be actually necessary) -->
<p:declare-step type="wxp:smart-http-get">
<p:output port="result" sequence="true" />
<p:option name="href" required="true"/>
<p:option name="user" select="''"/>
<p:option name="password" select="''"/>
<p:identity name="initial-request">
<p:input port="source">
<p:inline>
<c:request method="get" detailed="true" />
</p:inline>
</p:input>
</p:identity>
<p:add-attribute attribute-name="href" match="/c:request">
<p:with-option name="attribute-value" select="$href" />
</p:add-attribute>
<p:choose>
<!-- Catch invalid option definitions -->
<p:when test="($user != '' and $password = '') or ($user = '' and $password != '')">
<p:error code="invalid-options"/>
</p:when>
<p:when test="$user != '' and $password != ''">
<p:add-attribute attribute-name="auth-method" match="/c:request" attribute-value="Basic" />
<p:add-attribute attribute-name="send-authorization" match="/c:request" attribute-value="true" />
<p:add-attribute attribute-name="username" match="/c:request">
<p:with-option name="attribute-value" select="$user"/>
</p:add-attribute>
<p:add-attribute attribute-name="password" match="/c:request">
<p:with-option name="attribute-value" select="$password"/>
</p:add-attribute>
</p:when>
<p:otherwise>
<p:identity />
</p:otherwise>
</p:choose>
<p:http-request />
</p:declare-step>
<!-- Utility step for resolving inline placeholding variables -->
<p:declare-step type="wxp:resolve-placeholder">
<p:input port="source" primary="true"/>
<p:output port="result" primary="true"/>
<p:option name="placeholder" required="true"/>
<p:option name="value" required="true"/>
<p:string-replace match="text() | attribute()">
<p:with-option name="replace"
select="concat('replace(., "\$\{', $placeholder, '\}","', $value, '")')"
/>
</p:string-replace>
</p:declare-step>
<!-- Utility step for checking an http-response status -->
<p:declare-step type="wxp:check-status" name="check-status">
<p:input port="source"/>
<p:output port="result"/>
<p:option name="success-status" required="true"/>
<p:option name="return-string" select="'Success'"/>
<p:option name="failonerror" required="true"/>
<p:choose name="determine-result">
<!-- First, check if it worked -->
<p:when test="/c:response/@status = $success-status">
<p:string-replace match="c:result/text()">
<p:with-option name="replace" select="concat('"', $return-string, '"')"/>
<p:input port="source">
<p:inline>
<c:result>REPLACE</c:result>
</p:inline>
</p:input>
</p:string-replace>
</p:when>
<!-- If not, then determine if we need to fail loudly or quietly -->
<p:otherwise>
<p:choose>
<p:when test="$failonerror = 'true'">
<p:error>
<!-- I think this is correct, but Calabash always reports "No outputs allowed" -->
<!-- Not really sure how to test this. -->
<p:with-option name="code" select="concat('R', /c:response/@status)"/>
</p:error>
</p:when>
<p:otherwise>
<p:replace match="/*">
<p:input port="replacement">
<p:inline>
<c:result>Fail</c:result>
</p:inline>
</p:input>
</p:replace>
</p:otherwise>
</p:choose>
</p:otherwise>
</p:choose>
</p:declare-step>
</p:library>