CompareHTMLComponent : JAR to compare two HTML fragments

After my initial post yesterday CFX_CompareHTML ColdFusion HTML diff custom tag I got quite a bit of feedback and one of the items raised was that some people dont like CFX tags and I should use JavaLoader to use the JAR file from within ColdFusion. (Thanks Dan Vega)

2009-01-15 19:57:00.0

CompareHTMLComponent : JAR to compare two HTML fragments

After my initial post yesterday CFX_CompareHTML ColdFusion HTML diff custom tag I got quite a bit of feedback and one of the items raised was that some people dont like CFX tags and I should use JavaLoader to use the JAR file from within ColdFusion. (Thanks Dan Vega)

I have uploaded a JAR file called CompareHTMLComponent which allows you to do this.

To call the diff component use the following:

<cfset paths = arrayNew(1)>
<cfset paths[1] = expandPath("CompareHTMLComponent.jar")>
<cfset loader = createObject("component", "javaloader.JavaLoader").init(paths)>
<cfset compareComponent = loader.create("CompareHTMLComponent")>
<cfset diff = compareComponent.init()>

<cfoutput>#diff.compare("Some HTML","Some more HTML")#</cfoutput>

As always, feedback is most appreciated so hit me with your thoughts in the comments!

The JAR file and examples can be downloaded from here :

DOWNLOAD PACKAGE

Requires Java 1.5 JRE to be installed or it will throw an exception! ColdFusion 8 & Railo 3 should have this but CFMX7 may not.

Add to your del.icio.us    DIGG This!    Technorati Cosmos Link    Post to Reddit    Add to your Furl    Add to Blinklist
Comments [19] - Leave a comment

Comments

Dan Vega said:
Awesome - This will just make it so much easier to drop in and run with it, thanks!
Hey Dan,

Boy your quick!

If you get CompareHTMLComponent (Unsupported major.minor version 50.0) then the JRE is 1.4 and DaisyDiff requires 1.5. Railo and CF8 should be fine but CFMX7 and 6 may not.

I'll try and get a 1.4 version going...
Robert said:
Hi Dan,

thanks for this great function. Always waited for something like that. I have problems with special characters. My page is utf-8 encoded but everything passed to the function returns unwanted characters. Du you have any ideas for that?
Dan said:
Hi Robert,

If your using cffile to load the contents, specify the charset attribute of cffile.

Let me know how you get on,

Dan.
Robert said:
I have just two variables out of a database and passes them to your function. In source and regular output (browser) everything looks fine. Encoding, processingdirective and so on is set to utf-8. I have no glue what the problem might be.
Robert said:
Hi Dan! Have you found something about the ut-8 problem. This characters causes big problems:

#diff.compare('with utf-8 german special characters are no problem: ? ? ? ? ?','with utf-8 german special characters are no problem: ? ? ? ? ?')#
Harry said:
Dan, the component doesn't work under Linux because of this error:

ExceptionType: org.xml.sax.SAXException
Message: java.io.FileNotFoundException: /tmpb361d82f-7b11-4fdc-b031-4610dc878253.tmp (Permission denied)

Maybe you should check for a ending slash in the temp folder (java.io.tmpdir) in CompareHTMLComponent.java?

The only workaround (whitout source) right now could be to alter the temp path in the java args: $JAVA_HOME/bin/java -Djava.io.tmpdir=/var/tmp/alt/
Hi Harry,

Doh! i'll fix this and post up a new version. I have a newer one available that fixes UTF-8 chars so I'll add in a fix for this.

Dan.
Harry Klein said:
This could could work:

String fileSeparator = System.getProperty("file.separator");

/* Add separator for Linux and Mac:
http://rationalpi.wordpress.com/2007/01/26/javaiotmpdir-inconsitency/ */
if (!outputFile.endsWith(fileSeparator)) outputFile = outputFile fileSeparator;
outputFile = outputFile uid ".tmp";
Dan, this is great. Any chance of releasing the new version to fix the UTF-8 issue?

Great job.
Harry said:
>> Doh! i'll fix this and post up a new version. I have a newer one available that fixes UTF-8 chars so I'll add in a fix for this.

Any chance of releasing the new version? I also have UTF-8 problems.

btw - Is it necessary to write temporary files?
Terry Palmer said:
Thanks for the awesome work Dan. This is a really clean way to display diffs. Very easy to use.
ebella said:
Hi, i need code to comapre two html files visually.after searching lot i came across your site and got ComapreHTmlComponent...but now not getting how to use this..how can i run this?Do i need to run diff.cfm if yes?how to run cfm file
diana said:
how to use this
Drew said:
This is my first time using the JavaLoader. I have CF8. I am getting the error:

javax.xml.transform.TransformerFactoryConfigurationError: Provider org.apache.xalan.processor.TransformerFactoryImpl not found
Simon said:
Bloody brilliant work Dan thanks that has saved hours of work and lots of cash on a commercial option. Cheers
Simon said:
Just hit the same UTF-8 hitch as the others, is there any chance you could put your fix up? thanks again for your great work.
Matt said:
I'm getting a "Bad version number in .class file" error when trying to run it in java 1.5.0_22. Could you please confirm that it works in java 5.

Thanks!
Dave said:
I've been looking everywhere for the new version that supports UTF-8. If it's not posted online, I would be super appreciative if you could e-mail it to me. Great work!