Hiya, is there a cleaner way of doing the following?  I still want the buffer 
coded in the .java file, I've not looked at things like velocity as they seem 
overkill for simple things, the below works, but was wondering if it could be 
cleaned up in a better way?

  ....

  // Create a string buffer with content, and variables.
  StringBuffer buf = new StringBuffer();
  buf.append("public void set$entity( String $entity ) {\n");
  buf.append("\t this.$entity = $entity;\n");
  buf.append("\t this.propertyList.add( $entity ) \n");
  buf.append("}\n");
  String string = buf.toString();

  // Assign variables to map, could be many depending on template...
  HashMap map = new HashMap();
  map.put("entity", entity.getName());

  string = evalTemplate( string, map );

  // Send the translated string out through a PrintWriter
  out.println( string );

  ....

  // For each key in the map, replace
  public String evalTemplate( String template, HashMap vars )
  {
    String eval = template;
    it = map.keySet().iterator();
    while ( it.hasNext() )
    {
      String key = (String) it.next();
      eval = Pattern.compile( "\\$"+key ).matcher( eval ).replaceAll(
        (String) map.get(key) );
    }
    return eval;
  }


To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to