Utils

Package: MachII.util
Utility functions for the framework.

<!--- License: Copyright 2008 GreatBizTools, LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Copyright: GreatBizTools, LLC Author: Peter J. Farrell (peter@mach-ii.com) $Id: Utils.cfc 822 2008-06-16 23:34:16Z peterfarrell $ Created version: 1.5.0 Updated version: 1.5.0 Notes: --->

Method Summary
public Utils init()

Initialization function called by the framework.

public ThreadingAdapter createThreadingAdapter()

Creates a threading adapter if the ColdFusion engine has threading capabilities.

public string expandRelativePath(string baseDirectory, string relativePath)

Expands a relative path to an absolute path relative from a base (starting) directory.

public any recurseComplexValues(any node)

Recurses through complex values by type.

Method Detail
createThreadingAdapter

public ThreadingAdapter createThreadingAdapter( )

Creates a threading adapter if the ColdFusion engine has threading capabilities.

Parameters:

Code:

	<cffunction name="createThreadingAdapter" access="public" returntype="MachII.util.threading.ThreadingAdapter" output="false"
		hint="Creates a threading adapter if the ColdFusion engine has threading capabilities.">
		
		<cfset var threadingAdapter = "" />
		<cfset var serverName = server.coldfusion.productname />
		<cfset var serverMajorVersion = ListFirst(server.coldfusion.productversion, ",") />
		<cfset var serverMinorVersion = 0 />
		
		
		<cfif ListLen(server.coldfusion.productversion, ",") gt 1>
			<cfset serverMinorVersion = ListGetAt(server.coldfusion.productversion, 2, ",") />
		</cfif>
		
		
		<cfif FindNoCase("ColdFusion", serverName) AND serverMajorVersion GTE 8>
			<cfset threadingAdapter = CreateObject("component", "MachII.util.threading.ThreadingAdapterCF").init() />
		
		
		
		<cfelse>
			<cfset threadingAdapter = CreateObject("component", "MachII.util.threading.ThreadingAdapter").init() />
		</cfif>
		
		<cfreturn threadingAdapter />
	</cffunction> 

expandRelativePath

public string expandRelativePath( string baseDirectory, string relativePath )

Expands a relative path to an absolute path relative from a base (starting) directory.

Parameters:
string baseDirectory
string relativePath

Code:

	<cffunction name="expandRelativePath" access="public" returntype="string" output="false"
		hint="Expands a relative path to an absolute path relative from a base (starting) directory.">
		<cfargument name="baseDirectory" type="string" required="true"
			hint="The starting directory from which relative path is relative." />
		<cfargument name="relativePath" type="string" required="true"
			hint="The relative path to use." />
		
		<cfset var combinedWorkingPath = arguments.baseDirectory & arguments.relativePath />
		<cfset var pathCollection = 0 />
		<cfset var resolvedPath = "" />
		<cfset var hits = ArrayNew(1) />
		<cfset var offset = 0 />
		<cfset var i = 0 />
		
		
		<cfset combinedWorkingPath = Replace(combinedWorkingPath, "\", "/", "all") />
		<cfset combinedWorkingPath = Replace(combinedWorkingPath, "/./", "/", "all") />
		<cfset pathCollection = ListToArray(combinedWorkingPath, "/") />
		
		
		<cfloop from="1" to="#ArrayLen(pathCollection)#" index="i">
			<cfif pathCollection[i] IS "..">
				<cfset ArrayAppend(hits, i) />
			</cfif>
		</cfloop>
		<cfloop from="1" to="#ArrayLen(hits)#" index="i">
			<cfset ArrayDeleteAt(pathCollection, hits[i] - offset) />
			<cfset ArrayDeleteAt(pathCollection, hits[i] - (offset + 1)) />
			<cfset offset = offset + 2 />
		</cfloop>
		
		
		<cfset resolvedPath = ArrayToList(pathCollection, "/") />
		
		
		<cfif Left(arguments.baseDirectory, 1) IS "/">
			<cfset resolvedPath = "/" & resolvedPath />
		</cfif>
		
		
		<cfif Right(arguments.relativePath, 1) IS "/">
			<cfset resolvedPath = resolvedPath & "/" />
		</cfif>
		 
		<cfreturn resolvedPath />
	</cffunction> 

init

public Utils init( )

Initialization function called by the framework.

Parameters:

Code:

	<cffunction name="init" access="public" returntype="Utils" output="false"
		hint="Initialization function called by the framework.">
		<cfreturn this />
	</cffunction> 

recurseComplexValues

public any recurseComplexValues( any node )

Recurses through complex values by type.

Parameters:
any node

Code:

	<cffunction name="recurseComplexValues" access="public" returntype="any" output="false"
		hint="Recurses through complex values by type.">
		<cfargument name="node" type="any" required="true" />
		
		<cfset var value = "" />
		<cfset var child = "" />
		<cfset var i = "" />
		
		<cfif StructKeyExists(arguments.node.xmlAttributes, "value")>
			<cfset value = arguments.node.xmlAttributes["value"] />
		<cfelseif ArrayLen(arguments.node.xmlChildren)>
			<cfset child = arguments.node.xmlChildren[1] />
			<cfif child.xmlName EQ "value">
				<cfset value = child.xmlText />
			<cfelseif child.xmlName EQ "struct">
				<cfset value = StructNew() />
				<cfloop from="1" to="#ArrayLen(child.xmlChildren)#" index="i">
					<cfset value[child.xmlChildren[i].xmlAttributes["name"]] = recurseComplexValues(child.xmlChildren[i]) />
				</cfloop>
			<cfelseif child.xmlName EQ "array">
				<cfset value = ArrayNew(1) />
				<cfloop from="1" to="#ArrayLen(child.xmlChildren)#" index="i">			
					<cfset ArrayAppend(value, recurseComplexValues(child.xmlChildren[i])) />
				</cfloop>
			</cfif>
		</cfif>
		
		<cfreturn value />
	</cffunction>