1 package org.sapia.soto.state.cocoon.view; 2 3 import org.apache.commons.jelly.Jelly; 4 import org.apache.commons.jelly.JellyContext; 5 import org.apache.commons.jelly.Script; 6 import org.apache.commons.jelly.XMLOutput; 7 import org.apache.commons.lang.ClassUtils; 8 9 import org.sapia.soto.state.Result; 10 import org.sapia.soto.state.StateRuntimeException; 11 import org.sapia.soto.state.Step; 12 import org.sapia.soto.state.cocoon.CocoonContext; 13 import org.sapia.soto.state.xml.StyleStep; 14 import org.sapia.soto.state.xml.TransformState; 15 import org.sapia.soto.state.xml.XMLContext; 16 import org.sapia.soto.util.Param; 17 import org.sapia.soto.util.Resource; 18 import org.sapia.soto.util.ResourceHandler; 19 import org.sapia.soto.util.Utils; 20 21 import org.sapia.util.xml.confix.ConfigurationException; 22 23 import org.xml.sax.ContentHandler ; 24 25 import java.io.IOException ; 26 27 import java.net.URL ; 28 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.xml.transform.TransformerConfigurationException ; 34 35 69 public class JellyView extends TransformState implements Step { 70 private Script _jelly; 71 private String _uri; 72 private String _id; 73 private Resource _res; 74 private long _lastModified; 75 private List _params = new ArrayList (); 76 77 80 public String getName() { 81 return ClassUtils.getShortClassName(getClass()); 82 } 83 84 89 public void setSrc(String uri) throws IOException , Exception { 90 _uri = uri; 91 92 ResourceHandler handler = (ResourceHandler) env().getResourceHandlerFor(Utils.chopScheme( 93 uri)); 94 _res = handler.getResourceObject(uri); 95 96 Jelly j = new Jelly(); 97 _lastModified = _res.lastModified(); 98 j.setUrl(new URL (_res.getURI())); 99 _jelly = j.compileScript(); 100 } 101 102 108 public Param createParam(){ 109 Param p = new Param(); 110 _params.add(p); 111 return p; 112 } 113 114 115 118 protected void process(Result res, ContentHandler handler) { 119 XMLContext ctx = (XMLContext) res.getContext(); 120 121 Object model = null; 122 123 if (ctx.hasCurrentObject()) { 124 model = ctx.currentObject(); 125 } 126 127 try { 128 execute(model, ctx.getViewParams(), handler); 129 } catch (Throwable t) { 130 throw new StateRuntimeException("Could not run Jelly state: " + getId() + 131 " for script: " + _uri, t); 132 } 133 } 134 135 protected void execute(Object model, Map viewParams, ContentHandler handler) 136 throws Throwable { 137 if (_jelly == null) { 138 throw new IllegalStateException ("Jelly script not set on Jelly view"); 139 } 140 141 if (_lastModified != _res.lastModified()) { 142 reload(); 143 } 144 145 JellyContext jelly = new JellyContext(); 146 JellyUtils.copyParamsTo(jelly, viewParams); 147 148 jelly.setVariable(CocoonContext.MODEL_KEY, model); 149 150 Param p; 151 for(int i = 0; i < _params.size(); i++){ 152 p = (Param)_params.get(i); 153 if(p.getName() != null && p.getValue() != null){ 154 jelly.setVariable(p.getName(), p.getValue()); 155 } 156 } 157 158 XMLOutput output = new XMLOutput(handler); 159 output.startDocument(); 160 _jelly.run(jelly, output); 161 output.endDocument(); 162 } 163 164 167 public void handleObject(String name, Object obj) 168 throws ConfigurationException { 169 if (obj instanceof XslSource) { 170 try{ 171 XslSource xsl = (XslSource)obj; 172 StyleStep step = new StyleStep(); 173 step.setEnv(env()); 174 step.setSrc(xsl.getUri()); 175 }catch(TransformerConfigurationException e){ 176 throw new ConfigurationException("Could not instantiate StyleStep", e); 177 }catch(IOException e){ 178 throw new ConfigurationException("Could not inialize stylesheet", e); 179 } 180 } 181 else{ 182 super.handleObject(name, obj); 183 } 184 } 185 186 private synchronized void reload() throws Exception { 187 if (_res.lastModified() != _lastModified) { 188 Jelly j = new Jelly(); 189 j.setUrl(new URL (_res.getURI())); 190 _jelly = j.compileScript(); 191 } 192 193 _lastModified = _res.lastModified(); 194 } 195 } 196 | Popular Tags |