KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > tutorials > comp > HelloWorld3


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: HelloWorld3.java,v 1.16 2004/02/01 05:16:32 christianc Exp $
19  */

20 package org.enhydra.barracuda.tutorials.comp;
21
22 import java.io.*;
23 import java.util.*;
24 import javax.servlet.*;
25 import javax.servlet.http.*;
26
27 import org.w3c.dom.*;
28 import org.enhydra.xml.xmlc.*;
29
30 import org.enhydra.barracuda.core.comp.*;
31 import org.enhydra.barracuda.core.comp.helper.*;
32 import org.enhydra.barracuda.core.util.dom.*;
33 import org.enhydra.barracuda.plankton.data.*;
34 import org.enhydra.barracuda.tutorials.xmlc.*;
35
36 /**
37  * Here's an example of HelloWorld using the BTemplate
38  * component
39  */

40 public class HelloWorld3 extends ComponentGateway {
41
42     //xmlc factory (ok as static because its threadsafe)
43
// private static XMLCFactory xmlcFactory = null;
44

45     //-------------------- ComponentGateway ----------------------
46
/**
47      * Handle the default HttpRequest.
48      */

49     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
50         //figure out which page we're after by looking for an optional
51
//page parameter in the Req
52
String JavaDoc pageNo = req.getParameter("page");
53         if (pageNo==null) pageNo = "1";
54         boolean useIDTemplate = (!pageNo.equals("1"));
55         // rtl_20010612 Class pageCl = HelloWorld3HTML.class;
56
// rtl_20010612 if (useIDTemplate) pageCl = HelloWorld3aHTML.class;
57
Class JavaDoc pageCl = HelloWorld3XML.class;
58         if (useIDTemplate) pageCl = HelloWorld3aXML.class;
59
60         //load the DOM object
61
// if (xmlcFactory==null) xmlcFactory = new XMLCStdFactory(this.getClass().getClassLoader(), new StreamXMLCLogger());
62
// XMLObject page = xmlcFactory.create(pageCl);
63

64         //load the localized DOM template
65
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(pageCl, vc.getViewCapabilities().getClientLocale());
66
67         //find the node we're after
68
Node node = page.getDocumentElement();
69
70         //create a view on the node
71
TemplateView tv = null;
72         if (useIDTemplate) {
73             String JavaDoc propFileName = "HelloWorld3a.properties";
74             Properties props = new Properties();
75             try {
76                 props.load(this.getClass().getResourceAsStream(propFileName));
77                 tv = new DefaultTemplateView(node, "id", new MapStateMap(props));
78             } catch (IOException e) {
79                 System.out.println ("Fatal err loading properties file:"+e);
80             }
81         } else {
82             tv = new DefaultTemplateView(node, "tid", "tdir");
83         }
84
85         //create the component and bind it to the view
86
BTemplate templateComp = new BTemplate();
87         templateComp.setView(tv);
88
89         //now specify the model
90
templateComp.addModel(new HelloWorldModel(useIDTemplate));
91         String JavaDoc[] items = new String JavaDoc[] {"Cabbage", "Carrots", "Pickles", "Sugar", "Wheaties", "Flour", "Potatoes"};
92         String JavaDoc[] qtys = new String JavaDoc[] {"3 heads", "1 bag", "1 jar", "5 lbs", "3 bxs", "50 lbs", "25 lbs"};
93         templateComp.addModel(new GroceriesModel(items, qtys));
94
95         //add the component to the root
96
root.addChild(templateComp);
97
98         //return the page
99
return page;
100     }
101
102     //--------------- GroceriesModel -------------------------
103
/**
104      * Groceries Model
105      */

106     class GroceriesModel extends AbstractTemplateModel implements IterativeModel {
107
108         String JavaDoc[] items = null;
109         String JavaDoc[] qtys = null;
110         int cntr = -1;
111
112         //constructor
113
public GroceriesModel(String JavaDoc[] iitems, String JavaDoc[] iqtys) {
114             items = iitems;
115             qtys = iqtys;
116         }
117
118         //register the model by name
119
public String JavaDoc getName() {return "Groceries";}
120
121         //provide items by key
122
public Object JavaDoc getItem(String JavaDoc key) {
123             if (key.equals("Item")) return items[cntr];
124             else if (key.equals("Qty")) return qtys[cntr];
125             else return super.getItem(key);
126         }
127
128         //prepare for iteration
129
public void preIterate() {
130             cntr = -1;
131         }
132
133         //return true if there are more rows in the data model
134
public boolean hasNext() {
135             return (cntr<(items.length-1));
136         }
137
138         //load the next row in the model
139
public void loadNext() {
140             cntr++;
141         }
142
143         //cleanup after iteration
144
public void postIterate() {}
145     }
146
147     //--------------- HelloWorldModel ------------------------
148
/**
149      * HelloWorldModel
150      */

151     class HelloWorldModel extends AbstractTemplateModel {
152
153         String JavaDoc source = null;
154
155         public HelloWorldModel(boolean useIDTemplate) {
156             if (useIDTemplate) source = "Here the template processing instructions are stored in a separate .properties file and just referenced using the tid attribute.";
157             else source = "Here the template processing instructions are actually stored in the template file as directives embedded in the tdir attributes.";
158         }
159
160         //register the model by name
161
public String JavaDoc getName() {
162             return "HelloWorld";
163         }
164
165         //provide items by key
166
public Object JavaDoc getItem(String JavaDoc key) {
167             ViewContext vc = getViewContext();
168             if (key.equals("Title")) return "Hello World 3 example";
169             else if (key.equals("Descr")) return "This example shows BTemplate processing an XML template. "+source;
170             else if (key.equals("Hello")) return "Hello World! Hi Ma! Hi Pa!";
171             else return super.getItem(key);
172         }
173     }
174 }
175
Popular Tags