KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > xmlc > CompEx9


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 /**
21  * Make sure a reusable component can handle nested images within an anchor element
22  */

23 public class CompEx9 extends BTemplateGateway {
24
25     BComponent broot = null;
26
27     Document page = null;
28     BTemplate templateComp = null;
29     
30     //-------------------- BTemplateGateway ----------------------
31
/**
32      * Return an instance of the template model
33      */

34     public TemplateModel getTemplateModel() {
35         return new LocalTemplateModel();
36     }
37     
38     /**
39      * Return an instance of the template class (must implement Document)
40      */

41     public Class JavaDoc getTemplateClass() {
42         return CompEx9HTML.class;
43     }
44     
45     public DOMWriter getDOMWriter() {
46         return new DefaultDOMWriter(true);
47     }
48
49     /**
50      * Handle the default HttpRequest.
51      */

52     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
53
54         if (templateComp==null) {
55             //load the localized DOM template
56
page = DefaultDOMLoader.getGlobalInstance().getDOM(getTemplateClass());
57         }
58
59             //create a template component and bind it to the views
60
templateComp = new BTemplate(getTemplateModel());
61             templateComp.setView(new DefaultTemplateView(page.getDocumentElement()));
62             root.removeAll();
63             root.addChild(templateComp);
64
65         //return the page
66
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             //start by figuring out the ViewCapabilities
82
if (logger.isDebugEnabled()) logger.debug("Create the ViewContext");
83 //csc_101701.1 vc = new DefaultViewContext(new ViewCapabilities(req, resp));
84
vc = new DefaultViewContext(new ViewCapabilities(req, resp), req, resp); //csc_101701.1
85

86             //create our root component
87
if (logger.isDebugEnabled()) logger.debug("Create component root");
88             if (broot==null) {
89 // if (true) {
90
broot = new BComponent();
91                 broot.setName("Root");
92             }
93             
94             //give the implementation a chance to add any components to the root
95
if (logger.isDebugEnabled()) logger.debug("Handling default");
96             Document doc = handleDefault(broot, vc, req, resp);
97             
98             //now init the component
99
if (logger.isDebugEnabled()) logger.debug("Invoking initCycle on component hierarchy");
100 // broot.initCycle();
101
broot.validate();
102             
103             //now render the component
104
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             //its possible the implementor may want to recycle children...if so, remove them
110
//prior to calling destroy on the root
111
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             //now destroy the component
122
if (logger.isDebugEnabled()) logger.debug("Invoking destroyCycle on component hierarchy");
123 // broot.destroyCycle();
124

125             //csc_102201.1
126
//now adjust the outgoing page (this is critical to make sure we
127
//can accurately detect client scripting)
128
// ScriptDetector.prepareClientResp(doc, vc);
129

130             //now render the page
131
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 /*
142         } catch (ClientSideRedirectException re) {
143             //if we get a redirect exception, request the browser to redirect accordingly
144 // if (logger.isInfoEnabled()) logger.info("ClientSideRedirectException...redirecting to "+re.getRedirectURL());
145 // resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
146 // resp.setHeader("Location", re.getRedirectURL());
147
148             String url = URLRewriter.encodeRedirectURL(req, resp, re.getRedirectURL());
149             if (logger.isInfoEnabled()) logger.info("ClientSideRedirectException...redirecting to "+url);
150             resp.sendRedirect(url);
151 */

152         } catch (RenderException e) {
153             //if we get a RenderException, handle it
154
if (logger.isInfoEnabled()) logger.info("Handling RenderException:"+e);
155             handleRenderException(e, vc, req, resp);
156 /*
157         } catch (EventException e) {
158             //if we get an EventException, handle it
159             if (logger.isInfoEnabled()) logger.info("Handling EventException:"+e);
160             handleEventException(e, vc, req, resp);
161 */

162         } finally {
163             //nop
164
}
165     }
166
167
168
169
170     //--------------- TemplateModel --------------------------
171
/**
172      * LocalTemplateModel
173      */

174     class LocalTemplateModel extends AbstractTemplateModel {
175     
176         //register the model by name
177
public String JavaDoc getName() {return "Ex9";}
178         
179         //provide items by key
180
public Object JavaDoc getItem(String JavaDoc 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