| 1 package org.sapia.soto.state.xml; 2 3 import org.apache.commons.lang.ClassUtils; 4 import org.sapia.soto.Env; 5 import org.sapia.soto.EnvAware; 6 import org.sapia.soto.config.ThreadState; 7 import org.sapia.soto.state.Result; 8 import org.sapia.soto.state.Step; 9 import org.sapia.soto.util.Resource; 10 import org.sapia.soto.util.ResourceHandler; 11 import org.sapia.soto.util.Utils; 12 13 import org.sapia.util.text.TemplateContextIF; 14 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 18 import java.util.Iterator ; 19 import java.util.Map ; 20 21 import javax.xml.transform.Templates ; 22 import javax.xml.transform.TransformerConfigurationException ; 23 import javax.xml.transform.TransformerFactory ; 24 import javax.xml.transform.sax.SAXTransformerFactory ; 25 import javax.xml.transform.sax.TransformerHandler ; 26 import javax.xml.transform.stream.StreamSource ; 27 28 29 44 public class StyleStep implements EnvAware, Step { 45 private Templates _templ; 46 private SAXTransformerFactory _fac; 47 private Resource _file; 48 private Env _env; 49 private long _lastModified = System.currentTimeMillis(); 50 51 public StyleStep() throws TransformerConfigurationException { 52 _fac = (SAXTransformerFactory ) TransformerFactory.newInstance(); 53 } 54 55 protected StyleStep(SAXTransformerFactory fac) { 56 _fac = fac; 57 } 58 59 62 public String getName() { 63 return ClassUtils.getShortClassName(getClass()); 64 } 65 66 71 public void setSrc(String uri) 72 throws IOException , TransformerConfigurationException { 73 ResourceHandler handler = (ResourceHandler) _env.getResourceHandlerFor(uri); 74 _file = handler.getResourceObject(uri); 75 _templ = _fac.newTemplates(new StreamSource (process( 76 _file.getInputStream(), uri))); 77 } 78 79 80 83 public void execute(Result res) { 84 XMLContext ctx; 85 try{ 86 ctx = (XMLContext)res.getContext(); 87 }catch(ClassCastException e){ 88 throw new IllegalStateException ("Expected context to be instance of " + XMLContext.class.getName() + "; got: " + res.getContext().getClass().getName()); 89 } 90 91 Map params = ctx.getViewParams(); 92 93 if ((_file != null) && (_file.lastModified() != _lastModified)) { 94 try{ 95 reload(); 96 }catch(Exception e){ 97 res.error("Problem reloading stylesheet", e); 98 return; 99 } 100 } 101 102 TransformerHandler handler; 103 try{ 104 handler = _fac.newTransformerHandler(_templ); 105 }catch(TransformerConfigurationException e){ 106 res.error("Problem creating TransformerHandler", e); 107 return; 108 } 109 Object key; 110 Object val; 111 112 for (Iterator iter = params.keySet().iterator(); iter.hasNext();) { 113 key = iter.next(); 114 val = params.get(key); 115 116 if (val != null) { 117 handler.getTransformer().setParameter(key.toString(), val); 118 } 119 } 120 try{ 121 TransformChain chain = (TransformChain)ctx.currentObject(); 122 chain.add(handler); 123 }catch(ClassCastException e){ 124 throw new IllegalStateException ("Expected object on context stack to be instance of " + TransformChain.class.getName()); 125 } 126 } 127 128 131 public void setEnv(Env env) { 132 _env = env; 133 } 134 135 private synchronized void reload() 136 throws TransformerConfigurationException , IOException { 137 if (_file.lastModified() != _lastModified) { 138 _templ = _fac.newTemplates(new StreamSource (_file.getInputStream(), 139 _file.getURI())); 140 } 141 142 _lastModified = _file.lastModified(); 143 } 144 145 private InputStream process(InputStream is, String uri) 146 throws IOException { 147 TemplateContextIF ctx = ThreadState.currentTemplateContext(); 148 149 return Utils.replaceVars(ctx, is, uri); 150 } 151 152 public static class Export { 153 private String _name; 154 155 public void setName(String name) { 156 _name = name; 157 } 158 } 159 } 160 | Popular Tags |