1 26 27 package org.objectweb.openccm.generator.common.lib; 28 29 33 public class Generator 34 implements org.objectweb.openccm.generator.common.api.Generator 35 { 36 37 43 46 protected String ressource_path_; 47 48 51 protected String template_path_; 52 53 56 protected org.apache.velocity.app.VelocityEngine engine_; 57 58 61 protected org.apache.velocity.context.Context context_; 62 63 66 protected java.util.Hashtable streams_; 67 68 71 protected java.util.Hashtable filenames_; 72 73 76 protected java.io.Writer stream_; 77 78 81 protected String log_; 82 83 86 protected boolean log_enabled_; 87 88 91 protected org.objectweb.openccm.generator.common.lib.Indentor ind_; 92 93 99 public Generator() 100 { 101 engine_ = new org.apache.velocity.app.VelocityEngine(); 102 ressource_path_ = "."; 103 template_path_ = ""; 104 streams_ = new java.util.Hashtable (); 105 filenames_ = new java.util.Hashtable (); 106 log_ = "velocityError.log"; 107 log_enabled_ = true; 108 ind_ = new org.objectweb.openccm.generator.common.lib.Indentor(); 109 } 110 111 117 125 private void 126 map(String macro_name, java.lang.String [] params, java.io.Writer stream) 127 { 128 try { 129 engine_.invokeVelocimacro(macro_name, log_, params, context_, stream); 130 stream.flush(); 131 } catch (Exception e ) { 132 throw new Error ("Problem invoking macro : " + e ); 133 } 134 } 135 136 142 private void 143 map_file(String template_name, java.io.Writer stream) 144 { 145 try { 146 engine_.mergeTemplate(template_path_ + template_name, context_, stream); 147 } catch (java.io.FileNotFoundException ex) { 148 throw new Error ("Problem with io : " + ex ); 149 } catch (SecurityException ex) { 150 throw new Error ("Problem with io : " + ex ); 151 } catch (Exception e ) { 152 throw new Error ("Problem merging template : " + e ); 153 } 154 } 155 156 162 private void 163 print(String text, java.io.Writer stream) 164 { 165 try { 166 stream.write(text); 167 stream.flush(); 168 } catch (java.io.IOException e ) { 169 throw new Error ("Problem with I/O : " + e ); 170 } 171 } 172 173 179 184 public void 185 setRessourcePath(java.util.List list) 186 { 187 if ( (list!=null) && (list.size()>0) ) 188 { 189 String path = null; 190 ressource_path_ = (String ) list.get(0); 191 192 for (int i=1; i<list.size(); i++) 193 { 194 path = (String ) list.get(i); 195 ressource_path_ += "," + path; 196 } 197 } 198 } 199 200 205 public void 206 setLibrary(java.util.List list) 207 { 208 if ( (list!=null) && (list.size()>0) ) 209 { 210 String name = (String ) list.get(0); 211 212 for (int i=1; i<list.size(); i++) 213 { 214 name += "," + (String ) list.get(i); 215 } 216 try{ 217 engine_.setProperty(org.apache.velocity.app.VelocityEngine.VM_LIBRARY, name); 218 }catch(Exception e){ 219 e.printStackTrace(); 220 }; 221 } 222 } 223 224 229 public void 230 setContext(org.apache.velocity.context.Context context) 231 { 232 context_ = context; 233 } 234 235 238 public void 239 disableLog() 240 { 241 log_enabled_ = false; 242 } 243 244 247 public void 248 init() 249 { 250 try{ 252 if (!log_enabled_) 253 { 254 engine_.setProperty( org.apache.velocity.app.VelocityEngine.RUNTIME_LOG_LOGSYSTEM_CLASS, 255 "org.apache.velocity.runtime.log.NullLogSystem" ); 256 } 257 engine_.setProperty(org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER, "classpath"); 258 engine_.setProperty("classpath." + org.apache.velocity.app.VelocityEngine.RESOURCE_LOADER + ".class", 259 org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader.class.getName()); 260 engine_.init(); 261 }catch(Exception e){ 262 e.printStackTrace(); 263 }; 264 265 setContext(new org.apache.velocity.VelocityContext()); 267 } 268 269 275 public void 276 open(String filename, String id) 277 { 278 try { 280 java.io.File f = new java.io.File (filename); 281 java.io.FileOutputStream file = new java.io.FileOutputStream (f.getAbsolutePath()); 282 stream_ = new java.io.PrintWriter (file); streams_.put(id, stream_); 286 filenames_.put(id, filename); 288 } catch (Exception e ) { 289 throw new Error ("Problem with I/O : " + e ); 290 } 291 } 292 293 300 public void 301 close(String id, boolean indent) 302 { 303 java.io.PrintWriter stream = (java.io.PrintWriter ) streams_.remove(id); 304 String name = (String ) filenames_.remove(id); 305 306 if (stream == null) 307 return; 308 309 try { 310 stream_.close(); 311 if (indent) 312 ind_.indent(name); 313 } catch (java.io.IOException ex) { 314 throw new Error ("Problem with I/O : " + ex ); 315 } 316 } 317 318 323 public void 324 close(String id) 325 { 326 close(id, true); 327 } 328 329 334 public void 335 switchToFile(String id) 336 { 337 java.io.PrintWriter stream = null; 338 339 stream = (java.io.PrintWriter ) streams_.get(id); 340 if (stream != null) 342 stream_ = stream; 343 } 344 345 352 public Object 353 get(String key) 354 { 355 return context_.get(key); 356 } 357 358 364 public void 365 put(String key, Object value) 366 { 367 context_.put(key, value); 368 } 369 370 375 public void 376 remove(Object key) 377 { 378 context_.remove(key); 379 } 380 381 386 public void 387 map_file(String template_name) 388 { 389 if (stream_ != null) 390 map_file(template_name, stream_); 391 else 392 throw new Error ("No default output file available.\n"); 393 } 394 395 401 public void 402 map_file(String template_name, String id) 403 { 404 java.io.PrintWriter stream = (java.io.PrintWriter ) streams_.get(id); 405 if (stream != null) 406 map_file(template_name, stream); 407 else 408 throw new Error ("Not a valid ID.\n"); 409 } 410 411 416 public void 417 map(String macro_name) 418 { 419 if (stream_ != null) 420 map(macro_name, new String [0], stream_); 421 else 422 throw new Error ("No default output file available.\n"); 423 } 424 425 431 public void 432 map(String macro_name, org.apache.velocity.context.Context context) 433 { 434 if (stream_ != null) 435 { 436 try { 437 engine_.invokeVelocimacro(macro_name, log_, new String [0], context, stream_); 438 stream_.flush(); 439 } catch (Exception e ) { 440 throw new Error ("Problem invoking macro : " + e ); 441 } 442 } 443 else 444 { 445 throw new Error ("No default output file available.\n"); 446 } 447 } 448 449 455 public void 456 map(String macro_name, String id) 457 { 458 java.io.PrintWriter stream = (java.io.PrintWriter ) streams_.get(id); 459 if (stream != null) 460 map(macro_name, new String [0], stream); 461 else 462 throw new Error ("Not a valid ID.\n"); 463 } 464 465 470 public void 471 print(String text) 472 { 473 if (stream_ != null) 474 print(text, stream_); 475 else 476 throw new Error ("No default output file available.\n"); 477 } 478 479 485 public void 486 print(String text, String id) 487 { 488 java.io.PrintWriter stream = (java.io.PrintWriter ) streams_.get(id); 489 if (stream != null) 490 print(text, stream); 491 else 492 throw new Error ("No default output file available.\n"); 493 } 494 495 502 public String 503 print_spaces(int nb) 504 { 505 String res = ""; 506 507 for (int i=0; i<nb; i++) 508 res += " "; 509 return res; 510 } 511 } 512 | Popular Tags |