PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wheels/vendor/toXml/toXML.cfc

http://cfwheels.googlecode.com/
ColdFusion CFScript | 168 lines | 148 code | 20 blank | 0 comment | 1 complexity | 819be58e59bc71df95da994b0d39290e MD5 | raw file
Possible License(s): Apache-2.0, CPL-1.0
  1. <cfcomponent displayname="toXML" hint="Set of utility functions to generate XML" output="false">
  2. <!---
  3. Based on the toXML component by Raymond Camden: http://www.coldfusionjedi.com/index.cfm/2006/7/2/ToXML-CFC--Converting-data-types-to-XML
  4. toXML function made by Paul Klinkenberg, 25-feb-2009
  5. http://www.coldfusiondeveloper.nl/post.cfm/toxml-function-for-coldfusion
  6. Version 1.1, March 8, 2010
  7. Now using <cfsavecontent> while generating the xml output in the functions, since it increases process speed
  8. Thanks to Brian Meloche (http://www.brianmeloche.com/blog/) for pointing it out
  9. Version 1.2, September 1, 2010
  10. - Cleaned up variables to reference the arguments scope so CF doesn't have to search the different scopes
  11. - Cleaned up methods to only use var once per method call
  12. - Created a new method $simpleValueToXml()
  13. - Add tests outside of component
  14. --->
  15. <cffunction name="init" returntype="any" access="public" output="false" hint="I return the toXml Object">
  16. <cfreturn this />
  17. </cffunction>
  18. <cffunction name="toXML" returntype="string" access="public" output="no" hint="Recursively converts any kind of data to xml">
  19. <cfargument name="data" type="any" required="yes" />
  20. <cfargument name="rootelement" type="string" required="false" default="data" />
  21. <cfargument name="elementattributes" type="string" required="false" default="" hint="Optional string like 'order=2', which will be added into the starting rootElement tag." />
  22. <cfargument name="addXMLHeader" type="boolean" required="no" default="true" hint="Whether or not to add the &lt;?xml?&gt; tag" />
  23. <cfset var returnValue = "" />
  24. <cfif Len(arguments.elementattributes)>
  25. <cfset arguments.elementattributes = " " & Trim(arguments.elementattributes) />
  26. </cfif>
  27. <cfsavecontent variable="returnValue"><!---
  28. ---><cfoutput><!---
  29. ---><cfif arguments.addXMLHeader><!---
  30. ---><?xml version="1.0" encoding="UTF-8"?><!---
  31. ---></cfif><!---
  32. ---><cfif IsSimpleValue(arguments.data)><!---
  33. --->#$simpleValueToXml(argumentCollection=arguments)#<!---
  34. ---><cfelseif IsQuery(arguments.data)><!---
  35. --->#$queryToXML(argumentCollection=arguments)#<!---
  36. ---><cfelseif IsArray(arguments.data)><!---
  37. --->#$arrayToXML(argumentCollection=arguments)#<!---
  38. ---><cfelseif IsObject(arguments.data)><!---
  39. --->#$objectToXML(argumentCollection=arguments)#<!---
  40. ---><cfelseif IsStruct(arguments.data)><!---
  41. --->#$structToXML(argumentCollection=arguments)#<!---
  42. ---><cfelseif REFindNoCase("^coldfusion\..*Exception$", arguments.data.getClass().getName())><!---
  43. --->#$structToXML(argumentCollection=arguments)#<!---
  44. ---><cfelse><!---
  45. --->#$simpleValueToXml(data="Unknown object of type #arguments.data.getClass().getName()#", rootelement=arguments.rootelement, elementattributes=arguments.elementattributes)#<!---
  46. ---></cfif><!---
  47. ---></cfoutput><!---
  48. ---></cfsavecontent>
  49. <cfreturn returnValue />
  50. </cffunction>
  51. <cffunction name="$simpleValueToXml" access="public" output="false" returntype="string">
  52. <cfargument name="data" type="string" required="true" />
  53. <cfargument name="rootelement" type="string" required="false" default="data" />
  54. <cfargument name="elementattributes" type="string" required="false" default="" />
  55. <cfset var returnValue = "" />
  56. <cfset arguments.data = XmlFormat(arguments.data) />
  57. <cfsavecontent variable="returnValue"><!---
  58. ---><cfoutput><!---
  59. ---><cfif IsNumeric(arguments.data)><!---
  60. ---><#arguments.rootelement# type="numeric"#arguments.elementattributes#>#arguments.data#</#arguments.rootelement#><!---
  61. ---><cfelseif IsBoolean(arguments.data)><!---
  62. ---><#arguments.rootelement# type="boolean"#arguments.elementattributes#><cfif arguments.data>1<cfelse>0</cfif></#arguments.rootelement#><!---
  63. ---><cfelseif not Len(arguments.data)><!---
  64. ---><#arguments.rootelement# type="string"#arguments.elementattributes#/><!---
  65. ---><cfelse><!---
  66. ---><#arguments.rootelement# type="string"#arguments.elementattributes#>#arguments.data#</#arguments.rootelement#><!---
  67. ---></cfif><!---
  68. ---></cfoutput><!---
  69. ---></cfsavecontent>
  70. <cfreturn returnValue />
  71. </cffunction>
  72. <cffunction name="$arrayToXML" access="public" output="false" returntype="string" hint="Converts an array into XML">
  73. <cfargument name="data" type="array" required="true" />
  74. <cfargument name="rootelement" type="string" required="false" default="data" />
  75. <cfargument name="elementattributes" type="string" required="false" default="" />
  76. <cfargument name="itemelement" type="string" required="false" default="item" />
  77. <cfset var loc = {} />
  78. <cfsavecontent variable="loc.returnValue"><!---
  79. ---><cfoutput><!---
  80. ---><#arguments.rootelement# type="array"#elementattributes#><!---
  81. ---><cfloop from="1" to="#ArrayLen(arguments.data)#" index="loc.x"><!---
  82. --->#toXML(data=arguments.data[loc.x], rootelement=arguments.itemelement, elementattributes="order=""#loc.x#""", addXMLHeader=false)#<!---
  83. ---></cfloop><!---
  84. ---></#arguments.rootelement#><!---
  85. ---></cfoutput><!---
  86. ---></cfsavecontent>
  87. <cfreturn loc.returnValue />
  88. </cffunction>
  89. <cffunction name="$queryToXML" access="public" output="false" returntype="string" hint="Converts a query to XML">
  90. <cfargument name="data" type="query" required="true" />
  91. <cfargument name="rootelement" type="string" required="false" default="data" />
  92. <cfargument name="elementattributes" type="string" required="false" default="" />
  93. <cfargument name="itemelement" type="string" required="false" default="row" />
  94. <cfset var loc = {} />
  95. <cfset loc.columns = arguments.data.columnList />
  96. <cfsavecontent variable="loc.returnValue"><!---
  97. ---><cfoutput><!---
  98. ---><#arguments.rootelement# type="query"#arguments.elementattributes#><!---
  99. ---><cfloop query="arguments.data"><!---
  100. ---><#arguments.itemelement# order="#arguments.data.currentrow#"><!---
  101. ---><cfloop list="#loc.columns#" index="loc.col"><!---
  102. --->#toXML(data=arguments.data[loc.col][arguments.data.currentRow], rootElement=loc.col, addXMLHeader=false)#<!---
  103. ---></cfloop><!---
  104. ---></#arguments.itemelement#><!---
  105. ---></cfloop><!---
  106. ---></#arguments.rootelement#><!---
  107. ---></cfoutput><!---
  108. ---></cfsavecontent>
  109. <cfreturn loc.returnValue />
  110. </cffunction>
  111. <cffunction name="$structToXML" access="public" output="false" returntype="string" hint="Converts a struct into XML.">
  112. <cfargument name="data" type="any" required="true" hint="It should be a struct, but can also be an 'exception' type." />
  113. <cfargument name="rootelement" type="string" required="false" default="data" />
  114. <cfargument name="elementattributes" type="string" required="false" default="" />
  115. <cfset var loc = {} />
  116. <cfset loc.keys = StructKeyList(arguments.data) />
  117. <cfsavecontent variable="loc.returnValue"><!---
  118. ---><cfoutput><!---
  119. ---><#arguments.rootelement# type="struct"#arguments.elementattributes#><!---
  120. ---><cfloop list="#loc.keys#" index="loc.key"><!---
  121. --->#toXML(data=arguments.data[loc.key], rootelement=loc.key, addXMLHeader=false)#<!---
  122. ---></cfloop><!---
  123. ---></#arguments.rootelement#><!---
  124. ---></cfoutput><!---
  125. ---></cfsavecontent>
  126. <cfreturn loc.returnValue />
  127. </cffunction>
  128. <cffunction name="$objectToXML" access="public" output="false" returntype="string" hint="Converts a struct into XML.">
  129. <cfargument name="data" type="component" required="true" hint="It should be a struct, but can also be an 'exception' type." />
  130. <cfargument name="rootelement" type="string" required="false" default="data" />
  131. <cfargument name="elementattributes" type="string" required="false" default="" />
  132. <cfset var loc = {} />
  133. <cfset loc.keys = ListSort(StructKeyList(arguments.data), "textnocase", "asc") />
  134. <cfset loc.name = GetMetaData(arguments.data).name/>
  135. <cfsavecontent variable="loc.returnValue"><!---
  136. ---><cfoutput><!---
  137. ---><#arguments.rootelement# type="component" name="#loc.name#"#arguments.elementattributes#><!---
  138. ---><cfloop list="#loc.keys#" index="loc.key"><!---
  139. ---><cfif !IsCustomFunction(arguments.data[loc.key])><!---
  140. --->#toXML(data=arguments.data[loc.key], rootelement=loc.key, addXMLHeader=false)#<!---
  141. ---></cfif><!---
  142. ---></cfloop><!---
  143. ---></#arguments.rootelement#><!---
  144. ---></cfoutput><!---
  145. ---></cfsavecontent>
  146. <cfreturn loc.returnValue />
  147. </cffunction>
  148. </cfcomponent>