I've Decided To (try to) Learn Java

Head First Java

As the new year approaches, I'm adding "Learn Java" to my personal list of things to do in 2009. I've been wanting to pick up some Java skills for awhile now and never really set it as a priority. Don't worry, I have no plans to abandon ColdFusion. Quite the contrary really.

In my school days, I was required to learn a foreign language. Victims of "location" and "school budgets," each grade school within my childhood hometown of Akron, Ohio offered one foreign language option and mine happened to be Spanish. (Friends at other schools were able to learn French, or even German.) Eventually, in high school, Latin was also offered as a foreign language option. To my excitement, I took both Latin and Spanish. In the end, I truly believe Latin helped me in not only my Spanish, but also my English! (Which some of my readers might take argument with, to be sure.) Anyway, as it turned out, Latin was/is the basis of approximately 60% of the English language. In addition, Spanish, French, and many other languages are also based on Latin in some form or another, so it's no wonder that picking up Latin skills could in turn help one in other languages as well.

As most users of ColdFusion know, Java (or J2EE) is the underlying engine that drives ColdFusion. So in much the same way that I felt Latin helped me in my English and Spanish, I'm hoping Java will also help me in not only my ColdFusion programming, but in other areas as well (object oriented code, etc.).

I understand that learning a language is an ongoing process and never really "ends." Heck, I'm still learning ColdFusion even though I've been using it since CF5. I also understand that I'll probably never really achieve "greatness" in Java considering the fact that I personally know of people that have spent their full four years in college learning this extremely complex language.

After asking and searching around for a resource to help me in my quest for Java knowledge, I've decided on the book "Head First Java, 2nd Edition" by Kathy Sierra and Bert Bates published by O'Reilly. My book arrived just in time for Christmas and after reading the introduction and flipping around, I can say that I'm truly excited to dive right in. I'm just hoping I'll be able to swim the Java waters.

Extending ColdFusion's List Functions: Append or Prepend Individual List Items

Recently, I ran into a situation where I was typing out a bunch of cfinvokeargument tags for a long list of variables and when I began typing essentially the same information over and over again I thought "I could probably do this more efficiently!" So, I threw together a fairly simple user defined function (UDF) that I wrapped up into a ColdFusion component (.cfc).

The theory is fairly simple. Take a list, loop over each list item (or element) and prepend each individual list item with a predefined value.

For example, if a list contains the following values: (ItemName,ItemNumber,Quantity,PaymentDate). You could prepend each list item with "request." thus receiving a new list containing: (request.ItemName,request.ItemNumber,request.Quantity,request.PaymentDate).

This varies from ColdFusion's built in functions ListAppend and ListPrepend in that the built in functions add list items (or elements) to a list, but do not actually modify the list items themselves. Whereas I wanted to modify each individual list item or element, not necessarily add new list items at the beginning or end of the list.

While I was at it, I went ahead and threw in a method to append list items/elements as well. Hope this saves time for someone else too.

Here's the ColdFusion component:


<!---------------------------------------------------------------------------------------------------------------

    Document:        /extensions/components/listFunctionExtensions.cfc
    Author:            Steve Withington (http://www.stephenwithington.com)
    Creation Date:    12/22/2008
    Copyright:        (c) 2008 Stephen J. Withington, Jr. | stephenwithington.com

    License:        This code may be used freely.
                    You may modify this code as you see fit, however, this header must remain intact.
                    
                    This code is provided as is. I make no warranty or guarantee.
                    Use of this code is at your own risk.
    
    Purpose:        Prepend (or append) a value to the beginning (or end) of each item in a list.
    
                    For example, if a list contains the following values:
                    (ItemName,ItemNumber,Quantity,PaymentDate)
                    you could prepend each list item with "request." thus receiving a new list containing
                    (request.ItemName,request.ItemNumber,request.Quantity,request.PaymentDate).
                    This could easily be modified to allow for custom delimiters, etc. but
                    I wanted to keep this simple.

    Example Usage:
    http://www.stephenwithington.com/blog/index.cfm/2008/12/22/Extending-ColdFusions-List-Functions-Append-or-Prepend-Individual-List-Items
    
    Revision Log:    
    MM/DD/2008 - sjw - notes.

---------------------------------------------------------------------------------------------------------------->

<cfcomponent     displayname="List Function Extensions"
                output="false"
                hint="PrependListItems() and AppendListItems() methods.">


    <!--- prependListItems(currentList, prependValue) --->
    <cffunction name="prependListItems"
                access="public"
                returntype="string"
                output="false">


        <cfargument name="currentList"
                    type="string"
                    required="yes" />


        <cfargument name="prependValue"
                    type="string"
                    required="yes" />


        <cfset var listResult = "" />
        <cfset var listToPrepend = arguments.currentList />
        <cfset var theValue = arguments.prependValue />
        <cfset var tempArray = arrayNew(1) />

        <cfloop list="#listToPrepend#" index="i">
            <cfset temp = arrayAppend(tempArray, theValue & i) />
        </cfloop>

        <cfset listResult = arrayToList(tempArray, ",") />

        <cfreturn listResult />

    </cffunction>


    <!--- appendListItems(currentList, appendValue) --->
    <cffunction name="appendListItems"
                access="public"
                returntype="string"
                output="false">


        <cfargument name="currentList"
                    type="string"
                    required="yes" />


        <cfargument name="appendValue"
                    type="string"
                    required="yes" />


        <cfset var listResult = "" />
        <cfset var listToAppend = arguments.currentList />
        <cfset var theValue = arguments.appendValue />
        <cfset var tempArray = arrayNew(1)>

        <cfloop list="#listToAppend#" index="i">
            <cfset temp = arrayAppend(tempArray, i & theValue) />
        </cfloop>

        <cfset listResult = arrayToList(tempArray, ",") />

        <cfreturn listResult />

    </cffunction>

</cfcomponent>

Here's an example of both the PrependListItems method and AppendListItems method:


<cfset tempList = "ItemName,ItemNumber,Quantity,PaymentDate" />
                    
<cfscript>
    args = structNew();
    args.currentList = tempList;
    args.prependValue = "request.";
</cfscript>

<!--- you may need to modify this to your placement of listFunctionExtensions.cfc --->
<cfinvoke     component="extensions.components.listFunctionExtensions"
            method="prependListItems"
            argumentcollection="#args#"
            returnvariable="myNewPrependedList" />


<ul>
    <cfloop list="#myNewPrependedList#" index="i">
        <cfoutput><li>#i# – #listlast(i, '.')#</li></cfoutput>
    </cfloop>
</ul>

<hr size="1" />

<cfscript>
    args = structNew();
    args.currentList = tempList;
    args.appendValue = "1";
</cfscript>

<cfinvoke     component="extensions.components.listFunctionExtensions"
            method="appendListItems"
            argumentcollection="#args#"
            returnvariable="myNewAppendedList" />


<ul>
    <cfloop list="#myNewAppendedList#" index="i">
        <cfoutput><li>#i#</li></cfoutput>
    </cfloop>
</ul>

Experiencing Problem With ColdFusion's ExpandPath('/') Function

For some strange reason, I've been experiencing a (very weird) problem with the ColdFusion function ExpandPath('/'). I have several web sites on various ColdFusion servers in a shared-host environment. Under normal circumstances, ExpandPath('/') would return the base absolute path for the web site.

For example, if you try using ExpandPath('/') in the default ColdFusion installation, it would most likely return "C:\ColdFusion8\wwwroot\"

However if a site is set up in a multi-instance environment (versus the default installation), ExpandPath('/') should return something similar to: "D:\Inetpub\domainname\"

Where I'm confused is why ExpandPath('/') would return "C:\ColdFusion8\wwwroot\" in a shared-host environment. It just doesn't make sense. Stranger still is if I try ExpandPath('/index.cfm') the shared-host server actually returns the correct response of "D:\Inetpub\domainname\index.cfm"

Why do I care? Well, I would be surprised if I'm the only ColdFusion developer who uses ExpandPath('/') in their programming. I usually store this handy little function in a variable and call it out from time to time in various parts of my applications for those times when I need an "absolute, platform-appropriate path." There are many functions that require the absolute path including the drive, directory, etc.

For now, I've come up with a hack to address this but I would prefer to find out what the cause of the problem is.

Option 1, possibly the easiest to implement, but not as dynamic:


<cfset request.RootDirectory = "D:\Inetpub\domainname\" />

Option 2, easier to replicate throughout multiple sites without have to retype the actual path:


<cfscript>
    request.RootDirectory = expandpath("/index.cfm");
    x = listlen(request.RootDirectory, "\");
    request.RootDirectory = listdeleteat(request.RootDirectory, x, "\");
    request.RootDirectory = request.RootDirectory & "\";
</cfscript>

Obviously, none of these are as easy as what I would prefer to do:


<cfset request.RootDirectory = ExpandPath("/") />

More Entries

© 2026, Stephen J. Withington, Jr.  |  Hosted by Hostek.com

Creative Commons License   |   This work is licensed under a Creative Commons Attribution 3.0 Unported License.