Marco Casavecchia Morganti wrote:
Maybe we can add a test that checks for the JVM version before perform the call.
I made it. I tested the attached version on my 1.5 and it works. Can you test it on a 1.4? By MCM.
/* * Copyright 2001-2007 Hippo (www.hippo.nl) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package nl.hippo.cms.spellchecking; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.util.List; import java.util.StringTokenizer; import java.util.Vector; /** * This is a wrapper around the aspell command line utility * * @author Jeroen Reijn */ public class AspellWrapper { /** The process running the aspell command. */ private Process process = null; private Process process1 = null; /** The standard output for the aspell command. */ private BufferedReader aspellOutputStream = null; private BufferedReader aspellOutputStream1 = null; /** The standard input for the aspell command. */ private PrintStream aspellInputStream = null; /** The standard error for the aspell command. */ private BufferedReader aspellErrorStream = null; private String aSpellLocation = ""; private String aSpellDictionary = ""; public AspellWrapper(){ //System.out.println("Start up aSpellWrapper"); } public void setupAspell() { //System.out.println("Setup Aspell"); String[] aCommand = new String[5]; aCommand[0] = this.aSpellLocation + "aspell"; aCommand[1] = "-a"; aCommand[2] = "--keymapping=ispell"; aCommand[3] = "--lang="+this.aSpellDictionary; aCommand[4] = "--encoding=UTF-8"; //MCM: use LD_LIBRARY_PATH if present into environment String[] envArray = prepareEnv(); try { process = Runtime.getRuntime().exec(aCommand, envArray); aspellOutputStream = new BufferedReader(new InputStreamReader(process.getInputStream())); aspellInputStream = new PrintStream(new BufferedOutputStream(process.getOutputStream()),true); aspellErrorStream = new BufferedReader(new InputStreamReader(process.getErrorStream())); } catch(IOException ioe) { System.out.println("An error has occured: " + ioe); } } public Vector getDictionaries() { String[] aCommand = new String[3]; aCommand[0] = this.aSpellLocation + "aspell"; aCommand[1] = "dump"; aCommand[2] = "dicts"; Vector dicts = new Vector(); //MCM: use LD_LIBRARY_PATH if present into environment String[] envArray = prepareEnv(); try { process1 = Runtime.getRuntime().exec(aCommand, envArray); aspellOutputStream1 = new BufferedReader(new InputStreamReader(process1.getInputStream())); String line = ""; while((line=aspellOutputStream1.readLine())!=null) { dicts.add(line); } aspellOutputStream1.close(); process1 = null; } catch(IOException ioe) { System.out.println("An error has occured: " + ioe); } return dicts; } /** * Find spelling corrections for a given misspelled word. * * @param pTerm The word to correct. * @return An array of possible corrections. */ public String[] find(String pTerm) { String[] candidates = null; String badTerm = pTerm.trim().toLowerCase(); aspellInputStream.flush(); aspellInputStream.println(badTerm); aspellInputStream.flush(); try { String line = aspellOutputStream.readLine(); line = aspellOutputStream.readLine(); if (line.trim().length() <= 0) aspellInputStream.println(); candidates = convertFromAspell(pTerm, line); } catch (Exception e) { } return (candidates); } /** * Find the best spelling correction for a given misspelled word. * * @param pTerm The word to correct. * @return The best possible correction. */ public String findMostSimilar(String pTerm) { String[] candidates = find(pTerm); if(candidates == null || candidates.length==0) return null; return candidates[0]; } /** * Find spelling corrections for a given misspelled word. * * @param pTerm The word to correct. * @return A <code>List</code> with the possible corrections. */ public List findMostSimilarList(String pTerm) { String[] candidates = find(pTerm); List aux = new Vector(); if(candidates!=null){ for(int i=0; i<candidates.length; i++) aux.add(candidates[i]); } return aux; } /** * Converts the result from the aspell spelling checker * into a <code>String</code> array with the possible suggestions. * * @param pTerm The correctly spelled word. * @param pSuggestions A <code>String</code> with the suggestions in aspell format. * @return An array with the suggestions. */ private String[] convertFromAspell(String pTerm, String pSuggestions) { String[] candidates = null; int numberOfCandidates = 0; try { if (pSuggestions.equals("*")) { candidates = new String[1]; candidates[numberOfCandidates] = pTerm; numberOfCandidates++; } else { StringTokenizer st = new StringTokenizer(pSuggestions, ":", false); if (st.hasMoreTokens()) { st.nextToken(); String stuffAfterColon = st.nextToken(); StringTokenizer st2 = new StringTokenizer(stuffAfterColon, ",", false); candidates = new String[st2.countTokens()]; String suggestion = null; while (st2.hasMoreTokens()) { suggestion = st2.nextToken().trim().toLowerCase(); candidates[numberOfCandidates] = suggestion; numberOfCandidates++; } st2 = null; st = null; } } } catch (Exception e) { } return (candidates); } public void setASpellDictionary(String dictionary) { this.aSpellDictionary = dictionary; } public void setASpellLocation(String location) { this.aSpellLocation = location; } /** * MCM: Prepare the custom environment for the aspell command * by setting the LD_LIBRARY_PATH (if it was found into the * running environment) * This is useful for custom aspell installations (like my own) **/ private String[] prepareEnv() { // Check if the JVM is 1.4. // We must perform this test because only in version 1.4 // the getEnv() method was deprecated. String retValue[] = null; if (System.getProperty("java.vm.version").startsWith("1.4")) { //We run into a 1.4 jvm, so we can't use the environment variables retValue = new String[0]; } else { // use LD_LIBRARY_PATH if present into environment String libraryPath = System.getenv("LD_LIBRARY_PATH"); if (libraryPath == null) { retValue = new String[0]; } else { retValue = new String[1]; retValue[0] = "LD_LIBRARY_PATH=" + libraryPath; //System.out.println("MCM: Environment variable found: \""+retValue[0]+"\""); } } return retValue; } /** * Cleanup the process running aspell. */ public void cleanup() { try { aspellOutputStream.close(); aspellInputStream.close(); aspellErrorStream.close(); process = null; } catch (Exception e) { System.out.println("CLEANUP: " + e); } } }
******************************************** Hippocms-dev: Hippo CMS development public mailinglist
