1 package org.apache.velocity.test.provider;
2 
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21 
22 import java.util.ArrayList;
23 import java.util.Hashtable;
24 import java.util.List;
25 import java.util.Stack;
26 import java.util.Vector;
27 
28 /**
29  * This class is used by the testbed. Instances of the class
30  * are fed into the context that is set before the AST
31  * is traversed and dynamic content generated.
32  *
33  * @author <a href="mailto:[email protected]">Jason van Zyl</a>
34  * @version $Id$
35  */
36 public class TestProvider
37 {
38     String title = "lunatic";
39     boolean state;
40     Object ob = null;
41 
42     public static String PUB_STAT_STRING = "Public Static String";
43 
44     int stateint = 0;
45 
46 
getName()47     public String getName()
48     {
49         return "jason";
50     }
51 
getStack()52     public Stack getStack()
53     {
54         Stack stack = new Stack();
55         stack.push("stack element 1");
56         stack.push("stack element 2");
57         stack.push("stack element 3");
58         return stack;
59     }
60 
getEmptyList()61     public List getEmptyList()
62     {
63         return new ArrayList();
64     }
65 
getList()66     public List getList()
67     {
68         List list = new ArrayList();
69         list.add("list element 1");
70         list.add("list element 2");
71         list.add("list element 3");
72 
73         return list;
74     }
75 
getSearch()76     public Hashtable getSearch()
77     {
78         Hashtable h = new Hashtable();
79         h.put("Text", "this is some text");
80         h.put("EscText", "this is escaped text");
81         h.put("Title", "this is the title");
82         h.put("Index", "this is the index");
83         h.put("URL", "http://periapt.com");
84 
85         ArrayList al = new ArrayList();
86         al.add(h);
87 
88         h.put("RelatedLinks", al);
89 
90         return h;
91     }
92 
getHashtable()93     public Hashtable getHashtable()
94     {
95         Hashtable h = new Hashtable();
96         h.put("key0", "value0");
97         h.put("key1", "value1");
98         h.put("key2", "value2");
99 
100         return h;
101     }
102 
getRelSearches()103     public ArrayList getRelSearches()
104     {
105         ArrayList al = new ArrayList();
106         al.add(getSearch());
107 
108         return al;
109     }
110 
getTitle()111     public String getTitle()
112     {
113         return title;
114     }
115 
setTitle(String title)116     public void setTitle(String title)
117     {
118         this.title = title;
119     }
120 
getMenu()121     public Object[] getMenu()
122     {
123         //ArrayList al = new ArrayList();
124         Object[] menu = new Object[3];
125         for (int i = 0; i < 3; i++)
126         {
127             Hashtable item = new Hashtable();
128             item.put("id", "item" + (i+1));
129             item.put("name", "name" + (i+1));
130             item.put("label", "label" + (i+1));
131             //al.add(item);
132             menu[i] = item;
133         }
134 
135         //return al;
136         return menu;
137     }
138 
getCustomers()139     public ArrayList getCustomers()
140     {
141         ArrayList list = new ArrayList();
142 
143         list.add("ArrayList element 1");
144         list.add("ArrayList element 2");
145         list.add("ArrayList element 3");
146         list.add("ArrayList element 4");
147 
148         return list;
149     }
150 
getCustomers2()151     public ArrayList getCustomers2()
152     {
153         ArrayList list = new ArrayList();
154 
155         list.add(new TestProvider());
156         list.add(new TestProvider());
157         list.add(new TestProvider());
158         list.add(new TestProvider());
159 
160         return list;
161     }
162 
me()163     public Object me()
164     {
165         return this;
166     }
167 
toString()168     public String toString()
169     {
170         return ("test provider");
171     }
172 
getVector()173     public Vector getVector()
174     {
175         Vector list = new Vector();
176 
177         list.addElement("vector element 1");
178         list.addElement("vector element 2");
179 
180         return list;
181     }
182 
getArray()183     public String[] getArray()
184     {
185         String[] strings = new String[2];
186         strings[0] = "first element";
187         strings[1] = "second element";
188         return strings;
189     }
190 
theAPLRules()191     public boolean theAPLRules()
192     {
193         return true;
194     }
195 
getStateTrue()196     public boolean getStateTrue()
197     {
198         return true;
199     }
200 
getStateFalse()201     public boolean getStateFalse()
202     {
203         return false;
204     }
205 
objectArrayMethod(Object[] o)206     public String objectArrayMethod(Object[] o)
207     {
208         return "result of objectArrayMethod";
209     }
210 
concat(Object[] strings)211     public String concat(Object[] strings)
212     {
213         StringBuilder result = new StringBuilder();
214 
215         for (Object string : strings)
216         {
217             result.append((String) string).append(' ');
218         }
219 
220         return result.toString();
221     }
222 
concat(List strings)223     public String concat(List strings)
224     {
225         StringBuilder result = new StringBuilder();
226 
227         for (Object string : strings)
228         {
229             result.append((String) string).append(' ');
230         }
231 
232         return result.toString();
233     }
234 
objConcat(List objects)235     public String objConcat(List objects)
236     {
237         StringBuilder result = new StringBuilder();
238 
239         for (Object object : objects)
240         {
241             result.append(object).append(' ');
242         }
243 
244         return result.toString();
245     }
246 
parse(String a, Object o, String c, String d)247     public String parse(String a, Object o, String c, String d)
248     {
249         return a + o.toString() + c + d;
250     }
251 
concat(String a, String b)252     public String concat(String a, String b)
253     {
254         return a + b;
255     }
256 
257     // These two are for testing subclasses.
258 
getPerson()259     public Person getPerson()
260     {
261         return new Person();
262     }
263 
getChild()264     public Child getChild()
265     {
266         return new Child();
267     }
268 
showPerson(Person person)269     public String showPerson(Person person)
270     {
271         return person.getName();
272     }
273 
274     /**
275      * Chop i characters off the end of a string.
276      *
277      * @param string String to chop.
278      * @param i Number of characters to chop.
279      * @return String with processed answer.
280      */
chop(String string, int i)281     public String chop(String string, int i)
282     {
283         return(string.substring(0, string.length() - i));
284     }
285 
allEmpty(Object[] list)286     public boolean allEmpty(Object[] list)
287     {
288         int size = list.length;
289 
290         for (Object aList : list)
291             if (aList.toString().length() > 0)
292                 return false;
293 
294         return true;
295     }
296 
297     /*
298      * This can't have the signature
299      *
300      *    public void setState(boolean state)
301      *
302      *    or dynamically invoking the method
303      *    doesn't work ... you would have to
304      *    put a wrapper around a method for a
305      *    real boolean property that takes a
306      *    Boolean object if you wanted this to
307      *    work. Not really sure how useful it
308      *    is anyway. Who cares about boolean
309      *    values you can just set a variable.
310      *
311      */
312 
setState(Boolean state)313     public void setState(Boolean state)
314     {
315     }
316 
setBangStart( Integer i )317     public void setBangStart( Integer i )
318     {
319         System.out.println("SetBangStart() : called with val = " + i );
320         stateint = i;
321     }
bang()322     public Integer bang()
323     {
324         System.out.println("Bang! : " + stateint );
325         Integer ret = stateint;
326         stateint++;
327         return ret;
328     }
329 
330     /**
331      * Test the ability of vel to use a get(key)
332      * method for any object type, not just one
333      * that implements the Map interface.
334      */
get(String key)335     public String get(String key)
336     {
337         return key;
338     }
339 
340     /**
341      * Test the ability of vel to use a put(key)
342      * method for any object type, not just one
343      * that implements the Map interface.
344      */
put(String key, Object o)345     public String put(String key, Object o)
346     {
347         ob = o;
348         return key;
349     }
350 
getFoo()351     public String getFoo()
352         throws Exception
353     {
354         throw new Exception("From getFoo()");
355     }
356 
getThrow()357     public String getThrow()
358         throws Exception
359     {
360        throw new Exception("From getThrow()");
361     }
362 }
363