| 1 package org.sapia.soto.state.cocoon.view; 2 3 import org.apache.commons.lang.ClassUtils; 4 import org.infohazard.domify.DOMAdapter; 5 6 import org.sapia.soto.state.Result; 7 import org.sapia.soto.state.StateRuntimeException; 8 import org.sapia.soto.state.Step; 9 import org.sapia.soto.state.cocoon.CocoonContext; 10 import org.sapia.soto.state.xml.StyleStep; 11 import org.sapia.soto.state.xml.TransformState; 12 import org.sapia.soto.state.xml.XMLContext; 13 14 import org.sapia.util.xml.confix.ConfigurationException; 15 16 import org.xml.sax.ContentHandler ; 17 18 import java.io.IOException ; 19 import java.util.Map ; 20 21 import javax.xml.transform.Transformer ; 22 import javax.xml.transform.TransformerConfigurationException ; 23 import javax.xml.transform.TransformerFactory ; 24 import javax.xml.transform.dom.DOMSource ; 25 import javax.xml.transform.sax.SAXResult ; 26 27 44 public class DomifyView extends TransformState implements Step{ 45 private String _uri; 46 private String _name; 47 private String _id; 48 private TransformerFactory _fac; 49 50 public DomifyView() { 51 _fac = TransformerFactory.newInstance(); 52 } 53 54 57 public String getName() { 58 return ClassUtils.getShortClassName(getClass()); 59 } 60 61 62 66 public void setRootName(String name) { 67 _name = name; 68 } 69 70 73 protected void process(Result res, ContentHandler handler) { 74 XMLContext ctx = (XMLContext) res.getContext(); 75 76 if (ctx.getContentHandler() == null) { 77 return; 78 } 79 80 Object model = null; 81 82 if (ctx.hasCurrentObject()) { 83 model = ctx.currentObject(); 84 } else { 85 model = new NullObject(); 86 } 87 88 try { 89 execute(model, ctx.getViewParams(), handler); 90 } catch (Throwable t) { 91 throw new StateRuntimeException("Could not execute XSL pipeline", t); 92 } 93 } 94 95 protected void execute(Object model, Map viewParams, ContentHandler handler) 96 throws Throwable { 97 98 if (_name == null) { 99 _name = CocoonContext.MODEL_KEY; 100 } 101 102 DOMSource src = new DOMSource (new DOMAdapter().adapt(model, _name)); 103 SAXResult res = new SAXResult (handler); 104 Transformer tx = _fac.newTransformer(); 105 tx.transform(src, res); 106 } 107 108 111 public void handleObject(String name, Object obj) 112 throws ConfigurationException { 113 if (obj instanceof XslSource) { 114 try{ 115 XslSource xsl = (XslSource)obj; 116 StyleStep step = new StyleStep(); 117 step.setEnv(env()); 118 step.setSrc(xsl.getUri()); 119 }catch(TransformerConfigurationException e){ 120 throw new ConfigurationException("Could not instantiate StyleStep", e); 121 }catch(IOException e){ 122 throw new ConfigurationException("Could not inialize stylesheet", e); 123 } 124 } 125 else{ 126 super.handleObject(name, obj); 127 } 128 } 129 130 public static class NullObject { 132 } 133 } 134 | Popular Tags |