1 28 29 package org.objectweb.ccm.visitorIDL3.common; 30 31 38 39 public class Printer 40 extends fr.lifl.cim.jidlscript.JOSInterpreter 41 { 42 48 51 private PrintStreamManager the_stream_manager_; 52 53 56 private PrintStream stream_; 57 58 61 private org.objectweb.ccm.util.Table string_mappings_; 62 63 66 private org.objectweb.ccm.util.Vector file_mappings_; 67 68 71 private java.lang.String remaining_text_; 72 73 76 private int char_to_skip_; 77 78 81 private java.lang.String indentation_; 82 83 86 private char call_char_; 87 88 91 private char comment_char_; 92 93 96 private Generator generator_; 97 98 104 107 public 108 Printer(Generator gen, 109 char call_char, 110 char comment_char, 111 java.lang.String indentation) 112 throws fr.lifl.cim.jidlscript.lang.JISError 113 { 114 super(new java.lang.String [0]); 115 116 the_stream_manager_ = new PrintStreamManager(); 118 string_mappings_ = new org.objectweb.ccm.util.Table(); 119 file_mappings_ = new org.objectweb.ccm.util.Vector(); 120 remaining_text_ = ""; 121 char_to_skip_ = 0; 122 stream_ = the_stream_manager_.create("stdout", indentation); 123 setGenerator(gen); 124 indentation_ = indentation; 125 call_char_ = call_char; 126 comment_char_ = comment_char; 127 } 128 129 135 138 private void 139 print(java.lang.String text) 140 { 141 if (stream_==null) 142 return ; 143 144 int idx = text.indexOf("\\n"); 145 if (idx==-1) 146 remaining_text_ = remaining_text_ + text; 147 else 148 { 149 stream_.print(remaining_text_ + text.substring(0, idx) + '\n'); 150 remaining_text_ = ""; 151 if (idx+2<text.length()) 152 print(text.substring(idx+2, text.length())); 153 } 154 } 155 156 159 private void 160 analyze(java.lang.String id, 161 java.lang.String mapping) 162 { 163 org.objectweb.ccm.util.Vector mappings = new org.objectweb.ccm.util.Vector(); 164 org.objectweb.ccm.util.Vector to_eval = new org.objectweb.ccm.util.Vector(); 165 MapInfo info = null; 166 java.lang.String tmp = ""; 167 168 if (mapping.length()==0) 169 { 170 info = new MapInfo(mappings, to_eval); 171 string_mappings_.put(id.toUpperCase(), info); 172 return ; 173 } 174 175 int i = 0; 176 int j = mapping.indexOf(call_char_, i); 177 while (j!=-1) 178 { 179 if (i!=j) 180 { 181 tmp = mapping.substring(i, j); 182 mappings.add(tmp); 183 to_eval.add(new java.lang.Boolean (false)); 184 } 185 186 i = j+1; 187 if (i>=mapping.length()) 188 { 189 info = new MapInfo(mappings, to_eval); 190 string_mappings_.put(id.toUpperCase(), info); 191 return ; 192 } 193 j = mapping.indexOf(call_char_, i); 194 if (j==-1) 195 { 196 java.lang.String msg = 197 "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+ 198 " operation : printFromString\n"+ 199 " message : Missing " + call_char_ + " character after : "+ 200 mapping.substring(i)+" in : "+id; 201 System.err.println(msg); 203 } 204 205 if (i!=j) 206 { 207 tmp = mapping.substring(i, j); 208 mappings.add(tmp); 209 to_eval.add(new java.lang.Boolean (true)); 210 } 211 212 i = j+1; 213 if (i>=mapping.length()) 214 { 215 info = new MapInfo(mappings, to_eval); 216 string_mappings_.put(id.toUpperCase(), info); 217 return ; 218 } 219 j = mapping.indexOf(call_char_, i); 220 } 221 222 tmp = mapping.substring(i, mapping.length()); 223 mappings.add(tmp); 224 to_eval.add(new java.lang.Boolean (false)); 225 226 info = new MapInfo(mappings, to_eval); 227 string_mappings_.put(id.toUpperCase(), info); 228 } 229 230 233 private void 234 evaluate(MapInfo info, 235 java.lang.String [] aliases, 236 java.lang.Object [] objs) 237 { 238 java.lang.String [] mappings = 239 (java.lang.String [])info.mappings.toArray(new java.lang.String [0]); 240 java.lang.Boolean [] to_eval = 241 (java.lang.Boolean [])info.to_eval.toArray(new java.lang.Boolean [0]); 242 243 for (int i=0;i<mappings.length;i++) 244 { 245 if (to_eval[i].booleanValue()) 246 print(joshEval(aliases, objs, mappings[i])); 247 else if (char_to_skip_!=0) 248 { 249 print(mappings[i].substring(char_to_skip_)); 250 char_to_skip_ = 0; 251 } 252 else 253 print(mappings[i]); 254 } 255 } 256 257 260 private java.lang.String 261 joshEval(java.lang.String [] aliases, 262 java.lang.Object [] objs, 263 java.lang.String mapping) 264 { 265 fr.lifl.cim.jidlscript.lang.JISObject result = null; 266 try 267 { 268 for (int i=0;i<aliases.length;i++) 269 setAlias(aliases[i], objs[i]); 270 271 result = super.eval(mapping); 272 return ((fr.lifl.cim.jidlscript.java.JISJavaString)result).getValue(); 273 } 274 catch(java.lang.ClassCastException ex) 275 { 276 java.lang.String msg = 277 "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+ 278 " operation : joshEval\n"+ 279 " message : result of eval is not a string : "+ex.getMessage()+"\n"+ 280 " while visiting the following template :\n"+ 281 " "+mapping+"\n"; 282 System.err.println(msg); 284 return ""; 285 } 286 catch(fr.lifl.cim.jidlscript.lang.JISError ex) 287 { 288 java.lang.String msg = 289 "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+ 290 " operation : joshEval\n"+ 291 " message : an error occured while evaluating the following template :\n"+ 292 " "+mapping+"\n"+ 293 " reason : "+ex.exception.toString(); 294 System.err.println(msg); 296 ex.printStackTrace(); 297 return ""; 298 } 299 } 300 301 307 310 public final char 311 getCallChar() 312 { 313 return call_char_; 314 } 315 316 319 public final char 320 getCommentChar() 321 { 322 return comment_char_; 323 } 324 325 328 public final java.lang.String 329 getIndentation() 330 { 331 return indentation_; 332 } 333 334 337 public final Generator 338 getGenerator() 339 { 340 return generator_; 341 } 342 343 346 public final void 347 delete(int n) 348 { 349 if (remaining_text_.length()>n) 350 remaining_text_ = remaining_text_.substring(0, remaining_text_.length()-n); 351 else 352 remaining_text_ = ""; 353 } 354 355 358 public final void 359 skip(int n) 360 { 361 char_to_skip_ = n; 362 } 363 364 367 public final void 368 insert(int n) 369 { 370 int length = remaining_text_.length(); 371 java.lang.String tmp1 = remaining_text_.substring(0, length-n); 372 java.lang.String tmp2 = remaining_text_.substring(length-n, length); 373 remaining_text_ = tmp1 + "\n" + stream_.getTab() + tmp2; 374 } 375 376 379 public final void 380 loadFile(java.lang.String filename) 381 { 382 boolean already_parsed = file_mappings_.contains(filename); 383 if (already_parsed) 384 return ; 385 386 java.lang.String mapping = ""; 387 java.lang.String tmp = ""; 388 java.lang.String curr_id = ""; 389 try 390 { 391 java.io.BufferedReader file = new java.io.BufferedReader ( 392 new java.io.FileReader (generator_.getTemplateLocation()+"/"+filename)); 393 tmp = file.readLine(); 394 while (tmp!=null) 395 { 396 if ((tmp.length()>0) && (tmp.charAt(0)==comment_char_)) 397 { 398 if ((tmp.length()>2) && (tmp.substring(1, 3).toLowerCase().equals("id"))) 399 { 400 if (!mapping.equals("")) 401 analyze(curr_id, mapping); 402 403 mapping = ""; 404 curr_id = tmp.substring(4); 405 curr_id = curr_id.trim(); 406 } 407 } 408 else 409 mapping = mapping+modifyCurrentLine(tmp); 410 411 tmp = file.readLine(); 412 } 413 414 analyze(curr_id, mapping); 415 } 416 catch(java.io.FileNotFoundException ex) 417 { 418 java.lang.String msg = 419 "exception in class org.objectweb.ccm.visitorIDL3.Printer\n"+ 420 " operation : mapFromFile\n"+ 421 " message : "+ex.getMessage(); 422 System.err.println(msg); 424 } 425 catch(java.io.IOException ex) 426 { 427 java.lang.String msg = 428 "exception in class org.objectweb.ccm.visitorIDL3.Printer\n"+ 429 " operation : mapFromFile\n"+ 430 " message : "+ex.getMessage(); 431 System.err.println(msg); 433 } 434 435 file_mappings_.add(filename); 436 } 437 438 444 447 public void 448 setDirectory(java.lang.String dirname) 449 { 450 the_stream_manager_.setDirectory(dirname); 451 } 452 453 456 public java.lang.String 457 open(java.lang.String filename) 458 { 459 stream_ = the_stream_manager_.create(filename, indentation_); 460 return ""; 461 } 462 463 466 public java.lang.String 467 close() 468 { 469 the_stream_manager_.close(stream_.getName()); 470 stream_ = null; 471 return ""; 472 } 473 474 477 public java.lang.String 478 inc() 479 { 480 stream_.incDepth(); 481 return ""; 482 } 483 484 487 public java.lang.String 488 dec() 489 { 490 stream_.decDepth(); 491 return ""; 492 } 493 494 497 public java.lang.String 498 modifyCurrentLine(java.lang.String str) 499 { 500 return str; 501 } 502 503 509 512 public void 513 setAlias(java.lang.String alias, 514 java.lang.Object obj) 515 { 516 try 517 { 518 fr.lifl.cim.jidlscript.java.JISJavaClass jc = null; 519 fr.lifl.cim.jidlscript.java.JISJavaInstance ji = null; 520 java.lang.String itfname = obj.getClass().getName(); 521 522 if (itfname.equals("java.lang.String")) 523 ji = new fr.lifl.cim.jidlscript.java.JISJavaString((java.lang.String )obj); 524 else 525 { 526 jc = fr.lifl.cim.jidlscript.java.JISJavaClass.importClass( 527 new fr.lifl.cim.jidlscript.lang.JISString(itfname)); 528 ji = new fr.lifl.cim.jidlscript.java.JISJavaInstance(jc, obj); 529 } 530 531 fr.lifl.cim.jidlscript.lang.JISRootScope.root().insertScoped( 532 new fr.lifl.cim.jidlscript.lang.JISAlias(alias, ji)); 533 } 534 catch(fr.lifl.cim.jidlscript.lang.JISError ex) 535 { 536 java.lang.String msg = 537 "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+ 538 " operation : setAlias\n"+ 539 " message : "+ex.exception.toString(); 540 System.err.println(msg); 542 } 543 } 544 545 548 public void 549 setGenerator(Generator gen) 550 { 551 generator_ = gen; 552 553 setAlias("generator", gen); 555 } 556 557 560 public void 561 map(java.lang.String template_id, 562 java.lang.String [] aliases, 563 java.lang.Object [] objs) 564 { 565 java.lang.Object obj = string_mappings_.get(template_id); 566 if (obj==null) 567 { 568 java.lang.String msg = 569 "error in class org.objectweb.ccm.visitorIDL3.Printer\n"+ 570 " operation : map\n"+ 571 " message : Unknown mapping identifier : "+template_id+"\n"; 572 System.err.println(msg); 574 } 575 576 evaluate((MapInfo)obj, aliases, objs); 577 } 578 579 582 public void 583 printImmediately(java.lang.String text) 584 { 585 print(text); 586 } 587 } 588 589 590 593 class MapInfo 594 { 595 601 604 public org.objectweb.ccm.util.Vector mappings; 605 606 609 public org.objectweb.ccm.util.Vector to_eval; 610 611 617 620 protected 621 MapInfo(org.objectweb.ccm.util.Vector maps, 622 org.objectweb.ccm.util.Vector bools) 623 { 624 mappings = maps; 625 to_eval = bools; 626 } 627 } 628 | Popular Tags |