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:jvanzyl@apache.org">Jason van Zyl</a>
34   * @version $Id: TestProvider.java 463298 2006-10-12 16:10:32Z henning $
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  
47      public String getName()
48      {
49          return "jason";
50      }
51  
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  
61      public List getEmptyList()
62      {
63          List list = new ArrayList();
64          return list;
65      }
66  
67      public List getList()
68      {
69          List list = new ArrayList();
70          list.add("list element 1");
71          list.add("list element 2");
72          list.add("list element 3");
73  
74          return list;
75      }
76  
77      public Hashtable getSearch()
78      {
79          Hashtable h = new Hashtable();
80          h.put("Text", "this is some text");
81          h.put("EscText", "this is escaped text");
82          h.put("Title", "this is the title");
83          h.put("Index", "this is the index");
84          h.put("URL", "http://periapt.com");
85  
86          ArrayList al = new ArrayList();
87          al.add(h);
88  
89          h.put("RelatedLinks", al);
90  
91          return h;
92      }
93  
94      public Hashtable getHashtable()
95      {
96          Hashtable h = new Hashtable();
97          h.put("key0", "value0");
98          h.put("key1", "value1");
99          h.put("key2", "value2");
100 
101         return h;
102     }
103 
104     public ArrayList getRelSearches()
105     {
106         ArrayList al = new ArrayList();
107         al.add(getSearch());
108 
109         return al;
110     }
111 
112     public String getTitle()
113     {
114         return title;
115     }
116 
117     public void setTitle(String title)
118     {
119         this.title = title;
120     }
121 
122     public Object[] getMenu()
123     {
124         //ArrayList al = new ArrayList();
125         Object[] menu = new Object[3];
126         for (int i = 0; i < 3; i++)
127         {
128             Hashtable item = new Hashtable();
129             item.put("id", "item" + Integer.toString(i+1));
130             item.put("name", "name" + Integer.toString(i+1));
131             item.put("label", "label" + Integer.toString(i+1));
132             //al.add(item);
133             menu[i] = item;
134         }
135 
136         //return al;
137         return menu;
138     }
139 
140     public ArrayList getCustomers()
141     {
142         ArrayList list = new ArrayList();
143 
144         list.add("ArrayList element 1");
145         list.add("ArrayList element 2");
146         list.add("ArrayList element 3");
147         list.add("ArrayList element 4");
148 
149         return list;
150     }
151 
152     public ArrayList getCustomers2()
153     {
154         ArrayList list = new ArrayList();
155 
156         list.add(new TestProvider());
157         list.add(new TestProvider());
158         list.add(new TestProvider());
159         list.add(new TestProvider());
160 
161         return list;
162     }
163 
164     public Object me()
165     {
166         return this;
167     }
168 
169     public String toString()
170     {
171         return ("test provider");
172     }
173 
174     public Vector getVector()
175     {
176         Vector list = new Vector();
177 
178         list.addElement("vector element 1");
179         list.addElement("vector element 2");
180 
181         return list;
182     }
183 
184     public String[] getArray()
185     {
186         String[] strings = new String[2];
187         strings[0] = "first element";
188         strings[1] = "second element";
189         return strings;
190     }
191 
192     public boolean theAPLRules()
193     {
194         return true;
195     }
196 
197     public boolean getStateTrue()
198     {
199         return true;
200     }
201 
202     public boolean getStateFalse()
203     {
204         return false;
205     }
206 
207     public String objectArrayMethod(Object[] o)
208     {
209         return "result of objectArrayMethod";
210     }
211 
212     public String concat(Object[] strings)
213     {
214         StringBuffer result = new StringBuffer();
215 
216         for (int i = 0; i < strings.length; i++)
217         {
218             result.append((String) strings[i]).append(' ');
219         }
220 
221         return result.toString();
222     }
223 
224     public String concat(List strings)
225     {
226         StringBuffer result = new StringBuffer();
227 
228         for (int i = 0; i < strings.size(); i++)
229         {
230             result.append((String) strings.get(i)).append(' ');
231         }
232 
233         return result.toString();
234     }
235 
236     public String objConcat(List objects)
237     {
238         StringBuffer result = new StringBuffer();
239 
240         for (int i = 0; i < objects.size(); i++)
241         {
242             result.append(objects.get(i)).append(' ');
243         }
244 
245         return result.toString();
246     }
247 
248     public String parse(String a, Object o, String c, String d)
249     {
250         return a + o.toString() + c + d;
251     }
252 
253     public String concat(String a, String b)
254     {
255         return a + b;
256     }
257 
258     // These two are for testing subclasses.
259 
260     public Person getPerson()
261     {
262         return new Person();
263     }
264 
265     public Child getChild()
266     {
267         return new Child();
268     }
269 
270     public String showPerson(Person person)
271     {
272         return person.getName();
273     }
274 
275     /**
276      * Chop i characters off the end of a string.
277      *
278      * @param string String to chop.
279      * @param i Number of characters to chop.
280      * @return String with processed answer.
281      */
282     public String chop(String string, int i)
283     {
284         return(string.substring(0, string.length() - i));
285     }
286 
287     public boolean allEmpty(Object[] list)
288     {
289         int size = list.length;
290 
291         for (int i = 0; i < size; i++)
292             if (list[i].toString().length() > 0)
293                 return false;
294 
295         return true;
296     }
297 
298     /*
299      * This can't have the signature
300      *
301      *    public void setState(boolean state)
302      *
303      *    or dynamically invoking the method
304      *    doesn't work ... you would have to
305      *    put a wrapper around a method for a
306      *    real boolean property that takes a
307      *    Boolean object if you wanted this to
308      *    work. Not really sure how useful it
309      *    is anyway. Who cares about boolean
310      *    values you can just set a variable.
311      *
312      */
313 
314     public void setState(Boolean state)
315     {
316     }
317 
318     public void setBangStart( Integer i )
319     {
320         System.out.println("SetBangStart() : called with val = " + i );
321         stateint = i.intValue();
322     }
323     public Integer bang()
324     {
325         System.out.println("Bang! : " + stateint );
326         Integer ret = new Integer( stateint );
327         stateint++;
328         return ret;
329     }
330 
331     /**
332      * Test the ability of vel to use a get(key)
333      * method for any object type, not just one
334      * that implements the Map interface.
335      */
336     public String get(String key)
337     {
338         return key;
339     }
340 
341     /**
342      * Test the ability of vel to use a put(key)
343      * method for any object type, not just one
344      * that implements the Map interface.
345      */
346     public String put(String key, Object o)
347     {
348         ob = o;
349         return key;
350     }
351 
352     public String getFoo()
353         throws Exception
354     {
355         throw new Exception("From getFoo()");
356     }
357 
358     public String getThrow()
359         throws Exception
360     {
361        throw new Exception("From getThrow()");
362     }
363 }