1 15 package org.apache.tapestry.html; 16 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import org.apache.hivemind.ApplicationRuntimeException; 22 import org.apache.hivemind.Resource; 23 import org.apache.tapestry.AbstractComponent; 24 import org.apache.tapestry.IBinding; 25 import org.apache.tapestry.IEngine; 26 import org.apache.tapestry.IMarkupWriter; 27 import org.apache.tapestry.IRequestCycle; 28 import org.apache.tapestry.IScript; 29 import org.apache.tapestry.PageRenderSupport; 30 import org.apache.tapestry.Tapestry; 31 import org.apache.tapestry.TapestryUtils; 32 import org.apache.tapestry.engine.IScriptSource; 33 34 41 42 public abstract class Script extends AbstractComponent 43 { 44 49 50 private Map _symbols; 51 52 58 59 private Map getInputSymbols() 60 { 61 Map result = new HashMap (); 62 63 Map baseSymbols = getBaseSymbols(); 64 65 if (baseSymbols != null) 66 result.putAll(baseSymbols); 67 68 72 Iterator i = getBindingNames().iterator(); 73 while (i.hasNext()) 74 { 75 String bindingName = (String ) i.next(); 76 77 79 if (getSpecification().getParameter(bindingName) != null) 80 continue; 81 82 IBinding binding = getBinding(bindingName); 83 84 Object value = binding.getObject(); 85 86 result.put(bindingName, value); 87 } 88 89 return result; 90 } 91 92 95 96 private IScript getParsedScript(IRequestCycle cycle) 97 { 98 String scriptPath = getScriptPath(); 99 100 if (scriptPath == null) 101 throw Tapestry.createRequiredParameterException(this, "scriptPath"); 102 103 IEngine engine = cycle.getEngine(); 104 IScriptSource source = engine.getScriptSource(); 105 106 109 Resource rootLocation = getContainer().getSpecification().getSpecificationLocation(); 110 Resource scriptLocation = rootLocation.getRelativeResource(scriptPath); 111 112 try 113 { 114 return source.getScript(scriptLocation); 115 } 116 catch (RuntimeException ex) 117 { 118 throw new ApplicationRuntimeException(ex.getMessage(), this, null, ex); 119 } 120 121 } 122 123 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 124 { 125 if (!cycle.isRewinding()) 126 { 127 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this); 128 129 _symbols = getInputSymbols(); 130 131 getParsedScript(cycle).execute(cycle, pageRenderSupport, _symbols); 132 } 133 134 renderBody(writer, cycle); 136 } 137 138 public abstract String getScriptPath(); 139 140 142 public abstract Map getBaseSymbols(); 143 144 150 151 public Map getSymbols() 152 { 153 return _symbols; 154 } 155 156 protected void cleanupAfterRender(IRequestCycle cycle) 157 { 158 _symbols = null; 159 160 super.cleanupAfterRender(cycle); 161 } 162 163 } | Popular Tags |