CFLib.org – Common Function Library Project

component(path [, type])

Last updated May 13, 2003

Version: 1 | Requires: ColdFusion MX | Library: DataManipulationLib

 
Rated 2 time(s). Average Rating: 4.5

Description:
This UDF will create an instance of a CFC based upon a relative or absolute path or even upon the standard dot notation used in the createObject("component") function. I tried to make the "type" argument somewhat intelligent and it'll probably work correctly 99% of the time for you, but if you want to be absolutely sure you get the expected behavior, specify the type argument for the function.

Return Values:
Returns a CFC.

Example:

view plain print about
// same as createObject("component", 
//          "normal.dot.notation");
component("normal.dot.notation");

// invoke a component from a sub
// directory off the current web
// directory
component("./sub/directory/com.cfc");

// invoke a component on another drive
component("d:\components\com.cfc", "absolute");

Parameters:

Name Description Required
path Path for the component. Yes
type Type of the path. Possible values are "component" (normal dot notation), "relative" and "absolute". Defaults to component. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Creates a CFC instance based upon a relative, absolute or dot notation path.
 * 
 * @param path      Path for the component. (Required)
 * @param type      Type of the path. Possible values are "component" (normal dot notation), "relative" and "absolute". Defaults to component.  (Optional)
 * @return Returns a CFC. 
 * @author Dan G. Switzer, II (dswitzer@pengoworks.com) 
 * @version 1, May 13, 2003 
 */

function component(path){
    var sPath=Arguments.path;var oProxy="";var oFile="";var sType="";
    ifarrayLen(Arguments) gt 1 ) sType = lCase(Arguments[2]);

    // determine a default type    
    iflen(sType) eq 0 ){
        if( (sPath DOES NOT CONTAIN ".") OR ((sPath CONTAINS "."AND (sPath DOES NOT CONTAIN "/"AND (sPath DOES NOT CONTAIN "\")) ) sType = "component";
        else sType = "relative";
    }
    
    // create the component
    switchleft(sType,1) ){
        case "c":
            return createObject("component", sPath);
        break;

        default:
            ifleft(sType, 1) neq "a" ) sPath = expandPath(sPath);
            oProxy = createObject("java""coldfusion.runtime.TemplateProxy");
            oFile = createObject("java""java.io.File");
            oFile.init(sPath);
            return oProxy.resolveFile(getPageContext(), oFile);
        break;
    }
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson