1 package SnowMailClient.Language; 2 3 4 import snow.utils.storage.*; 5 6 import java.util.*; 7 import java.util.zip.*; 8 import java.io.*; 9 import java.net.URL ; 10 import javax.swing.table.*; 11 import javax.swing.event.*; 12 import java.awt.event.*; 13 14 15 19 public final class SentenceDictionary { 21 final List<Sentence> allSentencesVector = new Vector<Sentence>(); 23 24 final Map<String , String > sentences = new Hashtable<String , String >(); 28 private String language; 30 31 private boolean isEditable = true; 35 36 public SentenceDictionary(String language) 37 { 38 this.language = language; 39 } 40 41 42 public String getLanguage() {return language;} 43 public List<Sentence> getAllSentences() 44 { 45 return allSentencesVector; 46 } 47 48 public boolean getIsEditable() { return isEditable; } 49 50 51 57 public String getTranslatedSentence(String sentence) 58 { 59 String transl = sentences.get(sentence); 60 if(transl!=null) 61 { 62 return transl; 63 } 64 else 65 { 66 String try2 = getTranslationIgnoringSpaces(sentence); 68 if(try2!=null) 69 { 70 return try2; 72 } 73 74 Sentence sent = new Sentence(sentence, "Occured at runtime",-1, -1); 76 allSentencesVector.add(sent); 77 78 return sentence; 80 } 81 } 82 83 public boolean hasTranslatedSentence(String sentence) 84 { 85 String transl = sentences.get(sentence); 86 if(transl!=null) 87 { 88 return true; 89 } 90 else 91 { 92 String try2 = getTranslationIgnoringSpaces(sentence); 94 if(try2!=null) return true; 95 96 return false; 98 } 99 } 100 101 105 private String getTranslationIgnoringSpaces(String sentence) 106 { 107 String trimmed = sentence.trim(); 109 110 for(String elt : sentences.keySet()) 111 { 112 if(elt.trim().equals(trimmed)) 113 { 114 int pos = sentence.indexOf(trimmed); 117 StringBuffer reply = new StringBuffer (); 118 reply.append( sentence.substring(0,pos) ); 119 reply.append( sentences.get(elt).trim()); 120 reply.append( sentence.substring(pos+trimmed.length()) ); 121 return reply.toString(); 122 } 123 } 124 125 return null; 126 } 127 128 129 132 public void updateSentencesFromSource(Vector<Sentence> allEnglishSentencesFoundInSource) 133 { 134 System.out.println("Update dictionary with sentences parsed from source for language "+getLanguage()); 135 136 for(int p=0; p<allEnglishSentencesFoundInSource.size(); p++) 138 { 139 Sentence sent = allEnglishSentencesFoundInSource.elementAt(p); 140 verifyIfSentenceIsPresent(sent); 143 } 144 145 int removed = 0; 147 List<Sentence> toRemove = new Vector<Sentence>(); 148 for(int i=0; i<allSentencesVector.size(); i++) 149 { 150 Sentence s = allSentencesVector.get(i); 151 if(s.visited==false) 152 { 153 removed++; 154 toRemove.add(s); 155 } 156 } 157 158 for(int i=0; i<toRemove.size();i++) 159 { 160 Sentence s = toRemove.get(i); 161 sentences.remove(s.getSentence()); 162 allSentencesVector.remove(s); 163 } 164 165 fireChangeEvent(); 166 167 System.out.println("" +removed +" removed unused sentences"); 168 } 169 170 171 public String getStringOfAllTranslatedSentences() 172 { 173 StringBuffer sb = new StringBuffer (); 174 for(int i=0; i<allSentencesVector.size();i++) 175 { 176 Sentence s = allSentencesVector.get(i); 177 if(s.hasTranslation()) 178 { 179 sb.append("\n\n"+s.getTranslation()); 180 } 181 } 182 return sb.toString(); 183 } 184 185 187 private void verifyIfSentenceIsPresent(Sentence sent) 188 { 189 String sentence = sent.getSentence(); 190 191 int pos = positionInAllSentenceVector(sentence); 192 if(pos==-1) 193 { 194 allSentencesVector.add(sent); 196 sent.visited = true; 197 } 198 else 199 { 200 Sentence s = allSentencesVector.get(pos); 202 s.visited = true; 203 s.setLocationClass(sent.getLocationClass(), sent.getLinePosition()); 204 } 205 206 233 } 234 235 238 private int positionInAllSentenceVector(String sent) 239 { 240 String st = sent.trim(); 241 242 for(int i=0; i<allSentencesVector.size(); i++) 243 { 244 Sentence s = allSentencesVector.get(i); 245 if(s.getSentence().trim().equals(st)) return i; 246 } 247 return -1; 248 } 249 250 251 252 public int getNumberOfTranslatedSentences() { return sentences.size(); } 253 public int getNumberOfSentences() { return allSentencesVector.size(); } 254 255 256 257 259 public void addTranslation(String english, String translation) 260 { 261 sentences.put(english, translation); 263 int pos= this.positionInAllSentenceVector(english); 264 if(pos==-1) 265 { 266 Sentence se = new Sentence(english, "Added during runtime", 0, -1); 267 se.setTranslation(translation); 268 269 allSentencesVector.add(se); 270 System.out.println("### sentence not in dic "+english); 271 } 272 else 273 { 274 Sentence se = allSentencesVector.get(pos); 275 se.setTranslation(translation); 276 } 277 278 fireChangeEvent(); 280 } 281 282 List<ChangeListener> changeListeners = new Vector<ChangeListener>(); 285 public void addChangeListener(ChangeListener cl) 286 { 287 changeListeners.add(cl); 288 } 289 public void removeChangeListener(ChangeListener cl) 290 { 291 changeListeners.remove(cl); 292 } 293 private void fireChangeEvent() 294 { 295 for(ChangeListener cl: changeListeners) 296 { 297 cl.stateChanged(new ChangeEvent(this)); 298 } 299 } 300 301 302 public SentenceDictionary() 305 { 306 } 307 308 public Vector<Object > getVectorRepresentation() 309 { 310 Vector<Object > v = new Vector<Object >(); 311 v.addElement(4); v.addElement(language); Vector<Object > sentv = new Vector<Object >(); 314 v.addElement(sentv); for(Sentence s : allSentencesVector) 316 { 317 sentv.addElement(s.getVectorRepresentation()); 318 } 319 320 return v; 321 } 322 323 @SuppressWarnings ("unchecked") 324 public void createFromVectorRepresentation( List<Object > v ) 325 { 326 int version = (Integer ) v.get(0); 327 328 if(version==4) 329 { 330 language = (String ) v.get(1); 331 sentences.clear(); 332 Vector<Vector> sentv = (Vector<Vector>)v.get(2); 333 for(Vector sv: sentv) 334 { 335 Sentence s = new Sentence(); 336 s.createFromVectorRepresentation(sv); 337 allSentencesVector.add(s); 338 if(!s.getTranslation().equals("")) 339 { 340 sentences.put(s.getSentence(), s.getTranslation()); 341 } 342 343 } 345 } 346 347 else 348 { 349 throw new RuntimeException ("Bad version "+version); 350 } 351 } 352 353 354 355 public void saveToFile() throws Exception 356 { 357 if(language.compareToIgnoreCase("english")==0) return; 359 360 if(!isEditable) return; 361 362 File file = new File("SnowMailClient/Language/"+language+".translation"); 363 if(file.exists() && !file.canWrite()) 364 { 365 throw new Exception ("Language file "+language+" is read-only"); 366 } 367 368 System.out.println("saving dic "+file.getAbsolutePath()); 369 FileUtils.saveVectorToFile(file, this.getVectorRepresentation()); 370 } 371 372 373 374 375 378 public static String [] getInternalAvailableTranslations() 379 { 380 String [] internalLanguagesToLookFor = new String [] 381 { 382 Language.FRANCAIS, Language.DEUTSCH, Language.POLISH 383 }; 384 ClassLoader cl = SentenceDictionary.class.getClassLoader(); 385 if(cl==null) 386 { 387 return new String [0]; 388 } 389 390 Vector<String > foundLanguagesNames = new Vector<String >(); 391 for(int i=0; i<internalLanguagesToLookFor.length; i++) 392 { 393 InputStream is = null; 394 try 395 { 396 is = cl.getResourceAsStream("SnowMailClient/Language/" 397 + internalLanguagesToLookFor[i] + ".translation"); 398 if(is!=null) 399 { 400 foundLanguagesNames.addElement(internalLanguagesToLookFor[i]); 401 } 403 } 404 finally 405 { 406 if(is!=null) 407 { 408 try{is.close();} catch(Exception e){ e.printStackTrace();} 409 } 410 } 411 } 412 return foundLanguagesNames.toArray(new String [foundLanguagesNames.size()]); 413 } 414 415 416 417 public static SentenceDictionary readFromJarFile(String language) throws Exception 418 { 419 if(language.compareToIgnoreCase("english")==0) return null; 421 422 423 ClassLoader cl = SentenceDictionary.class.getClassLoader(); 424 if(cl==null) 425 { 426 System.out.println("Class loader is null for class SentenceDictionary."); 427 return null; 429 } 430 431 SentenceDictionary sd = new SentenceDictionary(language); 432 433 InputStream is = null; 434 try 435 { 436 is = cl.getResourceAsStream("SnowMailClient/Language/"+language+".translation"); 437 if(is==null) 438 { 439 System.out.println("ResourceAsStream is null for SnowMailClient/Language/"+language); 440 return null; 441 } 442 Vector<Object > content = FileUtils.loadVector(is); 443 sd.createFromVectorRepresentation(content); 444 sd.isEditable = false; 446 447 return sd; 448 } 449 finally 450 { 451 if(is!=null) 452 { 453 try{is.close();} catch(Exception e){ e.printStackTrace();} 454 } 455 } 456 457 } 458 459 460 public static SentenceDictionary ReadFromFile_ZIPPED(File file) throws Exception 461 { 462 463 SentenceDictionary sd = new SentenceDictionary("???"); 464 465 if(!file.canWrite()) sd.isEditable = false; 467 468 List<Object > rep = FileUtils.loadZippedVectorFromFile(file); 469 if(rep.size()==0) throw new Exception ("File "+file.getAbsolutePath()+" is empty"); 470 try 471 { 472 sd.createFromVectorRepresentation(rep); 473 } 474 catch(Exception e) 475 { 476 throw e; 477 } 478 479 return sd; 480 } 481 482 485 public static SentenceDictionary readFromFile(String language, boolean useEmbeddedIfFileNotFound) throws Exception 486 { 487 if(language.compareToIgnoreCase("english")==0) return null; 489 490 491 File file = new File("SnowMailClient/Language/"+language+".translation"); 492 493 System.out.println("loading dic "+file.getAbsolutePath()); 494 495 SentenceDictionary sd = new SentenceDictionary(language); 496 497 if(file.exists()) 498 { 499 if(!file.canWrite()) sd.isEditable = false; 501 502 Vector<Object > rep = FileUtils.loadVectorFromFile(file); 503 try 504 { 505 sd.createFromVectorRepresentation(rep); 506 } 507 catch(Exception e) 508 { 509 throw e; 510 } 511 } 514 else 515 { 516 System.out.println("Language not found in file "+file.getAbsolutePath()); 517 if(useEmbeddedIfFileNotFound) 519 { 520 SentenceDictionary sdj = readFromJarFile(language); 521 if(sdj!=null) 522 { 523 return sdj; 524 } 525 else 526 { 527 528 } 529 } 530 } 531 return sd; 532 } 533 534 public String getFileName() 535 { 536 File file = new File("SnowMailClient/Language/"+language+".translation"); 537 return file.getAbsolutePath(); 538 } 539 540 541 542 } | Popular Tags |