correct Error while loading module mu/auth.xqm: unexpected token:

xquery version "3.0";
module namespace auth="http://markup.co.nz/#auth";

import module namespace templates="http://exist-db.org/xquery/templates" ;
import module namespace util="http://exist-db.org/xquery/util";
import module namespace xmldb="http://exist-db.org/xquery/xmldb";
import module namespace config="http://exist-db.org/xquery/apps/config"  at "../../modules/config.xqm";

declare namespace  xhtml =  "http://www.w3.org/1999/xhtml";
declare namespace  atom =  "http://www.w3.org/2005/Atom";

(:~
: auth
:
: @author Grant MacKenzie
: @version 0.01
:
:)

declare
function auth:token($node as node(), $model as map(*)) {
<p>{ string('token') }</p>
};

xquery version "3.0";
module namespace auth="http://markup.co.nz/#auth";

import module namespace templates="http://exist-db.org/xquery/templates" ;
import module namespace util="http://exist-db.org/xquery/util";
import module namespace xmldb="http://exist-db.org/xquery/xmldb";
import module namespace config="http://exist-db.org/xquery/apps/config"  at "../../modules/config.xqm";

declare namespace  xhtml =  "http://www.w3.org/1999/xhtml";
declare namespace  atom =  "http://www.w3.org/2005/Atom";

(:~
: auth
:
: @author Grant MacKenzie
: @version 0.01
:
:)

declare
function auth:token($node as node(), $model as map(*)) {
<p{ string('token') }</p>
};


[MOD] DBA, querying: use adaptive serialization

(:~
 : Web functions.
 :
 : @author Christian Grün, BaseX GmbH, 2014-15
 :)
module namespace web = 'dba/web';

import module namespace Request = 'http://exquery.org/ns/request';
import module namespace G = 'dba/global' at 'global.xqm';

(:~
 :
Creates a URL representation for the specified URI path
 : and query parameters. All parameters will be URI-encoded
 : and appended to the link. Map entries with empty string values
 : will be ignored.

 :
An example:
 :   { 'a':'foo', 'b':'bar' } -> ?a=foo&b=bar.
 :

 : @param  $link    the target page
 : @param  $params  the parameters to add
 : @return url
 :)
declare function web:create-url(
  $link    as xs:string,
  $params  as map(*)
) as xs:string? {
  $link || string-join(
    for $k in map:keys($params) ! string()
    for $v in distinct-values($params($k))
    count $c
    return (
      if($c = 1) then '?' else '&',
      encode-for-uri($k) || '=' || encode-for-uri(xs:string($v))
    )
  )
};

(:~
 : Returns the mime-type for the specified file.
 : @param  $name  file name
 : @return mime type
 :)
declare function web:mime-type(
  $name  as xs:string
) as xs:string {
  Q{java:org.basex.io.MimeTypes}get($name)
};

(:~
 : Creates a RESTXQ (HTTP) redirect header for the specified page.
 : @param  $page  page to forward to
 :)
declare %updating function web:redirect(
  $page  as xs:string
) {
  web:redirect($page, map { })
};

(:~
 : Creates a RESTXQ (HTTP) redirect header for the specified page and parameters.
 : @param  $page    page to forward to
 : @param  $params  map with query parameters
 :)
declare %updating function web:redirect(
  $page    as xs:string,
  $params  as map(*)
) {
  db:output(
    web:redirect-ro($page, $params)
  )
};

(:~
 : Creates a RESTXQ (HTTP) redirect header for the specified page.
 : @param  $page    page to forward to
 : @return redirect header
 :)
declare function web:redirect-ro(
  $page    as xs:string
) as element(rest:redirect) {
  web:redirect-ro($page, map {})
};

(:~
 : Creates a RESTXQ (HTTP) redirect header for the specified page and parameters.
 : @param  $page    page to forward to
 : @param  $params  map with query parameters
 : @return redirect header
 :)
declare function web:redirect-ro(
  $page    as xs:string,
  $params  as map(*)
) as element(rest:redirect) {
  element rest:redirect { web:create-url($page, $params) }
};

(:~
 : Checks if the current client is logged in. If not, raises an error.
 :)
declare function web:check(
) as empty-sequence() {
  if($G:SESSION) then () else error($G:LOGIN-ERROR, 'Please log in again.')
};

(:~
 : Evaluates a query and returns the result.
 : @param  $query  query
 : @return result of query
 :)
declare function web:query(
  $query  as xs:string?
) as xs:string {
  web:query($query, '', map {})
};

(:~
 : Evaluates a query and returns the result.
 : @param  $query  query
 : @param  $map    bindings
 : @param  $vars   variable bindings
 : @return result of query
 :)
declare function web:query(
  $query  as xs:string?,
  $map    as xs:string,
  $vars   as map(*)
) as xs:string {
  let $limit := $G:MAX-CHARS
  let $query := if($query) then $query else '()'
  let $q := "xquery:eval($query, map {" || $map || "}, " || web:query-options() || ")"
  let $s := "serialize(" || $q || ", map{ 'limit': $limit*2, 'method': 'adaptive' })"
  return web:eval(
    $s ||
    "! (if(string-length(.) > $limit) then substring(., 1, $limit) || '...' else .)",
    map:merge((map { 'query': $query, 'limit': $limit }, $vars))
  )
};

(:~
 : Runs an updating query.
 : @param  $query  query
 : @return empty sequence
 :)
declare %updating function web:update-query(
  $query  as xs:string?
) {
  let $q := "xquery:update($query, map { }, " || web:query-options() || ")"
  return web:update($q, map { 'query': if($query) then $query else '()' })
};

(:~
 : Returns the options for evaluating a query.
 : @return options
 :)
declare %private function web:query-options() {
  "map { 'timeout':" || $G:TIMEOUT ||
       ",'memory':" || $G:MEMORY ||
       ",'permission':'" || $G:PERMISSION || "' }"
};

(:~
 : Evaluates the specified query locally or on a remote server and returns the results.
 : @param  $query  query to be executed
 : @return result
 :)
declare function web:eval(
  $query  as xs:string
) as item()* {
  web:eval($query, map {})
};

(:~
 : Evaluates the specified query locally or remotely and returns the resulting items.
 : @param  $query  query to be executed
 : @param  $vars   variables
 : @return result
 :)
declare function web:eval(
  $query  as xs:string,
  $vars   as map(*)
) as item()* {
  let $query := string-join((
    map:keys($vars) ! ('declare variable $' || . || ' external;'), $query
  ))
  return if($G:SESSION/host) then (
    web:remote-query($query, $vars)
  ) else (
    xquery:eval($query, $vars)
  )
};

(:~
 : Evaluates the specified updating query locally or remotely.
 : @param  $query  query to be executed
 : @param  $vars   variables
 :)
declare %updating function web:update(
  $query  as xs:string,
  $vars   as map(*)
) {
  let $query := string-join((
    map:keys($vars) ! ('declare variable $' || . || ' external;'), $query
  ))
  return if($G:SESSION/host) then (
    prof:void(web:remote-query($query, $vars))
  ) else (
    xquery:update($query, $vars)
  )
};

(:~
 : Evaluates the specified query on a remote server and returns the results.
 : @param  $query  query to be executed
 : @param  $vars   variables
 : @return result
 :)
declare %private function web:remote-query(
  $query  as xs:string,
  $vars   as map(*)
) as item()* {
  let $id := client:connect($G:SESSION/host, $G:SESSION/port, $G:SESSION/name, $G:SESSION/pass)
  return (
    client:query($id, $query, $vars),
    client:close($id)
  )
};

(:~
 : Web functions.
 :
 : @author Christian Grün, BaseX GmbH, 2014-15
 :)
module namespace web = 'dba/web';

import module namespace Request = 'http://exquery.org/ns/request';
import module namespace G = 'dba/global' at 'global.xqm';

(:~
 :
Creates a URL representation for the specified URI path
 : and query parameters. All parameters will be URI-encoded
 : and appended to the link. Map entries with empty string values
 : will be ignored.

 :
An example:
 :   { 'a':'foo', 'b':'bar' } -> ?a=foo&b=bar.
 :

 : @param  $link    the target page
 : @param  $params  the parameters to add
 : @return url
 :)
declare function web:create-url(
  $link    as xs:string,
  $params  as map(*)
) as xs:string? {
  $link || string-join(
    for $k in map:keys($params) ! string()
    for $v in distinct-values($params($k))
    count $c
    return (
      if($c = 1) then '?' else '&',
      encode-for-uri($k) || '=' || encode-for-uri(xs:string($v))
    )
  )
};

(:~
 : Returns the mime-type for the specified file.
 : @param  $name  file name
 : @return mime type
 :)
declare function web:mime-type(
  $name  as xs:string
) as xs:string {
  Q{java:org.basex.io.MimeTypes}get($name)
};

(:~
 : Creates a RESTXQ (HTTP) redirect header for the specified page.
 : @param  $page  page to forward to
 :)
declare %updating function web:redirect(
  $page  as xs:string
) {
  web:redirect($page, map { })
};

(:~
 : Creates a RESTXQ (HTTP) redirect header for the specified page and parameters.
 : @param  $page    page to forward to
 : @param  $params  map with query parameters
 :)
declare %updating function web:redirect(
  $page    as xs:string,
  $params  as map(*)
) {
  db:output(
    web:redirect-ro($page, $params)
  )
};

(:~
 : Creates a RESTXQ (HTTP) redirect header for the specified page.
 : @param  $page    page to forward to
 : @return redirect header
 :)
declare function web:redirect-ro(
  $page    as xs:string
) as element(rest:redirect) {
  web:redirect-ro($page, map {})
};

(:~
 : Creates a RESTXQ (HTTP) redirect header for the specified page and parameters.
 : @param  $page    page to forward to
 : @param  $params  map with query parameters
 : @return redirect header
 :)
declare function web:redirect-ro(
  $page    as xs:string,
  $params  as map(*)
) as element(rest:redirect) {
  element rest:redirect { web:create-url($page, $params) }
};

(:~
 : Checks if the current client is logged in. If not, raises an error.
 :)
declare function web:check(
) as empty-sequence() {
  if($G:SESSION) then () else error($G:LOGIN-ERROR, 'Please log in again.')
};

(:~
 : Evaluates a query and returns the result.
 : @param  $query  query
 : @return result of query
 :)
declare function web:query(
  $query  as xs:string?
) as xs:string {
  web:query($query, '', map {})
};

(:~
 : Evaluates a query and returns the result.
 : @param  $query  query
 : @param  $map    bindings
 : @param  $vars   variable bindings
 : @return result of query
 :)
declare function web:query(
  $query  as xs:string?,
  $map    as xs:string,
  $vars   as map(*)
) as xs:string {
  let $limit := $G:MAX-CHARS
  let $query := if($query) then $query else '()'
  let $q := "xquery:eval($query, map {" || $map || "}, " || web:query-options() || ")"
  let $s := "serialize(" || $q || ", map{ 'limit': $limit*2 })"
  return web:eval(
    $s ||
    "! (if(string-length(.) > $limit) then substring(., 1, $limit) || '...' else .)",
    map:merge((map { 'query': $query, 'limit': $limit }, $vars))
  )
};

(:~
 : Runs an updating query.
 : @param  $query  query
 : @return empty sequence
 :)
declare %updating function web:update-query(
  $query  as xs:string?
) {
  let $q := "xquery:update($query, map { }, " || web:query-options() || ")"
  return web:update($q, map { 'query': if($query) then $query else '()' })
};

(:~
 : Returns the options for evaluating a query.
 : @return options
 :)
declare %private function web:query-options() {
  "map { 'timeout':" || $G:TIMEOUT ||
       ",'memory':" || $G:MEMORY ||
       ",'permission':'" || $G:PERMISSION || "' }"
};

(:~
 : Evaluates the specified query locally or on a remote server and returns the results.
 : @param  $query  query to be executed
 : @return result
 :)
declare function web:eval(
  $query  as xs:string
) as item()* {
  web:eval($query, map {})
};

(:~
 : Evaluates the specified query locally or remotely and returns the resulting items.
 : @param  $query  query to be executed
 : @param  $vars   variables
 : @return result
 :)
declare function web:eval(
  $query  as xs:string,
  $vars   as map(*)
) as item()* {
  let $query := string-join((
    map:keys($vars) ! ('declare variable $' || . || ' external;'), $query
  ))
  return if($G:SESSION/host) then (
    web:remote-query($query, $vars)
  ) else (
    xquery:eval($query, $vars)
  )
};

(:~
 : Evaluates the specified updating query locally or remotely.
 : @param  $query  query to be executed
 : @param  $vars   variables
 :)
declare %updating function web:update(
  $query  as xs:string,
  $vars   as map(*)
) {
  let $query := string-join((
    map:keys($vars) ! ('declare variable $' || . || ' external;'), $query
  ))
  return if($G:SESSION/host) then (
    prof:void(web:remote-query($query, $vars))
  ) else (
    xquery:update($query, $vars)
  )
};

(:~
 : Evaluates the specified query on a remote server and returns the results.
 : @param  $query  query to be executed
 : @param  $vars   variables
 : @return result
 :)
declare %private function web:remote-query(
  $query  as xs:string,
  $vars   as map(*)
) as item()* {
  let $id := client:connect($G:SESSION/host, $G:SESSION/port, $G:SESSION/name, $G:SESSION/pass)
  return (
    client:query($id, $query, $vars),
    client:close($id)
  )
};


openhie language change: provider reigstry -> health worker regsitry

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

import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace csd_webconf =  "https://github.com/openhie/openinfoman/csd_webconf";

declare function page:redirect($redirect as xs:string) as element(restxq:redirect)
{
  <restxq:redirect>{ $redirect }</restxq:redirect>
};

declare updating
  %rest:path("/CSD/createDirectory")
  %rest:POST
  %rest:form-param("directory","{$directory}")
  %output:method("xhtml")
  function page:create_directory($directory)
{
   (
     if (not (csd_dm:document_source_exists($csd_webconf:db,$directory))) then
       csd_dm:empty($csd_webconf:db,$directory)
     else ()
     ,
   db:output(page:redirect(concat($csd_webconf:baseurl,"CSD")))
  )
};

declare
  %rest:path("/CSD")
  %rest:GET
  %output:method("xhtml")
  function page:list_functionality()
{
  let $what_is_dm :=

    The document manger registers documents to perform  careServicesRequests and getUpdatedServices requests  against.  Registered documents can be any of:

Local cache of a remote service directory -- this is useful for turning a Care Services Directory into an Open Info Manager

Sample Directories -- these loaded from an XML source file on the system and are useful for testing

The merged result of remote service directories

  let $csd :=

    {$what_is_dm}

      {let $docs := csd_dm:registered_documents($csd_webconf:db)
      return (
	  "You have ", count($docs) , " registered document(s) available: ",

	    {
	      for $doc in $docs
	      return

		 {$doc}
		 [
		   Empty
		  /  Delete
		  ]

	    }

	  )
      }

Create Blank Directory

      <form method='post' action="/CSD/createDirectory">
	<input name='directory' type='text'/>
	<input type='submit' value='Create'/>
      </form>

  let $svs :=

    In addition, there is some initial support for use of terminologies using the Sharing Value Sets(SVS) profile from IHE:

load sample Shared Value Sets

  let $adapters :=

      List of all adapters installed: CSD Adapters

  let $generic :=

These the top-level endpoints are exposed

Manage Stored Functions

Load and Register Sample Service Directories

Register and Poll Remote Service Service directories

Execute Ad-Hoc Care Services Requests and View Care Service Request Endpoints for Registered Documents

Endpoints for submitting getUpdatedServices Soap Request for Registered Documents

  return page:nocache(page:wrapper($csd,$svs,$adapters,$generic))
};

declare function page:nocache($response) {
(<http:response status="200" message="OK">
  <http:header name="Cache-Control" value="must-revalidate,no-cache,no-store"/>
</http:response>,
$response)
};

declare
  %rest:path("/CSD/getDirectory/{$name}")
  %rest:GET
  function page:get_directory($name)
{
  csd_dm:open_document($csd_webconf:db,$name)
};

declare updating
  %rest:path("/CSD/emptyDirectory/{$name}")
  %rest:GET
  function page:empty_directory($name)
{
  (
    csd_dm:empty($csd_webconf:db,$name)
    ,db:output(page:redirect(concat($csd_webconf:baseurl,"CSD")))
  )
};

declare updating
  %rest:path("/CSD/deleteDirectory/{$name}")
  %rest:GET
  function page:delete_directory($name)
{
  (
    csd_dm:delete($csd_webconf:db,$name)
    ,db:output(page:redirect(concat($csd_webconf:baseurl,"CSD")))
  )
};

declare function page:wrapper($csd,$svs,$adapters) {
  page:wrapper($csd,$svs,$adapters,())
};

declare function page:wrapper($csd,$svs,$adapters,$generic_csd) {
  let $generic :=

OpenInfoMan has been developed as part of OpenHIE and is intended to be the engine behind the CSD compliant Health Worker Registry and to be incorporated in OpenHIM.

    Source code is on github

  let $headers :=  (
    <link rel="stylesheet" type="text/css" media="screen"   href="{$csd_webconf:baseurl}static/bootstrap/js/tab.js"/>
    ,

    )
    let $content := (

Introduction

CSD Endpoints

SVS Endpoints

CSD Adapters

      ,

Welcome to OpenInfoMan

		  OpenInfoMan is an XQuery and RESTXQ based implementation of the Care Services Directory (CSD) profile from IHE which implements the following actors and transactions:

		    Info Manager :

		    Find Matching Services (Ad-Hoc and Stored) [ITI-73]

		    Query for  Updated Services Transaction [ITI-74]

		    Services Directory :

Query for  Updated Services Transaction [ITI-74]

		{$generic}
s

	      {$generic}
	      {$generic_csd}

	      {$csd}

	      {$generic}

	      {$svs}

	      {$generic}

	      {$adapters}

      )

  return csd_webconf:wrapper($content,$headers)
};

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

import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace csd_webconf =  "https://github.com/openhie/openinfoman/csd_webconf";

declare function page:redirect($redirect as xs:string) as element(restxq:redirect)
{
  <restxq:redirect>{ $redirect }</restxq:redirect>
};

declare updating
  %rest:path("/CSD/createDirectory")
  %rest:POST
  %rest:form-param("directory","{$directory}")
  %output:method("xhtml")
  function page:create_directory($directory)
{
   (
     if (not (csd_dm:document_source_exists($csd_webconf:db,$directory))) then
       csd_dm:empty($csd_webconf:db,$directory)
     else ()
     ,
   db:output(page:redirect(concat($csd_webconf:baseurl,"CSD")))
  )
};

declare
  %rest:path("/CSD")
  %rest:GET
  %output:method("xhtml")
  function page:list_functionality()
{
  let $what_is_dm :=

    The document manger registers documents to perform  careServicesRequests and getUpdatedServices requests  against.  Registered documents can be any of:

Local cache of a remote service directory -- this is useful for turning a Care Services Directory into an Open Info Manager

Sample Directories -- these loaded from an XML source file on the system and are useful for testing

The merged result of remote service directories

  let $csd :=

    {$what_is_dm}

      {let $docs := csd_dm:registered_documents($csd_webconf:db)
      return (
	  "You have ", count($docs) , " registered document(s) available: ",

	    {
	      for $doc in $docs
	      return

		 {$doc}
		 [
		   Empty
		  /  Delete
		  ]

	    }

	  )
      }

Create Blank Directory

      <form method='post' action="/CSD/createDirectory">
	<input name='directory' type='text'/>
	<input type='submit' value='Create'/>
      </form>

  let $svs :=

    In addition, there is some initial support for use of terminologies using the Sharing Value Sets(SVS) profile from IHE:

load sample Shared Value Sets

  let $adapters :=

      List of all adapters installed: CSD Adapters

  let $generic :=

These the top-level endpoints are exposed

Manage Stored Functions

Load and Register Sample Service Directories

Register and Poll Remote Service Service directories

Execute Ad-Hoc Care Services Requests and View Care Service Request Endpoints for Registered Documents

Endpoints for submitting getUpdatedServices Soap Request for Registered Documents

  return page:nocache(page:wrapper($csd,$svs,$adapters,$generic))
};

declare function page:nocache($response) {
(<http:response status="200" message="OK">
  <http:header name="Cache-Control" value="must-revalidate,no-cache,no-store"/>
</http:response>,
$response)
};

declare
  %rest:path("/CSD/getDirectory/{$name}")
  %rest:GET
  function page:get_directory($name)
{
  csd_dm:open_document($csd_webconf:db,$name)
};

declare updating
  %rest:path("/CSD/emptyDirectory/{$name}")
  %rest:GET
  function page:empty_directory($name)
{
  (
    csd_dm:empty($csd_webconf:db,$name)
    ,db:output(page:redirect(concat($csd_webconf:baseurl,"CSD")))
  )
};

declare updating
  %rest:path("/CSD/deleteDirectory/{$name}")
  %rest:GET
  function page:delete_directory($name)
{
  (
    csd_dm:delete($csd_webconf:db,$name)
    ,db:output(page:redirect(concat($csd_webconf:baseurl,"CSD")))
  )
};

declare function page:wrapper($csd,$svs,$adapters) {
  page:wrapper($csd,$svs,$adapters,())
};

declare function page:wrapper($csd,$svs,$adapters,$generic_csd) {
  let $generic :=

OpenInfoMan has been developed as part of OpenHIE and is intended to be the engine behind the CSD compliant Provider Registry and to be incorporated in OpenHIM.

    Source code is on github

  let $headers :=  (
    <link rel="stylesheet" type="text/css" media="screen"   href="{$csd_webconf:baseurl}static/bootstrap/js/tab.js"/>
    ,

    )
    let $content := (

Introduction

CSD Endpoints

SVS Endpoints

CSD Adapters

      ,

Welcome to OpenInfoMan

		  OpenInfoMan is an XQuery and RESTXQ based implementation of the Care Services Directory (CSD) profile from IHE which implements the following actors and transactions:

		    Info Manager :

		    Find Matching Services (Ad-Hoc and Stored) [ITI-73]

		    Query for  Updated Services Transaction [ITI-74]

		    Services Directory :

Query for  Updated Services Transaction [ITI-74]

		{$generic}
s

	      {$generic}
	      {$generic_csd}

	      {$csd}

	      {$generic}

	      {$svs}

	      {$generic}

	      {$adapters}

      )

  return csd_webconf:wrapper($content,$headers)
};