PageRenderTime 325ms CodeModel.GetById 120ms app.highlight 3ms RepoModel.GetById 198ms app.codeStats 1ms

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