From e0080e3741cea86d4edc5d773f8b15c8f0173f91 Mon Sep 17 00:00:00 2001 From: Dave Cassel Date: Mon, 13 Jan 2014 12:34:43 -0500 Subject: [PATCH] #169 reformatting to match coding standards, including using spaces instead of tabs --- src/roxy/config/defaults.xqy | 100 ++-- src/roxy/lib/cpf-private.xqy | 182 ++++---- src/roxy/lib/cpf.xqy | 22 +- src/roxy/lib/date-parser.xqy | 865 ++++++++++++++++++----------------- src/roxy/lib/request.xqy | 8 +- src/roxy/lib/view-helper.xqy | 8 +- src/roxy/router.xqy | 148 +++--- 7 files changed, 671 insertions(+), 662 deletions(-) diff --git a/src/roxy/config/defaults.xqy b/src/roxy/config/defaults.xqy index 305df7b7..21c9fb85 100644 --- a/src/roxy/config/defaults.xqy +++ b/src/roxy/config/defaults.xqy @@ -29,63 +29,63 @@ declare namespace rest = "http://marklogic.com/appservices/rest"; : *********************************************** :) declare variable $ROXY-OPTIONS := - - - appbuilder + + + appbuilder - - main + + main - - html + + html - - - two-column - - ; + + + two-column + + ; declare variable $ROXY-ROUTES := - (: The default Roxy routes :) - let $default-controller := - ( - $config:ROXY-OPTIONS/*:default-controller, - $ROXY-OPTIONS/*:default-controller - )[1] - let $default-function := - ( - $config:ROXY-OPTIONS/*:default-function, - $ROXY-OPTIONS/*:default-function - )[1] - return - - - - $1 - - - - + (: The default Roxy routes :) + let $default-controller := + ( + $config:ROXY-OPTIONS/*:default-controller, + $ROXY-OPTIONS/*:default-controller + )[1] + let $default-function := + ( + $config:ROXY-OPTIONS/*:default-function, + $ROXY-OPTIONS/*:default-function + )[1] + return + + + + $1 + + + + - - $1 - $2 - $3 - - - - - $1 - $2 - $3 - - - - - - ; + + $1 + $2 + $3 + + + + + $1 + $2 + $3 + + + + + + ; (: : *********************************************** diff --git a/src/roxy/lib/cpf-private.xqy b/src/roxy/lib/cpf-private.xqy index 265bf947..140a709e 100644 --- a/src/roxy/lib/cpf-private.xqy +++ b/src/roxy/lib/cpf-private.xqy @@ -26,20 +26,20 @@ declare option xdmp:mapping "false"; declare private function local:clean-cpf() { - let $config := try { dom:configuration-get() } catch ($e) {} - return - if ($config) then - ( - for $d in dom:domains() - let $name as xs:string := $d/dom:domain-name - return - dom:remove($name), + let $config := try { dom:configuration-get() } catch ($e) {} + return + if ($config) then + ( + for $d in dom:domains() + let $name as xs:string := $d/dom:domain-name + return + dom:remove($name), - for $x as xs:string in p:pipelines( )/p:pipeline-name - return - p:remove($x) - ) - else () + for $x as xs:string in p:pipelines( )/p:pipeline-name + return + p:remove($x) + ) + else () }; declare private function local:get-cpf-files($dir as xs:string, $extension as xs:string) as xs:string* @@ -97,15 +97,15 @@ declare private function local:install-cpf-pipelines( { for $uri in $pipeline-uris let $doc := - let $map := - let $map := map:map() - let $_ := ( - map:put($map, "function", "get-doc"), - map:put($map, "uri", $uri)) - return - $map - return - local:call-private-function($map, $modules-database) + let $map := + let $map := map:map() + let $_ := ( + map:put($map, "function", "get-doc"), + map:put($map, "uri", $uri)) + return + $map + return + local:call-private-function($map, $modules-database) return p:insert($doc/*) }; @@ -118,81 +118,81 @@ declare private function local:create-cpf-domain( $pipeline-ids as xs:unsignedLong*, $permissions as element(sec:permission)*) as xs:unsignedLong { - let $domain-id := try { fn:data(dom:get($domain-name)/dom:domain-id) } catch ($e) { () } - return - (: update the existing domain :) - if ($domain-id) then - let $_ := - ( - dom:set-description($domain-name, $description), - dom:set-domain-scope($domain-name, $domain-scope), - dom:set-evaluation-context($domain-name, $context), - dom:set-pipelines($domain-name, $pipeline-ids), - dom:set-permissions($domain-name, $permissions) - ) - return - $domain-id - (: create a new domain :) - else - let $domain-id := dom:create($domain-name, $description, $domain-scope, $context, $pipeline-ids, $permissions) - return - $domain-id + let $domain-id := try { fn:data(dom:get($domain-name)/dom:domain-id) } catch ($e) { () } + return + (: update the existing domain :) + if ($domain-id) then + let $_ := + ( + dom:set-description($domain-name, $description), + dom:set-domain-scope($domain-name, $domain-scope), + dom:set-evaluation-context($domain-name, $context), + dom:set-pipelines($domain-name, $pipeline-ids), + dom:set-permissions($domain-name, $permissions) + ) + return + $domain-id + (: create a new domain :) + else + let $domain-id := dom:create($domain-name, $description, $domain-scope, $context, $pipeline-ids, $permissions) + return + $domain-id }; declare private function local:create-cpf-configuration( $restart-user as xs:string, - $evaluation-context as element(dom:evaluation-context), - $default-domain as xs:unsignedLong, - $permissions as element(sec:permission)*) + $evaluation-context as element(dom:evaluation-context), + $default-domain as xs:unsignedLong, + $permissions as element(sec:permission)*) { - let $config := - let $map := - let $map := map:map() - let $_ := map:put($map, "function", "configuration-get") - return - $map - return - local:call-private-function($map, ()) - return - if ($config) then - let $config := dom:configuration-set-restart-user($restart-user) - let $config := dom:configuration-set-evaluation-context($evaluation-context) - let $config := dom:configuration-set-default-domain($default-domain) - let $config := dom:configuration-set-permissions($permissions) - let $config := dom:configuration-set-conversion-enabled(fn:false()) - return - "Updated configuration" - else - let $config := - let $map := - let $map := map:map() - let $_ := ( - map:put($map, "function", "configuration-create"), - map:put($map, "restart-user", $restart-user), + let $config := + let $map := + let $map := map:map() + let $_ := map:put($map, "function", "configuration-get") + return + $map + return + local:call-private-function($map, ()) + return + if ($config) then + let $config := dom:configuration-set-restart-user($restart-user) + let $config := dom:configuration-set-evaluation-context($evaluation-context) + let $config := dom:configuration-set-default-domain($default-domain) + let $config := dom:configuration-set-permissions($permissions) + let $config := dom:configuration-set-conversion-enabled(fn:false()) + return + "Updated configuration" + else + let $config := + let $map := + let $map := map:map() + let $_ := ( + map:put($map, "function", "configuration-create"), + map:put($map, "restart-user", $restart-user), map:put($map, "evaluation-context", $evaluation-context), map:put($map, "default-domain", $default-domain), map:put($map, "permissions", $permissions)) - return - $map - return - local:call-private-function($map, ()) - let $option := - let $map := - let $map := map:map() - let $_ := ( - map:put($map, "function", "enable-conversion"), - map:put($map, "enable", fn:false())) - return - $map - return - local:call-private-function($map, ()) - return - "Created configuration" + return + $map + return + local:call-private-function($map, ()) + let $option := + let $map := + let $map := map:map() + let $_ := ( + map:put($map, "function", "enable-conversion"), + map:put($map, "enable", fn:false())) + return + $map + return + local:call-private-function($map, ()) + return + "Created configuration" }; declare private function local:evaluation-context( $database as xs:unsignedLong, - $root as xs:string) as element(dom:evaluation-context) + $root as xs:string) as element(dom:evaluation-context) { dom:evaluation-context($database, $root) }; @@ -208,9 +208,9 @@ declare private function local:configuration-get() as element(dom:configuration) declare private function local:configuration-create( $restart-user as xs:string, - $evaluation-context as element(dom:evaluation-context), - $default-domain as xs:unsignedLong, - $permissions as element(sec:permission)*) as xs:unsignedLong + $evaluation-context as element(dom:evaluation-context), + $default-domain as xs:unsignedLong, + $permissions as element(sec:permission)*) as xs:unsignedLong { dom:configuration-create($restart-user, $evaluation-context, $default-domain, $permissions) }; @@ -229,9 +229,9 @@ declare private function local:call-private-function( (xs:QName("map"), $map), if ($database-id) then - {$database-id} - - else ()) + {$database-id} + + else ()) }; declare private function local:get-doc($uri as xs:string) diff --git a/src/roxy/lib/cpf.xqy b/src/roxy/lib/cpf.xqy index 8782b7e6..94782539 100644 --- a/src/roxy/lib/cpf.xqy +++ b/src/roxy/lib/cpf.xqy @@ -64,10 +64,10 @@ declare option xdmp:mapping "false"; your-restart-user-name - - read - admin - + + read + admin + @@ -198,9 +198,9 @@ declare function cpf:create-cpf-domain( :) declare function cpf:create-cpf-configuration( $restart-user as xs:string, - $evaluation-context as element(dom:evaluation-context), - $default-domain as xs:unsignedLong, - $permissions as element(sec:permission)*) + $evaluation-context as element(dom:evaluation-context), + $default-domain as xs:unsignedLong, + $permissions as element(sec:permission)*) { let $map := map:map() let $_ := @@ -241,8 +241,8 @@ declare function cpf:domain-scope( : @return - a constructed dom:evaluation-context element :) declare function cpf:evaluation-context( - $database as xs:unsignedLong, - $root as xs:string) as element(dom:evaluation-context) + $database as xs:unsignedLong, + $root as xs:string) as element(dom:evaluation-context) { dom:evaluation-context($database, $root) }; @@ -266,6 +266,6 @@ declare private function cpf:call-private-function( "/roxy/lib/cpf-private.xqy", (xs:QName("map"), $map), - {$database-id} - ) + {$database-id} + ) }; \ No newline at end of file diff --git a/src/roxy/lib/date-parser.xqy b/src/roxy/lib/date-parser.xqy index 3d68bac1..270540ba 100644 --- a/src/roxy/lib/date-parser.xqy +++ b/src/roxy/lib/date-parser.xqy @@ -25,502 +25,511 @@ declare variable $analyzeString := try { xdmp:function(xs:QName("fn:analyze-stri declare variable $regexSupported := try { exists(xdmp:apply($analyzeString, " ", " ")) } catch ($e) { false() }; declare variable $dateparser:FORMATS as element(format)+ := ( - (: Thu Jul 07 2011 11:05:42 GMT-0700 (PDT) :) - - \w+ - - \w+ - - \d\d - - \d\d\d\d - - \d\d - : - \d\d - : - \d\d - - gmt - -\d\d\d\d|\+\d\d\d\d - - \(\w+\) - , - - (: 2011:04:19 12:29:42 :) - - \d\d\d\d - : - \d\d - : - \d\d - - \d\d - : - \d\d - : - \d\d - , - - (: Sun Aug 15 19:42:00 2004 :) - - \w+ - - \w+ - - \d\d - - \d\d - : - \d\d - : - \d\d - - \d\d\d\d - , - - (: 25-Oct-2004 17:06:46 -0500 :) - - \d\d - - - \w+ - - - \d\d\d\d - - \d\d - : - \d\d - : - \d\d - - -\d\d\d\d|\+\d\d\d\d - , - - (: Mon, 23 Sep 0102 23:14:26 +0900 :) - - \w+, - - \d\d - - \w+ - - \d\d\d\d - - \d\d - : - \d\d - : - \d\d - - -\d\d\d\d|\+\d\d\d\d - , - - (: 30 Jun 2006 09:39:08 -0500 :) - - \d\d - - \w+ - - \d\d\d\d - - \d\d - : - \d\d - : - \d\d - - -\d\d\d\d|\+\d\d\d\d - , - - (: Apr 16 13:49:06 2003 +0200 :) - - \w+ - - \d\d - - \d\d - : - \d\d - : - \d\d - - \d\d\d\d - - -\d\d\d\d|\+\d\d\d\d - , - - (: Aug 04 11:44:58 EDT 2003 :) - - \w+ - - \d\d - - \d\d - : - \d\d - : - \d\d - - \w\w\w - - \d\d\d\d - , - - (: 4 Jan 98 0:41 EDT :) - - \d\d? - - \w+ - - \d\d - - \d\d? - : - \d\d - - \w\w\w - , - - (: 08/20/2007 5:58:20 AM:) - (: 08/20/07 5:58:20 AM:) - - \d\d? - / - \d\d? - / - \d\d\d\d|\d\d - - \d\d? - : - \d\d - : - \d\d - - - , - - - (: 08-20-2007 :) - (: 08-20-07 :) - - \d\d - - - \d\d - - - \d\d\d\d|\d\d - , - - (: 2007/08/20 :) - (: 07/08/20 :) - - \d\d\d\d|\d\d - / - \d\d - / - \d\d - , - - (: 08/20/2007 :) - (: 08/20/07 :) - - \d\d - / - \d\d - / - \d\d\d\d|\d\d - , - - (: 20070920 :) - - \d\d\d\d - \d\d - \d\d - , - - (: December 20th, 2005 :) - - \w+ - - \d\d|\d?\dth|1st|2nd|3rd - ,? - - \d\d\d\d - , - - (: 2009/04/15 14:26:51+12'00' :) - - \d\d\d\d - / - \d\d - / - \d\d - - \d\d - : - \d\d - : - \d\d - -\d\d'\d\d'|\+\d\d'\d\d'|\w+ - + (: Thu Jul 07 2011 11:05:42 GMT-0700 (PDT) :) + + \w+ + + \w+ + + \d\d + + \d\d\d\d + + \d\d + : + \d\d + : + \d\d + + gmt + -\d\d\d\d|\+\d\d\d\d + + \(\w+\) + , + + (: 2011:04:19 12:29:42 :) + + \d\d\d\d + : + \d\d + : + \d\d + + \d\d + : + \d\d + : + \d\d + , + + (: Sun Aug 15 19:42:00 2004 :) + + \w+ + + \w+ + + \d\d + + \d\d + : + \d\d + : + \d\d + + \d\d\d\d + , + + (: 25-Oct-2004 17:06:46 -0500 :) + + \d\d + - + \w+ + - + \d\d\d\d + + \d\d + : + \d\d + : + \d\d + + -\d\d\d\d|\+\d\d\d\d + , + + (: Mon, 23 Sep 0102 23:14:26 +0900 :) + + \w+, + + \d\d + + \w+ + + \d\d\d\d + + \d\d + : + \d\d + : + \d\d + + -\d\d\d\d|\+\d\d\d\d + , + + (: 30 Jun 2006 09:39:08 -0500 :) + + \d\d + + \w+ + + \d\d\d\d + + \d\d + : + \d\d + : + \d\d + + -\d\d\d\d|\+\d\d\d\d + , + + (: Apr 16 13:49:06 2003 +0200 :) + + \w+ + + \d\d + + \d\d + : + \d\d + : + \d\d + + \d\d\d\d + + -\d\d\d\d|\+\d\d\d\d + , + + (: Aug 04 11:44:58 EDT 2003 :) + + \w+ + + \d\d + + \d\d + : + \d\d + : + \d\d + + \w\w\w + + \d\d\d\d + , + + (: 4 Jan 98 0:41 EDT :) + + \d\d? + + \w+ + + \d\d + + \d\d? + : + \d\d + + \w\w\w + , + + (: 08/20/2007 5:58:20 AM:) + (: 08/20/07 5:58:20 AM:) + + \d\d? + / + \d\d? + / + \d\d\d\d|\d\d + + \d\d? + : + \d\d + : + \d\d + + + , + + + (: 08-20-2007 :) + (: 08-20-07 :) + + \d\d + - + \d\d + - + \d\d\d\d|\d\d + , + + (: 2007/08/20 :) + (: 07/08/20 :) + + \d\d\d\d|\d\d + / + \d\d + / + \d\d + , + + (: 08/20/2007 :) + (: 08/20/07 :) + + \d\d + / + \d\d + / + \d\d\d\d|\d\d + , + + (: 20070920 :) + + \d\d\d\d + \d\d + \d\d + , + + (: December 20th, 2005 :) + + \w+ + + \d\d|\d?\dth|1st|2nd|3rd + ,? + + \d\d\d\d + , + + (: 2009/04/15 14:26:51+12'00' :) + + \d\d\d\d + / + \d\d + / + \d\d + + \d\d + : + \d\d + : + \d\d + -\d\d'\d\d'|\+\d\d'\d\d'|\w+ + ); declare function dateparser:isSupported( ) as xs:boolean { - $regexSupported + $regexSupported }; declare function dateparser:parse( - $date as xs:string + $date as xs:string ) { - if($date castable as xs:dateTime) - then xs:dateTime($date) - else if($date castable as xs:date) - then adjust-dateTime-to-timezone(xs:dateTime(concat($date, "T00:00:00")), implicit-timezone()) - else if($regexSupported) - then - let $date := normalize-space($date) - for $format in $dateparser:FORMATS - let $regex := dateparser:assembleFormat($format) - where matches($date, $regex, "i") - return dateparser:analyzedStringToDate(xdmp:apply($analyzeString, $date, $regex, "i"), $format) - else () + if($date castable as xs:dateTime) then + xs:dateTime($date) + else if($date castable as xs:date) then + adjust-dateTime-to-timezone(xs:dateTime(concat($date, "T00:00:00")), implicit-timezone()) + else if($regexSupported) then + let $date := normalize-space($date) + for $format in $dateparser:FORMATS + let $regex := dateparser:assembleFormat($format) + where matches($date, $regex, "i") + return dateparser:analyzedStringToDate(xdmp:apply($analyzeString, $date, $regex, "i"), $format) + else () }; declare private function dateparser:assembleFormat( - $format as element(format) + $format as element(format) ) as xs:string { - let $groups := - for $token in $format/* - return - if(local-name($token) = "whitespace") - then "\s+" - else if(local-name($token) = "string") - then string($token) - else if(local-name($token) = "meridiem") - then "(am|pm|a\.m\.|p\.m\.)" - else concat("(", string($token), ")") - return concat("^", string-join($groups, ""), "$") + let $groups := + for $token in $format/* + return + if(local-name($token) = "whitespace") then + "\s+" + else if(local-name($token) = "string") then + string($token) + else if(local-name($token) = "meridiem") then + "(am|pm|a\.m\.|p\.m\.)" + else + concat("(", string($token), ")") + return concat("^", string-join($groups, ""), "$") }; declare private function dateparser:analyzedStringToDate( - $string as element(s:analyze-string-result), - $format as element(format) + $string as element(s:analyze-string-result), + $format as element(format) ) as xs:dateTime? { - let $yearPosition := dateparser:extractLocationFromAnalyzedString("year", $format) - let $monthPosition := dateparser:extractLocationFromAnalyzedString("month", $format) - let $dayPosition := dateparser:extractLocationFromAnalyzedString("day", $format) - let $hourPosition := dateparser:extractLocationFromAnalyzedString("hour", $format) - let $minutePosition := dateparser:extractLocationFromAnalyzedString("minute", $format) - let $secondPosition := dateparser:extractLocationFromAnalyzedString("second", $format) - let $timezonePosition := dateparser:extractLocationFromAnalyzedString("timezone", $format) - let $meridiemPosition := dateparser:extractLocationFromAnalyzedString("meridiem", $format) - let $year := dateparser:processYear(string($string//s:group[@nr = $yearPosition])) - let $month := dateparser:processMonth(string($string//s:group[@nr = $monthPosition])) - let $day := dateparser:processDay(string($string//s:group[@nr = $dayPosition])) - let $hourString := - if($meridiemPosition) - then dateparser:adjustHourForMeridiem(string($string//s:group[@nr = $hourPosition]), string($string//s:group[@nr = $meridiemPosition])) - else string($string//s:group[@nr = $hourPosition]) - let $hour := dateparser:expandTwoDigits($hourString, "00") - let $minute := dateparser:expandTwoDigits(string($string//s:group[@nr = $minutePosition]), "00") - let $second := dateparser:expandTwoDigits(string($string//s:group[@nr = $secondPosition]), "00") - let $timezone := dateparser:processZone(string($string//s:group[@nr = $timezonePosition])) - - let $possibleDate := concat($year, "-", $month, "-", $day, "T", $hour, ":", $minute, ":", $second, $timezone) - where $possibleDate castable as xs:dateTime - return - if($timezone = "") - then adjust-dateTime-to-timezone(xs:dateTime($possibleDate), implicit-timezone()) - else xs:dateTime($possibleDate) + let $yearPosition := dateparser:extractLocationFromAnalyzedString("year", $format) + let $monthPosition := dateparser:extractLocationFromAnalyzedString("month", $format) + let $dayPosition := dateparser:extractLocationFromAnalyzedString("day", $format) + let $hourPosition := dateparser:extractLocationFromAnalyzedString("hour", $format) + let $minutePosition := dateparser:extractLocationFromAnalyzedString("minute", $format) + let $secondPosition := dateparser:extractLocationFromAnalyzedString("second", $format) + let $timezonePosition := dateparser:extractLocationFromAnalyzedString("timezone", $format) + let $meridiemPosition := dateparser:extractLocationFromAnalyzedString("meridiem", $format) + let $year := dateparser:processYear(string($string//s:group[@nr = $yearPosition])) + let $month := dateparser:processMonth(string($string//s:group[@nr = $monthPosition])) + let $day := dateparser:processDay(string($string//s:group[@nr = $dayPosition])) + let $hourString := + if($meridiemPosition) then + dateparser:adjustHourForMeridiem(string($string//s:group[@nr = $hourPosition]), string($string//s:group[@nr = $meridiemPosition])) + else + string($string//s:group[@nr = $hourPosition]) + let $hour := dateparser:expandTwoDigits($hourString, "00") + let $minute := dateparser:expandTwoDigits(string($string//s:group[@nr = $minutePosition]), "00") + let $second := dateparser:expandTwoDigits(string($string//s:group[@nr = $secondPosition]), "00") + let $timezone := dateparser:processZone(string($string//s:group[@nr = $timezonePosition])) + + let $possibleDate := concat($year, "-", $month, "-", $day, "T", $hour, ":", $minute, ":", $second, $timezone) + where $possibleDate castable as xs:dateTime + return + if($timezone = "") then + adjust-dateTime-to-timezone(xs:dateTime($possibleDate), implicit-timezone()) + else + xs:dateTime($possibleDate) }; declare private function dateparser:adjustHourForMeridiem( - $hourString as xs:string, - $meridiemString as xs:string + $hourString as xs:string, + $meridiemString as xs:string ) as xs:string { - if(starts-with($meridiemString, "a") or starts-with($meridiemString, "A")) - then - if($hourString eq "12") - then "00" - else $hourString + if (starts-with($meridiemString, "a") or starts-with($meridiemString, "A")) then + if ($hourString eq "12") then + "00" + else + $hourString + else + if ($hourString eq "12") then + $hourString else - if($hourString eq "12") - then $hourString - else xs:string(xs:integer($hourString) + 12) + xs:string(xs:integer($hourString) + 12) }; declare private function dateparser:extractLocationFromAnalyzedString( - $part as xs:string, - $format as element(format) + $part as xs:string, + $format as element(format) ) as xs:integer? { - let $element := $format/*[local-name(.) = $part] - where exists($element) - return count($element/preceding-sibling::*[not(local-name(.) = ("whitespace", "string"))]) + 1 + let $element := $format/*[local-name(.) = $part] + where exists($element) + return count($element/preceding-sibling::*[not(local-name(.) = ("whitespace", "string"))]) + 1 }; declare private function dateparser:processYear( - $year as xs:string? + $year as xs:string? ) as xs:string? { - if($year castable as xs:integer) - then - if(xs:integer($year) < 100) - then - if(xs:integer($year) > 50) - then concat("19", xs:string(xs:integer($year))) - else if(xs:integer($year) = 0) - then "1900" - else concat("20", dateparser:stringPad("0", 2 - string-length($year)), $year) - else if(xs:integer($year) >= 100 and xs:integer($year) < 200) (: 102 = 2002, thanks Java :) - then xs:string(1900 + xs:integer($year)) - else if(xs:integer($year) > 9999) - then "9999" - else $year - else $year + if ($year castable as xs:integer) then + if (xs:integer($year) < 100) then + if (xs:integer($year) > 50) then + concat("19", xs:string(xs:integer($year))) + else if (xs:integer($year) = 0) then + "1900" + else + concat("20", dateparser:stringPad("0", 2 - string-length($year)), $year) + else if (xs:integer($year) >= 100 and xs:integer($year) < 200) then + (: 102 = 2002, thanks Java :) + xs:string(1900 + xs:integer($year)) + else if (xs:integer($year) > 9999) then + "9999" + else + $year + else + $year }; (: - Takes in a 'month' as a string and returns it as a number. For example: - "1" -> "01" - "01" -> "01" - "february" -> "02" - "feb" -> "02" + Takes in a 'month' as a string and returns it as a number. For example: + "1" -> "01" + "01" -> "01" + "february" -> "02" + "feb" -> "02" :) declare private function dateparser:processMonth( - $month as xs:string? + $month as xs:string? ) as xs:string { - let $month := lower-case($month) - let $parsedMonth := ( - let $months := ( - "jan", "january", "enero", "janvier", "januar", "gennaio", - "feb", "february", "febrero", "fevrier", "februar", "febbraio", - "mar", "march", "marzo", "mars", "marz", "marzo", - "apr", "april", "abril", "avril", "april", "aprile", - "may", "may", "mayo", "mai", "mai", "maggio", - "jun", "june", "junio", "juin", "juni", "giugno", - "jul", "july", "julio", "juillet", "juli", "luglio", - "aug", "august", "agosto", "aout", "august", "agosto", - "sep", "september", "septiembre", "septembre", "september", "settembre", - "oct", "october", "octubre", "octobre", "oktober", "ottobre", - "nov", "november", "noviembre", "novembre", "november", "novembre", - "dec", "december", "diciembre", "decembre", "dezember", "dicembre" - ) - let $monthSansDiacritics := xdmp:diacritic-less($month) - for $i at $pos in $months - let $pos := ceiling($pos div 6) - where $i = $monthSansDiacritics - return concat(dateparser:stringPad("0", 2 - string-length(string($pos))), $pos) - )[1] - return - if(exists($parsedMonth)) - then $parsedMonth - else dateparser:expandTwoDigits($month, "01") + let $month := lower-case($month) + let $parsedMonth := ( + let $months := ( + "jan", "january", "enero", "janvier", "januar", "gennaio", + "feb", "february", "febrero", "fevrier", "februar", "febbraio", + "mar", "march", "marzo", "mars", "marz", "marzo", + "apr", "april", "abril", "avril", "april", "aprile", + "may", "may", "mayo", "mai", "mai", "maggio", + "jun", "june", "junio", "juin", "juni", "giugno", + "jul", "july", "julio", "juillet", "juli", "luglio", + "aug", "august", "agosto", "aout", "august", "agosto", + "sep", "september", "septiembre", "septembre", "september", "settembre", + "oct", "october", "octubre", "octobre", "oktober", "ottobre", + "nov", "november", "noviembre", "novembre", "november", "novembre", + "dec", "december", "diciembre", "decembre", "dezember", "dicembre" + ) + let $monthSansDiacritics := xdmp:diacritic-less($month) + for $i at $pos in $months + let $pos := ceiling($pos div 6) + where $i = $monthSansDiacritics + return concat(dateparser:stringPad("0", 2 - string-length(string($pos))), $pos) + )[1] + return + if (exists($parsedMonth)) then + $parsedMonth + else + dateparser:expandTwoDigits($month, "01") }; declare private function dateparser:processDay( - $day as xs:string? + $day as xs:string? ) as xs:string { - dateparser:expandTwoDigits( - if(matches($day, "^(\d?\d)(st|nd|rd|th)$")) - then replace($day, "st|nd|rd|th", "") - else $day - , "01") + dateparser:expandTwoDigits( + if (matches($day, "^(\d?\d)(st|nd|rd|th)$")) then + replace($day, "st|nd|rd|th", "") + else + $day, + "01") }; declare private function dateparser:processZone( - $zone as xs:string? + $zone as xs:string? ) as xs:string { - let $zone := replace($zone, "'", "") - return - if(matches($zone, "[+-]\d\d\d\d")) - then concat(substring($zone, 0, 4), ":", substring($zone, 4)) - else if(matches($zone, "\w\w\w")) - then dateparser:timezoneLookup($zone) - else "" + let $zone := replace($zone, "'", "") + return + if(matches($zone, "[+-]\d\d\d\d")) then + concat(substring($zone, 0, 4), ":", substring($zone, 4)) + else if(matches($zone, "\w\w\w")) then + dateparser:timezoneLookup($zone) + else + "" }; declare private function dateparser:stringPad( - $string as xs:string, - $count as xs:integer + $string as xs:string, + $count as xs:integer ) as xs:string { - string-join(for $i in (1 to $count) return $string, "") + string-join(for $i in (1 to $count) return $string, "") }; declare private function dateparser:timezoneLookup( - $tz as xs:string + $tz as xs:string ) as xs:string { - let $tz := upper-case($tz) - return - if($tz = "MIT") then "-11:00" else - if($tz = "HST") then "-10:00" else - if($tz = "AST") then "-09:00" else - if($tz = "PST") then "-08:00" else - if($tz = "MST") then "-07:00" else - if($tz = "PNT") then "-07:00" else - if($tz = "CST") then "-06:00" else - if($tz = "EST") then "-05:00" else - if($tz = "IET") then "-05:00" else - if($tz = "PRT") then "-04:00" else - if($tz = "CNT") then "-03:00" else - if($tz = "AGT") then "-03:00" else - if($tz = "BET") then "-03:00" else - if($tz = "GMT") then "+00:00" else - if($tz = "UCT") then "+00:00" else - if($tz = "UTC") then "+00:00" else - if($tz = "WET") then "+00:00" else - if($tz = "CET") then "+01:00" else - if($tz = "ECT") then "+01:00" else - if($tz = "MET") then "+01:00" else - if($tz = "ART") then "+02:00" else - if($tz = "CAT") then "+02:00" else - if($tz = "EET") then "+02:00" else - if($tz = "EAT") then "+03:00" else - if($tz = "NET") then "+04:00" else - if($tz = "PLT") then "+05:00" else - if($tz = "IST") then "+05:00" else - if($tz = "BST") then "+06:00" else - if($tz = "VST") then "+07:00" else - if($tz = "CTT") then "+08:00" else - if($tz = "PRC") then "+08:00" else - if($tz = "JST") then "+09:00" else - if($tz = "ROK") then "+09:00" else - if($tz = "ACT") then "+09:00" else - if($tz = "AET") then "+10:00" else - if($tz = "SST") then "+11:00" else - if($tz = "NST") then "+12:00" else - if($tz = "PDT") then "-07:00" else - if($tz = "MDT") then "-06:00" else - if($tz = "CDT") then "-05:00" else - if($tz = "EDT") then "-04:00" else - "" + let $tz := upper-case($tz) + return + if ($tz = "MIT") then "-11:00" else + if ($tz = "HST") then "-10:00" else + if ($tz = "AST") then "-09:00" else + if ($tz = "PST") then "-08:00" else + if ($tz = "MST") then "-07:00" else + if ($tz = "PNT") then "-07:00" else + if ($tz = "CST") then "-06:00" else + if ($tz = "EST") then "-05:00" else + if ($tz = "IET") then "-05:00" else + if ($tz = "PRT") then "-04:00" else + if ($tz = "CNT") then "-03:00" else + if ($tz = "AGT") then "-03:00" else + if ($tz = "BET") then "-03:00" else + if ($tz = "GMT") then "+00:00" else + if ($tz = "UCT") then "+00:00" else + if ($tz = "UTC") then "+00:00" else + if ($tz = "WET") then "+00:00" else + if ($tz = "CET") then "+01:00" else + if ($tz = "ECT") then "+01:00" else + if ($tz = "MET") then "+01:00" else + if ($tz = "ART") then "+02:00" else + if ($tz = "CAT") then "+02:00" else + if ($tz = "EET") then "+02:00" else + if ($tz = "EAT") then "+03:00" else + if ($tz = "NET") then "+04:00" else + if ($tz = "PLT") then "+05:00" else + if ($tz = "IST") then "+05:00" else + if ($tz = "BST") then "+06:00" else + if ($tz = "VST") then "+07:00" else + if ($tz = "CTT") then "+08:00" else + if ($tz = "PRC") then "+08:00" else + if ($tz = "JST") then "+09:00" else + if ($tz = "ROK") then "+09:00" else + if ($tz = "ACT") then "+09:00" else + if ($tz = "AET") then "+10:00" else + if ($tz = "SST") then "+11:00" else + if ($tz = "NST") then "+12:00" else + if ($tz = "PDT") then "-07:00" else + if ($tz = "MDT") then "-06:00" else + if ($tz = "CDT") then "-05:00" else + if ($tz = "EDT") then "-04:00" else + "" }; declare private function dateparser:expandTwoDigits( - $num as xs:string?, - $default as xs:string + $num as xs:string?, + $default as xs:string ) as xs:string { - (: - The first check is designed to see if the value is some bogus input, eg: 2007/foo/bar. - If $num is less than 0, isn't a number or is empty, return "01". - Else pad $num with 0's if need be. - :) - if(exists($num) and $num != "" and not($num castable as xs:integer)) - then "NAN" - else if(empty($num) or not($num castable as xs:integer) or xs:integer($num) <= 0) - then $default - else concat(dateparser:stringPad("0", 2 - string-length($num)), $num) + (: + : The first check is designed to see if the value is some bogus input, eg: 2007/foo/bar. + : If $num is less than 0, isn't a number or is empty, return "01". + : Else pad $num with 0's if need be. + :) + if (exists($num) and $num != "" and not($num castable as xs:integer)) then + "NAN" + else if (empty($num) or not($num castable as xs:integer) or xs:integer($num) <= 0) then + $default + else + concat(dateparser:stringPad("0", 2 - string-length($num)), $num) }; \ No newline at end of file diff --git a/src/roxy/lib/request.xqy b/src/roxy/lib/request.xqy index c02b5f47..eb75ebba 100644 --- a/src/roxy/lib/request.xqy +++ b/src/roxy/lib/request.xqy @@ -250,10 +250,10 @@ declare function req:required( { let $value := req:get($name, $options) return - if (fn:exists($value)) then - $value - else - fn:error( + if (fn:exists($value)) then + $value + else + fn:error( xs:QName("MISSING-CONTROLLER-PARAM"), fn:concat("Required parameter '", $name, "' is missing"), ($name, $r:__CALLER_FILE__)) diff --git a/src/roxy/lib/view-helper.xqy b/src/roxy/lib/view-helper.xqy index e103869b..ce39fdbc 100644 --- a/src/roxy/lib/view-helper.xqy +++ b/src/roxy/lib/view-helper.xqy @@ -27,10 +27,10 @@ declare function vh:required($name as xs:string) { let $value := map:get($vh:map, $name) return - if (fn:exists($value)) then - $value - else - fn:error(xs:QName("MISSING-PARAM"), $name) + if (fn:exists($value)) then + $value + else + fn:error(xs:QName("MISSING-PARAM"), $name) }; declare function vh:render($view as xs:string, $format as xs:string) diff --git a/src/roxy/router.xqy b/src/roxy/router.xqy index 33b0985b..01065757 100644 --- a/src/roxy/router.xqy +++ b/src/roxy/router.xqy @@ -30,86 +30,86 @@ declare variable $controller as xs:QName := req:get("controller", "type=xs:QName declare variable $controller-path as xs:string := fn:concat("/app/controllers/", $controller, ".xqy"); declare variable $func as xs:string := req:get("func", "main", "type=xs:string"); declare variable $default-format := -( - $config:ROXY-OPTIONS/*:default-format, - $def:ROXY-OPTIONS/*:default-format -)[1]; + ( + $config:ROXY-OPTIONS/*:default-format, + $def:ROXY-OPTIONS/*:default-format + )[1]; declare variable $format as xs:string := req:get("format", $default-format, "type=xs:string"); declare variable $default-view as xs:string := fn:concat($controller, "/", $func); (: assume no default layout for xml, json, text :) declare variable $default-layout as xs:string? := -( - $config:ROXY-OPTIONS/*:layouts/*:layout[@format = $format], - $def:ROXY-OPTIONS/*:layouts/*:layout[@format = $format] -)[1]; + ( + $config:ROXY-OPTIONS/*:layouts/*:layout[@format = $format], + $def:ROXY-OPTIONS/*:layouts/*:layout[@format = $format] + )[1]; declare function router:route() { - (: run the controller. errors bubble up to the error module :) - let $data := - xdmp:apply( - xdmp:function( - fn:QName(fn:concat("http://marklogic.com/roxy/controller/", $controller), $func), - $controller-path)) - - (: Roxy options :) - let $options := - for $key in map:keys($ch:map) - where fn:starts-with($key, "ch:config-") - return - map:get($ch:map, $key) - - (: remove options from the data :) - let $_ := - for $key in map:keys($ch:map) - where fn:starts-with($key, "ch:config-") - return - map:delete($ch:map, $key) - - let $format as xs:string := ($options[self::ch:config-format][ch:formats/ch:format = $format]/ch:format, $format)[1] - let $_ := rh:set-content-type($format) - - (: controller override of the view :) - let $view := ($options[self::ch:config-view][ch:formats/ch:format = $format]/ch:view, $default-view)[1][. ne ""] - - (: controller override of the layout :) - let $layout := - if (fn:exists($options[self::ch:config-layout][ch:formats/ch:format = $format])) then - $options[self::ch:config-layout][ch:formats/ch:format = $format]/ch:layout[. ne ""] - else - $default-layout - - (: if the view return something other than the map or () then bypass the view and layout :) - let $bypass as xs:boolean := fn:exists($data) and fn:not($data instance of map:map) - - return - if (fn:not($bypass) and (fn:exists($view) or fn:exists($layout))) then - let $view-result := - if (fn:exists($ch:map) and fn:exists($view)) then - rh:render-view($view, $format, $ch:map) - else - () - return - if (fn:not($bypass) and fn:exists($layout)) then - let $_ := - if (fn:exists($view-result) and - fn:not($view-result instance of map:map) and - fn:not(fn:deep-equal(document {$ch:map}, document {$view-result}))) then - map:put($ch:map, "view", $view-result) - else - map:put($ch:map, "view", - for $key in map:keys($ch:map) - return - map:get($ch:map, $key)) - return - rh:render-layout($layout, $format, $ch:map) - else - $view-result - else if (fn:not($bypass)) then - for $key in map:keys($ch:map) - return - map:get($ch:map, $key) - else - $data + (: run the controller. errors bubble up to the error module :) + let $data := + xdmp:apply( + xdmp:function( + fn:QName(fn:concat("http://marklogic.com/roxy/controller/", $controller), $func), + $controller-path)) + + (: Roxy options :) + let $options := + for $key in map:keys($ch:map) + where fn:starts-with($key, "ch:config-") + return + map:get($ch:map, $key) + + (: remove options from the data :) + let $_ := + for $key in map:keys($ch:map) + where fn:starts-with($key, "ch:config-") + return + map:delete($ch:map, $key) + + let $format as xs:string := ($options[self::ch:config-format][ch:formats/ch:format = $format]/ch:format, $format)[1] + let $_ := rh:set-content-type($format) + + (: controller override of the view :) + let $view := ($options[self::ch:config-view][ch:formats/ch:format = $format]/ch:view, $default-view)[1][. ne ""] + + (: controller override of the layout :) + let $layout := + if (fn:exists($options[self::ch:config-layout][ch:formats/ch:format = $format])) then + $options[self::ch:config-layout][ch:formats/ch:format = $format]/ch:layout[. ne ""] + else + $default-layout + + (: if the view return something other than the map or () then bypass the view and layout :) + let $bypass as xs:boolean := fn:exists($data) and fn:not($data instance of map:map) + + return + if (fn:not($bypass) and (fn:exists($view) or fn:exists($layout))) then + let $view-result := + if (fn:exists($ch:map) and fn:exists($view)) then + rh:render-view($view, $format, $ch:map) + else + () + return + if (fn:not($bypass) and fn:exists($layout)) then + let $_ := + if (fn:exists($view-result) and + fn:not($view-result instance of map:map) and + fn:not(fn:deep-equal(document {$ch:map}, document {$view-result}))) then + map:put($ch:map, "view", $view-result) + else + map:put($ch:map, "view", + for $key in map:keys($ch:map) + return + map:get($ch:map, $key)) + return + rh:render-layout($layout, $format, $ch:map) + else + $view-result + else if (fn:not($bypass)) then + for $key in map:keys($ch:map) + return + map:get($ch:map, $key) + else + $data }; \ No newline at end of file