1 5 package com.dotmarketing.util; 6 7 import java.io.File ; 8 import java.io.FileFilter ; 9 import java.io.FileNotFoundException ; 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 17 import com.swabunga.spell.engine.SpellDictionary; 18 import com.swabunga.spell.engine.SpellDictionaryHashMap; 19 import com.swabunga.spell.engine.Word; 20 import com.swabunga.spell.event.AbstractWordFinder; 21 import com.swabunga.spell.event.SpellChecker; 22 import com.swabunga.spell.event.WordFinder; 23 import com.swabunga.spell.event.WordNotFoundException; 24 25 31 public class DotSpellChecker { 32 33 38 public class MisspelledWord { 39 40 43 public String word; 44 47 public String [] suggestions; 48 51 public int wordBegin = -1; 52 55 public int wordEnd = -1; 56 57 62 public MisspelledWord(String word, String [] suggestions) { 63 super(); 64 this.word = word; 65 this.suggestions = suggestions; 66 } 67 68 75 public MisspelledWord(String word, int begin, int end, String [] suggestions) { 76 super(); 77 this.word = word; 78 this.suggestions = suggestions; 79 wordBegin = begin; 80 wordEnd = end; 81 } 82 83 87 public String [] getSuggestions() { 88 return suggestions; 89 } 90 91 95 public void setSuggestions(String [] suggestions) { 96 this.suggestions = suggestions; 97 } 98 99 103 public String getWord() { 104 return word; 105 } 106 107 111 public void setWord(String word) { 112 this.word = word; 113 } 114 115 119 public int getWordBegin() { 120 return wordBegin; 121 } 122 123 127 public void setWordBegin(int wordBegin) { 128 this.wordBegin = wordBegin; 129 } 130 131 135 public int getWordEnd() { 136 return wordEnd; 137 } 138 139 143 public void setWordEnd(int wordEnd) { 144 this.wordEnd = wordEnd; 145 } 146 } 147 148 153 public class DotWordFinder extends AbstractWordFinder { 154 155 159 public DotWordFinder(String inText) { 160 super(inText); 161 } 162 163 168 public com.swabunga.spell.event.Word next() { 169 170 if (currentWord == null) 171 throw new WordNotFoundException("No more words found."); 172 173 currentWord.copy(nextWord); 174 175 setSentenceIterator(currentWord); 176 177 int i = currentWord.getEnd(); 178 boolean finished = false; 179 boolean started = false; 180 181 search: 182 while (i < text.length() && !finished) { 183 if (!started && isWordChar(i)) { 184 nextWord.setStart(i++); 185 started = true; 186 continue search; 187 } else if (started) { 188 if (isWordChar(i)) { 189 i++; 190 continue search; 191 } else { 192 nextWord.setText(text.substring(nextWord.getStart(), i)); 193 finished = true; 194 break search; 195 } 196 } 197 198 int lastIndex = i; 199 i = ignore(i, "<style", "/style>"); 202 i = ignore(i, "<script", "/script>"); 204 i = ignore(i, "<", ">"); 206 i = ignore(i, "&", ";"); 208 i = ignore(i, "#end", ""); 210 i = ignore(i, "#", ")"); 211 i = ignore(i, "${", "}"); 212 i = ignore(i, "$!{", "}"); 213 214 if (lastIndex == i) 215 i++; 216 } 217 218 if (!started) { 219 nextWord = null; 220 } else if (!finished) { 221 nextWord.setText(text.substring(nextWord.getStart(), i)); 222 } 223 224 return currentWord; 225 } 226 } 227 228 private SpellChecker spellCheck = null; 229 private WordFinder finder = null; 230 private SpellDictionary personalDict = null; 231 private String currentDict = ""; 232 233 private static Map <String , List <SpellDictionary>> dicts; 234 private static boolean initialized = false; 235 236 245 public synchronized static void initializeDicts(boolean force, boolean sync) { 246 if (initialized && !force) 247 return; 248 initialized = true; 249 250 Thread initThread = new Thread ("Spellchecker Init Thread") { 251 public void run() { 252 Logger.debug(DotSpellChecker.class, "Initializing SpellChecker dicts thread"); 253 long before = System.currentTimeMillis(); 254 255 dicts = new HashMap <String , List <SpellDictionary>>(); 256 257 String dictsDirPath = Config.CONTEXT.getRealPath(Config.getStringProperty("MASTER_DICTS_DIR")); 258 259 File dictsDir = new File (dictsDirPath); 260 261 if (dictsDir.exists() && dictsDir.isDirectory()) { 262 File [] dicsDirs = dictsDir.listFiles(); 263 264 if (dicsDirs.length == 0) { 265 Logger.error(DotSpellChecker.class, "No dictionaries found inside: " 266 + dictsDir.getAbsolutePath()); 267 } 268 269 for (int i = 0; i < dicsDirs.length; i++) { 270 File currentDir = dicsDirs[i]; 271 if (currentDir.isDirectory()) { 272 File [] dictFiles = currentDir.listFiles(new FileFilter () { 273 public boolean accept(File pathname) { 274 return pathname.isFile() && pathname.canRead() 275 && pathname.getName().endsWith(".dict"); 276 } 277 }); 278 File phonetFile = new File (currentDir.getAbsolutePath() + File.separator 279 + currentDir.getName() + ".phonet"); 280 boolean withPhonetFile = false; 281 if (phonetFile.exists() && phonetFile.canRead()) 282 withPhonetFile = true; 283 284 List <SpellDictionary> spellDicts = new ArrayList <SpellDictionary>(); 285 for (int j = 0; j < dictFiles.length; j++) { 286 SpellDictionary dictionary = null; 287 try { 288 if (withPhonetFile) { 289 dictionary = new SpellDictionaryHashMap(dictFiles[j], phonetFile); 290 } else { 291 dictionary = new SpellDictionaryHashMap(dictFiles[j]); 292 } 293 } catch (Exception e) { 294 Logger.error(DotSpellChecker.class, "Exception initializing dictionaries", e); 295 } 296 spellDicts.add(dictionary); 297 } 298 if (dictFiles.length > 0) 299 dicts.put(currentDir.getName(), spellDicts); 300 } 301 } 302 303 } else { 304 Logger.error(DotSpellChecker.class, "MASTER_DICTS_DIR = " + dictsDir.getAbsolutePath() + " not found."); 305 } 306 307 long after = System.currentTimeMillis(); 308 309 Logger.debug(DotSpellChecker.class, "End Initializing SpellChecker dicts thread time: " + (after - before) + " ms."); 310 311 } 312 }; 313 initThread.start(); 314 if (sync) 315 try { 316 initThread.join(); 317 } catch (InterruptedException e) { 318 Logger.error(DotSpellChecker.class, "Error Initializing SpellChecker dicts thread", e); 319 } 320 321 } 322 323 329 public DotSpellChecker() throws FileNotFoundException , IOException { 330 super(); 331 332 if (!DotSpellChecker.initialized) { 333 DotSpellChecker.initializeDicts(false, true); 334 } 335 336 spellCheck = new SpellChecker(); 338 339 List defaultDics = null; 341 String defaultDicName = Config.getStringProperty("DEFAULT_DIC"); 342 ; 343 344 if (UtilMethods.isSet(defaultDicName) && dicts.get(defaultDicName) != null) { 345 defaultDics = (List ) dicts.get(defaultDicName); 346 } 347 348 if (defaultDics == null) { 349 List <String > keys = new ArrayList <String >(dicts.keySet()); 350 defaultDicName = (String ) keys.get(0); 351 defaultDics = (List ) dicts.get(keys.get(0)); 352 } 353 354 if (defaultDics != null) { 355 Iterator it = defaultDics.iterator(); 356 while (it.hasNext()) { 357 SpellDictionary dictionary = (SpellDictionary) it.next(); 358 spellCheck.addDictionary(dictionary); 359 } 360 } else 361 throw new FileNotFoundException ("Default dictionary: " + defaultDicName + " not found."); 362 363 currentDict = defaultDicName; 364 365 String dictsDirPath = Config.CONTEXT.getRealPath(Config.getStringProperty("MASTER_DICTS_DIR")); 366 File personalDictFile = new File (dictsDirPath + File.separator + defaultDicName + File.separator 367 + defaultDicName + ".personal"); 368 if (!personalDictFile.exists()) 369 personalDictFile.createNewFile(); 370 if (personalDictFile.exists() && personalDictFile.canRead()) { 371 personalDict = new SpellDictionaryHashMap(personalDictFile); 372 } 373 spellCheck.addDictionary(personalDict); 374 375 } 376 377 381 public String getCurrentDictionary() { 382 return currentDict; 383 } 384 385 392 public boolean changeDictionary(String newDict) throws FileNotFoundException , IOException { 393 394 if (currentDict.equals(newDict)) 395 return false; 396 397 List newDicts = null; 399 400 if (UtilMethods.isSet(newDict) && dicts.get(newDict) != null) { 401 newDicts = (List ) dicts.get(newDict); 402 } 403 404 if (newDicts == null) { 405 throw new FileNotFoundException ("Dictionary: " + newDict + " not found."); 406 } 407 408 spellCheck = new SpellChecker(); 410 411 Iterator it = newDicts.iterator(); 412 while (it.hasNext()) { 413 SpellDictionary dictionary = (SpellDictionary) it.next(); 414 spellCheck.addDictionary(dictionary); 415 } 416 417 String dictsDirPath = Config.CONTEXT.getRealPath(Config.getStringProperty("MASTER_DICTS_DIR")); 418 File personalDictFile = new File (dictsDirPath + File.separator + newDict + File.separator + newDict 419 + ".personal"); 420 if (!personalDictFile.exists()) 421 personalDictFile.createNewFile(); 422 if (personalDictFile.exists() && personalDictFile.canRead()) { 423 personalDict = new SpellDictionaryHashMap(personalDictFile); 424 } 425 spellCheck.addDictionary(personalDict); 426 427 currentDict = newDict; 428 429 return true; 430 } 431 432 436 public void startSpellChecking(String text) { 437 finder = new DotWordFinder(text); 438 } 439 440 444 public MisspelledWord getNextMisspelledWord() { 445 boolean found = false; 446 while (finder.hasNext()) { 447 com.swabunga.spell.event.Word nextWord = finder.next(); 448 if (nextWord.length() <= 0 || nextWord.getText().toUpperCase().equals(nextWord.getText())) 449 continue; 450 String nextWordStr = nextWord.getText(); 451 boolean firstInUpperCase = false; 452 if (Character.isUpperCase(nextWordStr.charAt(0))) { 453 nextWordStr = Character.toLowerCase(nextWordStr.charAt(0)) 454 + ((nextWordStr.length() > 1) ? nextWordStr.substring(1, nextWordStr.length()) : ""); 455 firstInUpperCase = true; 456 } 457 List suggestions = spellCheck.getSuggestions(nextWordStr, 3); 458 String [] suggestionsArr = new String [suggestions.size()]; 459 if (suggestions.size() > 0) { 460 int i = 0; 461 Iterator it = suggestions.iterator(); 462 while (it.hasNext()) { 463 Word sug = (Word) it.next(); 464 if (sug.getCost() == 0) { 465 found = false; 466 break; 467 } 468 found = true; 469 String sugStr = sug.getWord(); 470 if (firstInUpperCase) { 471 sugStr = Character.toUpperCase(sugStr.charAt(0)) 472 + ((sugStr.length() > 1) ? sugStr.substring(1, sugStr.length()) : ""); 473 } 474 suggestionsArr[i++] = sugStr; 475 } 476 } else { 477 found = true; 478 } 479 if (found) { 480 int wordPos = nextWord.getStart(); 481 int wordEnd = nextWord.getEnd(); 482 MisspelledWord word = new MisspelledWord(nextWord.getText(), wordPos, wordEnd, suggestionsArr); 483 return word; 484 } 485 } 486 return null; 487 } 488 489 494 public String replaceWord(String newWord) { 495 finder.replace(newWord); 496 return finder.getText(); 497 } 498 499 503 public void addToDictionary(String newWord) { 504 if (personalDict != null) 505 personalDict.addWord(newWord); 506 } 507 508 514 public MisspelledWord getSuggestions(String nextWord) { 515 boolean found = false; 516 if (!UtilMethods.isSet(nextWord)) 517 return null; 518 519 String nextWordStr = nextWord; 520 boolean firstInUpperCase = false; 521 if (Character.isUpperCase(nextWordStr.charAt(0))) { 522 nextWordStr = Character.toLowerCase(nextWordStr.charAt(0)) 523 + ((nextWordStr.length() > 1) ? nextWordStr.substring(1, nextWordStr.length()) : ""); 524 firstInUpperCase = true; 525 } 526 527 List suggestions = spellCheck.getSuggestions(nextWordStr, 3); 528 String [] suggestionsArr = new String [suggestions.size()]; 529 if (suggestions.size() > 0) { 530 int i = 0; 531 Iterator it = suggestions.iterator(); 532 while (it.hasNext()) { 533 Word sug = (Word) it.next(); 534 found = true; 535 String sugStr = sug.getWord(); 536 if (firstInUpperCase) { 537 sugStr = Character.toUpperCase(sugStr.charAt(0)) 538 + ((sugStr.length() > 1) ? sugStr.substring(1, sugStr.length()) : ""); 539 } 540 suggestionsArr[i++] = sugStr; 541 } 542 } else { 543 found = false; 544 } 545 if (found) { 546 MisspelledWord word = new MisspelledWord(nextWord, suggestionsArr); 547 return word; 548 } 549 return null; 550 } 551 552 556 public String [] getDictionaries() { 557 return (String []) dicts.keySet().toArray(new String [0]); 558 } 559 560 } 561
| Popular Tags
|