Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. <#-- dump.ftl
  2. --
  3. -- Generates tree representations of data model items.
  4. --
  5. -- Usage:
  6. -- <#import "dump.ftl" as dumper>
  7. --
  8. -- <#assign foo = something.in["your"].data[0].model />
  9. --
  10. -- <@dumper.dump foo />
  11. --
  12. -- When used within html pages you've to use <pre>-tags to get the wanted
  13. -- result:
  14. -- <pre>
  15. -- <@dumper.dump foo />
  16. -- <pre>
  17. -->
  18.  
  19. <#-- @ftlvariable name=".data_model" type="java.util.Map" -->
  20. <#-- @ftlvariable name="request" type="java.util.Map" -->
  21.  
  22. <#-- The black_list contains bad hash keys. Any hash key which matches a
  23. -- black_list entry is prevented from being displayed.
  24. -->
  25. <#assign black_list = ["class", "request", "downloadURL", "getDownloadURL", "getreader", "getinputstream", "writer"] />
  26.  
  27. <#--The max depth to recurse when expanding the data model and request vars.-->
  28. <#--If you set this to more than about 5, your machine might melt. Give it a go!-->
  29. <#assign maxdepth = 3>
  30.  
  31.  
  32. <#--
  33. -- The main macro.
  34. -->
  35.  
  36. <#macro dump key data>
  37. <#if data?is_enumerable>
  38. <p><b>${key}</b>
  39. <@printList data,[], 0 />
  40. <#elseif data?is_hash_ex>
  41. <p><b>${key}</b>
  42. <@printHashEx data,[] />
  43. <#else>
  44. <p><@printItem data!,[], key, false, 0 /></#if>
  45. </#macro>
  46.  
  47. <#-- private helper macros. it's not recommended to use these macros from
  48. -- outside the macro library.
  49. -->
  50.  
  51. <#macro printList list has_next_array depth>
  52. <#local counter=0 />
  53. <#list list as item>
  54. <#list has_next_array+[true] as has_next><#if !has_next> <#else> | </#if></#list>
  55. <#list has_next_array as has_next><#if !has_next> <#else> | </#if></#list><#t>
  56. <#t><@printItem item!"unk",has_next_array+[item_has_next], counter, false, depth />
  57. <#local counter = counter + 1/>
  58. </#list>
  59. </#macro>
  60.  
  61. <#macro printHashEx hash has_next_array>
  62. = <code>${(hash.data!(hash.toString()))!"No String Representation"}</code>
  63. <ul>Callable methods:
  64. <ul>
  65. <#list hash?keys as key>
  66. <#list has_next_array+[true] as has_next><#if !has_next> <#else> | </#if></#list>
  67. <#list has_next_array as has_next><#if !has_next> <#else> | </#if></#list><#t>
  68. ${key}
  69. </#list>
  70. </ul>
  71. </ul>
  72. </#macro>
  73.  
  74. <#macro printHashExFull hash has_next_array depth>
  75. <#list hash?keys as key>
  76. <#--<#list has_next_array+[true] as has_next><#if !has_next> <#else> | </#if></#list>-->
  77. <#--<#list has_next_array as has_next><#if !has_next> <#else> | </#if></#list><#t>-->
  78. <#t><@printItem hash[key]!,has_next_array+[key_has_next], key, true, depth />
  79. </#list>
  80. </#macro>
  81.  
  82.  
  83. <#macro printItem item has_next_array key full depth>
  84. <#if (depth > maxdepth) >
  85. <#return>
  86. </#if>
  87. <br>
  88. <#assign tabs = "">
  89. <#list 0..depth as dt>
  90. <#assign tabs = tabs + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;">
  91. </#list>
  92. ${tabs}|<br>
  93. <#attempt>
  94. <#if item?is_method>
  95. ${tabs}+- <b>${key}</b> = ?? (method)
  96. <#elseif item?is_enumerable>
  97. ${tabs}+- <b>${key} (list)</b>
  98. <@printList item, has_next_array, depth+1 /><#t>
  99. <#elseif item?is_hash_ex && omit(key?string)><#-- omit bean-wrapped java.lang.Class objects -->
  100. ${tabs}+- <b>${key} (map)</b> (omitted)
  101. <#elseif item?is_hash_ex>
  102. ${tabs}+- <b>${key} (map)</b>
  103. <#if full>
  104. <@printHashExFull item, has_next_array, depth+1 /><#t>
  105. <#else>
  106. <@printHashEx item, has_next_array /><#t>
  107. </#if>
  108. <#elseif item?is_number>
  109. ${tabs}+- <b>${key} (number)</b> = <code>${item}</code>
  110. <#elseif item?is_string>
  111. ${tabs}+- <b>${key} (string)</b> = <code>"${item}"</code>
  112. <#elseif item?is_boolean>
  113. ${tabs}+- <b>${key} (boolean)</b> = <code>${item?string}</code>
  114. <#elseif item?is_date>
  115. ${tabs}+- <b>${key} (date)</b> = <code>${item?string("yyyy-MM-dd HH:mm:ss zzzz")}</code>
  116. <#elseif item?is_transform>
  117. ${tabs}+- <b>${key}</b> = ?? (transform)
  118. <#elseif item?is_macro>
  119. ${tabs}+- <b>${key}</b> = ?? (macro)
  120. <#elseif item?is_hash>
  121. ${tabs}+- <b>${key}</b> = ?? (hash)
  122. <#elseif item?is_node>
  123. ${tabs}+- <b>${key}</b> = ?? (node)
  124. </#if>
  125. <#recover>
  126. ${tabs}+- <b>${key}</b> = ?? (failed to resolve, sorry!)
  127. </#attempt>
  128. </#macro>
  129.  
  130. <#function omit key>
  131. <#local what = key?lower_case>
  132. <#list black_list as item>
  133. <#if what?index_of(item) gte 0>
  134. <#return true>
  135. </#if>
  136. </#list>
  137. <#return false>
  138. </#function>
  139.  
  140. <h1>All Liferay Variables</h1>
  141. <em>See the bottom for the <code>request</code> variable expansion</em>
  142. <hr>
  143. <#list .data_model?keys as var>
  144. <#if var?index_of("writer") lt 0>
  145. <@dump var,.data_model[var] />
  146. </#if>
  147. </#list>
  148. <hr>
  149. <h1>The <code>request</code> object</h1>
  150.  
  151. <@printHashExFull request,[], 0 />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement