1 37 package org.webharvest.runtime.templaters; 38 39 import org.webharvest.runtime.scripting.ScriptEngine; 40 41 45 public class BaseTemplater { 46 47 public static String VAR_START = "${"; 48 public static String VAR_END = "}"; 49 50 public static String execute(String source, ScriptEngine scriptEngine) { 51 if (source == null) { 52 return source; 53 } 54 55 String result = ""; 56 57 int startIndex = source.indexOf(VAR_START); 58 int endIndex = -1; 59 60 while (startIndex >= 0 && startIndex < source.length()) { 61 result += source.substring(endIndex + 1, startIndex); 62 endIndex = source.indexOf(VAR_END, startIndex); 63 64 if (endIndex > startIndex) { 65 String expression = source.substring(startIndex + VAR_START.length(), endIndex); 66 Object resultObj = scriptEngine.eval(expression); 67 result += resultObj == null ? "" : resultObj.toString(); 68 } 69 70 startIndex = source.indexOf( VAR_START, Math.max(endIndex + VAR_END.length(), startIndex + 1) ); 71 } 72 73 result += source.substring(endIndex + 1); 74 75 return result; 76 } 77 78 } | Popular Tags |