Nov 04

Make your XML look pretty

Posted by James Netherton | Tuesday 04 November 2008 4:52 PM | In ColdFusion,Java

Credit goes to Joe Rinehart for most of this from this post.

ColdFusion ships with JDom which gives you full access to the JDom API. Many of the applications I work with have some kind of XML component to them and sometimes the XML is difficult to read if it's not formatted nicely.

I've used Joe's code example to create a simple interface on top of it. It also works with valid XHTML.

<cfparam name="form.submit" default=""/>
<cfparam name="form.xml" default=""/>
<cfscript>
function prettyXML(xmlString){

   var fileObj = "";
   var builder = "";
   var format = "";
   var out = "";
   var document = "";
   var fileInStream = "";
   var fileOutStream = "";
   var filePath = expandPath("./prettyXML.xml");

   try{
      fileWrite(filePath, arguments.xmlString);

      fileObj = createObject("java", "java.io.File").init(filePath) ;
      builder = createObject("java", "org.jdom.input.SAXBuilder").init() ;
      format = createObject("java", "org.jdom.output.Format").getPrettyFormat() ;

      format.setIndent("   ");
      out = createObject("java", "org.jdom.output.XMLOutputter").init(format);
      fileInStream = createObject("java", "java.io.FileInputStream").init(fileObj );

      document = builder.build(fileInStream);

      fileInStream.close();

      fileDelete(filePath);

      return out.outputString(document);
   }
   catch(Any excpt){
      if(isDefined("fileInStream") and fileInStream neq ""){
         fileInStream.close();
      }

      writeOutput("<p>#excpt.message#</p>");
      writeOutput("<p>#excpt.stacktrace#</p>");
   }
}
</cfscript>
<html>
<head>
<title>Paste your ugly XML</title>
<style type="text/css">
   body, input{
      font-family:Trebuchet MS;
   }
</style>
</head>
<body>
<h3>Input your XML:</h3>
<cfoutput>
<form action="#cgi.SCRIPT_NAME#" method="post">
<p><textarea name="xml" rows="20" cols="100">#form.xml#</textarea></p>
<input name="submit" type="submit" value="Go"/>
</form>
<h3>Pretty XML</h3>
<cfif len(form.submit) and len(form.xml)>#htmlCodeFormat(prettyXML(form.xml))#</cfif>
</cfoutput>
</body>
</html>

 

Oct 16

London fashion - Reading whilst walking along

Posted by James Netherton | Thursday 16 October 2008 10:13 PM | In Blogging

Just one of many observations made on my commute to and from work......

Tis the current fashion in London to read a newspaper or book whilst walking along. While this does allow one to finish a chapter or complete the crossword whilst on the move, it does have the minor drawback of not being able to see where one is going.

This behaviour usually exhibits itself on crowded streets, busy tube platforms and station concourses. The effect of this is demonstrated thus.....

The unfashionable non reader walks by using the standard method of looking where he is going and thus avoids others:

Standard Walking

The alternate method of reading whilst walking along produces the following effect:

Reading While Walking

So absorbing is the book that the reader weaves from side to side while others take avoiding action until the inevitable collision occurs.

 

Oct 10

MySQL - Can't connect to MySQL server on 'server'

Posted by James Netherton | Friday 10 October 2008 10:28 AM | In MySQL

I've just wasted a couple of hours trying to figure out why my mac keeps getting the response "Can't connect to MySQL server on 'server'" when trying to connect to MySQL on my new vmware Ubuntu setup.

The solution is rather simple!

Open up your MySQL configuration file and find the --bind-address directive. It's most likely that it'll be set to 127.0.0.1 which makes MySQL listen for connections only via the loopback IP.

Comment out this line and save the configuration file. Restarting MySQL didn't seem enough to make this work for me so I had to restart the OS.

 

Sep 13

Just married

Posted by James Netherton | Saturday 13 September 2008 4:54 PM | In Blogging

Just married nearly three weeks ago in fact! Hence my lack of blogging or interest in anything computer related over the past month or so, since there has been so much to do and organise.

My wife Sophia and I had a fantastic day. We were married at Romsey Abbey in Hampshire which proved to be a spectacular setting for the marriage service and a great backdrop for our (many) photographs.

The reception was also really enjoyable and my speech seemed to go down well with everyone, which was a relief considering I wrote it on the morning of the wedding!

There were some slight hiccups. Closer inspection of my marriage certificate revealed some embarrassing spelling errors on my father's, Sophia's and my occupation description. Not sure who the illiterate person was who filled out the details but I'm trying to get it changed.

Then there was the debacle over our (rather expensive) wedding cake. The staff at the reception cut portions of the cake so everyone could have a piece. Overnight it transpired that the reception venue staff had dumped the remainder of the cake in the bin, without any consultation with our parents or Sophia and I! Even worse, I recently found out that some of the staff had been helping themselves to bits of the cake while the evening party was going on.

For honeymoon we decided to go to Paphos in Cyprus for two weeks. The first five days were quite relaxing and then Sophia fell ill with a nasty stomach virus. Things got so bad that she ended up in hospital for three days. When she was released there was a strict diet for her to follow, so we were pretty much confined to the hotel for the remainder of the holiday. I didn't expect to be tested on the 'sickness and in health' vow quite so soon!

Problems aside, we still had a great time together and there's still a day or so for us to enjoy together before we head back to work.

 

Aug 05

Call custom tags without cf_ or cfmodule

Posted by James Netherton | Tuesday 05 August 2008 1:59 PM | In ColdFusion

This may be common knowledge but I just stumbled upon it.....

The standard convention for invoking ColdFusion custom tags is to use:

<cf_mytag/>

or

<cfmodule template="mytag.cfm"/>

There's a way in which you are able to dispense with cf_ prefix and the <cfmodule> call. Browse to your ColdFusion WEB-INF\cftags directory. You should see some files relating to ColdFusion tags such as <cfdump> and <cfsavecontent>. You can add your own custom tag to this directory and invoke it from any CF template. For example:

Custom Tag - mytag.cfm

<cfdump var="#attributes#"/>

Calling Template

<cfmytag attribute1="test1" attribute2="test2"/>

Note that the cf_ or <cfmodule> prefix wasn't used. Instead the tag call looks like any normal ColdFusion tag invocation.

I doubt this has any real world usage but I thought it was worth blogging anyway.

 

Jul 08

Reserved ColdFusion function names

Posted by James Netherton | Tuesday 08 July 2008 8:55 PM | In ColdFusion

Most people are aware that you cannot give a name to a ColdFusion function that is equal to the name of one of the ColdFusion "built in" functions.

This started me thinking that as ColdFusion templates and CFC's are compiled down to Java servlets, there must be additional function names that are 'reserved'. Each template and CFC page will naturally extend java.lang.Object and therefore inherit all of Object's public methods.

For example, try the following:

<cffunction name="getClass">
</cffunction>

The result is "The name getClass is the name of a built-in ColdFusion function". You get the same result when declaring a function with the names "wait", "notify" and "equals", all of which are public methods on java.lang.Object.

These are only a few examples, I'm assuming that there must be more reserved function names given the number of classes that ColdFusion template and CFC servlets could potentially extend from.

The getClass method opens up the potential for some fun with ColdFusion:

Get the current template class name

<cfoutput>#getClass().getName()#</cfoutput>

Get the current template class properties

<cfset fields = getClass().getFields()/>

<cfloop array="#fields#" index="field">
   <cfoutput>#field.getName()#<br/></cfoutput>
</cfloop>

Get the current template class methods

<cfset methods = getClass().getDeclaredMethods()/>

<cfloop array="#methods#" index="method">
   <cfoutput>#method.getName()#<br/></cfoutput>
</cfloop>

Get the name of the class that the current template extends from

<cfoutput>#getClass().getSuperclass().getName()#</cfoutput>

 

Jul 07

Flex coding standards and best practices

Posted by James Netherton | Monday 07 July 2008 1:00 PM | In Flex

Adobe have added a list of Flex standards and best practices on their open source wiki.

Apparently it's not 100% complete yet but it's a nice set of guidelines nonetheless.

http://opensource.adobe.com/wiki/display/flexsdk/Coding+Conventions

 

Jul 01

Air IIS log reader

Posted by James Netherton | Tuesday 01 July 2008 7:00 AM | In AIR

After an hour or so hacking, I've put together a very simple Adobe Air application to read IIS log files that are in the W3C extended log file format.

Usage of the application is fairly obvious once it's up and running so I won't go into any detail on how it works. Here are some screen grabs that explain the basics. Click the thumbnails to see the full sized images.

Viewing a log file

Viewing a log file

Show / hide data columns

Show / hide data columns

View data row (double click datagrid row)

View data row

Here is the Zipped Air file and MXML source:

WebLogReader.zip
Air File

WebLogReader-Source.zip
Source Code


 

Jun 28

Disabling the Firefox smart location bar

Posted by James Netherton | Saturday 28 June 2008 11:51 AM | In FireFox

I've finally upgraded to Firefox 3, and after 10 minutes, I've found that I can't live with the "awesome" smart location bar.

Here's how to disable it:

1. Navigate to about:config and continue past the warning message

2. Search for the setting browser.urlbar.matchOnlyTyped and change its value to true

3. Restart Firefox

 

Jun 12

Disabling ColdFusion administrator authentication

Posted by James Netherton | Thursday 12 June 2008 11:51 AM | In ColdFusion

I find having to log into the ColdFusion administrator a pain when I'm working on applications in my development environment. You can disable the authentication process by way of the following:

1. Open up WEB-INF\cfusion\lib\neo-security.xml on multiserver installations or cfinsntallationdir\lib\neo-security.xml on standalone installs

2. Edit the admin.security.enabled property so that it appears as follows:

<var name="admin.security.enabled">
   <boolean value="false"/>
</var>

3. Save the file and restart ColdFusion

Obviously you would not want to be doing this in a production or publicly accessible environment!