Skip to content
Snippets Groups Projects
Commit aed9eeeb authored by Peter Stadler's avatar Peter Stadler
Browse files

added more XQuery modules

parent 63f2de48
No related branches found
No related tags found
No related merge requests found
......@@ -2,12 +2,11 @@ xquery version "3.1" encoding "UTF-8";
module namespace app-shared="http://xquery.weber-gesamtausgabe.de/modules/app-shared";
declare namespace tei="http://www.tei-c.org/ns/1.0";
declare namespace mei="http://www.music-encoding.org/ns/mei";
import module namespace functx="http://www.functx.com";
import module namespace str="http://xquery.weber-gesamtausgabe.de/modules/str" at "str.xqm";
import module namespace templates="http://exist-db.org/xquery/templates" at "/db/apps/shared-resources/content/templates.xql";
declare variable $app-shared:FUNCTION_LOOKUP_ERROR := QName("http://xquery.weber-gesamtausgabe.de/modules/app-shared", "FunctionLookupError");
(:~
: Set an attribute to the value given in the $model map
......@@ -60,8 +59,6 @@ declare
: A non-wrapping alternative to the standard templates:each()
: Gets rid of the superfluous first list item
:
: At present, only $callbackArity=2 is supported
:
: @author Peter Stadler
:)
declare
......@@ -73,8 +70,8 @@ declare
if($max castable as xs:integer and $max != '0') then subsequence($model($from), 1, $max)
else $model($from)
let $callbackFunc :=
try { function-lookup(xs:QName($callback), xs:int($callbackArity)) }
catch * { core:logToFile('error', 'Failed to lookup function "' || $callback ) }
try { function-lookup(xs:QName($callback), 2) }
catch * { error($app-shared:FUNCTION_LOOKUP_ERROR, 'Failed to lookup function "' || $callback || '". Error code was "' || $err:code || '". Error message was "' || $err:description || '".') }
return (
for $item in $items
return
......
xquery version "3.1" encoding "UTF-8";
(:~
: XQuery module for caching documents and collections
~:)
module namespace cache="http://xquery.weber-gesamtausgabe.de/modules/cache";
import module namespace functx="http://www.functx.com";
import module namespace wega-util-shared="http://xquery.weber-gesamtausgabe.de/modules/wega-util-shared" at "wega-util-shared.xqm";
declare variable $cache:TOO_MANY_PARAMETERS_ERROR := QName("http://xquery.weber-gesamtausgabe.de/modules/cache", "TooManyParametersError");
declare variable $cache:UNSUPPORTED_PARAMETER_VALUE_ERROR := QName("http://xquery.weber-gesamtausgabe.de/modules/cache", "UnsupportedParameterValueError");
(:~
: A caching function for documents (XML and binary)
:
: @author Peter Stadler
: @param $docURI the database URI of the document
: @param $callBack a function to create the document content when the document is outdated or not available
: @param $lease an xs:dayTimeDuration value of how long the cache should persist, e.g. P999D (= 999 days).
: Alternatively, $lease can be a callback function – then one argument (the last change date of the file as xs:date()?) will be provided
: and the function should return xs:boolean
: @return the cached document
:)
declare function cache:doc($docURI as xs:string, $callback as function() as item(), $callback-params as item()*, $lease as item()?, $onFailure as function() as item()*) as item()* {
let $fileName := functx:substring-after-last($docURI, '/')
let $collection := functx:substring-before-last($docURI, '/')
let $currentDateTimeOfFile :=
if(wega-util-shared:doc-available($docURI)) then xmldb:last-modified($collection, $fileName)
else if(util:binary-doc-available($docURI)) then xmldb:last-modified($collection, $fileName)
else ()
let $updateNecessary :=
typeswitch($lease)
case xs:dayTimeDuration return
$currentDateTimeOfFile + $lease lt current-dateTime()
or empty($lease)
or empty($currentDateTimeOfFile)
case function() as xs:boolean return $lease($currentDateTimeOfFile)
default return error($cache:UNSUPPORTED_PARAMETER_VALUE_ERROR, 'The parameter value for $lease must be xs:dayTimeDuration()? or a function reference which must take exactly one argument.')
return
if($updateNecessary) then (
let $content :=
if(count($callback-params) eq 0) then $callback()
else if(count($callback-params) eq 1) then $callback($callback-params)
else if(count($callback-params) eq 2) then $callback($callback-params[1], $callback-params[2])
else if(count($callback-params) eq 3) then $callback($callback-params[1], $callback-params[2], $callback-params[3])
else if(count($callback-params) eq 4) then $callback($callback-params[1], $callback-params[2], $callback-params[3], $callback-params[4])
else error($cache:TOO_MANY_PARAMETERS_ERROR, 'Too many arguments to callback function within cache:doc(). A maximum of 4 arguments is supported')
let $mime-type := wega-util-shared:guess-mimeType-from-suffix(functx:substring-after-last($docURI, '.'))
let $store-file := cache:store-file($collection, $fileName, $content, $mime-type, $onFailure)
return
if(util:binary-doc-available($store-file)) then util:binary-doc($store-file)
else if(wega-util-shared:doc-available($store-file)) then doc($store-file)
else ()
)
else if(util:binary-doc-available($docURI)) then util:binary-doc($docURI)
else if(wega-util-shared:doc-available($docURI)) then doc($docURI)
else ()
};
(:~
: Store some content as file in the db
: (Helper function for cache:doc())
:
: @author Peter Stadler
: @param $collection the collection to put the file in. If it does not exist, it will be created
: @param $fileName the filename of the to be created resource with filename extension
: @param $contents the content to store. Either a node, an xs:string, a Java file object or an xs:anyURI
: @return Returns the path to the newly created resource, empty sequence otherwise
:)
declare %private function cache:store-file($collection as xs:string, $fileName as xs:string, $contents as item(), $mime-type as xs:string, $onFailure as function() as item()*) as xs:string? {
let $createCollection :=
for $coll in tokenize($collection, '/')
let $parentColl := substring-before($collection, $coll)
return
if(xmldb:collection-available($parentColl || '/' || $coll)) then ()
else xmldb:create-collection($parentColl, $coll)
return
try { xmldb:store($collection, $fileName, $contents, $mime-type) }
catch * { $onFailure($err:code, $err:description) }
};
xquery version "3.1" encoding "UTF-8";
(:~
: XQuery module with utility functions
~:)
module namespace wega-util-shared="http://xquery.weber-gesamtausgabe.de/modules/wega-util-shared";
(:~
: A slight modification of the standard XPath function fn:doc-available()
: which will return false() for binary documents instead of failing
~:)
declare function wega-util-shared:doc-available($uri as xs:string?) as xs:boolean {
try {doc-available($uri)}
catch * {false()}
};
(:~
: Helper function for guessing a mime-type from a file extension
: (Should be expanded to read in $exist.home$/mime-types.xml)
:
: @author Peter Stadler
: @param $suffix the file extension
: @return the mime-type or the empty sequence when no match was found
:)
declare function wega-util-shared:guess-mimeType-from-suffix($suffix as xs:string) as xs:string? {
switch($suffix)
case 'xml' return 'application/xml'
case 'rdf' return 'application/rdf+xml'
case 'jpg' return 'image/jpeg'
case 'png' return 'image/png'
case 'txt' return 'text/plain'
default return ()
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment