2009.12.01

More fun with regular expressions

I ( like many programmers I know) get just a little befuddled when trying to use regular expressions.  Over the years I’ve used numerous online RE parsers to test code.   Recently  I found a small RE parser that I have absolutely love called “The Regex Coach”.

This little gem of a program  is great.  (As a bonus it is  free for both commercial and non-commercial uses.) Unlike other RE testers that I’ve used over the years, this one lets you actually step thru the parsing sequence and also see the execution tree of the RE.  These are very handy.  I find I am able to get to a functional RE statement in about a 1/4 the time as before.   You can get “The Regex Coach” from here.  http://weitz.de/regex-coach/

As a little bonus… here is a little gem of an RE statement (for field validation) for a field that must contain no value or one or more three digit numbers separated by commas.
(^(\d{3})(\,\d{3})*$)|(^$)

so in JavaScript the RE literal would be
/(^(\d{3})(\,\d{3})*$)|(^$)/

2009.11.27

Ext.iterate Array vs. Object Gotcha!

When calling Ext.iterate with an object the parameters passed back to the iterated function are:

(String key, Object value)

However when calling Ext.iterate with an array the parameters are:

(Object item, Number index, Array allItems)

The gotcha here is that in the Ext 3.0 docs only the object call back is shown.  Because of this you might think that the array call returns the index then the actual item bu as you can see that is not the case.

There is a thread on the Ext forum here about maybe making this consistent in the next major release.

2009.11.18

Beginings with Ext 3.0 JS library (combobox)

I started working on a project a few months ago that leveraged the Ext JavaScript  library for just a handful of features.  After some requests for some improvements in one section I decided to try to leverage a little more of the Ext library to improve it.  I decided to start just by redoing a form to use remoting.   Now I’ve done this kind of thing before  in all of the  the Adobe Action-script platforms ( Flex, Flash and Air)  so I am rather comfortable with the basic concepts.    I am also very comfortable with JavaScript having worked with it for over 10 years and using DHTML and other tricks (img src updates, etc.)  to do Ajax type magic before Ajax was even in the lexicon. I will say that some of the conventions used by JS developers drive me nuts… but that’s all formatting.   One really completely new area for me was JSON.  I’ve always wanted to try it so I figured this was the perfect opportunity.  So I started with a simple task on this one form.   Populate a single Combo Box remotely from a JSON datasource.  Now when I learn to program a new language (or library in this case) I need two things.   The docs and working examples.    On the documentation side of things Ext has a very good API documentation at http://www.extjs.com/deploy/dev/docs/ The one area I found lacking was in the way of simple examples.    One thing I always liked from Adobe documentation was the examples that were included the docs.   Always very simple and always just focused around the one object you were looking at.    Ext has provided some really nice sample applications at http://www.extjs.com/deploy/dev/examples/samples.html however it can be a bit hit and miss to locate the combination of examples that have the features you are looking for.   In particular I could not locate a single example that was just as simple as …. here is how you load JSON data into a combo-box.   I finally found a good one at  http://technopaper.blogspot.com/2009/11/dynamic-loading-of-combobox-using-extjs.html#comments I hope to leave some additional posts as I find bits and pieces here and there.

2009.10.13

Cast Number Conversion Exception

All strings being equal…:) this was one strange problem to figure out.

I was using the CF debugger in eclipse yesterday and my ColdFusion (CF8) code kept throwing the same debugger error on simple string equality statements such as : if( stringVar1 EQ stringVar2)

Here is the debugger error:

coldfusion.runtime.Cast$NumberConversionException
The value XX cannot be converted to a number.

castnumberconversionexception

It took a little digging to figure out what the problem was but I think I finally got it Keep reading →

2009.01.07

Regular Expression String to replace “&=”

So I have an issue where I need to move some CF8 code to a CF7 server. Unfortunately my code is littered with one of my favorite operators “&=” for string concatenation and this construct is not valid in CF7. In order to make my code work I need to convert all of my “&=” operators into var = var & string type statements.

Since I use FlexBuilder/Eclipse for my development I am able to take advantage of the advanced Find/Replace feature that uses regular expressions.

Here is the regular expression that I came up with to do my replacements.

Find: (^\s+)(.+)(\x26=)
Replace With: $1$2 = $2 \&

Here is a breakdown of what each part of each of these does

The Find string:

( starts capture group #1
^ matches the start of a line
\s matches white space
+ says to match ‘many’ of the previous char (white space)
) ends capture group #1

( starts capture group #2
. matches any character
+ says to match ‘many’ of the previous char (any character)
) ends capture group #2

( starts capture group #3
\x26 matches a char with hex code of 26 (ampersand)
= matches an equal sign
) ends capture group #3

The Replace string:

$1 Inserts capture group 1
$2 Inserts capture group 2
= Inserts and equal sign
$2 Inserts capture group 2
\& Inserts an ampersand (the \ is for escaping)

So the way this works is

Given the following line of code
stringVar &= ’some more text’;

  • Capture group 1 (^\s+) matches the start of the new line and the indentation.
  • Capture group 2 (.+) then matches to the var name “stringVar”
  • Capture group 3 (\x26=) then matches to the operator “&=”
  • Replace then inserts capture group 1 (the new line and indentation)
  • then it inserts capture group 2 (the var name “stringVar”)
  • then it inserts and equal sign “=”
  • then it inserts capture group 2 again (the var name “stringVar”)
  • finally it inserts an ampersand “&”

The final string becomes
stringVar = stringVar & ’some more text’;

Note that the expression only actually operates on the part of the line in blue and does not alter the rest of the line of code.

2008.11.10

Alternating between CF7 and CF8 running under IIS

Not sure if anyone else has a need for this information but I figured I would go ahead an post it just in case anyone might find it useful.

I know everyone knows that you can run both ColdFusion 7 and ColdFusion 8 on the same machine at the same time.   The common way to do this is to run one or both using the built in web server.  Now while this works I found that it is quite easy to switch IIS integration between ColdFusion 7 and 8 allowing both to run this way when needed.

I upgraded my local development environment to ColdFusion 8 about a year ago however my corporate development environment is still at ColdFusion 7.  90% of the time this is not a problem just as long as I am aware of what is and is not available.  (Mainly I have to make sure that I still use EQ, NEQ, GT etc…)    The one area that I found it necessary to have ColdFusion 7 for is for configuring the Flex / AMF gateway  (services-config.xml).

I do not have direct access to these configuration files on my corporate dev server (or ability to restart the server)  This is not usually a problem but can make for extra steps in this case.   In order for me to request a new channel or destination to be added to the config file I need to write it up and send it to the system administrator and have him add it and then restart the server.  Because to of the extra steps involved I have to make sure that it is configured exactly correctly.

When I was simply using CF7 on my local box I would just get a copy of the service-config.xml file from the dev server and then copy and paste the channel and destination sections from my local service-config.xml file and then send it back to the system administrator.  However now with CF8 my settings may not directly match.  First there is the issue that by default ColdFusion 8 splits the channel definitions and destination definitions into two separate files services-config.xml and remoting-config.xml.   OK not too big a deal however now I have to make sure to consolidate the settings into the one services-config.xml file for movement to the server.

OK Well to get back to my point.  I needed to be able to run both CF8 and CF7 as if they were the only version installed and running through IIS on my local box so that I could effectivly test these configuration files before sending them to our system administrator.    In the end my solution was to install both and switch between them.

The set up is not that difficult.  I first installed ColdFusion 8 using the default install path of C:\ColdFusion8\.  I then stopped the services and renamed the CFIDE directory in the Inetpub\wwwroot directory to CFIDE8.  Then I installed ColdFusion 7 to the default install path of C:\CFusionMX7\.   Then renamed the CFIDE directory it created in the the Inetpub\wwwroot directory to CFIDE7.   Then I pull the shortcuts each of the Web Server Configuration tools out of their respective start menu locations and put them on my desktop and renamed them so I would know which version applied to which.

Now to switch between them I just follow a fairly simple process… I stop IIS, and the version of coldfusion that is running.  Then I open up that version’s Web Server Configuration tool and remove IIS from the list. I close that Web Server Configuration tool and open the other version’s Web Server Configuration tool and then Add IIS to it.  Then I go into the Inetpub\wwwroot directory and rename the CFIDE directory that is there back what it should be (If i WAS running CF8 then CFIDE8) and then rename the other CFIDE? to CFIDE (say CFIDE7 to CFIDE)   Now the correct version of CF is connected up to IIS, the correct CFIDE directory is in place and I can just restart IIS and the appropriate ColdFusion server and now I’m running under the other version.

This set up is not as easy as running both versions concurently, however I found that I rarely did that anyway as it ate up system resources.  Once it is set up, this only adds a few steps to switching over from one version to the other and further I am running through IIS for both versions.

2008.09.03

Flash Window opening off screen

Here is a discription of the problem…

You have a laptop or other machine where you sometimes have 2 screens or monitors and at other times you only have one. Sometimes when you open the Flash CS3 IDE (more often when only using one screen but sometimes with both) The IDE or dialog boxes open way off screen. You can’t move it back and no matter what you do it won’t reset and just open on your primary (or only) screen.

The simplest solution I could find was to just delete the settings registry key

run regedit.exe and navigate to the following “folder”

HKEY_CURRENT_USER\Software\Adobe\Flash 9\Settings\

you should just be able to delete the Window value or reset it but this doesn’t always work for some reason. So just delete the whole Settings folder. Don’t worry Flash will rebuild it when you restart it. If this still doesn’t work delete the whole Flash 9 tree … again Flash will rebuild it when you restart it. (as always with regedit you should back up at least this part of the registry before messing with it… Don’t say I didn’t warn you.)

This fixed my problem… YMMV

If this post helps you… please leave a reply Thanks!

2008.06.03

Patent Issued

My first patent was issued today from the US Patent and Trademark office. Patent number 07380665 issued June 3, 2008.

3 on bucket

It might not mean a lot to many people but for as long as I can remember I’ve had ideas and inventions floating around in my head. A couple years ago I decided to start patenting my ideas…

Keep reading →

2008.04.22

Install server or ca certificate in Coldfusion or Java Virtual Machine

The other day I was working on a project in ColdFusion where I wanted to connect to a secure web server using CFHttp and do some screen scrapes. The problem I encountered was that the server was using a self issued certificate that therefore did not have a proper certificate chain. ColdFusion simply returned the following error:

I/O Exception: peer not authenticated
coldfusioncerterror

After some detective work.. found out what the problem was and devised an effective solution which I provide you with now…. You will need a couple of free tools and access to your server. These instructions are for Windows based machines but the concepts and tools should work on Mac or Unix based platforms.

1. Install and Setup Tools
Download and Install the Cert Viewer Plus plug-in for Firefox
https://addons.mozilla.org/en-US/firefox/addon/1964

Download and install Portecle JVM certificate manager
(portecle-1.3.zip) You do not need the source (src) version
https://sourceforge.net/projects/portecle
The easiest way to install Portecle is to unzip the contents of the zip file to a directory such as C:\Program Files\portecle-1.3\ Then find the file “Portecle.jar”, right click it and send it to your desktop as a shortcut. You can then use this shortcut to launch Portecle. (optionally you can also change the name of the shortcut and change the icon to use the portecle.ico file for the icon)

2. Extract the Certificate
If you cannot get the certificate you need directly from the issuer, you can get the certificate for the website you are trying to access from the Firefox certificate store using the Cert Viewer Plus plug-in for Firefox.

Under the [Tools] menu in Firefox select [Certificate Manager].
In the Certificate Manager screen, select the certificate desired on either the [Web Sites] or [Authorities] tab. Select the [View] button.
On the Certificate Viewer screen select [Export…]
Select a save location and name for the certificate, save as an X.509 Certificate
Remember the name and location of this certificate.

3. Install Certificate to Java Virtual Machine and/or ColdFusion server
Start Portecele.
On the [File] menu, select [Open Keystore File] navigate to and locate the keystore you are interested in.
For many java installations this will be located in your “jre\lib\security” directory and might be named “cacerts”
For the ColdFusion 8 default developer install the path will be “c:\coldfusion8\runtime\jre\lib\security\cacerts”
You will have to enter the password for the keystore. The default password is “changeit”

Now you are ready to import the key.
From the [Tools] menu select [Import Trusted Certificate]
Navigate to and locate the certificate you saved from Firefox and click [Import]
You will need to reenter the keystore password.
You can change the alias if you desire but I would recommend leaving it as the default
Note: You may also be prompted to “trust” the certificate.

4. Restart you Java Virtual Machine.
For ColdFusion not installed on top of JRun you just need to restart the ColdFusion service. If you are running on top of JRun you also need to restart JRun.

Let me know if this works for you or if you have any further suggestions.

2008.04.21

Can your code be too structured?

I recently had the opportunity to work with some code developed by [NAME WITHHELD TO PROTECT THE INNOCENT] The code is absolutely great code, does some interesting things however…. it was just TOO well organized and way to verbose. Now normally if I’m going to criticize someone’s coding style, (which I generally do not do) it would be for the exact opposite reason as this. However this code was so well structured as to make it difficult to follow, and understand, much less fit on a screen.

Every argument for every CF tag began on it’s own line with every new tag separated by at least one line. Thus causing a single cffunction definition header (without any actual code in) to run to 40+ lines. As here

[VARIABLE AND METHOD NAMES HAVE BEEN MODIFIED IN THE FOLLOWING CODE TO PROTECT THE AUTHORS IDENTITY]

<cffunction
name=”functionName”
access=”public”
returntype=”any”
output=”false”
hint=”provides blah blah blah blah.”>

<!— Define arguments. —>
<cfargument
name=”Name”
type=”string”
required=”true”
hint=”some name.”
/>

<cfargument
name=”Type”
type=”string”
required=”true”
hint=”sometype.”
/>

<cfargument
name=”Location”
type=”string”
required=”false”
default=”here”
hint=”some location”
/>

ETC…..

Additionally inside of a CFswitch statement … every case statement was preceded by the exact same comment and more than sufficiently spaced apart.

<cfswitch expression=”#whatsittype#”>

<cfcase value=”1″>

<!— exact same comment create a type of whatsit—>
<cfset scope.var = {
x = x,
y = y
} />

</cfcase>

<cfcase value=”2″>

<!— exact same comment create a type of whatsit—>
<cfset scope.var = {
x = x,
y = y
z = z
} />

</cfcase>

<cfcase value=”3″>

<!— exact same comment create a type of whatsit—>
<cfset scope.var = {
x = x,
b = b
e = e
z = z
} />

</cfcase>

<cfcase value=”4″>

<!— exact same comment create a type of whatsit—>
<cfset scope.var = {
d = d,
y = y
z = z
} />

</cfcase>

ETC….

Now don’t think that I believe that code needs to be short… I am a big fan of white space where needed… however if I need to scroll the screen to just read the method definition, there is a little too much verbosity in the code.

Coding is very much a matter of personal preference however when publishing code to the public it is generally accepted to follow some general guidelines on form. If you code in this style and share your code with the world… please take the time to condense your code prior to posting.

If you code in this style… why? What are your opinions on this coding style, is it helpful or does it make more work for the next programmer? What kind of style do you program in?