1 package org.apache.velocity.texen; 2 3 18 19 import java.io.File; 20 import java.io.InputStream; 21 import java.io.FileInputStream; 22 import java.io.BufferedInputStream; 23 import java.io.Writer; 24 import java.io.FileWriter; 25 import java.io.StringWriter; 26 import java.io.OutputStreamWriter; 27 import java.io.BufferedWriter; 28 import java.io.FileOutputStream; 29 30 import java.util.Enumeration; 31 import java.util.Hashtable; 32 import java.util.Iterator; 33 import java.util.Properties; 34 35 import org.apache.velocity.Template; 36 import org.apache.velocity.context.Context; 37 import org.apache.velocity.VelocityContext; 38 import org.apache.velocity.app.VelocityEngine; 39 40 47 public class Generator 48 { 49 52 public static final String OUTPUT_PATH = "output.path"; 53 54 57 public static final String TEMPLATE_PATH = "template.path"; 58 59 63 private static final String DEFAULT_TEXEN_PROPERTIES = 64 "org/apache/velocity/texen/defaults/texen.properties"; 65 66 69 private Properties props = new Properties(); 70 71 74 private Context controlContext; 75 76 83 private Hashtable writers = new Hashtable(); 84 85 90 private static Generator instance = new Generator(); 91 92 95 protected String outputEncoding; 96 97 101 protected String inputEncoding; 102 103 106 protected VelocityEngine ve; 107 108 111 private Generator() 112 { 113 setDefaultProps(); 114 } 115 116 121 public static Generator getInstance() 122 { 123 return instance; 124 } 125 126 129 public void setVelocityEngine(VelocityEngine ve) 130 { 131 this.ve = ve; 132 } 133 134 143 public Generator (String propFile) 144 { 145 try 146 { 147 BufferedInputStream bi = null; 148 try 149 { 150 bi = new BufferedInputStream (new FileInputStream (propFile)); 151 props.load (bi); 152 } 153 finally 154 { 155 if (bi != null) 156 { 157 bi.close(); 158 } 159 } 160 } 161 catch (Exception e) 162 { 163 166 setDefaultProps(); 167 } 168 } 169 170 176 public Generator (Properties props) 177 { 178 this.props = (Properties)props.clone(); 179 } 180 181 184 protected void setDefaultProps() 185 { 186 ClassLoader classLoader = VelocityEngine.class.getClassLoader(); 187 try 188 { 189 InputStream inputStream = null; 190 try 191 { 192 inputStream = classLoader.getResourceAsStream( 193 DEFAULT_TEXEN_PROPERTIES); 194 195 props.load( inputStream ); 196 } 197 finally 198 { 199 if (inputStream != null) 200 { 201 inputStream.close(); 202 } 203 } 204 } 205 catch (Exception ioe) 206 { 207 System.err.println("Cannot get default properties!"); 208 } 209 } 210 211 217 public void setTemplatePath(String templatePath) 218 { 219 props.put(TEMPLATE_PATH, templatePath); 220 } 221 222 227 public String getTemplatePath() 228 { 229 return props.getProperty(TEMPLATE_PATH); 230 } 231 232 238 public void setOutputPath(String outputPath) 239 { 240 props.put(OUTPUT_PATH, outputPath); 241 } 242 243 249 public String getOutputPath() 250 { 251 return props.getProperty(OUTPUT_PATH); 252 } 253 254 257 public void setOutputEncoding(String outputEncoding) 258 { 259 this.outputEncoding = outputEncoding; 260 } 261 262 265 public void setInputEncoding(String inputEncoding) 266 { 267 this.inputEncoding = inputEncoding; 268 } 269 270 276 public Writer getWriter(String path, String encoding) throws Exception { 277 Writer writer; 278 if (encoding == null || encoding.length() == 0 || encoding.equals("8859-1") || encoding.equals("8859_1")) { 279 writer = new FileWriter(path); 280 } 281 else { 282 writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path), encoding)); 283 } 284 return writer; 285 } 286 287 293 public Template getTemplate(String templateName, String encoding) throws Exception { 294 Template template; 295 if (encoding == null || encoding.length() == 0 || encoding.equals("8859-1") || encoding.equals("8859_1")) { 296 template = ve.getTemplate(templateName); 297 } 298 else { 299 template = ve.getTemplate(templateName, encoding); 300 } 301 return template; 302 } 303 304 312 public String parse (String inputTemplate, String outputFile) 313 throws Exception 314 { 315 return parse(inputTemplate, outputFile, null, null); 316 } 317 318 330 public String parse (String inputTemplate, 331 String outputFile, 332 String objectID, 333 Object object) 334 throws Exception 335 { 336 return parse(inputTemplate, null, outputFile, null, objectID, object); 337 } 338 352 public String parse (String inputTemplate, 353 String intputEncoding, 354 String outputFile, 355 String outputEncoding, 356 String objectID, 357 Object object) 358 throws Exception 359 { 360 if (objectID != null && object != null) 361 { 362 controlContext.put(objectID, object); 363 } 364 365 Template template = getTemplate(inputTemplate, inputEncoding != null ? inputEncoding : this.inputEncoding); 366 367 if (outputFile == null || outputFile.equals("")) 368 { 369 StringWriter sw = new StringWriter(); 370 template.merge (controlContext,sw); 371 return sw.toString(); 372 } 373 else 374 { 375 Writer writer = null; 376 377 if (writers.get(outputFile) == null) 378 { 379 383 writer = getWriter( 384 getOutputPath() + File.separator + outputFile, 385 outputEncoding != null ? outputEncoding : this.outputEncoding 386 ); 387 388 392 writers.put(outputFile, writer); 393 } 394 else 395 { 396 writer = (Writer) writers.get(outputFile); 397 } 398 399 VelocityContext vc = new VelocityContext( controlContext ); 400 template.merge (vc,writer); 401 402 405 return ""; 406 } 407 } 408 409 417 public String parse (String controlTemplate, Context controlContext) 418 throws Exception 419 { 420 this.controlContext = controlContext; 421 fillContextDefaults(this.controlContext); 422 fillContextProperties(this.controlContext); 423 424 Template template = getTemplate(controlTemplate, inputEncoding); 425 StringWriter sw = new StringWriter(); 426 template.merge (controlContext,sw); 427 428 return sw.toString(); 429 } 430 431 432 440 protected Context getContext (Hashtable objs) 441 { 442 fillContextHash (controlContext,objs); 443 return controlContext; 444 } 445 446 452 protected void fillContextHash (Context context, Hashtable objs) 453 { 454 Enumeration enum = objs.keys(); 455 while (enum.hasMoreElements()) 456 { 457 String key = enum.nextElement().toString(); 458 context.put (key, objs.get(key)); 459 } 460 } 461 462 467 protected void fillContextDefaults (Context context) 468 { 469 context.put ("generator", instance); 470 context.put ("outputDirectory", getOutputPath()); 471 } 472 473 480 protected void fillContextProperties (Context context) 481 { 482 Enumeration enum = props.propertyNames(); 483 484 while (enum.hasMoreElements()) 485 { 486 String nm = (String)enum.nextElement(); 487 if (nm.startsWith ("context.objects.")) 488 { 489 490 String contextObj = props.getProperty (nm); 491 int colon = nm.lastIndexOf ('.'); 492 String contextName = nm.substring (colon+1); 493 494 try 495 { 496 Class cls = Class.forName (contextObj); 497 Object o = cls.newInstance(); 498 context.put (contextName,o); 499 } 500 catch (Exception e) 501 { 502 e.printStackTrace(); 503 } 505 } 506 } 507 } 508 509 514 public void shutdown() 515 { 516 Iterator iterator = writers.values().iterator(); 517 518 while(iterator.hasNext()) 519 { 520 Writer writer = (Writer) iterator.next(); 521 522 try 523 { 524 writer.flush(); 525 writer.close(); 526 } 527 catch (Exception e) 528 { 529 530 } 531 } 532 writers.clear(); 534 } 535 } 536 | Popular Tags |