Listing 1: Model - PigLatingizer.cfc
<cfcomponent name="PigLatinizer">
<!--- Constructor --->
<cffunction name="init" returntype="PigLatinizer">
<cfreturn this />
</cffunction>
<!--- Translates a phrase to Pig Latin --->
<cffunction name="Translate" retuntype="string">
<cfargument name="phrase" type="string">
<!--- Declare local variables --->
<cfset var result = "" />
<cfset var word = "" />
<cfset firstVowel = 0 />
<cfloop list="#arguments.phrase#" index="word" delimiters=" ">
<cfset firstVowel = reFindNoCase("[aeiouy]", word) - 1/>
<cfif firstVowel gt 0>
<cfset word = right(word, len(word) - firstVowel) & left(word, firstVowel) />
</cfif>
<cfset result = result & word & "ay " />
</cfloop>
<cfreturn result />
</cffunction>
</cfcomponent>
Listing 2: View - Index.cfm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MVC Pig Latin Translator</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
Enter a phrase, and I'll change it to Pig Latin!
<cfif structKeyExists(url, "result")>
<cfoutput>
<p><strong>Result: #url.result#</strong></p>
</cfoutput>
</cfif>
<cfform name="phrase" action="index.cfm?method=translate">
Phrase: <cfinput type="text" name="phrase" /><br>
<input type="submit" value="Go" />
</cfform>
</body>
</html>
Listing 3: Controller - PigLatinController.cfc
<cfcomponent displayname="PigLatinController">
<!--- Constructor --->
<cffunction name="init" returntype="PigLatinController">
<cfreturn this />
</cffunction>
<!--- Reverses a name --->
<cffunction name="Translate">
<!--- Declare local variables --->
<cfset var reversedName = "" />
<!--- Create the needed CFCs from our model --->
<cfset var translator = createObject("component", "PigLatinizer").init() />
<!--- Use the Model to reverse the name --->
<cfset result = translator.Translate(phrase) />
<!--- Forward the user to the appropriate page --->
<cflocation url="index.cfm?result=#urlEncodedFormat(result)#"
addToken="no" />
</cffunction>
</cfcomponent>
Listing 4: Framework - Application.cfm
<cfapplication name="IgpayAtlinlayAnslatortray">
<!--- Our mini-MVC Framework --->
<!--- Make sure a controller exists --->
<cfif not structKeyExists(application, "init")
or structKeyExists(url, "init")>
<cfset application.PigLatinController = createObject("component", "PigLatinController").init() />
</cfif>
<!--- Url.method states that a method of the controller should be called --->
<cfif structKeyExists(url, "method")>
<cfinvoke component="#application.PigLatinController#" method="#url.method#" />
</cfif>
Listing 5: Java-Enhanced Model
<cfcomponent name="PigLatinizer">
<!--- Constructor --->
<cffunction name="init" returntype="PigLatinizer">
<cfreturn this />
</cffunction>
<!--- Translates a phrase to Pig Latin --->
<cffunction name="Translate" retuntype="string">
<cfargument name="phrase" type="string">
<cfset var translator = createObject("Java", "PigLatinizer").init() />
<cfreturn translator.translate(arguments.phrase) />
<cfreturn result />
</cffunction>
</cfcomponent>
Listing 6: Java PigLatinizer
import java.util.*;
public class PigLatinizer {
public PigLatinizer(){
}
public String Translate (String phrase) {
String[] words = phrase.split(" ");
String result = "";
for (int i=0;i<words.length;i++) {
result += TranslateWord(words[i]);
}
return result;
}
private String TranslateWord(String word) {
if (StartsWithVowel(word)) {
return word + "way ";
} else {
int i = FindFirstVowel(word);
if (i != -1) {
word = word.substring(i) + word.substring(0,i) + "ay ";
} else {
word += "ay ";
}
}
return word;
}
private int FindFirstVowel(String word) {
String vowels = "aeiouyAEIOU";
// if you have 200 consontants in a row, something is wrong.
int pos = 200;
int i, j;
for(i=0;i<vowels.length();i++) {
j = word.indexOf(vowels.charAt(i));
if ((j < pos) && (j != -1))
pos = j;
}
if (pos == 200)
return -1;
else
return pos;
}
private boolean StartsWithVowel (String word) {
char let = word.charAt(0);
switch (let) {
case 'a':case 'e':case 'i':case 'o':case 'u':case 'A':case 'E':case 'I':case 'O':case 'U':
return true;
default:
return false;
}
}
}