KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.dom.*;
15 import org.enhydra.barracuda.core.event.*;
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  * Test TemplateHelper's ability to a) remove empty tags and
22  * b) support nested directives
23  */

24 public class CompEx6 extends ComponentGateway {
25
26     //-------------------- ComponentGateway ----------------------
27
/**
28      * Handle the default HttpRequest.
29      */

30     public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException {
31         //load the localized DOM template
32
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(CompEx6HTML.class);
33
34         //create a template component and bind it to the views
35
BTemplate templateComp = new BTemplate(new LocalTemplateModel(req));
36         templateComp.addModel(new LocalIterativeModel("Cntr1",2));
37         templateComp.addModel(new LocalIterativeModel("Cntr2",3));
38         templateComp.setView(new DefaultTemplateView(page.getDocumentElement()));
39         root.addChild(templateComp);
40
41         //return the page
42
return page;
43     }
44
45     /**
46      * <p>Get a DOMWriter. By default, we use a DefaultDOMWriter. If
47      * you'd like to use something else, override this method.
48      *
49      * @return a DOMWriter to be used to render the DOM
50      */

51     public DOMWriter getDOMWriter() {
52         //return DefaultDOM writer
53
return new DefaultDOMWriter(true);
54     }
55
56     //--------------- TemplateModel --------------------------
57
/**
58      * LocalTemplateModel
59      */

60     class LocalTemplateModel extends AbstractTemplateModel {
61     
62         HttpServletRequest req = null;
63         int attr = 0;
64         int cntr = 0;
65                         
66         public LocalTemplateModel(HttpServletRequest ireq) {
67             req = ireq;
68         }
69         
70         //register the model by name
71
public String JavaDoc getName() {return "Test1";}
72         
73         //provide items by key
74
public Object JavaDoc getItem(String JavaDoc key) {
75             ViewContext vc = getViewContext();
76             ViewCapabilities vcap = vc.getViewCapabilities();
77             if (key.equals("NullItem")) {
78                 return null;
79             } else if (key.equals("Link1")) {
80                 BLink blink = new BLink("some text","someurl", vc);
81                 return blink;
82             } else if (key.equals("Image1")) {
83                 return "/Barracuda/images/key.gif";
84             } else if (key.equals("Markup1")) {
85                 try {
86                     CompEx6aHTML snippetPage = (CompEx6aHTML) DefaultDOMLoader.getGlobalInstance().getDOM(CompEx6aHTML.class);
87                     Node targetNode = snippetPage.getElementMarkupBlockID();
88                     return vc.getElementFactory().getDocument().importNode(targetNode, true);
89                 } catch (IOException e) {
90                     return "Unable to find "+key+" snippet:"+e;
91                 }
92             } else if (key.equals("Text1")) {
93                 return "Some text goes here...";
94             } else if (key.startsWith("GetAttr")) {
95                 BComponent comp = new BComponent();
96                 comp.setAttr("cntr", ""+(++attr));
97 //System.out.println("Returning node="+attr+" for key:"+key);
98
return comp;
99             } else {
100                 return super.getItem(key);
101             }
102         }
103     }
104
105     /**
106      * LocalIterativeModel
107      */

108     class LocalIterativeModel extends AbstractTemplateModel implements IterativeModel {
109     
110         String JavaDoc modelName = null;
111         int max = -1;
112         int cntr = -1;
113                         
114         public LocalIterativeModel(String JavaDoc imodelName, int imax) {
115             modelName = imodelName;
116             max = imax;
117         }
118         
119         //register the model by name
120
public String JavaDoc getName() {return modelName;}
121         
122         //provide items by key
123
public Object JavaDoc getItem(String JavaDoc key) {
124             ViewContext vc = getViewContext();
125             ViewCapabilities vcap = vc.getViewCapabilities();
126             if (key.equals("Cntr")) {
127                 return new BText("[model "+modelName+"]."+"Iterative Item"+cntr);
128             } else {
129                 return super.getItem(key);
130             }
131         }
132
133
134         public void preIterate() {
135             cntr = 0;
136         }
137         
138         public void postIterate() {
139             cntr = 99;
140         }
141         
142         public boolean hasNext() {
143             return (cntr<max);
144         }
145         
146         public void loadNext() {
147             cntr++;
148         }
149         
150     }
151 }
152
Popular Tags