|
|
YOUR FEEDBACK
SOA World Conference
Virtualization Conference $200 Savings Expire May 16, 2008... – Register Today! Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS CF & Java
'Jazz' up Your Applications with Open Source Java
Add spell-check functionality
By: Darron J. Schall
Digg This!
By now it should be no surprise to hear ColdFusion and Java mentioned in the same sentence. You've probably seen the examples, read the tutorials, and poured over lines of Java code in hope of enhancing your ColdFusion applications. Either that, or you've filed the information in a "nice to know" vault and continued on with your ordinary development habits. No matter which category you fall into, this article is for you. One of the greatest benefits of ColdFusion and Java integration is being able to rely on Java's immense open source community to enhance your ColdFusion applications. So, chances are, whatever you're looking for has already been built in Java - just waiting to be leveraged in your ColdFusion code. No idea how to integrate the two? Not a problem - this article is here to help. With a little bit of coding, I'll show you how to leverage an open source Java spell-checking engine in your ColdFusion applications. Let's see... Java. Open source. Spell checking. Free. Do I have your attention yet?
Scoping out the Playing Field One search on Google for "open source Java spell check" yielded approximately 65,000 results, with the very first result showing promise. I followed the link and discovered "Jazzy," hosted on SourceForge (sourceforge.net). The first two sentences on the project page caught my attention: "There are currently no Java open source spell checkers. This is a project that seeks to remedy that." Sweet. I snagged the source and documentation and got down to business.
Preparing Your Development Environment
Now that we have everything we need and the development environment is set up, it's time to figure out how Jazzy works so we can integrate it in our ColdFusion code. Fire up the Eclipse IDE, and create a new project by selecting File -> New -> Project. Select "Java" and press the next button. Give the project a name of "CFSpellCheck" and make note of where the project directory is created. On Windows, the default location will be a directory with the same name as the project, off of the "workspace" directory under wherever you chose to unzip Eclipse to. In this example, that directory is "C:eclipseworkspaceCFSpellCheck." Press the finish button to create the project. After pressing finish, you may get prompted by Eclipse to switch to the Java Perspective. If so, click yes. Now that the project is created we can get to work with the Jazzy source code. Extract the jazzy-doc.zip and jazzy-src.zip archives to a temporary directory. Copy everything under the "src" directory (a .java file and the "com" directory) to the project directory that we just created. Don't be surprised to see a ".project" and a ".classpath" file in the project directory - those are created by default when Eclipse creates the project. Next, make a "dict" directory under the project directory, and extract english.0.zip to it. Right-click on the project name in the "Package Explorer" in Eclipse, and select "Refresh" from the menu to update the project. With the source code in place, we can build their examples. Expand "com.swabunga.spell.examples" and double-click the SpellCheckExample.java file. From the menubar, select Run -> Run As -> Java Application. When you run the example, you'll notice that the "Console" panel in Eclipse contains an error that occurred while trying to run the program. The very first line of the error ("java.io.FileNotFoundException: dictphonet.en") indicates that the program is looking for "phonet.en" in the "dict" directory. Doh! Now, before you get mad at me for showing you an example that doesn't work, I did this on purpose. Whenever you're trying something like this, there's no guarantee that it's going to work the first time. I wanted you to experience a problem right away so that when something does go wrong for you, you don't get frustrated. Open source is great, but it does have its pitfalls. If you find yourself running into trouble, ask around on mailing lists, forums, newsgroups, or try search engines. Speaking of search engines... Google to the rescue again! When we search for "phonet.en" only a few links come up, but thankfully they're all associated with Jazzy. Visit http://cvs.sourceforge.net/viewcvs.py/jazzy/jazzy/dict/phonet.en and click on the download link next to the "Revision 1.1" heading. Make a new text document named "phonet.en" in the "dict" subdirectory under your project directory, copy and paste the text from the previous link into that file, then run the example again. With the program now running successfully, we're prompted to enter some text to spell check. I purposely entered text with spelling errors and was pleasantly surprised when the program found a misspelled word and offered the correct spelling as a suggestion. Try it for yourself! Everyone loves the "Hello world" example, so go ahead and spell that wrong to see if the program will correct it for you. Awesome, it works... now, to dig in and figure out how to get this to work with ColdFusion.
Understanding Jazzy's Innards But wait... before we start, what are we even looking for? Because we need to use a Java object in ColdFusion, we're looking for a class with methods like setText, setDictionary, and runSpellCheck or checkSpelling. We'll also need a way to get the spelling error information from Jazzy to ColdFusion. This information would include the mispelled word, the list of suggested spellings, and the string position where the spelling error was detected, so we need a method along the lines of getErrors or getSpellingMistakes. Before going any further, make sure that line numbers are being displayed inside of Eclipse. This can be accomplished by selecting "Window" from the menu bar and clicking on "Preferences." Expand "Java" on the left, then click "Editor." Find the "Show line numbers" check box, then hit "Apply." Click "OK" to close the dialog. Looking at SpellCheckExample.java we don't see any of the methods we're looking for. However, at line 30 we can see the dictionary being created with the dictionary and phonet files. Line 42 is the checkSpelling call that invokes the spell-checking engine with the text to check as a parameter. At line 49 we see a possible bottleneck. Whenever a spelling error occurs, an event is raised and the spellingError method handles it. This is great for allowing the user to fix mistakes as they arise in a Java application, but that level of interactivity won't work in a ColdFusion application since all of the processing completes on the server before the client gets a chance to interact with the application. We'll have to create a workaround for that. With a little insight into how Jazzy works, we can take a look at the documentation provided to see if any other classes have the desired methods. Ideally, we just want to create a Java object and use it without having to do any additional coding. Find the index.html in the temporary directory you extracted the jazzy-doc.zip to, and open it up in a Web browser. What you're looking at is documentation generated automatically by JavaDoc. If you're not sure what JavaDoc is, check it out on Sun's Web site at http://java.sun.com/j2se/javadoc/. The left-hand column is a listing of all of the classes associated with Jazzy. When you click on a class name you'll see a listing of all of the methods available in that class. Now is a good time to click around and see what you find, and at the same time, familiarize yourself with JavaDoc style documentation if you've never seen it before. What you're looking for specifically are public methods. ColdFusion is not allowed to call private or protected methods in a Java class - these methods are internal to the class that defines them and are not intended to be used by developers using the class. If you see the static keyword, it means that you don't need an instance of the class to call the method. You can create the Java object in ColdFusion and use the method right away, without initializing it. After looking around some, it doesn't look like there's a class that will do what we want, so we'll have to just go ahead and make one!
Writing a Java Wrapper for Jazzy Click "Finish" to create the class and accept the default values Eclipse provides. Copy the code in Listing 1 into the file that was just created. (Code examples for this article can be downloaded from here.) Select Run -> Run As -> Java Application to see the wrapper in action. Inside the main method beginning on line 71, you can see that we first create a new CFSpellCheck object, set the dictionary, set the text, run the spell check, and then get the errors. This is the same flow that we'll have when we use CFSpellCheck in our ColdFusion code. It is not my intent to explain every single line of code in the Java wrapper. However, I do want to highlight some of the more interesting aspects. I've omitted the phonet file for the sake of simplicity and brevity. There is only an option to specify a dictionary file, with a setDictionary method that's defined starting on line 59. The file name is passed into the method as a string, and a dictionary is created by the file. If any error occurs during the dictionary creation process, the error is just dumped to the screen. The checkSpelling method on line 38 is where most of the magic happens. I've created an automated event handler for spelling errors. Whenever a spelling mistake is encountered, the word, position, and all of its suggestions are saved into an array. In order to save that information, I needed to create a data type to hold that data. There is no "StructNew" command in Java that I could leverage to create a container for the data, so I had to create a container on my own. This called for another class to be created with private variables to store the data and methods to manipulate those variables. Because I only want to use this class as a data type inside of the CFSpellCheck class, I defined it as an "inner class" by using the class keyword inside of a class and marked it as "private" to restrict access so that only CFSpellCheck can use it. The private inner class SpellingError starts on line 82 and will save the word, the position, and the list of suggestions for the word. A toString method in the SpellingError class is defined starting on line 113 that will return a string containing the values inside of a SpellingError variable, useful for debugging purposes. As you can see, there really isn't a lot to the wrapper class, and it should be fairly straightforward and easy to understand. The next step is invoking the wrapper from our ColdFusion application. Before doing this, we can go ahead and comment out the main method since it's no longer needed. This can be accomplished by using Java's multi-line comment operators. Place a "/*" on line 71 before the "public" keyword. At line 80, after the closing curly brace, place a "*/" to mark the end of the comment. We can also comment out the informational messages generated by lines 43 and 50. To comment out a single line, use Java's single-line comment operator, two forward slashes in a row. Before the word "System" place a "//" there to mark the line as a comment so that the Java compiler ignores the line. By default, you should see the line turn green inside Eclipse. Finally, we'll need to export our project. There are two ways to export the project. One is to bundle everything in a .jar file (Java Archive). The other is the manual process of copying all of the .class files to the directory we want to deploy from. Creating a .jar file is the easier and more elegant of the two approaches, so I'll be taking that approach. The latter approach makes missing a file easy, and requires multiple files to be shuffled around. The .jar file is a single file containing all of the required .class files for project deployment. To create a .jar in Eclipse, first make sure the CFSpellCheck.java file has been saved. Then, right-click on the project name in the Package Explorer. Select "Export" from the menu. Select "JAR File" from the dialog and then click the "Next" button. Uncheck all of the files on the right-hand side (.project, .classpath, and possibly CFSpellCheck.jar) as they will not be needed for deployment. Click "Browse" to choose a location where the .jar file will be created, and then click "Finish" to create the .jar file. We're now ready for the ColdFusion side of things... finally!
Calling the Java Wrapper from ColdFusion Once the classpath has been saved, you'll need to restart the ColdFusion service for the changes to take effect. After doing this, copy the .jar file to the "JavaClasses" directory. Additionally, you'll need to copy the "english.0" dictionary file to a directory on your Web server. I just copied the "dict" directory under the Eclipse project directory to "C:JavaClasses" as well. If you're using the J2EE version of ColdFusion MX, the easiest way to make the .jar file accessible is to either copy it to the "lib" directory for your application server or for the particular instance you want to make it available to in a multiple instances environment. To make the .jar available to all instances in JRun, this would be the {JRun install directory}/lib directory. To make the .jar available to one particular instance, create a "lib" directory off of the {JRun install directory}/servers/{instance name}/SERVER-INF directory and put the .jar file there. Either approach requires that you stop and restart any application server instance that will use the spell checker. The "english.0" file can be copied along with the .jar or can be put in any other directory on the server, as you specify the path to this file in the Java code. Now that the classpath is set up and the .jar file and dictionary file are in place, we can start using the CFSpellCheck class in our applications. The ColdFusion code is shown in Listing 2. This example is about as simple as they come. A form is presented to the user that, when submitted, runs the text in the textarea through the spell checker. If any errors are encountered they're just dumped to the screen. In the example, take note of the path to the dictionary. I'm using an absolute path from the C: drive and using forward slashes to separate the directories. This is standard Java syntax for defining directory paths. If you get a "dictionary must be non-null" error it means that the path to the dictionary file is not correct and the specified dictionary file could not be found. Also, note how similar the ColdFusion code looks to the main method we commented out in the CFSpellCheck class. Interesting, no? Now that we've got the spell checker working successfully... what's next?
Where We Go from Here Other than that, the only feature I have in mind can be implemented in client-side code via JavaScript and doesn't involve modification to the Java class. This feature would be popping up a "spelling error" dialog with options for replace, ignore, replace all, and ignore all, which would give users the level of interactivity that they have probably come to expect when spell checking text. If you're wondering what else you can do with Java, here are three different project ideas:
The open source Java community presents many possibilities for ColdFusion developers. Integrating Java and ColdFusion may be easier and require less coding than you expect. In this article, I showed you how to leverage an open source Java spell checker named Jazzy by writing a small wrapper class that exposed the key methods necessary for spell checking. I also showed you how to call this wrapper from ColdFusion to enable spell checking in your applications. I hope that I've gotten you excited at the possibilities of leveraging Java in ColdFusion and that the information presented in the article shows you the necessary steps that lead to success.
Resources
CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||