1 5 package xdoclet.modules.apache; 6 7 import java.util.Properties ; 8 import org.apache.commons.logging.Log; 9 import xdoclet.XDocletException; 10 11 import xdoclet.tagshandler.AbstractProgramElementTagsHandler; 12 import xdoclet.template.TemplateEngine; 13 import xdoclet.template.TemplateException; 14 import xdoclet.util.LogUtil; 15 16 19 public abstract class ScriptEngineTagHandler extends AbstractProgramElementTagsHandler 20 { 21 protected final String XDTSectionStart = "<XDt>"; 23 protected final String XDTSectionEnd = "</XDt>"; 25 26 34 protected final String getSubTemplateVariable(SubTemplateEngine subengine, Properties attributes) throws XDocletException 35 { 36 String vname = attributes.getProperty("name"); 37 38 if (vname == null) { 39 throw new XDocletException("Missing name property the name of the Velocity variable!"); 40 } 41 42 Object value = subengine.getVariable(vname); 43 44 if (value == null) { 45 String defaultValue = attributes.getProperty("default", ""); 46 47 return defaultValue; 48 } 49 return value.toString(); 50 } 51 52 59 protected final void generate(SubTemplateEngine subengine, String template, Properties attributes) throws TemplateException 60 { 61 if ("yes".equalsIgnoreCase(attributes.getProperty("disable"))) { 62 return; 63 } 64 65 fillVariables(subengine); 66 67 String result = subengine.generate(template, attributes); 68 Log log = LogUtil.getLog(ScriptEngineTagHandler.class, "generate"); 69 70 log.debug("Subengine generated results:" + result); 71 72 StringBuffer output = new StringBuffer (result); 73 74 if (!"yes".equalsIgnoreCase(attributes.getProperty("silent"))) { 76 TemplateEngine engine = getEngine(); 77 78 escapeResults(engine, output); 79 engine.print(output.toString()); 80 } 81 } 82 83 84 90 protected final void fillVariables(SubTemplateEngine templateEngine) throws XDocletException 91 { 92 Log log = LogUtil.getLog(ScriptEngineTagHandler.class, "fillVariables"); 93 94 log.debug("fillVariables() called"); 95 try { 96 templateEngine.setVariable("tagHandler", this); 97 templateEngine.setVariable("currentPackage", getCurrentPackage()); 98 templateEngine.setVariable("currentClass", getCurrentClass()); 99 templateEngine.setVariable("currentMethod", getCurrentMethod()); 100 templateEngine.setVariable("currentConstructor", getCurrentConstructor()); 101 templateEngine.setVariable("currentField", getCurrentField()); 102 templateEngine.setVariable("currentClassTag", getCurrentClassTag()); 103 templateEngine.setVariable("currentFieldTag", getCurrentFieldTag()); 104 templateEngine.setVariable("currentMethodTag", getCurrentMethodTag()); 105 } 106 catch (Exception ex) { 107 log.error("Exception when setting variables", ex); 108 throw new XDocletException(ex, "Exception when setting variables"); 109 } 110 } 111 112 120 protected final void escapeResults(TemplateEngine engine, StringBuffer results) throws TemplateException 121 { 122 Log log = LogUtil.getLog(ScriptEngineTagHandler.class, "escapeResults"); 123 124 while (true) { 126 int startidx = results.indexOf(XDTSectionStart); 128 int endidx = results.indexOf(XDTSectionEnd); 129 130 boolean templatefound = (startidx >= 0 && endidx >= 0); 131 132 if (!templatefound) 133 return; 134 135 String subXDtTemplate = results.substring(startidx + XDTSectionStart.length(), endidx); 137 138 log.debug("subTemplate found:<" + subXDtTemplate + ">"); 139 140 results.delete(startidx, endidx + XDTSectionEnd.length()); 142 log.debug("Results after XDt section removed:<" + results.toString() + ">"); 143 144 String subTemplateOutput = engine.outputOf(subXDtTemplate); 146 147 log.debug("Generated subTempalte output:" + subTemplateOutput); 148 149 results.insert(startidx, subTemplateOutput); 151 } 152 } 153 } 154 | Popular Tags |