Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- xquery version "3.0";
- declare function local:object.new($xml-fragment as item())
- {
- let $clone := $xml-fragment (: fragment passed by value ? :)
- (: util:deep-copy($xml-fragment) :) (: TODO -- necessary? :)
- return map:new
- (
- (
- map:entry("__id", util:uuid()),
- map:entry("__hash", util:hash($clone,'MD5')),
- map:entry("__object", $clone),
- map:entry
- (
- "valid()",
- (
- element description { 'Ensures that the internal hash matches that of the embedded object.' },
- function($function) { inspect:inspect-function($function) },
- function($map as map) as xs:boolean { $map("__hash") eq util:hash($map("__object"),'MD5') }
- )
- ),
- map:entry
- (
- "id()",
- (
- element description { 'Returns the identifier for this object.' },
- function($function) { inspect:inspect-function($function) },
- function($map as map) as xs:string { $map("__id") }
- )
- ),
- for $object at $pos in $clone//*[@type/string() eq "object"] (:TODO:) (: store each found object into map :)
- return
- (
- map:entry
- (
- (:if($object/@name) then $object/@name/string() else $pos,:) (: BUG -- line throws exception on map value retrieval :)
- $object/@name/string() || $pos, (: assign map key :)
- util:node-id($object) (: store the node-id as map value :)
- )
- )
- )
- )
- };
- declare function local:object.get($object, $property-name)
- {
- if(map:contains($object, $property-name))
- then util:node-by-id($object("__object"), $object($property-name))
- else error(QName('http://haptixgames.com/err', 'PropertyNotFound'), $property-name || ' not found.')
- };
- declare function local:object.call($object, $method-name)
- {
- local:object.call($object, $method-name, $object)
- };
- declare function local:object.call($object, $method-name, $arguments)
- {
- if(map:contains($object, $method-name))
- then $object($method-name)[3]($arguments)
- else error(QName('http://haptixgames.com/err', 'MethodNotFound'), $method-name || ' not found.')
- };
- declare function local:object.inspect($object)
- {
- element object.inspect
- {
- element internals
- {
- for $key in map:keys($object)
- where starts-with($key, "__") and $key ne '__object'
- order by $key ascending
- return element { replace($key, "__", "") } { $object($key) }
- },
- element methods
- {
- for $key in map:keys($object)
- where ends-with($key, "()")
- order by $key ascending
- return
- (
- element method
- {
- element name { $key },
- element description { $object($key)[1]/text() },
- element signature
- {
- let $method := $object($key)
- return $method[2]($method[3])/*
- }
- }
- )
- },
- element properties
- {
- for $key in map:keys($object)
- where not(starts-with($key, "__")) and not(ends-with($key, "()"))
- order by $key ascending
- return
- (
- element property
- {
- element name { $key }, (: show the key :)
- element node-id { $object($key) }, (: show the value which is the node-id :)
- element node { util:node-by-id($object("__object"), $object($key)) } (: turn the node-id into a node using the stored fragment :)
- }
- )
- }
- }
- };
- let $xml :=
- <json type="object">
- <pair name="firstName" type="string">John</pair>
- <pair name="lastName" type="string">Smith</pair>
- <pair name="age" type="number">25</pair>
- <pair name="address" type="object">
- <pair name="streetAddress" type="string">21 2nd Street</pair>
- <pair name="city" type="string">New York</pair>
- <pair name="state" type="string">NY</pair>
- <pair name="postalCode" type="number">10021</pair>
- <pair name="alive" type="boolean">true</pair>
- </pair>
- <pair name="phoneNumbers" type="array">
- <item type="object">
- <pair name="type" type="string">home</pair>
- <pair name="number" type="string">212 555-1234</pair>
- </item>
- <item type="object">
- <pair name="type" type="string">fax</pair>
- <pair name="number" type="string">646 555-4567</pair>
- </item>
- </pair>
- </json>
- let $object := local:object.new($xml)
- let $xml := <nothing><more/><to/><see/><here/></nothing>
- return
- (
- local:object.inspect($object),
- local:object.get($object, "address1"),
- local:object.call($object, "valid()"),
- local:object.call($object, "id()")
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement