1 package SnowMailClient.Language; 2 3 4 import java.io.*; 5 import java.util.*; 6 import java.util.zip.*; 7 8 9 import snow.utils.storage.*; 10 11 12 17 public final class SourceSentencesParser 18 { 19 public static final String START = "Language"+".translate("; 21 private SourceSentencesParser() 22 { 23 } 24 25 26 31 public static String parseWholeSourceAndSaveInFile(File sourceDir, StringBuffer warningBuffer) 32 { 33 SourceSentencesParser ssp = new SourceSentencesParser(); 35 Vector<Sentence> collected = ssp.collectStringsToTranslateRecurse(sourceDir, sourceDir.getAbsolutePath(), warningBuffer); 36 37 System.out.println(""+collected.size()+" sentences found in source"); 38 39 if(collected.size()==0) 40 { 41 String msg = "Zero sentences to translate found in "+sourceDir.getAbsolutePath()+"\nStopping without saving."; 42 System.out.println(msg); 44 return msg; 45 } 46 47 Vector<Object > v = new Vector<Object >(); 49 for(int i=0;i<collected.size(); i++) 50 { 51 try 52 { 53 Sentence se = collected.elementAt(i); 54 v.add(se.getVectorRepresentation()); 55 } 56 catch(Exception e) 57 { 58 e.printStackTrace(); 59 } 60 } 61 62 File out = new File("SnowMailClient/Language/english_sentences.vec"); 63 64 try 65 { 66 FileUtils.saveVectorToFile(out, v); 67 Vector<Sentence> test = readEnglishSourceCodeSentencesFromFile(); 68 if(test==null) throw new Exception ("XXX"); 69 } 70 catch(Exception e) 71 { 72 e.printStackTrace(); 73 } 74 75 return ""+collected.size()+" sentences parsed and stored."; 76 } 77 78 79 81 @SuppressWarnings ("unchecked") 82 public static Vector<Sentence> readEnglishSourceCodeSentencesFromFile() 83 { 84 Vector<Sentence> sentences = new Vector<Sentence>(); 85 86 File in = new File("SnowMailClient/Language/english_sentences.vec"); 87 if (!in.exists()) return sentences; 88 89 try 90 { 91 Vector<Object > v = FileUtils.loadVectorFromFile(in); 92 for(int i=0;i<v.size();i++) 93 { 94 Vector sv = (Vector) v.elementAt(i); 95 Sentence se = new Sentence(); 96 se.createFromVectorRepresentation(sv); 97 sentences.addElement(se); 98 } 99 } 100 catch(Exception e) 101 { 102 e.printStackTrace(); 103 } 104 105 return sentences; 106 } 107 108 112 @SuppressWarnings ("unchecked") 113 public static Vector<Sentence> readEnglishSentencesVectorFromJarFile() throws Exception 114 { 115 Vector<Sentence> sentences = new Vector<Sentence>(); 116 ClassLoader cl = SentenceDictionary.class.getClassLoader(); 117 if(cl==null) 118 { 119 System.out.println("Class loader is null in ReadEnglishSentencesVectorFromJarFile "); 120 return sentences; } 123 124 InputStream is = null; 125 try 126 { 127 is = cl.getResourceAsStream("SnowMailClient/Language/english_sentences.vec"); 128 if(is==null) 129 { 130 System.out.println("ResourceAsStream is null for english_sentences.vec"); 131 return sentences; 132 } 133 Vector<Object > v = FileUtils.loadVector(is); 134 135 136 for(int i=0;i<v.size();i++) 137 { 138 Vector<Object > sv = (Vector<Object >) v.elementAt(i); 139 Sentence se = new Sentence(); 140 se.createFromVectorRepresentation(sv); 141 sentences.addElement(se); 142 } 143 144 145 return sentences; 146 } 147 finally 148 { 149 if(is!=null) 150 { 151 try{is.close();} catch(Exception e){ e.printStackTrace();} 152 } 153 } 154 } 155 156 157 158 159 164 public Vector<Sentence> collectStringsToTranslateRecurse(File base, String baseString, StringBuffer warningBuffer) 165 { 166 Vector<Sentence> sentences = new Vector<Sentence>(); 167 168 170 if(base.isFile()) 171 { 172 if( base.getName().endsWith(".java")) 173 { 174 sentences.addAll(collectStringsToTranslate(base, baseString, warningBuffer)); 175 } 176 } 177 178 else if(base.isDirectory()) 179 { 180 File[] files = base.listFiles(); 181 for(int i=0;i<files.length;i++) 182 { 183 sentences.addAll(collectStringsToTranslateRecurse(files[i], baseString, warningBuffer)); 184 } 185 } 186 return sentences; 187 } 188 189 190 195 private String interpretSourceStrings(final String sourceSentence) 196 { 197 String sent = sourceSentence; 198 final char BS = '\\'; 199 200 int pos = -1; 201 202 while(true) 203 { 204 pos = sent.indexOf(BS, pos+1); if(pos==-1) return sent; 207 if(pos+1 >= sent.length()) return sent; 209 char cp1 = sent.charAt(pos+1); 211 213 if(cp1=='"') sent = sent.substring(0,pos)+"\""+sent.substring(pos+2); 214 else if(cp1=='\'') sent = sent.substring(0,pos)+"'"+ sent.substring(pos+2); 215 else if(cp1=='n') sent = sent.substring(0,pos)+"\n"+sent.substring(pos+2); 216 else if(cp1=='r') sent = sent.substring(0,pos)+"\r"+sent.substring(pos+2); 217 else if(cp1=='t') sent = sent.substring(0,pos)+"\t"+sent.substring(pos+2); 218 else if(cp1=='\\') sent = sent.substring(0,pos)+"\\"+sent.substring(pos+2); 219 else if(cp1=='u') 220 { 221 223 if(pos+6 >= sent.length()) return sent; 225 String unic = sent.substring(pos+2, pos+6); 226 try 227 { 228 int code = Integer.parseInt(unic); 230 231 sent = sent.substring(0,pos)+((char) code)+sent.substring(pos+6); 232 } 233 catch(NumberFormatException e) 234 { 235 System.out.println("Cannot interpret unicode sequence in "+sent); 237 return sent; 238 } 239 } 240 else 241 { 242 System.out.println("Cannot interpret escape sequence in "+ sent); 243 } 244 } 245 } 246 247 248 251 public Vector<Sentence> collectStringsToTranslate(File file, String basePath, StringBuffer warningBuffer) 252 { 253 String relativeClassPath = file.getAbsolutePath().substring(basePath.length()+1); 254 if(relativeClassPath.length()==0) relativeClassPath = file.getAbsolutePath(); 256 257 258 Vector<Sentence> sentences = new Vector<Sentence>(); 259 FileInputStream fis = null; 260 StringBuffer sb = new StringBuffer (); 261 try 262 { 263 fis = new FileInputStream(file); 264 byte[] buffer = new byte[256]; 265 int read = 0; 266 while((read=fis.read(buffer))!=-1) 267 { 268 sb.append(new String (buffer,0,read)); 269 } 270 } 271 catch(IOException e) 272 { 273 e.printStackTrace(); 274 } 275 finally 276 { 277 if(fis!=null) try{fis.close();} catch(Exception ee) { ee.printStackTrace();} 278 } 279 280 String text = sb.toString(); 281 int startLength = START.length(); 282 283 286 int pos = text.indexOf(START); 287 while(pos!=-1) 288 { 289 int linePos = getNumberOfReturnUpToPosition(text, pos); 290 if(text.charAt( pos-1)=='"') 291 { 292 System.out.println("***************************************************** Found"); 294 } 295 296 Sentence sent = this.parseSentence(text, pos+startLength, relativeClassPath, linePos); 297 if(sent.getEndPosition()==-1) 298 { 299 String relativeJavaPath = convertSystemPathToJavaPath(file.getAbsolutePath().substring(basePath.length()+1)); 301 302 String msg = "Language.Throwable: The sentence should be HARDCODED in Language.translate_() " 305 + "\n\tat "+relativeJavaPath+" ("+file.getName()+":"+linePos+")\r\n"; 306 307 System.out.println(msg); 308 warningBuffer.append("\n"+msg); 309 310 pos = text.indexOf(START, pos+startLength); 312 313 } 314 else 315 { 316 317 sentences.addElement(sent); 318 319 try 321 { 322 int na = Common.getNumberOfParametersInSentence(sent.getSentence()); 323 } 324 catch(Exception exc) 325 { 326 String msg = "java.lang.Throwable: Bad parameter syntax "+exc.getMessage() 327 + "\n\tat "+Common.convertPathToJavaClassPathSyntax(relativeClassPath)+" ("+file.getName()+":"+linePos+")\r\n"; 328 329 warningBuffer.append( "\nBad parameters in sentence="+sent.getSentence()); 330 warningBuffer.append( "\n"+msg); 331 } 332 333 pos = text.indexOf(START, sent.getEndPosition()); 335 336 } 337 338 339 } 340 return sentences; 341 } 342 343 344 347 private Sentence parseSentence(String text, int from, String className, int line) 348 { 349 int start = text.indexOf("\"", from); 350 if(start==-1) 351 { 352 return new Sentence("Error: no opening \" in sentence", className, line, -1); 353 } 354 int end = start; 356 while(true) 357 { 358 end = text.indexOf("\"",end+1); 359 if(end==-1) 360 { 361 return new Sentence("Error: no ending \" in sentence", className, line, -1); 362 } 363 if(text.charAt(end-1)!='\\') 364 { 365 break; 366 } } 368 369 String sent = text.substring(start+1, end); 370 sent = interpretSourceStrings(sent); 372 373 int posPlus = text.indexOf('+', end); 375 int posComa = text.indexOf(',', end); 376 int posEndBracket = text.indexOf(')', end); 377 int posEnd = posEndBracket; if(posComa>=0 && posComa<posEndBracket) posEnd = posComa; 379 380 if(posPlus>=0 && posPlus<posEnd) 381 { 382 Sentence s2 = parseSentence(text.substring(posPlus+1, text.length() ), 0, "", -1); 385 sent += s2.getSentence(); 387 end += s2.getEndPosition(); 388 389 } 390 391 392 return new Sentence(sent, className, line, end); 394 } 395 396 399 public static int getNumberOfReturnUpToPosition(String content, int position) 400 { 401 int n = 1; 403 int retPos = -1; 404 while( (retPos = content.indexOf("\n", retPos+1)) != -1) 405 { 406 if(retPos>position) break; 407 n++; 408 } 409 return n; 410 } 411 412 414 public static String convertSystemPathToJavaPath(String sysPath) 415 { 416 String s = sysPath.replace('\\','.'); 417 s = s.replace('/','.'); 418 return s; 419 } 420 421 422 423 424 public static void main(String [] a) 425 { 426 StringBuffer warningBuffer = new StringBuffer (); 427 428 System.out.println( 429 430 parseWholeSourceAndSaveInFile(new File("c:/proj/mail/client/src"), warningBuffer) 434 ); 435 if(warningBuffer.length()>0) 436 { 437 System.out.println("Warnings:"+warningBuffer.toString()); 438 } 439 440 Language.translate("ceci est un autre " 443 +" test"); 444 } 449 450 451 } | Popular Tags |