001: package org.apache.velocity.test.provider;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.util.ArrayList;
023: import java.util.Hashtable;
024: import java.util.List;
025: import java.util.Stack;
026: import java.util.Vector;
027:
028: /**
029: * This class is used by the testbed. Instances of the class
030: * are fed into the context that is set before the AST
031: * is traversed and dynamic content generated.
032: *
033: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
034: * @version $Id: TestProvider.java 463298 2006-10-12 16:10:32Z henning $
035: */
036: public class TestProvider {
037: String title = "lunatic";
038: boolean state;
039: Object ob = null;
040:
041: public static String PUB_STAT_STRING = "Public Static String";
042:
043: int stateint = 0;
044:
045: public String getName() {
046: return "jason";
047: }
048:
049: public Stack getStack() {
050: Stack stack = new Stack();
051: stack.push("stack element 1");
052: stack.push("stack element 2");
053: stack.push("stack element 3");
054: return stack;
055: }
056:
057: public List getEmptyList() {
058: List list = new ArrayList();
059: return list;
060: }
061:
062: public List getList() {
063: List list = new ArrayList();
064: list.add("list element 1");
065: list.add("list element 2");
066: list.add("list element 3");
067:
068: return list;
069: }
070:
071: public Hashtable getSearch() {
072: Hashtable h = new Hashtable();
073: h.put("Text", "this is some text");
074: h.put("EscText", "this is escaped text");
075: h.put("Title", "this is the title");
076: h.put("Index", "this is the index");
077: h.put("URL", "http://periapt.com");
078:
079: ArrayList al = new ArrayList();
080: al.add(h);
081:
082: h.put("RelatedLinks", al);
083:
084: return h;
085: }
086:
087: public Hashtable getHashtable() {
088: Hashtable h = new Hashtable();
089: h.put("key0", "value0");
090: h.put("key1", "value1");
091: h.put("key2", "value2");
092:
093: return h;
094: }
095:
096: public ArrayList getRelSearches() {
097: ArrayList al = new ArrayList();
098: al.add(getSearch());
099:
100: return al;
101: }
102:
103: public String getTitle() {
104: return title;
105: }
106:
107: public void setTitle(String title) {
108: this .title = title;
109: }
110:
111: public Object[] getMenu() {
112: //ArrayList al = new ArrayList();
113: Object[] menu = new Object[3];
114: for (int i = 0; i < 3; i++) {
115: Hashtable item = new Hashtable();
116: item.put("id", "item" + Integer.toString(i + 1));
117: item.put("name", "name" + Integer.toString(i + 1));
118: item.put("label", "label" + Integer.toString(i + 1));
119: //al.add(item);
120: menu[i] = item;
121: }
122:
123: //return al;
124: return menu;
125: }
126:
127: public ArrayList getCustomers() {
128: ArrayList list = new ArrayList();
129:
130: list.add("ArrayList element 1");
131: list.add("ArrayList element 2");
132: list.add("ArrayList element 3");
133: list.add("ArrayList element 4");
134:
135: return list;
136: }
137:
138: public ArrayList getCustomers2() {
139: ArrayList list = new ArrayList();
140:
141: list.add(new TestProvider());
142: list.add(new TestProvider());
143: list.add(new TestProvider());
144: list.add(new TestProvider());
145:
146: return list;
147: }
148:
149: public Object me() {
150: return this ;
151: }
152:
153: public String toString() {
154: return ("test provider");
155: }
156:
157: public Vector getVector() {
158: Vector list = new Vector();
159:
160: list.addElement("vector element 1");
161: list.addElement("vector element 2");
162:
163: return list;
164: }
165:
166: public String[] getArray() {
167: String[] strings = new String[2];
168: strings[0] = "first element";
169: strings[1] = "second element";
170: return strings;
171: }
172:
173: public boolean theAPLRules() {
174: return true;
175: }
176:
177: public boolean getStateTrue() {
178: return true;
179: }
180:
181: public boolean getStateFalse() {
182: return false;
183: }
184:
185: public String objectArrayMethod(Object[] o) {
186: return "result of objectArrayMethod";
187: }
188:
189: public String concat(Object[] strings) {
190: StringBuffer result = new StringBuffer();
191:
192: for (int i = 0; i < strings.length; i++) {
193: result.append((String) strings[i]).append(' ');
194: }
195:
196: return result.toString();
197: }
198:
199: public String concat(List strings) {
200: StringBuffer result = new StringBuffer();
201:
202: for (int i = 0; i < strings.size(); i++) {
203: result.append((String) strings.get(i)).append(' ');
204: }
205:
206: return result.toString();
207: }
208:
209: public String objConcat(List objects) {
210: StringBuffer result = new StringBuffer();
211:
212: for (int i = 0; i < objects.size(); i++) {
213: result.append(objects.get(i)).append(' ');
214: }
215:
216: return result.toString();
217: }
218:
219: public String parse(String a, Object o, String c, String d) {
220: return a + o.toString() + c + d;
221: }
222:
223: public String concat(String a, String b) {
224: return a + b;
225: }
226:
227: // These two are for testing subclasses.
228:
229: public Person getPerson() {
230: return new Person();
231: }
232:
233: public Child getChild() {
234: return new Child();
235: }
236:
237: public String showPerson(Person person) {
238: return person.getName();
239: }
240:
241: /**
242: * Chop i characters off the end of a string.
243: *
244: * @param string String to chop.
245: * @param i Number of characters to chop.
246: * @return String with processed answer.
247: */
248: public String chop(String string, int i) {
249: return (string.substring(0, string.length() - i));
250: }
251:
252: public boolean allEmpty(Object[] list) {
253: int size = list.length;
254:
255: for (int i = 0; i < size; i++)
256: if (list[i].toString().length() > 0)
257: return false;
258:
259: return true;
260: }
261:
262: /*
263: * This can't have the signature
264: *
265: * public void setState(boolean state)
266: *
267: * or dynamically invoking the method
268: * doesn't work ... you would have to
269: * put a wrapper around a method for a
270: * real boolean property that takes a
271: * Boolean object if you wanted this to
272: * work. Not really sure how useful it
273: * is anyway. Who cares about boolean
274: * values you can just set a variable.
275: *
276: */
277:
278: public void setState(Boolean state) {
279: }
280:
281: public void setBangStart(Integer i) {
282: System.out.println("SetBangStart() : called with val = " + i);
283: stateint = i.intValue();
284: }
285:
286: public Integer bang() {
287: System.out.println("Bang! : " + stateint);
288: Integer ret = new Integer(stateint);
289: stateint++;
290: return ret;
291: }
292:
293: /**
294: * Test the ability of vel to use a get(key)
295: * method for any object type, not just one
296: * that implements the Map interface.
297: */
298: public String get(String key) {
299: return key;
300: }
301:
302: /**
303: * Test the ability of vel to use a put(key)
304: * method for any object type, not just one
305: * that implements the Map interface.
306: */
307: public String put(String key, Object o) {
308: ob = o;
309: return key;
310: }
311:
312: public String getFoo() throws Exception {
313: throw new Exception("From getFoo()");
314: }
315:
316: public String getThrow() throws Exception {
317: throw new Exception("From getThrow()");
318: }
319: }
|