//filename : spellchecker.jsp for java developer
<%@page language="java"%>
<%@page import="java.io.*"%>
<%@page import="java.util.Enumeration"%>
<html>
<body>
<%
String osName = System.getProperty("os.name");
String s = null;
//The following variables values must reflect your installation needs.
//convert from php to jsp by windu gata 10 October 2007, Budi Luhur University, Indonesia
//String aspell_prog = "C:\\Program Files\\Aspell\\bin\\aspell.exe"; //windows
//String aspell_prog = "/usr/bin/aspell"; //linux
String aspell_prog = "/opt/local/bin/aspell"; //apple
String aspell_opts = "-a --lang=en_US --encoding=utf-8 -H --rem-sgml-check=alt";
String spellercss = "../spellerStyle.css";
String word_win_src = "../wordWindow.js";
String input_separator = "A";
String escape_char[] = {"%20", "%3Cbr", "%3E", "%3B", "%26nbsp"};
String textinputs = request.getParameter("textinputs[]");
for (int i=0; i<escape_char.length;i++) {
textinputs = textinputs.replace(escape_char[i], " ");
}
ServletContext context=request.getSession().getServletContext();
File tmpdir=(File)context.getAttribute("javax.servlet.context.tempdir");
String tmpfile="/aspell_data_";
try {
PrintWriter printout = new PrintWriter (new BufferedWriter (new FileWriter (tmpdir.getPath() + tmpfile)));
printout.print (textinputs);
printout.flush ();
printout.close ();
}
catch (IOException e) {
out.write("Cannot Create File Temporary " + e.toString()) ;
}
try {
// Windows Nt or XP, change "cmd " t0 "command" on windows 98
// String[] cmd = {
// "cmd",
// "-c",
// aspell_prog + " " + aspell_opts + "<" + tmpdir.getPath() + tmpfile + " 2>&1"
// };
String[] cmd = {
"/bin/sh",
"-c",
aspell_prog + " " + aspell_opts + "<" + tmpdir.getPath() + tmpfile + " 2>&1"
};
Process p1 = Runtime.getRuntime().exec(cmd);
int exitValue = p1.waitFor();
if (exitValue == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p1.getInputStream()));
while ((s = stdInput.readLine()) != null) {
if (s.startsWith("&")) {
String word[] = s.split(":");
String wordleft[] = word[0].split(" ");
out.write("<b>" + wordleft[1] + " Found : " + wordleft[2] + " Suggest : " + wordleft[3] + "</b><br>");
out.write(word[1] + "<br><br>");
}
if (s.startsWith("#")) {
out.write("<br><b> No Suggestion " + s.substring(1) + "</b><br>");
}
}
}
else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p1.getErrorStream()));
while ((s = stdErr.readLine()) != null)
{
out.write(s + "<br>");
}
}
}
catch (Exception e) {
System.out.println(e);
}
%>
</body></html>
<%@page language="java"%>
<%@page import="java.io.*"%>
<%@page import="java.util.Enumeration"%>
<html>
<body>
<%
String osName = System.getProperty("os.name");
String s = null;
//The following variables values must reflect your installation needs.
//convert from php to jsp by windu gata 10 October 2007, Budi Luhur University, Indonesia
//String aspell_prog = "C:\\Program Files\\Aspell\\bin\\aspell.exe"; //windows
//String aspell_prog = "/usr/bin/aspell"; //linux
String aspell_prog = "/opt/local/bin/aspell"; //apple
String aspell_opts = "-a --lang=en_US --encoding=utf-8 -H --rem-sgml-check=alt";
String spellercss = "../spellerStyle.css";
String word_win_src = "../wordWindow.js";
String input_separator = "A";
String escape_char[] = {"%20", "%3Cbr", "%3E", "%3B", "%26nbsp"};
String textinputs = request.getParameter("textinputs[]");
for (int i=0; i<escape_char.length;i++) {
textinputs = textinputs.replace(escape_char[i], " ");
}
ServletContext context=request.getSession().getServletContext();
File tmpdir=(File)context.getAttribute("javax.servlet.context.tempdir");
String tmpfile="/aspell_data_";
try {
PrintWriter printout = new PrintWriter (new BufferedWriter (new FileWriter (tmpdir.getPath() + tmpfile)));
printout.print (textinputs);
printout.flush ();
printout.close ();
}
catch (IOException e) {
out.write("Cannot Create File Temporary " + e.toString()) ;
}
try {
// Windows Nt or XP, change "cmd " t0 "command" on windows 98
// String[] cmd = {
// "cmd",
// "-c",
// aspell_prog + " " + aspell_opts + "<" + tmpdir.getPath() + tmpfile + " 2>&1"
// };
String[] cmd = {
"/bin/sh",
"-c",
aspell_prog + " " + aspell_opts + "<" + tmpdir.getPath() + tmpfile + " 2>&1"
};
Process p1 = Runtime.getRuntime().exec(cmd);
int exitValue = p1.waitFor();
if (exitValue == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p1.getInputStream()));
while ((s = stdInput.readLine()) != null) {
if (s.startsWith("&")) {
String word[] = s.split(":");
String wordleft[] = word[0].split(" ");
out.write("<b>" + wordleft[1] + " Found : " + wordleft[2] + " Suggest : " + wordleft[3] + "</b><br>");
out.write(word[1] + "<br><br>");
}
if (s.startsWith("#")) {
out.write("<br><b> No Suggestion " + s.substring(1) + "</b><br>");
}
}
}
else {
BufferedReader stdErr = new BufferedReader(new InputStreamReader(p1.getErrorStream()));
while ((s = stdErr.readLine()) != null)
{
out.write(s + "<br>");
}
}
}
catch (Exception e) {
System.out.println(e);
}
%>
</body></html>
Re: Need spellchecker.jsp ? ( not just .php, .cfm, or .pl)
better code .....
<%@page language="java"%>
<%@page import="java.io.*"%>
<%@page import="java.util.Enumeration"%>
<%
String osName = System.getProperty("os.name");
String s = null;
//The following variables values must reflect your installation needs.
//convert from php to jsp by windu gata 10 October 2007
//String aspell_prog = "C:\\Program Files\\Aspell\\bin\\aspell.exe"; //windows
//String aspell_prog = "/usr/bin/aspell"; //linux
String aspell_prog = "/opt/local/bin/aspell"; //apple
String aspell_opts = "-a --lang=en_US --encoding=utf-8 -H --rem-sgml-check=alt";
String spellercss = "../spellerStyle.css";
String word_win_src = "../wordWindow.js";
String input_separator = "A";
String escape_char[] = {"%20", "%3Cbr", "%3E", "%3B", "%26nbsp"};
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="<%=spellercss %>" />
<script language="javascript" src="<%=word_win_src %>"></script>
<script language="javascript">
var suggs = new Array();
var words = new Array();
var textinputs = new Array();
var error;
<%
//get parameter
String textinputs = request.getParameter("textinputs[]");
//delete escape character
for (int i=0; i<escape_char.length;i++) {
textinputs = textinputs.replace(escape_char[i], " ");
}
//insert variable textinputs in javascript
String[] textinput = textinputs.split(" ");
for (int i=0;i<textinput.length; i++) {
out.write("textinputs[" + i + "] = decodeURIComponent(\"" + textinput[i] + "\");\n");
}
//get temporary servlet directory
ServletContext context=request.getSession().getServletContext();
File tmpdir=(File)context.getAttribute("javax.servlet.context.tempdir");
String tmpfile="/aspell_data_";
try {
//save temporary file
PrintWriter printout = new PrintWriter (new BufferedWriter (new FileWriter (tmpdir.getPath() + tmpfile)));
printout.print (textinputs);
printout.flush ();
printout.close ();
}
catch (IOException e) {
out.write("Cannot Create File Temporary " + e.toString()) ;
}
try {
// Windows Nt or XP, change "cmd " t0 "command" on windows 98
// String[] cmd = {
// "cmd",
// "-c",
// aspell_prog + " " + aspell_opts + "<" + tmpdir.getPath() + tmpfile + " 2>&1"
// };
String[] cmd = {
"/bin/sh",
"-c",
aspell_prog + " " + aspell_opts + "<" + tmpdir.getPath() + tmpfile + " 2>&1"
};
Process p1 = Runtime.getRuntime().exec(cmd);
int exitValue = p1.waitFor();
if (exitValue == 0){
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p1.getInputStream()));
int index = 0;
int text_input_idx = -1;
while ((s = stdInput.readLine()) != null) {
if (s.startsWith("*")) {
text_input_idx++;
out.write("words[" + text_input_idx + "] = [];\n");
out.write("suggs[" + text_input_idx + "] = [];\n");
index = 0;
}else if (s.startsWith("&") || s.startsWith("#")) {
if (text_input_idx==-1) {
text_input_idx++;
}
String word[] = s.split(":");
String wordleft[] = word[0].split(" ");
out.write ("words[" + text_input_idx + "][" + index + "] = '" + wordleft[1].trim().replace("\'","\\'") + "';\n");
if ( word.length > 0) {
String suggs[] = word[1].split(", ");
out.write("suggs[" + text_input_idx + "][" + index + "] = [");
for (int i=0; i< suggs.length; i++) {
out.write( "'" + suggs[i].trim().replace("\'","") + "'");
if (i < suggs.length -1) {
out.write(",");
}
}
out.write("];\n");
}
index++;
} else if (!s.startsWith("@") || !s.startsWith("")) {
String aspell_err= s;
}
}
}
}
catch (Exception e) {
System.out.println(e);
}
%>
var wordWindowObj = new wordWindow();
wordWindowObj.originalSpellings = words;
wordWindowObj.suggestions = suggs;
wordWindowObj.textInputs = textinputs;
function init_spell() {
// check if any error occured during server-side processing
if( error ) {
alert( error );
} else {
// call the init_spell() function in the parent frameset
if (parent.frames.length) {
parent.init_spell( wordWindowObj );
} else {
alert('This page was loaded outside of a frameset. It might not display properly');
}
}
}
</script>
</head>
<!-- <body onLoad="init_spell();"> by FredCK -->
<body onLoad="init_spell();" bgcolor="#ffffff">
<script type="text/javascript">
wordWindowObj.writeBody();
</script>
</body>
</html>
Re: Need spellchecker.jsp ( not just .php, .cfm, or .pl)
Thanks for posting this.
I copied the spellchecker.jsp in to my application. Did all the necessary configuration required in the fckconfig.js file as mentioned in the thread.
I am getting this javascript error...
this.originalSpellings[textIndex] is undefined
alert(this.originalSpellings[textIndex].length);
Any help is appreciated!
Thanks,
Tulasiram
Re: Need spellchecker.jsp ( not just .php, .cfm, or .pl)
I've updated spellchecker.jsp to be more complete and actually work (for me). Way too hard to try to cut-n-paste and I can't attach, so see my blog post:
http://haxx.sinequanon.net/2011/10/ckeditor-spellchecker-jsp/
Re: Need spellchecker.jsp ( not just .php, .cfm, or .pl)
Thanks. But when I use it, process p1 never returns. I tried to see if there are any security issues on my Windows 7 machine.
Have any suggestions?
Regards,
Abhishek
Re: Need spellchecker.jsp ( not just .php, .cfm, or .pl)
I don't do windows, Abhishek, so I can only suggest to check the paths for aspell_prog and make it sure it works from the command-line interface...