[testsuite] Fix running tests on windows

xquery version "3.1";

(:~
 : Test fn:unparsed-text and friends
 :)
module namespace upt="http://exist-db.org/xquery/test/unparsed-text";

import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";

declare
    %test:setUp
function upt:store() {
    let $col := xmldb:create-collection("/db", "test-unparsed-text")
    return (
        xmldb:store($col, "test.txt", "Hello
world!
Hello
world!", "application/octet-stream"),
        xmldb:store($col, "test.xml", "
Hello world!
", "application/xml")
    )
};

declare
    %test:tearDown
function upt:cleanup() {
    xmldb:remove("/db/test-unparsed-text")
};

declare
    %test:assertXPath("contains($result, 'eXist')")
function upt:unparsed-text-from-url() {
    unparsed-text("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README")
};

declare
    %test:assertXPath("contains($result, '----')")
function upt:unparsed-text-lines-from-url() {
    unparsed-text-lines("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README")[2]
};

declare
    %test:assertEquals(6)
function upt:unparsed-text-lines-from-url-count() {
    count(unparsed-text-lines("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README"))
};

declare
    %test:assertXPath("contains($result, 'eXist')")
function upt:unparsed-text-from-url-encoding() {
    unparsed-text("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README", "UTF-8")
};

declare
    %test:assertEquals("Hello
world!
Hello
world!")
function upt:unparsed-text-from-db() {
    unparsed-text("/db/test-unparsed-text/test.txt")
};

declare
    %test:assertEquals("Hello
world!
Hello
world!")
function upt:unparsed-text-from-db-full() {
    unparsed-text("xmldb:exist:///db/test-unparsed-text/test.txt")
};

declare
    %test:assertEquals("Hello", "world!", "Hello", "world!")
function upt:unparsed-text-lines-from-db() {
    unparsed-text-lines("/db/test-unparsed-text/test.txt")
};

declare
    %test:assertEquals("
Hello world!
")
function upt:unparsed-text-from-db-xml() {
    unparsed-text("/db/test-unparsed-text/test.xml")
};

declare
    %test:assertEquals(26436)
function upt:unparsed-text-from-file-dba() {
    let $home := translate(system:get-exist-home(), "\", "/")
    let $url := ``[file://`{$home}`/LICENSE]``
    return
        string-length(unparsed-text($url))
};

declare
    %test:assertEquals(504)
function upt:unparsed-text-lines-from-file-dba() {
    let $home := translate(system:get-exist-home(), "\", "/")
    let $url := ``[file://`{$home}`/LICENSE]``
    return
        count(unparsed-text-lines($url))
};

declare
    %test:assertError("FOUT1170")
function upt:unparsed-text-from-file-not-allowed() {
    system:as-user("guest", "guest", unparsed-text("file:///etc/passwd"))
};

declare
    %test:assertError("FOUT1170")
function upt:fragment-identifier() {
    unparsed-text-lines("http://www.example.org/#fragment")
};

declare
    %test:assertError("FOUT1170")
function upt:fragment-identifier-encoding() {
    unparsed-text("http://www.example.org/#fragment", "UTF-8")
};

declare
    %test:assertError("FOUT1170")
function upt:invalid-uri() {
    unparsed-text-lines("http://www.example.org/%gg")
};

declare
    %test:assertError("FOUT1170")
function upt:invalid-uri2() {
    unparsed-text-lines(":/")
};

declare
    %test:assertError("FOUT1170")
function upt:non-existant() {
    unparsed-text-lines("http://www.w3.org/fots/unparsed-text/does-not-exist.txt")
};

declare
    %test:assertError("FOUT1170")
function upt:unsupported-scheme() {
    fn:unparsed-text-lines("surely-nobody-supports-this:/path.txt")
};

declare
    %test:assertError("FOUT1170")
function upt:windows-path() {
    unparsed-text-lines("C:\file-might-exist.txt", "utf-8")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-url() {
    unparsed-text-available("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-url-encoding() {
    unparsed-text-available("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README", "UTF-8")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-db() {
    unparsed-text-available("/db/test-unparsed-text/test.txt")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-db-full() {
    unparsed-text-available("xmldb:exist:///db/test-unparsed-text/test.txt")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-db-xml() {
    unparsed-text-available("/db/test-unparsed-text/test.xml")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-fragment-identifier() {
    unparsed-text-available("http://www.example.org/#fragment")
};

declare
    %test:assertFalse
function upt:fragment-identifier-encoding() {
    unparsed-text-available("http://www.example.org/#fragment", "UTF-8")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-invalid-uri() {
    unparsed-text-available("http://www.example.org/%gg")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-invalid-uri2() {
    unparsed-text-available(":/")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-non-existant() {
    unparsed-text-available("http://www.w3.org/fots/unparsed-text/does-not-exist.txt")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-unsupported-scheme() {
    fn:unparsed-text-available("surely-nobody-supports-this:/path.txt")
};

xquery version "3.1";

(:~
 : Test fn:unparsed-text and friends
 :)
module namespace upt="http://exist-db.org/xquery/test/unparsed-text";

import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";

declare
    %test:setUp
function upt:store() {
    let $col := xmldb:create-collection("/db", "test-unparsed-text")
    return (
        xmldb:store($col, "test.txt", "Hello
world!
Hello
world!", "application/octet-stream"),
        xmldb:store($col, "test.xml", "
Hello world!
", "application/xml")
    )
};

declare
    %test:tearDown
function upt:cleanup() {
    xmldb:remove("/db/test-unparsed-text")
};

declare
    %test:assertXPath("contains($result, 'eXist')")
function upt:unparsed-text-from-url() {
    unparsed-text("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README")
};

declare
    %test:assertXPath("contains($result, '----')")
function upt:unparsed-text-lines-from-url() {
    unparsed-text-lines("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README")[2]
};

declare
    %test:assertEquals(6)
function upt:unparsed-text-lines-from-url-count() {
    count(unparsed-text-lines("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README"))
};

declare
    %test:assertXPath("contains($result, 'eXist')")
function upt:unparsed-text-from-url-encoding() {
    unparsed-text("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README", "UTF-8")
};

declare
    %test:assertEquals("Hello
world!
Hello
world!")
function upt:unparsed-text-from-db() {
    unparsed-text("/db/test-unparsed-text/test.txt")
};

declare
    %test:assertEquals("Hello
world!
Hello
world!")
function upt:unparsed-text-from-db-full() {
    unparsed-text("xmldb:exist:///db/test-unparsed-text/test.txt")
};

declare
    %test:assertEquals("Hello", "world!", "Hello", "world!")
function upt:unparsed-text-lines-from-db() {
    unparsed-text-lines("/db/test-unparsed-text/test.txt")
};

declare
    %test:assertEquals("
Hello world!
")
function upt:unparsed-text-from-db-xml() {
    unparsed-text("/db/test-unparsed-text/test.xml")
};

declare
    %test:assertEquals(26436)
function upt:unparsed-text-from-file-dba() {
    let $home := system:get-exist-home()
    let $url := "file:" || $home || "/LICENSE"
    return
        string-length(unparsed-text($url))
};

declare
    %test:assertEquals(504)
function upt:unparsed-text-lines-from-file-dba() {
    let $home := system:get-exist-home()
    let $url := "file:" || $home || "/LICENSE"
    return
        count(unparsed-text-lines($url))
};

declare
    %test:assertError("FOUT1170")
function upt:unparsed-text-from-file-not-allowed() {
    system:as-user("guest", "guest", unparsed-text("file:///etc/passwd"))
};

declare
    %test:assertError("FOUT1170")
function upt:fragment-identifier() {
    unparsed-text-lines("http://www.example.org/#fragment")
};

declare
    %test:assertError("FOUT1170")
function upt:fragment-identifier-encoding() {
    unparsed-text("http://www.example.org/#fragment", "UTF-8")
};

declare
    %test:assertError("FOUT1170")
function upt:invalid-uri() {
    unparsed-text-lines("http://www.example.org/%gg")
};

declare
    %test:assertError("FOUT1170")
function upt:invalid-uri2() {
    unparsed-text-lines(":/")
};

declare
    %test:assertError("FOUT1170")
function upt:non-existant() {
    unparsed-text-lines("http://www.w3.org/fots/unparsed-text/does-not-exist.txt")
};

declare
    %test:assertError("FOUT1170")
function upt:unsupported-scheme() {
    fn:unparsed-text-lines("surely-nobody-supports-this:/path.txt")
};

declare
    %test:assertError("FOUT1170")
function upt:windows-path() {
    unparsed-text-lines("C:\file-might-exist.txt", "utf-8")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-url() {
    unparsed-text-available("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-url-encoding() {
    unparsed-text-available("https://raw.githubusercontent.com/eXist-db/exist/develop/test/src/xquery/README", "UTF-8")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-db() {
    unparsed-text-available("/db/test-unparsed-text/test.txt")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-db-full() {
    unparsed-text-available("xmldb:exist:///db/test-unparsed-text/test.txt")
};

declare
    %test:assertTrue
function upt:unparsed-text-available-from-db-xml() {
    unparsed-text-available("/db/test-unparsed-text/test.xml")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-fragment-identifier() {
    unparsed-text-available("http://www.example.org/#fragment")
};

declare
    %test:assertFalse
function upt:fragment-identifier-encoding() {
    unparsed-text-available("http://www.example.org/#fragment", "UTF-8")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-invalid-uri() {
    unparsed-text-available("http://www.example.org/%gg")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-invalid-uri2() {
    unparsed-text-available(":/")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-non-existant() {
    unparsed-text-available("http://www.w3.org/fots/unparsed-text/does-not-exist.txt")
};

declare
    %test:assertFalse
function upt:unparsed-text-available-unsupported-scheme() {
    fn:unparsed-text-available("surely-nobody-supports-this:/path.txt")
};


[ignore] eXide dialogs: order resources alphabetically

svn path=/trunk/eXist/; revision=14834

(:
 :  eXist Open Source Native XML Database
 :  Copyright (C) 2011 The eXist Project
 :  http://exist-db.org
 :
 :  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 2
 :  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 library; if not, write to the Free Software
 :  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 :
 :  $Id$
 :)
xquery version "1.0";

declare namespace json="http://www.json.org";

declare option exist:serialize "method=json media-type=text/javascript";

import module namespace date="http://exist-db.org/xquery/admin-interface/date" at "../admin/dates.xqm";

declare function local:sub-collections($root as xs:string, $children as xs:string*, $user as xs:string) {
        for $child in $children
		let $processChild :=
			local:collections(concat($root, '/', $child), $child, $user)
		where exists($processChild)
		order by $child ascending
        return
            <children json:array="true">
			{ $processChild }
			</children>
};

declare function local:canRead($collection as xs:string, $user as xs:string) as xs:boolean {
	if (xmldb:is-admin-user($user)) then
		true()
	else
		let $permissions := xmldb:permissions-to-string(xmldb:get-permissions($collection))
		let $owner := xmldb:get-owner($collection)
		let $group := xmldb:get-group($collection)
		let $groups := xmldb:get-user-groups($user)
		return
			if ($owner eq $user) then
				substring($permissions, 1, 1) eq 'r'
			else if ($group = $groups) then
				substring($permissions, 4, 1) eq 'r'
			else
				substring($permissions, 7, 1) eq 'r'
};

declare function local:canWrite($collection as xs:string, $user as xs:string) as xs:boolean {
	if (xmldb:is-admin-user($user)) then
		true()
	else
    	let $permissions := xmldb:permissions-to-string(xmldb:get-permissions($collection))
    	let $owner := xmldb:get-owner($collection)
    	let $group := xmldb:get-group($collection)
    	let $groups := xmldb:get-user-groups($user)
    	return
        	if ($owner eq $user) then
            	substring($permissions, 2, 1) eq 'w'
        	else if ($group = $groups) then
            	substring($permissions, 5, 1) eq 'w'
        	else
            	substring($permissions, 8, 1) eq 'w'
};

declare function local:canWriteResource($collection as xs:string, $resource as xs:string, $user as xs:string) as xs:boolean {
    if (xmldb:is-admin-user($user)) then
		true()
	else
		let $permissions := xmldb:permissions-to-string(xmldb:get-permissions($collection, $resource))
		let $owner := xmldb:get-owner($collection, $resource)
		let $group := xmldb:get-group($collection, $resource)
		let $groups := xmldb:get-user-groups($user)
		return
			if ($owner eq $user) then
				substring($permissions, 2, 1) eq 'w'
			else if ($group = $groups) then
				substring($permissions, 5, 1) eq 'w'
			else
				substring($permissions, 8, 1) eq 'w'
};

declare function local:collections($root as xs:string, $child as xs:string,
	$user as xs:string) {
    let $children := xmldb:get-child-collections($root)
    let $canWrite := local:canWrite($root, $user)
    return
        if (local:canRead($root, $user)) then (
            <title>{$child}</title>,
            <isFolder json:literal="true">true</isFolder>,
            <key>{$root}</key>,
            <writable json:literal="true">{if ($canWrite) then 'true' else 'false'}</writable>,
            <addClass>{if ($canWrite) then 'writable' else 'readable'}</addClass>,
            	if (exists($children)) then
                local:sub-collections($root, $children, $user)
            	else
                ()
        ) else
            ()
};

declare function local:list-collection-contents($collection as xs:string, $user as xs:string) {
    let $subcollections :=
        for $child in xmldb:get-child-collections($collection)
        return
            concat("/", $child)
    for $resource in ($subcollections, xmldb:get-child-resources($collection))
	order by $resource ascending
	return
		$resource
};

declare function local:resources($collection as xs:string, $user as xs:string) {
    let $start := number(request:get-parameter("start", 0)) + 1
    let $endParam := number(request:get-parameter("end", 1000000)) + 1
    let $resources := local:list-collection-contents($collection, $user)
    let $end := if ($endParam gt count($resources)) then count($resources) else $endParam
    let $subset := subsequence($resources, $start, $end - $start + 1)
    return
        <json:value>
            <total json:literal="true">{count($resources)}</total>
            <items>
        {
            for $resource in $subset
            let $isCollection := starts-with($resource, "/")
            let $path :=
                if ($isCollection) then
                    concat($collection, $resource)
                else
                    concat($collection, "/", $resource)
            let $permissions :=
                if ($isCollection) then
                    xmldb:permissions-to-string(xmldb:get-permissions($path))
                else
                    xmldb:permissions-to-string(xmldb:get-permissions($collection, $resource))
            let $owner :=
                if ($isCollection) then
                    xmldb:get-owner($path)
                else
                    xmldb:get-owner($collection, $resource)
            let $group :=
                if ($isCollection) then
                    xmldb:get-group($path)
                else
                    xmldb:get-group($collection, $resource)
            let $lastMod :=
                if ($isCollection) then
                    date:format-dateTime(xmldb:created($path))
                else
                    date:format-dateTime(xmldb:created($collection, $resource))
            let $canWrite :=
                if ($isCollection) then
                    local:canWrite(concat($collection, "/", $resource), $user)
                else
                    local:canWriteResource($collection, $resource, $user)
            order by $resource ascending
            return
                <json:value json:array="true">
                    <name>{if ($isCollection) then substring-after($resource, "/") else $resource}</name>
                    <permissions>{$permissions}</permissions>
                    <owner>{$owner}</owner>
                    <group>{$group}</group>
                    <last-modified>{$lastMod}</last-modified>
                    <writable json:literal="true">{$canWrite}</writable>
                    <isCollection json:literal="true">{$isCollection}</isCollection>
                </json:value>
        }
            </items>
        </json:value>
};

declare function local:create-collection($collName as xs:string, $user as xs:string) {
    let $parent := request:get-parameter("collection", "/db")
    return
        if (local:canWrite($parent, $user)) then
            let $null := xmldb:create-collection($parent, $collName)
            return
                <response status="ok"/>
        else
            <response status="fail">
                <message>You are not allowed to write to collection {$parent}</message>
            </response>
};

declare function local:delete-collection($collName as xs:string, $user as xs:string) {
    if (local:canWrite($collName, $user)) then
        let $null := xmldb:remove($collName)
        return
            <response status="ok"/>
    else
        <response status="fail">
            <message>You are not allowed to write to collection {$collName}</message>
        </response>
};

declare function local:delete-resource($collection as xs:string, $resources as xs:string+, $user as xs:string) {
    let $canWrite :=
        every $resource in $resources
        satisfies
            local:canWriteResource($collection, $resource, $user)
    return
    if ($canWrite) then
        let $null :=
            for $resource in $resources
            return xmldb:remove($collection, $resource)
        return
            <response status="ok"/>
    else
        <response status="fail">
            <message>You are not allowed to delete the resources {string-join($resources, ", ")} in collection {$collection}</message>
        </response>
};

let $deleteCollection := request:get-parameter("remove", ())
let $deleteResource := request:get-parameter("remove[]", ())
let $createCollection := request:get-parameter("create", ())
let $view := request:get-parameter("view", "c")
let $collection := request:get-parameter("root", "/db")
let $collName := replace($collection, "^.*/([^/]+$)", "$1")
let $user := if (session:get-attribute('myapp.user')) then session:get-attribute('myapp.user') else "guest"
return
    if ($deleteCollection) then
        local:delete-collection($deleteCollection, $user)
    else if (exists($deleteResource)) then
        local:delete-resource($collection, $deleteResource, $user)
    else if ($createCollection) then
        local:create-collection($createCollection, $user)
    else if ($view eq "c") then
        <collection json:array="true">
        {local:collections($collection, $collName, $user)}
        </collection>
    else
        local:resources($collection, $user)

(:
 :  eXist Open Source Native XML Database
 :  Copyright (C) 2011 The eXist Project
 :  http://exist-db.org
 :
 :  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 2
 :  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 library; if not, write to the Free Software
 :  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 :
 :  $Id$
 :)
xquery version "1.0";

declare namespace json="http://www.json.org";

declare option exist:serialize "method=json media-type=text/javascript";

import module namespace date="http://exist-db.org/xquery/admin-interface/date" at "../admin/dates.xqm";

declare function local:sub-collections($root as xs:string, $children as xs:string*, $user as xs:string) {
        for $child in $children
		let $processChild :=
			local:collections(concat($root, '/', $child), $child, $user)
		where exists($processChild)
		order by $child ascending
        return
            <children json:array="true">
			{ $processChild }
			</children>
};

declare function local:canRead($collection as xs:string, $user as xs:string) as xs:boolean {
	if (xmldb:is-admin-user($user)) then
		true()
	else
		let $permissions := xmldb:permissions-to-string(xmldb:get-permissions($collection))
		let $owner := xmldb:get-owner($collection)
		let $group := xmldb:get-group($collection)
		let $groups := xmldb:get-user-groups($user)
		return
			if ($owner eq $user) then
				substring($permissions, 1, 1) eq 'r'
			else if ($group = $groups) then
				substring($permissions, 4, 1) eq 'r'
			else
				substring($permissions, 7, 1) eq 'r'
};

declare function local:canWrite($collection as xs:string, $user as xs:string) as xs:boolean {
	if (xmldb:is-admin-user($user)) then
		true()
	else
    	let $permissions := xmldb:permissions-to-string(xmldb:get-permissions($collection))
    	let $owner := xmldb:get-owner($collection)
    	let $group := xmldb:get-group($collection)
    	let $groups := xmldb:get-user-groups($user)
    	return
        	if ($owner eq $user) then
            	substring($permissions, 2, 1) eq 'w'
        	else if ($group = $groups) then
            	substring($permissions, 5, 1) eq 'w'
        	else
            	substring($permissions, 8, 1) eq 'w'
};

declare function local:canWriteResource($collection as xs:string, $resource as xs:string, $user as xs:string) as xs:boolean {
    if (xmldb:is-admin-user($user)) then
		true()
	else
		let $permissions := xmldb:permissions-to-string(xmldb:get-permissions($collection, $resource))
		let $owner := xmldb:get-owner($collection, $resource)
		let $group := xmldb:get-group($collection, $resource)
		let $groups := xmldb:get-user-groups($user)
		return
			if ($owner eq $user) then
				substring($permissions, 2, 1) eq 'w'
			else if ($group = $groups) then
				substring($permissions, 5, 1) eq 'w'
			else
				substring($permissions, 8, 1) eq 'w'
};

declare function local:collections($root as xs:string, $child as xs:string,
	$user as xs:string) {
    let $children := xmldb:get-child-collections($root)
    let $canWrite := local:canWrite($root, $user)
    return
        if (local:canRead($root, $user)) then (
            <title>{$child}</title>,
            <isFolder json:literal="true">true</isFolder>,
            <key>{$root}</key>,
            <writable json:literal="true">{if ($canWrite) then 'true' else 'false'}</writable>,
            <addClass>{if ($canWrite) then 'writable' else 'readable'}</addClass>,
            	if (exists($children)) then
                local:sub-collections($root, $children, $user)
            	else
                ()
        ) else
            ()
};

declare function local:list-collection-contents($collection as xs:string, $user as xs:string) {
    let $subcollections :=
        for $child in xmldb:get-child-collections($collection)
        return
            concat("/", $child)
    return
        ($subcollections, xmldb:get-child-resources($collection))
};

declare function local:resources($collection as xs:string, $user as xs:string) {
    let $start := number(request:get-parameter("start", 0)) + 1
    let $endParam := number(request:get-parameter("end", 1000000)) + 1
    let $resources := local:list-collection-contents($collection, $user)
    let $end := if ($endParam gt count($resources)) then count($resources) else $endParam
    let $subset := subsequence($resources, $start, $end - $start + 1)
    return
        <json:value>
            <total json:literal="true">{count($resources)}</total>
            <items>
        {
            for $resource in $subset
            let $isCollection := starts-with($resource, "/")
            let $path :=
                if ($isCollection) then
                    concat($collection, $resource)
                else
                    concat($collection, "/", $resource)
            let $permissions :=
                if ($isCollection) then
                    xmldb:permissions-to-string(xmldb:get-permissions($path))
                else
                    xmldb:permissions-to-string(xmldb:get-permissions($collection, $resource))
            let $owner :=
                if ($isCollection) then
                    xmldb:get-owner($path)
                else
                    xmldb:get-owner($collection, $resource)
            let $group :=
                if ($isCollection) then
                    xmldb:get-group($path)
                else
                    xmldb:get-group($collection, $resource)
            let $lastMod :=
                if ($isCollection) then
                    date:format-dateTime(xmldb:created($path))
                else
                    date:format-dateTime(xmldb:created($collection, $resource))
            let $canWrite :=
                if ($isCollection) then
                    local:canWrite(concat($collection, "/", $resource), $user)
                else
                    local:canWriteResource($collection, $resource, $user)
            order by $resource ascending
            return
                <json:value json:array="true">
                    <name>{if ($isCollection) then substring-after($resource, "/") else $resource}</name>
                    <permissions>{$permissions}</permissions>
                    <owner>{$owner}</owner>
                    <group>{$group}</group>
                    <last-modified>{$lastMod}</last-modified>
                    <writable json:literal="true">{$canWrite}</writable>
                    <isCollection json:literal="true">{$isCollection}</isCollection>
                </json:value>
        }
            </items>
        </json:value>
};

declare function local:create-collection($collName as xs:string, $user as xs:string) {
    let $parent := request:get-parameter("collection", "/db")
    return
        if (local:canWrite($parent, $user)) then
            let $null := xmldb:create-collection($parent, $collName)
            return
                <response status="ok"/>
        else
            <response status="fail">
                <message>You are not allowed to write to collection {$parent}</message>
            </response>
};

declare function local:delete-collection($collName as xs:string, $user as xs:string) {
    if (local:canWrite($collName, $user)) then
        let $null := xmldb:remove($collName)
        return
            <response status="ok"/>
    else
        <response status="fail">
            <message>You are not allowed to write to collection {$collName}</message>
        </response>
};

declare function local:delete-resource($collection as xs:string, $resources as xs:string+, $user as xs:string) {
    let $canWrite :=
        every $resource in $resources
        satisfies
            local:canWriteResource($collection, $resource, $user)
    return
    if ($canWrite) then
        let $null :=
            for $resource in $resources
            return xmldb:remove($collection, $resource)
        return
            <response status="ok"/>
    else
        <response status="fail">
            <message>You are not allowed to delete the resources {string-join($resources, ", ")} in collection {$collection}</message>
        </response>
};

let $deleteCollection := request:get-parameter("remove", ())
let $deleteResource := request:get-parameter("remove[]", ())
let $createCollection := request:get-parameter("create", ())
let $view := request:get-parameter("view", "c")
let $collection := request:get-parameter("root", "/db")
let $collName := replace($collection, "^.*/([^/]+$)", "$1")
let $user := if (session:get-attribute('myapp.user')) then session:get-attribute('myapp.user') else "guest"
return
    if ($deleteCollection) then
        local:delete-collection($deleteCollection, $user)
    else if (exists($deleteResource)) then
        local:delete-resource($collection, $deleteResource, $user)
    else if ($createCollection) then
        local:create-collection($createCollection, $user)
    else if ($view eq "c") then
        <collection json:array="true">
        {local:collections($collection, $collName, $user)}
        </collection>
    else
        local:resources($collection, $user)


added back in POST by applicaiton/xml and text/xml

module namespace page = 'http://basex.org/modules/web-page';

import module namespace csr_proc = "https://github.com/his-interop/openinfoman/csr_proc" at "../repo/csr_processor.xqm";

declare   namespace   csd = "urn:ihe:iti:csd:2013";
declare default element  namespace   "urn:ihe:iti:csd:2013";

declare
  %rest:path("/CSD/careServicesRequest")
  %rest:consumes("application/xml", "text/xml", "multipart/form-data")
  %rest:POST("{$careServicesRequest}")
  function page:process_CSR_post($careServicesRequest)
{

for $doc in collection('provider_directory')
where matches(document-uri($doc), 'providers.xml')
return csr_proc:process_CSR($careServicesRequest/careServicesRequest,$doc)

};

declare
  %rest:path("/CSD/careServicesRequest")
  %rest:consumes("application/x-www-form-urlencoded")
  %rest:POST("{$careServicesRequest}")
  function page:process_CSR_post_encoded($careServicesRequest)
{
 page:process_CSR_post(parse-xml($careServicesRequest))
};

module namespace page = 'http://basex.org/modules/web-page';

import module namespace csr_proc = "https://github.com/his-interop/openinfoman/csr_proc" at "../repo/csr_processor.xqm";

declare   namespace   csd = "urn:ihe:iti:csd:2013";
declare default element  namespace   "urn:ihe:iti:csd:2013";

declare
  %rest:path("/CSD/careServicesRequest")
  %rest:consumes( "multipart/form-data")
  %rest:POST("{$careServicesRequest}")
  function page:process_CSR_post($careServicesRequest)
{

for $doc in collection('provider_directory')
where matches(document-uri($doc), 'providers.xml')
return csr_proc:process_CSR($careServicesRequest/careServicesRequest,$doc)

};

declare
  %rest:path("/CSD/careServicesRequest")
  %rest:consumes("application/x-www-form-urlencoded")
  %rest:POST("{$careServicesRequest}")
  function page:process_CSR_post_encoded($careServicesRequest)
{
 page:process_CSR_post(parse-xml($careServicesRequest))
};


Delete search-list-query.xqy

Delete search-list-query.xqy

/*

Add this piece of code in search-list-query.xqy available at /Users/sreddy/Library/MarkLogic/Modules/MarkLogic/rest-api/endpoints dir for GET and POST
This is important for the server to handle requests and enable CORS
Otherwise use a proxy server

*/

  return
	if (exists($response)) then
	(eput:add-response-header("Access-Control-Allow-Origin", xdmp:get-request-header('Origin')),
	eput:add-response-header("Access-Control-Allow-Credentials","true"),
$response)


Install directly from the DB, do not pass through its REST interface any more.

svn path=/trunk/eXist/; revision=17344

xquery version "1.0";

module namespace repomanager="http://exist-db.org/xquery/admin-interface/repo";

declare namespace package="http://expath.org/ns/pkg";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace util="http://exist-db.org/xquery/util";

declare variable $repomanager:coll := "/db/system/repo";
declare variable $repomanager:repo-uri :=
    if (request:get-parameter("repository-url", ())) then
        request:get-parameter("repository-url", ())
    else
        "http://demo.exist-db.org/exist/apps/public-repo/retrieve.html"
        (: "http://192.168.2.104:8080/exist/apps/public-repo/retrieve.html" :)
;

declare function local:entry-data($path as xs:anyURI, $type as xs:string, $data as item()?, $param as item()*) as item()?
{
    if (starts-with($path, "icon")) then
        <icon>{$path}</icon>
    else
    	<entry>
    		<path>{$path}</path>
    		<type>{$type}</type>
    		<data>{$data}</data>
    	</entry>
};

declare function local:entry-filter($path as xs:anyURI, $type as xs:string, $param as item()*) as xs:boolean
{
	starts-with($path, "icon") or $path = ("repo.xml", "expath-pkg.xml")
};

declare function repomanager:publicrepo() as element()
{
    let $package-url := request:get-parameter("package-url", ())
    return

Actions:

                {
                if (ends-with($package-url,'.xar')) then (

Downloaded package: {$package-url}
,

                    let $http-response := httpclient:get(xs:anyURI($package-url), false(), ())
                    let $name := tokenize($package-url, "/")[last()]
                    (: application/xar is not correct, it is not officially registered. :)
                    (: Must be application/x-xar or application/x-xar+zip instead. :)
                    (: I am not sure about the +zip, it should not be treated as a ZIP file. :)
                    (: TODO: Add the XAR MIME type to the Pkg spec. :)
                    let $package-mimetype := "application/xar"
                    let $package-data := xs:base64Binary($http-response/httpclient:body/text())
                    let $stored := xmldb:store($repomanager:coll, $name, $package-data, $package-mimetype)
                    let $meta :=
                        compression:unzip(
                            util:binary-doc($stored), util:function(xs:QName("local:entry-filter"), 3),
                            (),  util:function(xs:QName("local:entry-data"), 4), ()
                        )
                    let $package := $meta//package:package/string(@name)
                    let $type := $meta//repo:meta//repo:type/string()
                    let $install := repo:install-from-db($stored)
                    let $deploy :=
                        if ($type eq "application") then
                            repo:deploy($package)
                        else
                            ()
                    return

Package installed.

                )
                else

Error uploading - Must be a valid Package archive (.xar file extension)

                }

            Important: installed XQuery library mappings will not become visible until eXist is restarted.

};

declare function repomanager:upload() as element()
{

Actions:

        {
            let $name := request:get-parameter("name", ())
            let $repocol :=  if (collection($repomanager:coll)) then () else xmldb:create-collection('/db/system','repo')
            let $docName := if($name) then $name else request:get-uploaded-file-name("upload")
            let $file := request:get-uploaded-file-data("upload")
            return
                if ($docName) then
                    let $stored := xdb:store($repomanager:coll, xdb:encode-uri($docName), $file)
                    let $meta :=
                        compression:unzip(
                            util:binary-doc($stored), util:function(xs:QName("local:entry-filter"), 3),
                            (),  util:function(xs:QName("local:entry-data"), 4), ()
                        )
                    let $package := $meta//package:package/string(@name)
                    let $type := $meta//repo:meta//repo:type/string()
                    let $install :=
                        (:repo:install(concat('http://localhost:',request:get-server-port(),request:get-context-path(), '/rest',$stored)):)
                        repo:install-from-db($stored)
                    let $deployed :=
                        if ($type eq "application") then
                            repo:deploy($package)
                        else
                            ()
                    return

Package uploaded and installed: {$name}

                else

Error uploading - Must be a valid Package archive (.xar file extension)

        }

};

declare function repomanager:cxan-install() as element()
{
  let $abbrev  := request:get-parameter('pkg-abbrev', ())
  let $name    := request:get-parameter('pkg-name', ())
  let $version := request:get-parameter('pkg-version', ())
  let $host    := ( 'http://test.cxan.org/', 'http://cxan.org/' )[1]
  let $uri     := concat($host, 'file?name=', $name, '&id=', $abbrev, '&version=', $version)
  return

Actions:

Package abbrev: '{ $abbrev }'

Package name: '{ $name }'

Package version: '{ $version }'

 {
            if ( $abbrev[.] and $name[.] ) then
              Package abbrev and name cannot be both set!
            else if ( repo:install($uri) ) then
              Successfully installed
            else
              Error installing the package (check the logs)
          }

};

declare function repomanager:activate() as element()
{
    let $name := request:get-parameter("name", ())
    let $package := request:get-parameter("package", ())
    let $type := request:get-parameter("type", ())
    let $hostname := request:get-hostname()            (:  pkg-repo.jar needs http or file scheme :)
    let $port := request:get-server-port()
     return

Actions:

Installed package: {$name}

                {
                    repo:install(concat('http://localhost:',$port,request:get-context-path(), '/rest',$repomanager:coll,'/',$name,'.xar')),
                    if ($type eq "application") then
                        repo:deploy($package)
                    else
                        ()
                }

};

declare function repomanager:deactivate() as element()
{
    let $name := request:get-parameter("name", ())
    let $package := request:get-parameter("package", ())
    let $type := request:get-parameter("type", ())
    let $undeploy :=
        if ($type eq "application") then
            repo:undeploy($package)
        else
            ()
    let $remove := repo:remove($package)
    (: let $delete := xmldb:remove($repomanager:coll, $name) :)
    return

Actions:

Uninstalled package: {$name}

};

declare function repomanager:process-action() as element()*
{
    let $action := request:get-parameter("action", ()) return
        util:catch("java.lang.Exception",
            if($action eq "activate") then
            (
                repomanager:activate()
            )
            else if($action eq "deactivate") then
            (
                repomanager:deactivate()
            )
            else if($action eq "Upload Package") then
            (
                repomanager:upload()
            )
            else if($action eq "CXAN Install") then
            (
                repomanager:cxan-install()
            )
            else if($action eq "download") then
            (
                repomanager:publicrepo()
            )else(),

                An error occurred while processing the action:

                {$util:exception-message}

        )
};

declare function repomanager:all-packages() {
    for $repo in repo:list()
    return
        <package url="{$repo}">
        {
            for $r in ("repo.xml", "expath-pkg.xml")
            let $meta := repo:get-resource($repo, $r)
            let $data := if (exists($meta)) then util:binary-to-string($meta) else ()
            return
                if (exists($data)) then
                    util:parse($data)
                else
                    ()
        }
        {
            let $icon := repo:get-resource($repo, "icon.png")
            return
                if (exists($icon)) then
                    <icon>icon.png</icon>
                else
                    ()
        }
        </package>
};

declare function repomanager:view-installed() {
    let $packages := repomanager:all-packages()
    return

        {
            for $package in $packages
            let $iconURL :=
                if ($package/icon) then
                    concat("get-icon.xql?package=", $package/@url)
                else
                    "images/package.png"
            let $pkg-name := $package/package:package/string(@name)
            let $pkg-abbrev := $package/package:package/string(@abbrev)
            let $type := $package//repo:type/string()
            let $version := $package/package:package/@version/string()
            let $app-url :=
				if ($package//repo:target) then
                	concat(
                    	request:get-context-path(), "/apps",
                    	substring-after($package//repo:target, "/db"), "/"
                	)
				else
					()
            return

{$package//package:title/string()} ({$version})

                                Title:
                                { $package//package:title/text() }

							{
								if ($app-url) then

										Local URL:

											{$app-url}

								else
									()
							}

                                Author(s):

                                    {
                                        for $author in $package//repo:author
                                        return

{$author/text()}

                                    }

                                Version:
                                { $version }

                                Description:
                                { $package//repo:description/text() }

                                License:
                                { $package//repo:license/text() }

                                Website:
                                { $package//repo:website/text() }

                                    <form method="POST" action="admin.xql">
                                        <input type="hidden" name="panel" value="repo"/>
                                        <input type="hidden" name="name" value="{$pkg-abbrev}-{$version}.xar"/>
                                        <input type="hidden" name="package" value="{$pkg-name}"/>
                                        <input type="hidden" name="type" value="{$type}"/>
                                        <button type="submit" name="action" value="deactivate">

                                        </button>
                                    </form>

        }

};

declare function repomanager:show-public() {

        <input name="repository-url" size="60" value="{$repomanager:repo-uri}"/>
        <button name="retrieve-repo" id="retrieve-repo">Retrieve packages</button>

Retrieving packages ...

};

declare function repomanager:show-cxan() {
    <form method="POST" action="admin.xql">
        <input type="hidden" name="panel" value="repo"/>

Install a package from CXAN directly within eXist.

               Abbrev:
               <input name="pkg-abbrev" size="50" value=""/>
               e.g. functx

               Name:
               <input name="pkg-name" size="50" value=""/>
               e.g. http://www.functx.com

               Version:
               <input name="pkg-version" size="50" value=""/>
               e.g. 1.0.2, empty for latest

               <input type="submit" name="action" value="CXAN Install"/>

Note: Package abbrev and name are mutually exclusive, but one is required.

Note: For now, this uses CXAN's repository at test.cxan.org.  We will make cxan.org available in the future.

    </form>
};

declare function repomanager:main() as element() {
    let $action := lower-case(request:get-parameter("action", "set repository"))
    let $repocol :=  if (collection($repomanager:coll)) then () else xmldb:create-collection('/db/system','repo')

    return

Package Repository

            { repomanager:process-action() }

Installed

Public Repo

Upload

CXAN

                    { repomanager:view-installed() }

                    { repomanager:show-public() }

                    <form method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="panel" value="repo"/>

                                <input type="submit" name="action" value="Upload Package"/>
                                <input type="file" size="30" name="upload"/>

                        Note: You can upload example .xar packages located under webapp/repo/packages
                    </form>

                    { repomanager:show-cxan() }

};

xquery version "1.0";

module namespace repomanager="http://exist-db.org/xquery/admin-interface/repo";

declare namespace package="http://expath.org/ns/pkg";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace util="http://exist-db.org/xquery/util";

declare variable $repomanager:coll := "/db/system/repo";
declare variable $repomanager:repo-uri :=
    if (request:get-parameter("repository-url", ())) then
        request:get-parameter("repository-url", ())
    else
        "http://demo.exist-db.org/exist/apps/public-repo/retrieve.html"
        (: "http://192.168.2.104:8080/exist/apps/public-repo/retrieve.html" :)
;

declare function local:entry-data($path as xs:anyURI, $type as xs:string, $data as item()?, $param as item()*) as item()?
{
    if (starts-with($path, "icon")) then
        <icon>{$path}</icon>
    else
    	<entry>
    		<path>{$path}</path>
    		<type>{$type}</type>
    		<data>{$data}</data>
    	</entry>
};

declare function local:entry-filter($path as xs:anyURI, $type as xs:string, $param as item()*) as xs:boolean
{
	starts-with($path, "icon") or $path = ("repo.xml", "expath-pkg.xml")
};

declare function repomanager:publicrepo() as element()
{
    let $package-url := request:get-parameter("package-url", ())
    return

Actions:

                {
                if (ends-with($package-url,'.xar')) then (

Downloaded package: {$package-url}
,

                    let $http-response := httpclient:get(xs:anyURI($package-url), false(), ())
                    let $name := tokenize($package-url, "/")[last()]
                    (: application/xar is not correct, it is not officially registered. :)
                    (: Must be application/x-xar or application/x-xar+zip instead. :)
                    (: I am not sure about the +zip, it should not be treated as a ZIP file. :)
                    (: TODO: Add the XAR MIME type to the Pkg spec. :)
                    let $package-mimetype := "application/xar"
                    let $package-data := xs:base64Binary($http-response/httpclient:body/text())
                    let $stored := xmldb:store($repomanager:coll, $name, $package-data, $package-mimetype)
                    let $meta :=
                        compression:unzip(
                            util:binary-doc($stored), util:function(xs:QName("local:entry-filter"), 3),
                            (),  util:function(xs:QName("local:entry-data"), 4), ()
                        )
                    let $package := $meta//package:package/string(@name)
                    let $type := $meta//repo:meta//repo:type/string()
                    let $install :=
                        repo:install(concat('http://localhost:',request:get-server-port(), request:get-context-path(), '/rest',$stored))
                    let $deploy :=
                        if ($type eq "application") then
                            repo:deploy($package)
                        else
                            ()
                    return

Package installed.

                )
                else

Error uploading - Must be a valid Package archive (.xar file extension)

                }

            Important: installed XQuery library mappings will not become visible until eXist is restarted.

};

declare function repomanager:upload() as element()
{

Actions:

        {
            let $name := request:get-parameter("name", ())
            let $repocol :=  if (collection($repomanager:coll)) then () else xmldb:create-collection('/db/system','repo')
            let $docName := if($name) then $name else request:get-uploaded-file-name("upload")
            let $file := request:get-uploaded-file-data("upload")
            return
                if ($docName) then
                    let $stored := xdb:store($repomanager:coll, xdb:encode-uri($docName), $file)
                    let $meta :=
                        compression:unzip(
                            util:binary-doc($stored), util:function(xs:QName("local:entry-filter"), 3),
                            (),  util:function(xs:QName("local:entry-data"), 4), ()
                        )
                    let $package := $meta//package:package/string(@name)
                    let $type := $meta//repo:meta//repo:type/string()
                    let $install :=
                        (:repo:install(concat('http://localhost:',request:get-server-port(),request:get-context-path(), '/rest',$stored)):)
                        repo:install-from-db($stored)
                    let $deployed :=
                        if ($type eq "application") then
                            repo:deploy($package)
                        else
                            ()
                    return

Package uploaded and installed: {$name}

                else

Error uploading - Must be a valid Package archive (.xar file extension)

        }

};

declare function repomanager:cxan-install() as element()
{
  let $abbrev  := request:get-parameter('pkg-abbrev', ())
  let $name    := request:get-parameter('pkg-name', ())
  let $version := request:get-parameter('pkg-version', ())
  let $host    := ( 'http://test.cxan.org/', 'http://cxan.org/' )[1]
  let $uri     := concat($host, 'file?name=', $name, '&id=', $abbrev, '&version=', $version)
  return

Actions:

Package abbrev: '{ $abbrev }'

Package name: '{ $name }'

Package version: '{ $version }'

 {
            if ( $abbrev[.] and $name[.] ) then
              Package abbrev and name cannot be both set!
            else if ( repo:install($uri) ) then
              Successfully installed
            else
              Error installing the package (check the logs)
          }

};

declare function repomanager:activate() as element()
{
    let $name := request:get-parameter("name", ())
    let $package := request:get-parameter("package", ())
    let $type := request:get-parameter("type", ())
    let $hostname := request:get-hostname()            (:  pkg-repo.jar needs http or file scheme :)
    let $port := request:get-server-port()
     return

Actions:

Installed package: {$name}

                {
                    repo:install(concat('http://localhost:',$port,request:get-context-path(), '/rest',$repomanager:coll,'/',$name,'.xar')),
                    if ($type eq "application") then
                        repo:deploy($package)
                    else
                        ()
                }

};

declare function repomanager:deactivate() as element()
{
    let $name := request:get-parameter("name", ())
    let $package := request:get-parameter("package", ())
    let $type := request:get-parameter("type", ())
    let $undeploy :=
        if ($type eq "application") then
            repo:undeploy($package)
        else
            ()
    let $remove := repo:remove($package)
    (: let $delete := xmldb:remove($repomanager:coll, $name) :)
    return

Actions:

Uninstalled package: {$name}

};

declare function repomanager:process-action() as element()*
{
    let $action := request:get-parameter("action", ()) return
        util:catch("java.lang.Exception",
            if($action eq "activate") then
            (
                repomanager:activate()
            )
            else if($action eq "deactivate") then
            (
                repomanager:deactivate()
            )
            else if($action eq "Upload Package") then
            (
                repomanager:upload()
            )
            else if($action eq "CXAN Install") then
            (
                repomanager:cxan-install()
            )
            else if($action eq "download") then
            (
                repomanager:publicrepo()
            )else(),

                An error occurred while processing the action:

                {$util:exception-message}

        )
};

declare function repomanager:all-packages() {
    for $repo in repo:list()
    return
        <package url="{$repo}">
        {
            for $r in ("repo.xml", "expath-pkg.xml")
            let $meta := repo:get-resource($repo, $r)
            let $data := if (exists($meta)) then util:binary-to-string($meta) else ()
            return
                if (exists($data)) then
                    util:parse($data)
                else
                    ()
        }
        {
            let $icon := repo:get-resource($repo, "icon.png")
            return
                if (exists($icon)) then
                    <icon>icon.png</icon>
                else
                    ()
        }
        </package>
};

declare function repomanager:view-installed() {
    let $packages := repomanager:all-packages()
    return

        {
            for $package in $packages
            let $iconURL :=
                if ($package/icon) then
                    concat("get-icon.xql?package=", $package/@url)
                else
                    "images/package.png"
            let $pkg-name := $package/package:package/string(@name)
            let $pkg-abbrev := $package/package:package/string(@abbrev)
            let $type := $package//repo:type/string()
            let $version := $package/package:package/@version/string()
            let $app-url :=
				if ($package//repo:target) then
                	concat(
                    	request:get-context-path(), "/apps",
                    	substring-after($package//repo:target, "/db"), "/"
                	)
				else
					()
            return

{$package//package:title/string()} ({$version})

                                Title:
                                { $package//package:title/text() }

							{
								if ($app-url) then

										Local URL:

											{$app-url}

								else
									()
							}

                                Author(s):

                                    {
                                        for $author in $package//repo:author
                                        return

{$author/text()}

                                    }

                                Version:
                                { $version }

                                Description:
                                { $package//repo:description/text() }

                                License:
                                { $package//repo:license/text() }

                                Website:
                                { $package//repo:website/text() }

                                    <form method="POST" action="admin.xql">
                                        <input type="hidden" name="panel" value="repo"/>
                                        <input type="hidden" name="name" value="{$pkg-abbrev}-{$version}.xar"/>
                                        <input type="hidden" name="package" value="{$pkg-name}"/>
                                        <input type="hidden" name="type" value="{$type}"/>
                                        <button type="submit" name="action" value="deactivate">

                                        </button>
                                    </form>

        }

};

declare function repomanager:show-public() {

        <input name="repository-url" size="60" value="{$repomanager:repo-uri}"/>
        <button name="retrieve-repo" id="retrieve-repo">Retrieve packages</button>

Retrieving packages ...

};

declare function repomanager:show-cxan() {
    <form method="POST" action="admin.xql">
        <input type="hidden" name="panel" value="repo"/>

Install a package from CXAN directly within eXist.

               Abbrev:
               <input name="pkg-abbrev" size="50" value=""/>
               e.g. functx

               Name:
               <input name="pkg-name" size="50" value=""/>
               e.g. http://www.functx.com

               Version:
               <input name="pkg-version" size="50" value=""/>
               e.g. 1.0.2, empty for latest

               <input type="submit" name="action" value="CXAN Install"/>

Note: Package abbrev and name are mutually exclusive, but one is required.

Note: For now, this uses CXAN's repository at test.cxan.org.  We will make cxan.org available in the future.

    </form>
};

declare function repomanager:main() as element() {
    let $action := lower-case(request:get-parameter("action", "set repository"))
    let $repocol :=  if (collection($repomanager:coll)) then () else xmldb:create-collection('/db/system','repo')

    return

Package Repository

            { repomanager:process-action() }

Installed

Public Repo

Upload

CXAN

                    { repomanager:view-installed() }

                    { repomanager:show-public() }

                    <form method="POST" enctype="multipart/form-data">
                        <input type="hidden" name="panel" value="repo"/>

                                <input type="submit" name="action" value="Upload Package"/>
                                <input type="file" size="30" name="upload"/>

                        Note: You can upload example .xar packages located under webapp/repo/packages
                    </form>

                    { repomanager:show-cxan() }

};


Fix test failures

Fix test failures

- Russian and French example asserted the wrong days of the week for the dates tested
- Russian example incorrectly capitalized days of the week and months of the year (I confirmed at see http://learningrussian.net/vocabulary/russian-words/russian-months-and-seasons.php that "Russians don't capitalize months or seasons unless they occur in the beginning of a sentence")
- Fixed typo in fd:written-day-en

xquery version "3.0";

module namespace fd="http://exist-db.org/xquery/test/format-dates";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.6.2012")
    %test:args("2012-06-01")
    %test:assertEquals("1.6.2012")
function fd:simple-date($date as xs:date) {
    format-date($date, "[D].[M].[Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.06.2012")
    %test:args("2012-06-01")
    %test:assertEquals("01.06.2012")
function fd:simple-format($date as xs:date) {
    format-date($date, "[D00].[M01].[Y0]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.6.2012")
    %test:args("2012-06-01")
    %test:assertEquals("1.6.2012")
function fd:format-month-numeric-width($date as xs:date) {
    format-date($date, "[D,1-2].[M,*-*].[Y,*]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26 JUN 2012")
    %test:args("1970-10-07")
    %test:assertEquals("07 OCT 1970")
function fd:format-month-string-width-caps($date as xs:date) {
    format-date($date, "[D01] [MN,*-3] [Y0001]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26 Jun 2012")
    %test:args("1970-10-07")
    %test:assertEquals("07 Oct 1970")
function fd:format-month-string-width-title($date as xs:date) {
    format-date($date, "[D01] [MNn,*-3] [Y0001]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tuesday, June 26, 2012")
    %test:args("1970-10-07")
    %test:assertEquals("Wednesday, October 7, 1970")
function fd:format-names($date as xs:date) {
    format-date($date, "[FNn], [MNn] [D], [Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tue Jun 26 2012")
    %test:args("1970-10-07")
    %test:assertEquals("Wed Oct 7 1970")
function fd:format-names-width($date as xs:date) {
    format-date($date, "[FNn,*-3] [MNn,*-3] [D] [Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tuesday, June 26th, 2012")
    %test:args("2012-06-01")
    %test:assertEquals("Friday, June 1st, 2012")
    %test:args("2012-06-02")
    %test:assertEquals("Saturday, June 2nd, 2012")
    %test:args("2012-06-03")
    %test:assertEquals("Sunday, June 3rd, 2012")
function fd:format-ordinal($date as xs:date) {
    format-date($date, "[FNn], [MNn] [D0o], [Y]")
};

declare
    %test:args("2002-12-31")
    %test:assertEquals("[2002-12-31]")
function fd:format-square-brackets($date as xs:date) {
    format-date($date, "[[[Y0001]-[M01]-[D01]]]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("11:14 pm on Tuesday, June 26th, 2012")
function fd:format-dateTime($date as xs:dateTime) {
    format-dateTime($date, "[h00]:[m00] [P] on [FNn], [MNn] [D1o], [Y]")
};

declare
    %test:args("2012-06-26+02:00")
    %test:assertEquals("20120626GMT+02:00")
function fd:format-date-z($date as xs:date) {
    format-date($date, "[Y0001][M01][D01][z]")
};

declare
    %test:args("2012-06-26+02:00")
    %test:assertEquals("20120626+02:00")
function fd:format-date-Z($date as xs:date) {
    format-date($date, "[Y0001][M01][D01][Z]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("20120626231422GMT+02:00")
function fd:format-dateTime-z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("GMT-10:00")
function fd:format-dateTime-z-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("GMT-05:00")
function fd:format-dateTime-z-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("GMT+00:00")
function fd:format-dateTime-z-zero($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("GMT+05:30")
function fd:format-dateTime-z-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("GMT+13:00")
function fd:format-dateTime-z-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("20120626231422+02:00")
function fd:format-dateTime-Z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+00:00")
function fd:format-dateTime-Z-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T23:14:22.566")
    %test:assertEquals("20120626231422")
function fd:format-dateTime-no-tz-z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][z]")
};

declare
    %test:args("2012-06-26T23:14:22.566")
    %test:assertEquals("20120626231422")
function fd:format-dateTime-no-tz-Z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10")
function fd:format-dateTime-Z0-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-5")
function fd:format-dateTime-Z0-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0")
function fd:format-dateTime-Z0-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+5:30")
function fd:format-dateTime-Z0-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13")
function fd:format-dateTime-Z0-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z0-00-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-5:00")
function fd:format-dateTime-Z0-00-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0:00")
function fd:format-dateTime-Z0-00-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+5:30")
function fd:format-dateTime-Z0-00-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z0-00-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z00-00-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z00-00-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+00:00")
function fd:format-dateTime-Z00-00-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z00-00-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z00-00-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-1000")
function fd:format-dateTime-Z0000-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-0500")
function fd:format-dateTime-Z0000-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0000")
function fd:format-dateTime-Z0000-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+0530")
function fd:format-dateTime-Z0000-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+1300")
function fd:format-dateTime-Z0000-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z00-00t-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z00-00t-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("Z")
function fd:format-dateTime-Z00-00t-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z00-00t-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z00-00t-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("W")
function fd:format-dateTime-ZZ-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("R")
function fd:format-dateTime-ZZ-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("Z")
function fd:format-dateTime-ZZ-zero($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-ZZ-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-ZZ-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("HST")
function fd:format-dateTime-ZN-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("EST")
function fd:format-dateTime-ZN-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("GMT")
function fd:format-dateTime-ZN-zero($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("IST")
function fd:format-dateTime-ZN-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-ZN-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("06:00 EST")
function fd:format-dateTime-ZN-NY-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("12:00 EST")
function fd:format-dateTime-ZN-NY-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("07:00 EST")
function fd:format-dateTime-ZN-NY-zero($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("01:30 EST")
function fd:format-dateTime-ZN-NY-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("18:00 EST")
function fd:format-dateTime-ZN-NY-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("17:45:50")
    %test:assertEquals("17:45:50")
    %test:args("09:45:50")
    %test:assertEquals("09:45:50")
function fd:simple-time($time as xs:time) {
    format-time($time, "[H00]:[m00]:[s]")
};

declare
    %test:args("17:45:50")
    %test:assertEquals("05:45 pm")
    %test:args("09:45:50")
    %test:assertEquals("09:45 am")
function fd:time-am-pm($time as xs:time) {
    format-time($time, "[h00]:[m00] [P]")
};

declare
    %test:assertError
function fd:date-format-in-format-time() {
    format-time(current-time(), "[Y]")
};

declare
    %test:assertError
function fd:time-format-in-format-date() {
    format-date(current-dateTime(), "[H]")
};

declare
    %test:args("2012-06-27Z")
    %test:assertEquals("Mittwoch, 27. Juni 2012")
    %test:args("1970-10-07Z")
    %test:assertEquals("Mittwoch, 7. Oktober 1970")
function fd:language-de($date as xs:date) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "de", (), ())
};

declare
    %test:args("2012-06-01Z")
    %test:assertEquals("пятница, 1 июня 2012")
    %test:args("1970-10-07Z")
    %test:assertEquals("среда, 7 октября 1970")
function fd:language-ru($date as xs:date) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "ru", (), ())
};

declare
%test:args("2012-06-01Z")
%test:assertEquals("vendredi, 1er juin 2012")
%test:args("1970-10-07Z")
%test:assertEquals("mercredi, 7 octobre 1970")
function fd:language-fr($date as xs:date) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "fr", (), ())
};

declare
    %test:args("2018-05-21Z")
    %test:assertEquals("Monday")
    %test:args("2018-05-22Z")
    %test:assertEquals("Tuesday")
    %test:args("2018-05-23Z")
    %test:assertEquals("Wednesday")
    %test:args("2018-05-24Z")
    %test:assertEquals("Thursday")
    %test:args("2018-05-25Z")
    %test:assertEquals("Friday")
    %test:args("2018-05-26Z")
    %test:assertEquals("Saturday")
    %test:args("2018-05-27Z")
    %test:assertEquals("Sunday")
function fd:written-day-en($date-str as xs:string) {
    format-date(xs:date($date-str), "[FNn]", "en", (), ())
};

xquery version "3.0";

module namespace fd="http://exist-db.org/xquery/test/format-dates";

declare namespace test="http://exist-db.org/xquery/xqsuite";

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.6.2012")
    %test:args("2012-06-01")
    %test:assertEquals("1.6.2012")
function fd:simple-date($date as xs:date) {
    format-date($date, "[D].[M].[Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.06.2012")
    %test:args("2012-06-01")
    %test:assertEquals("01.06.2012")
function fd:simple-format($date as xs:date) {
    format-date($date, "[D00].[M01].[Y0]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26.6.2012")
    %test:args("2012-06-01")
    %test:assertEquals("1.6.2012")
function fd:format-month-numeric-width($date as xs:date) {
    format-date($date, "[D,1-2].[M,*-*].[Y,*]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26 JUN 2012")
    %test:args("1970-10-07")
    %test:assertEquals("07 OCT 1970")
function fd:format-month-string-width-caps($date as xs:date) {
    format-date($date, "[D01] [MN,*-3] [Y0001]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("26 Jun 2012")
    %test:args("1970-10-07")
    %test:assertEquals("07 Oct 1970")
function fd:format-month-string-width-title($date as xs:date) {
    format-date($date, "[D01] [MNn,*-3] [Y0001]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tuesday, June 26, 2012")
    %test:args("1970-10-07")
    %test:assertEquals("Wednesday, October 7, 1970")
function fd:format-names($date as xs:date) {
    format-date($date, "[FNn], [MNn] [D], [Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tue Jun 26 2012")
    %test:args("1970-10-07")
    %test:assertEquals("Wed Oct 7 1970")
function fd:format-names-width($date as xs:date) {
    format-date($date, "[FNn,*-3] [MNn,*-3] [D] [Y]")
};

declare
    %test:args("2012-06-26")
    %test:assertEquals("Tuesday, June 26th, 2012")
    %test:args("2012-06-01")
    %test:assertEquals("Friday, June 1st, 2012")
    %test:args("2012-06-02")
    %test:assertEquals("Saturday, June 2nd, 2012")
    %test:args("2012-06-03")
    %test:assertEquals("Sunday, June 3rd, 2012")
function fd:format-ordinal($date as xs:date) {
    format-date($date, "[FNn], [MNn] [D0o], [Y]")
};

declare
    %test:args("2002-12-31")
    %test:assertEquals("[2002-12-31]")
function fd:format-square-brackets($date as xs:date) {
    format-date($date, "[[[Y0001]-[M01]-[D01]]]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("11:14 pm on Tuesday, June 26th, 2012")
function fd:format-dateTime($date as xs:dateTime) {
    format-dateTime($date, "[h00]:[m00] [P] on [FNn], [MNn] [D1o], [Y]")
};

declare
    %test:args("2012-06-26+02:00")
    %test:assertEquals("20120626GMT+02:00")
function fd:format-date-z($date as xs:date) {
    format-date($date, "[Y0001][M01][D01][z]")
};

declare
    %test:args("2012-06-26+02:00")
    %test:assertEquals("20120626+02:00")
function fd:format-date-Z($date as xs:date) {
    format-date($date, "[Y0001][M01][D01][Z]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("20120626231422GMT+02:00")
function fd:format-dateTime-z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("GMT-10:00")
function fd:format-dateTime-z-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("GMT-05:00")
function fd:format-dateTime-z-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("GMT+00:00")
function fd:format-dateTime-z-zero($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("GMT+05:30")
function fd:format-dateTime-z-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("GMT+13:00")
function fd:format-dateTime-z-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[z]")
};

declare
    %test:args("2012-06-26T23:14:22.566+02:00")
    %test:assertEquals("20120626231422+02:00")
function fd:format-dateTime-Z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+00:00")
function fd:format-dateTime-Z-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z]")
};

declare
    %test:args("2012-06-26T23:14:22.566")
    %test:assertEquals("20120626231422")
function fd:format-dateTime-no-tz-z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][z]")
};

declare
    %test:args("2012-06-26T23:14:22.566")
    %test:assertEquals("20120626231422")
function fd:format-dateTime-no-tz-Z($date as xs:dateTime) {
    format-dateTime($date, "[Y0001][M01][D01][H01][m01][s01][Z]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10")
function fd:format-dateTime-Z0-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-5")
function fd:format-dateTime-Z0-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0")
function fd:format-dateTime-Z0-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+5:30")
function fd:format-dateTime-Z0-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13")
function fd:format-dateTime-Z0-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z0-00-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-5:00")
function fd:format-dateTime-Z0-00-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0:00")
function fd:format-dateTime-Z0-00-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+5:30")
function fd:format-dateTime-Z0-00-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z0-00-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z00-00-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z00-00-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+00:00")
function fd:format-dateTime-Z00-00-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z00-00-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z00-00-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-1000")
function fd:format-dateTime-Z0000-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-0500")
function fd:format-dateTime-Z0000-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("+0000")
function fd:format-dateTime-Z0000-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+0530")
function fd:format-dateTime-Z0000-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+1300")
function fd:format-dateTime-Z0000-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z0000]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("-10:00")
function fd:format-dateTime-Z00-00t-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("-05:00")
function fd:format-dateTime-Z00-00t-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("Z")
function fd:format-dateTime-Z00-00t-zero($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-Z00-00t-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-Z00-00t-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[Z00:00t]")
};

declare
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("W")
function fd:format-dateTime-ZZ-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("R")
function fd:format-dateTime-ZZ-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("Z")
function fd:format-dateTime-ZZ-zero($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("+05:30")
function fd:format-dateTime-ZZ-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-ZZ-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZZ]")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("HST")
function fd:format-dateTime-ZN-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("EST")
function fd:format-dateTime-ZN-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("GMT")
function fd:format-dateTime-ZN-zero($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("IST")
function fd:format-dateTime-ZN-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("+13:00")
function fd:format-dateTime-ZN-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[ZN]", (), (), "us")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000-10:00")
    %test:assertEquals("06:00 EST")
function fd:format-dateTime-ZN-NY-negative-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000-05:00")
    %test:assertEquals("12:00 EST")
function fd:format-dateTime-ZN-NY-negative-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+00:00")
    %test:assertEquals("07:00 EST")
function fd:format-dateTime-ZN-NY-zero($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+05:30")
    %test:assertEquals("01:30 EST")
function fd:format-dateTime-ZN-NY-positive-single-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:pending("[ZN] is not yet supported")
    %test:args("2012-06-26T12:00:00.000+13:00")
    %test:assertEquals("18:00 EST")
function fd:format-dateTime-ZN-NY-positive-double-digit($date as xs:dateTime) {
    format-dateTime($date, "[H00]:[M00] [ZN]", (), (), "America/New_York")
};

declare
    %test:args("17:45:50")
    %test:assertEquals("17:45:50")
    %test:args("09:45:50")
    %test:assertEquals("09:45:50")
function fd:simple-time($time as xs:time) {
    format-time($time, "[H00]:[m00]:[s]")
};

declare
    %test:args("17:45:50")
    %test:assertEquals("05:45 pm")
    %test:args("09:45:50")
    %test:assertEquals("09:45 am")
function fd:time-am-pm($time as xs:time) {
    format-time($time, "[h00]:[m00] [P]")
};

declare
    %test:assertError
function fd:date-format-in-format-time() {
    format-time(current-time(), "[Y]")
};

declare
    %test:assertError
function fd:time-format-in-format-date() {
    format-date(current-dateTime(), "[H]")
};

declare
    %test:args("2012-06-27T10:21:55.082+02:00")
    %test:assertEquals("Mittwoch, 27. Juni 2012")
    %test:args("1970-10-07T10:21:55.082+02:00")
    %test:assertEquals("Mittwoch, 7. Oktober 1970")
function fd:language($date as xs:dateTime) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "de", (), ())
};

declare
    %test:args("2012-06-27T10:21:55.082+02:00")
    %test:assertEquals("Среда, 27 Июня 2012")
    %test:args("1970-10-07T10:21:55.082+02:00")
    %test:assertEquals("Среда, 7 Октября 1970")
function fd:language-ru($date as xs:dateTime) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "ru", (), ())
};

declare
%test:args("2012-06-01")
%test:assertEquals("jeudi, 1er juin 2012")
%test:args("1970-10-07")
%test:assertEquals("jeudi, 7 octobre 1970")
function fd:language-fr($date as xs:date) {
    format-date($date, "[FNn], [D1o] [MNn] [Y]", "fr", (), ())
};

declare
    %test:args("2018-05-21Z")
    %test:assertEquals("Monday")
    %test:args("2018-05-22Z")
    %test:assertEquals("Tuesday")
    %test:args("2018-05-23Z")
    %test:assertEquals("Wednesday")
    %test:args("2018-05-24Z")
    %test:assertEquals("Thursday")
    %test:args("2018-05-25Z")
    %test:assertEquals("Friday")
    %test:args("2018-05-26Z")
    %test:assertEquals("Saturday")
    %test:args("2018-05-27Z")
    %test:assertEquals("Sunday")
function fd:written-day-en($date-str as xs:string) {
    format-date(xs:date(date-str), "[FNn]", "en", (), ())
};