KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > provider > TestProvider


1 package org.apache.velocity.test.provider;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.*;
20
21 /**
22  * This class is used by the testbed. Instances of the class
23  * are fed into the context that is set before the AST
24  * is traversed and dynamic content generated.
25  *
26  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
27  * @version $Id: TestProvider.java,v 1.21.8.1 2004/03/03 23:23:06 geirm Exp $
28  */

29 public class TestProvider
30 {
31     String JavaDoc title = "lunatic";
32     boolean state;
33     Object JavaDoc ob = null;
34
35     public static String JavaDoc PUB_STAT_STRING = "Public Static String";
36
37     int stateint = 0;
38
39
40     public String JavaDoc getName()
41     {
42         return "jason";
43     }
44
45     public Stack getStack()
46     {
47         Stack stack = new Stack();
48         stack.push("stack element 1");
49         stack.push("stack element 2");
50         stack.push("stack element 3");
51         return stack;
52     }
53
54     public List getEmptyList()
55     {
56         List list = new ArrayList();
57         return list;
58     }
59
60     public List getList()
61     {
62         List list = new ArrayList();
63         list.add("list element 1");
64         list.add("list element 2");
65         list.add("list element 3");
66         
67         return list;
68     }
69     
70     public Hashtable getSearch()
71     {
72         Hashtable h = new Hashtable();
73         h.put("Text", "this is some text");
74         h.put("EscText", "this is escaped text");
75         h.put("Title", "this is the title");
76         h.put("Index", "this is the index");
77         h.put("URL", "http://periapt.com");
78
79         ArrayList al = new ArrayList();
80         al.add(h);
81         
82         h.put("RelatedLinks", al);
83
84         return h;
85     }
86
87     public Hashtable getHashtable()
88     {
89         Hashtable h = new Hashtable();
90         h.put("key0", "value0");
91         h.put("key1", "value1");
92         h.put("key2", "value2");
93         
94         return h;
95     }
96
97     public ArrayList getRelSearches()
98     {
99         ArrayList al = new ArrayList();
100         al.add(getSearch());
101         
102         return al;
103     }
104
105     public String JavaDoc getTitle()
106     {
107         return title;
108     }
109
110     public void setTitle(String JavaDoc title)
111     {
112         this.title = title;
113     }
114
115     public Object JavaDoc[] getMenu()
116     {
117         //ArrayList al = new ArrayList();
118
Object JavaDoc[] menu = new Object JavaDoc[3];
119         for (int i = 0; i < 3; i++)
120         {
121             Hashtable item = new Hashtable();
122             item.put("id", "item" + Integer.toString(i+1));
123             item.put("name", "name" + Integer.toString(i+1));
124             item.put("label", "label" + Integer.toString(i+1));
125             //al.add(item);
126
menu[i] = item;
127         }
128             
129         //return al;
130
return menu;
131     }
132
133     public ArrayList getCustomers()
134     {
135         ArrayList list = new ArrayList();
136
137         list.add("ArrayList element 1");
138         list.add("ArrayList element 2");
139         list.add("ArrayList element 3");
140         list.add("ArrayList element 4");
141
142         return list;
143     }
144
145     public ArrayList getCustomers2()
146     {
147         ArrayList list = new ArrayList();
148
149         list.add(new TestProvider());
150         list.add(new TestProvider());
151         list.add(new TestProvider());
152         list.add(new TestProvider());
153
154         return list;
155     }
156
157     public Object JavaDoc me()
158     {
159         return this;
160     }
161
162     public String JavaDoc toString()
163     {
164         return ("test provider");
165     }
166
167     public Vector getVector()
168     {
169         Vector list = new Vector();
170
171         list.addElement("vector element 1");
172         list.addElement("vector element 2");
173         
174         return list;
175     }
176
177     public String JavaDoc[] getArray()
178     {
179         String JavaDoc[] strings = new String JavaDoc[2];
180         strings[0] = "first element";
181         strings[1] = "second element";
182         return strings;
183     }
184
185     public boolean theAPLRules()
186     {
187         return true;
188     }
189
190     public boolean getStateTrue()
191     {
192         return true;
193     }
194     
195     public boolean getStateFalse()
196     {
197         return false;
198     }
199
200     public String JavaDoc objectArrayMethod(Object JavaDoc[] o)
201     {
202         return "result of objectArrayMethod";
203     }
204
205     public String JavaDoc concat(Object JavaDoc[] strings)
206     {
207         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
208         
209         for (int i = 0; i < strings.length; i++)
210         {
211             result.append((String JavaDoc) strings[i]).append(' ');
212         }
213         
214         return result.toString();
215     }
216
217     public String JavaDoc concat(List strings)
218     {
219         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
220         
221         for (int i = 0; i < strings.size(); i++)
222         {
223             result.append((String JavaDoc) strings.get(i)).append(' ');
224         }
225         
226         return result.toString();
227     }
228
229     public String JavaDoc objConcat(List objects)
230     {
231         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
232
233         for (int i = 0; i < objects.size(); i++)
234         {
235             result.append(objects.get(i)).append(' ');
236         }
237
238         return result.toString();
239     }
240
241     public String JavaDoc parse(String JavaDoc a, Object JavaDoc o, String JavaDoc c, String JavaDoc d)
242     {
243         return a + o.toString() + c + d;
244     }
245
246     public String JavaDoc concat(String JavaDoc a, String JavaDoc b)
247     {
248         return a + b;
249     }
250
251     // These two are for testing subclasses.
252

253     public Person getPerson()
254     {
255         return new Person();
256     }
257     
258     public Child getChild()
259     {
260         return new Child();
261     }
262
263     public String JavaDoc showPerson(Person person)
264     {
265         return person.getName();
266     }
267
268     /**
269      * Chop i characters off the end of a string.
270      *
271      * @param string String to chop.
272      * @param i Number of characters to chop.
273      * @return String with processed answer.
274      */

275     public String JavaDoc chop(String JavaDoc string, int i)
276     {
277         return(string.substring(0, string.length() - i));
278     }
279
280     public boolean allEmpty(Object JavaDoc[] list)
281     {
282         int size = list.length;
283         
284         for (int i = 0; i < size; i++)
285             if (list[i].toString().length() > 0)
286                 return false;
287         
288         return true;
289     }
290
291     /*
292      * This can't have the signature
293     
294     public void setState(boolean state)
295     
296     or dynamically invoking the method
297     doesn't work ... you would have to
298     put a wrapper around a method for a
299     real boolean property that takes a
300     Boolean object if you wanted this to
301     work. Not really sure how useful it
302     is anyway. Who cares about boolean
303     values you can just set a variable.
304     
305     */

306
307     public void setState(Boolean JavaDoc state)
308     {
309     }
310
311     public void setBangStart( Integer JavaDoc i )
312     {
313         System.out.println("SetBangStart() : called with val = " + i );
314         stateint = i.intValue();
315     }
316     public Integer JavaDoc bang()
317     {
318         System.out.println("Bang! : " + stateint );
319         Integer JavaDoc ret = new Integer JavaDoc( stateint );
320         stateint++;
321         return ret;
322     }
323
324     /**
325      * Test the ability of vel to use a get(key)
326      * method for any object type, not just one
327      * that implements the Map interface.
328      */

329     public String JavaDoc get(String JavaDoc key)
330     {
331         return key;
332     }
333
334     /**
335      * Test the ability of vel to use a put(key)
336      * method for any object type, not just one
337      * that implements the Map interface.
338      */

339     public String JavaDoc put(String JavaDoc key, Object JavaDoc o)
340     {
341         ob = o;
342         return key;
343     }
344
345     public String JavaDoc getFoo()
346         throws Exception JavaDoc
347     {
348         System.out.println("Hello from getfoo");
349
350         throw new Exception JavaDoc("From getFoo()");
351     }
352
353     public String JavaDoc getThrow()
354         throws Exception JavaDoc
355     {
356         System.out.println("Hello from geThrow");
357        throw new Exception JavaDoc("From getThrow()");
358     }
359 }
360
Popular Tags