1 package org.enhydra.barracuda.examples.xmlc; 2 3 import java.io.*; 4 import java.util.*; 5 import javax.servlet.*; 6 import javax.servlet.http.*; 7 8 import org.w3c.dom.*; 9 import org.w3c.dom.html.*; 10 import org.enhydra.xml.xmlc.*; 11 12 import org.enhydra.barracuda.core.comp.*; 13 import org.enhydra.barracuda.core.comp.helper.*; 14 import org.enhydra.barracuda.core.event.*; 15 import org.enhydra.barracuda.plankton.http.*; 16 import org.enhydra.barracuda.core.util.dom.*; 17 import org.enhydra.barracuda.core.view.*; 18 import org.enhydra.barracuda.examples.xmlc.data.*; 19 20 23 public class CompEx9 extends BTemplateGateway { 24 25 BComponent broot = null; 26 27 Document page = null; 28 BTemplate templateComp = null; 29 30 34 public TemplateModel getTemplateModel() { 35 return new LocalTemplateModel(); 36 } 37 38 41 public Class getTemplateClass() { 42 return CompEx9HTML.class; 43 } 44 45 public DOMWriter getDOMWriter() { 46 return new DefaultDOMWriter(true); 47 } 48 49 52 public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException { 53 54 if (templateComp==null) { 55 page = DefaultDOMLoader.getGlobalInstance().getDOM(getTemplateClass()); 57 } 58 59 templateComp = new BTemplate(getTemplateModel()); 61 templateComp.setView(new DefaultTemplateView(page.getDocumentElement())); 62 root.removeAll(); 63 root.addChild(templateComp); 64 65 return page; 67 } 68 69 protected void handleDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 70 71 long bmillis = 0; 72 long smillis = 0; 73 long emillis = 0; 74 long elapsed1 = 0; 75 long elapsed2 = 0; 76 if (logger.isInfoEnabled()) bmillis = System.currentTimeMillis(); 77 if (logger.isInfoEnabled()) logger.info("Handling incoming HTTP request in "+this); 78 79 ViewContext vc = null; 80 try { 81 if (logger.isDebugEnabled()) logger.debug("Create the ViewContext"); 83 vc = new DefaultViewContext(new ViewCapabilities(req, resp), req, resp); 86 if (logger.isDebugEnabled()) logger.debug("Create component root"); 88 if (broot==null) { 89 broot = new BComponent(); 91 broot.setName("Root"); 92 } 93 94 if (logger.isDebugEnabled()) logger.debug("Handling default"); 96 Document doc = handleDefault(broot, vc, req, resp); 97 98 if (logger.isDebugEnabled()) logger.debug("Invoking initCycle on component hierarchy"); 100 broot.validate(); 102 103 if (logger.isDebugEnabled()) logger.debug("Rendering component hierarchy"); 105 if (logger.isDebugEnabled()) smillis = System.currentTimeMillis(); 106 broot.render(vc); 107 if (logger.isDebugEnabled()) elapsed1 = System.currentTimeMillis()-smillis; 108 109 if (recycleChildren) { 112 if (logger.isDebugEnabled()) logger.debug("Recycling child components"); 113 List children = broot.getChildren(); 114 if (children!=null) { 115 for (int i=children.size()-1; i>=0; i--) { 116 broot.removeChild(i); 117 } 118 } 119 } 120 121 if (logger.isDebugEnabled()) logger.debug("Invoking destroyCycle on component hierarchy"); 123 125 130 if (logger.isDebugEnabled()) logger.debug("Rendering the DOM"); 132 if (logger.isInfoEnabled()) smillis = System.currentTimeMillis(); 133 this.getDOMWriter().write(doc, resp); 134 if (logger.isInfoEnabled()) { 135 elapsed2 = System.currentTimeMillis()-smillis; 136 emillis = System.currentTimeMillis(); 137 } 138 139 if (logger.isInfoEnabled()) logger.info("Dispatching complete! (rendered in "+(elapsed1)+"/written in "+(elapsed2)+" of "+(emillis-bmillis)+" millis)"); 140 141 152 } catch (RenderException e) { 153 if (logger.isInfoEnabled()) logger.info("Handling RenderException:"+e); 155 handleRenderException(e, vc, req, resp); 156 162 } finally { 163 } 165 } 166 167 168 169 170 174 class LocalTemplateModel extends AbstractTemplateModel { 175 176 public String getName() {return "Ex9";} 178 179 public Object getItem(String key) { 181 ViewContext vc = getViewContext(); 182 ViewCapabilities vcap = vc.getViewCapabilities(); 183 if (key.equals("Link1")) { 184 return new BLink("Click me", "CompTest9"); 185 } else { 186 return super.getItem(key); 187 } 188 } 189 } 190 } 191 | Popular Tags |