KickJava   Java API By Example, From Geeks To Geeks.

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


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
11 import org.apache.log4j.Logger;
12
13 import org.enhydra.xml.xmlc.*;
14
15 import org.enhydra.barracuda.core.comp.*;
16 import org.enhydra.barracuda.core.util.dom.*;
17 import org.enhydra.barracuda.core.comp.helper.ComponentGateway;
18 import org.enhydra.barracuda.core.view.*;
19 import org.enhydra.barracuda.examples.xmlc.data.*;
20
21 /**
22  * Here's an example of how the component stuff can be implemented
23  * in a standalone servlet.
24  */

25 public class TemplateTableServlet extends ComponentGateway {
26
27     //public constants
28
protected static final Logger logger = Logger.getLogger(TemplateTableServlet.class.getName());
29
30     //xmlc factory (ok as static because its threadsafe)
31
// private static XMLCFactory xmlcFactory = null;
32

33     //-------------------- ComponentGateway ----------------------
34
/**
35      * Handle the default HttpRequest.
36      */

37     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
38         //figure out which page we're after
39
String JavaDoc pageName = ""+req.getParameter("page");
40         Class JavaDoc pageCl = CompEx3HTML.class;
41         if (pageName.equals("CompEx3a")) pageCl = CompEx3aHTML.class;
42         else if (pageName.equals("CompEx3b")) pageCl = CompEx3bHTML.class;
43         
44         //load it from the XMLC factory
45
// if (xmlcFactory==null) xmlcFactory = new XMLCStdFactory(this.getClass().getClassLoader(), new StreamXMLCLogger());
46
// XMLObject page = xmlcFactory.create(pageCl);
47

48         //load the localized DOM template
49
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(pageCl, vc.getViewCapabilities().getClientLocale());
50
51         //create the component and bind it to the proper page (note that we're
52
//doing this on a per-request basis...if we wanted to optimize we could
53
//store the component hierarchy on a per-session basis)
54
BTemplate wcTemplate = new BTemplate();
55 // wcTemplate.addView(new DefaultTemplateView(page.getDocument().getElementById("UserReport")));
56
wcTemplate.addView(new DefaultTemplateView(page.getDocumentElement())); //id "UserReport" is attached to the HTML element. There is only on HTML element anyway, no need to use an id
57
wcTemplate.addModel(new UserReportModel("org.enhydra.barracuda.examples.xmlc.UserReport"));
58         wcTemplate.addModel(new UserDataModel());
59         root.addChild(wcTemplate);
60     
61         //return the page
62
return page;
63     }
64
65     //--------------- UserReportModel ------------------------
66
/**
67      * UserReport Model
68      */

69     class UserReportModel extends DefaultPropertiesModel {
70
71 // CompEx3Footer2HTML footer = (CompEx3Footer2HTML) xmlcFactory.create(CompEx3Footer2HTML.class);
72
// Node footerNode = footer.getElementUserReportFooter();
73

74         /**
75          * Instantiate the model by pointing it to the given
76          * properties file (fully qualified name)
77          */

78         public UserReportModel(String JavaDoc propFileName) {
79             super(propFileName);
80         }
81
82         /**
83          * This is where you would return an item based on a given key.
84          */

85         public Object JavaDoc getItem(String JavaDoc key) {
86             ViewContext vc = getViewContext();
87             Object JavaDoc result = null;
88             if (key.equals("Footer")) {
89                 Document footer = null;
90                 //load the localized DOM template
91
try {footer = DefaultDOMLoader.getGlobalInstance().getDOM(CompEx3Footer2HTML.class, vc.getViewCapabilities().getClientLocale());}
92                 catch (IOException ioe) {TemplateTableServlet.logger.fatal ("Fatal Error loading DOM template:", ioe);}
93                 Node footerNode = ((CompEx3Footer2HTML)footer).getElementUserReportFooter();
94                 result = vc.getElementFactory().getDocument().importNode(footerNode, true);
95             }
96             else result = (String JavaDoc) super.getItem(key);
97             if (result==null) result = " ";
98             return result;
99         }
100
101     }
102     
103     //--------------- UserDataModel --------------------------
104
/**
105      * UserData Model
106      */

107     class UserDataModel extends AbstractTemplateModel implements IterativeModel {
108
109         //data structures
110
UsersList users = null;
111         UserData ud = null;
112         Iterator it = null;
113
114         //template nodes
115
boolean useSelectionImages = true;
116         Node selNode = null;
117         Node unselNode = null;
118
119         /**
120          * Return the name of this model
121          */

122         public String JavaDoc getName() {return "UserData";}
123
124         /**
125          * This is where you would return an item based on a given key
126          */

127         public Object JavaDoc getItem(String JavaDoc key) {
128             ViewContext vc = getViewContext();
129             Object JavaDoc result = null;
130             if (key.equals("Selected")) {
131                 if (useSelectionImages) {
132                     ElementFactory ef = vc.getElementFactory();
133                     selNode = ef.getElement("SelectedImageCell");
134                     unselNode = ef.getElement("UnselectedImageCell");
135                     useSelectionImages = false; //so we don't continually look for them if they're not there
136
}
137                 boolean selected = ((Boolean JavaDoc) ud.get(UserData.SELECTED)).booleanValue();
138                 if (selNode!=null && selected) result = selNode.cloneNode(true);
139                 else if (unselNode!=null && !selected) result = unselNode.cloneNode(true);
140                 else result = (selected ? "Yes" : "No");
141             }
142             else if (key.equals("FirstName")) result = (String JavaDoc) ud.get(UserData.FIRST_NAME);
143             else if (key.equals("LastName")) result = (String JavaDoc) ud.get(UserData.LAST_NAME);
144             else if (key.equals("Gender")) result = (((Boolean JavaDoc) ud.get(UserData.GENDER)).booleanValue() ? "Male" : "Female");
145             else if (key.equals("Age")) result = ""+ud.get(UserData.AGE);
146             else if (key.equals("Phone")) result = (String JavaDoc) ud.get(UserData.PHONE);
147             else if (key.equals("Email")) {
148 // result = (String) ud.get(UserData.EMAIL);
149
String JavaDoc email = (String JavaDoc) ud.get(UserData.EMAIL);
150                 result = new BLink(email, "mailto:"+email, vc);
151             }
152             else if (key.equals("Notes")) result = (String JavaDoc) ud.get(UserData.NOTES);
153             else result = (String JavaDoc) super.getItem(key);
154             if (result==null) result = " ";
155             return result;
156         }
157
158         /**
159          * Process any special directives
160          */

161         public boolean processDirective(TemplateDirective td) {
162             if (td.getCommand().equals("Veto_If_Null")) {
163                 String JavaDoc data = td.getKeyName();
164                 if (data!=null) {
165                     if (null!=ud) {
166                         StringTokenizer st = new StringTokenizer(data,",");
167                         while (st.hasMoreTokens()) {
168                             String JavaDoc key = st.nextToken();
169                             Object JavaDoc value = ud.get(key);
170                             if (value==null || value.toString().length()<1) return false; //don't allow this directive
171
}
172                     } else {
173                         return false; //don't allow this directive
174
}
175                 }
176             }
177             return true; //by default, allow all directives
178
}
179         
180         /**
181          * Prepare for iteration
182          */

183         public void preIterate() {
184             if (users==null) users = UsersList.getSampleInstance();
185             it = users.iterator();
186         }
187         
188         /**
189          * Return true if there are more rows in the data model
190          */

191         public boolean hasNext() {
192             return (it!=null && it.hasNext());
193         }
194         
195         /**
196          * Load the next row in the model
197          */

198         public void loadNext() {
199             ud = (UserData) it.next();
200         }
201         
202         /**
203          * Post iteration cleanup
204          */

205         public void postIterate() {
206             it = null;
207         }
208     }
209
210 }
211
212
Popular Tags