Source Code Cross Referenced for VelocityFormatter.java in  » Template-Engine » Velocity » org » apache » velocity » app » tools » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Template Engine » Velocity » org.apache.velocity.app.tools 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.velocity.app.tools;
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.lang.reflect.Array;
023:        import java.text.DateFormat;
024:        import java.util.Date;
025:        import java.util.List;
026:
027:        import org.apache.velocity.context.Context;
028:
029:        /**
030:         * Formatting tool for inserting into the Velocity WebContext.  Can
031:         * format dates or lists of objects.
032:         *
033:         * <p>Here's an example of some uses:
034:         *
035:         * <code><pre>
036:         * $formatter.formatShortDate($object.Date)
037:         * $formatter.formatLongDate($db.getRecord(232).getDate())
038:         * $formatter.formatArray($array)
039:         * $formatter.limitLen(30, $object.Description)
040:         * </pre></code>
041:         *
042:         * @deprecated This servlet has been replaced by NumberTool and DateTool,
043:         * both available from the Velocity-Tools sub-project.
044:         * VelocityFormatter will be removed in a future version of Velocity.
045:         *
046:         * @author <a href="sean@somacity.com">Sean Legassick</a>
047:         * @author <a href="dlr@collab.net">Daniel Rall</a>
048:         * @version $Id: VelocityFormatter.java 463298 2006-10-12 16:10:32Z henning $
049:         */
050:        public class VelocityFormatter {
051:            Context context = null;
052:
053:            /**
054:             * Constructor needs a backpointer to the context.
055:             *
056:             * @param context A Context.
057:             */
058:            public VelocityFormatter(Context context) {
059:                this .context = context;
060:            }
061:
062:            /**
063:             * Formats a date in <code>DateFormat.SHORT</code> style.
064:             *
065:             * @param date The date to format.
066:             * @return The formatted date as text.
067:             */
068:            public String formatShortDate(Date date) {
069:                return DateFormat.getDateInstance(DateFormat.SHORT)
070:                        .format(date);
071:            }
072:
073:            /**
074:             * Formats a date in <code>DateFormat.LONG</code> style.
075:             *
076:             * @param date The date to format.
077:             * @return The formatted date as text.
078:             */
079:            public String formatLongDate(Date date) {
080:                return DateFormat.getDateInstance(DateFormat.LONG).format(date);
081:            }
082:
083:            /**
084:             * Formats a date/time in 'short' style.
085:             *
086:             * @param date The date to format.
087:             * @return The formatted date as text.
088:             */
089:            public String formatShortDateTime(Date date) {
090:                return DateFormat.getDateTimeInstance(DateFormat.SHORT,
091:                        DateFormat.SHORT).format(date);
092:            }
093:
094:            /**
095:             * Formats a date/time in 'long' style.
096:             *
097:             * @param date The date to format.
098:             * @return The formatted date as text.
099:             */
100:            public String formatLongDateTime(Date date) {
101:                return DateFormat.getDateTimeInstance(DateFormat.LONG,
102:                        DateFormat.LONG).format(date);
103:            }
104:
105:            /**
106:             * Formats an array into the form "A, B and C".
107:             *
108:             * @param array An Object.
109:             * @return A String.
110:             */
111:            public String formatArray(Object array) {
112:                return formatArray(array, ", ", " and ");
113:            }
114:
115:            /**
116:             * Formats an array into the form
117:             * "A&lt;delim&gt;B&lt;delim&gt;C".
118:             *
119:             * @param array An Object.
120:             * @param delim A String.
121:             * @return A String.
122:             */
123:            public String formatArray(Object array, String delim) {
124:                return formatArray(array, delim, delim);
125:            }
126:
127:            /**
128:             * Formats an array into the form
129:             * "A&lt;delim&gt;B&lt;finaldelim&gt;C".
130:             *
131:             * @param array An Object.
132:             * @param delim A String.
133:             * @param finaldelim A String.
134:             * @return A String.
135:             */
136:            public String formatArray(Object array, String delim,
137:                    String finaldelim) {
138:                StringBuffer sb = new StringBuffer();
139:                int arrayLen = Array.getLength(array);
140:                for (int i = 0; i < arrayLen; i++) {
141:                    // Use the Array.get method as this will automatically
142:                    // wrap primitive types in a suitable Object-derived
143:                    // wrapper if necessary.
144:                    sb.append(Array.get(array, i).toString());
145:                    if (i < arrayLen - 2) {
146:                        sb.append(delim);
147:                    } else if (i < arrayLen - 1) {
148:                        sb.append(finaldelim);
149:                    }
150:                }
151:                return sb.toString();
152:            }
153:
154:            /**
155:             * Formats a vector into the form "A, B and C".
156:             *
157:             * @param list The list of elements to format.
158:             * @return A String.
159:             */
160:            public String formatVector(List list) {
161:                return formatVector(list, ", ", " and ");
162:            }
163:
164:            /**
165:             * Formats a vector into the form "A&lt;delim&gt;B&lt;delim&gt;C".
166:             *
167:             * @param list The list of elements to format.
168:             * @param delim A String.
169:             * @return A String.
170:             */
171:            public String formatVector(List list, String delim) {
172:                return formatVector(list, delim, delim);
173:            }
174:
175:            /**
176:             * Formats a list into the form
177:             * "Adelim&gt;B&lt;finaldelim&gt;C".
178:             *
179:             * @param list The list of elements to format.
180:             * @param delim A String.
181:             * @param finaldelim A String.
182:             * @return A String.
183:             */
184:            public String formatVector(List list, String delim,
185:                    String finaldelim) {
186:                StringBuffer sb = new StringBuffer();
187:                int size = list.size();
188:                for (int i = 0; i < size; i++) {
189:                    sb.append(list.get(i));
190:                    if (i < size - 2) {
191:                        sb.append(delim);
192:                    } else if (i < size - 1) {
193:                        sb.append(finaldelim);
194:                    }
195:                }
196:                return sb.toString();
197:            }
198:
199:            /**
200:             * Limits 'string' to 'maxlen' characters.  If the string gets
201:             * curtailed, "..." is appended to it.
202:             *
203:             * @param maxlen An int with the maximum length.
204:             * @param string A String.
205:             * @return A String.
206:             */
207:            public String limitLen(int maxlen, String string) {
208:                return limitLen(maxlen, string, "...");
209:            }
210:
211:            /**
212:             * Limits 'string' to 'maxlen' character.  If the string gets
213:             * curtailed, 'suffix' is appended to it.
214:             *
215:             * @param maxlen An int with the maximum length.
216:             * @param string A String.
217:             * @param suffix A String.
218:             * @return A String.
219:             */
220:            public String limitLen(int maxlen, String string, String suffix) {
221:                String ret = string;
222:                if (string.length() > maxlen) {
223:                    ret = string.substring(0, maxlen - suffix.length())
224:                            + suffix;
225:                }
226:                return ret;
227:            }
228:
229:            /**
230:             * Class that returns alternating values in a template.  It stores
231:             * a list of alternate Strings, whenever alternate() is called it
232:             * switches to the next in the list.  The current alternate is
233:             * retrieved through toString() - i.e. just by referencing the
234:             * object in a Velocity template.  For an example of usage see the
235:             * makeAlternator() method below.
236:             */
237:            public class VelocityAlternator {
238:                /**
239:                 *
240:                 */
241:                protected String[] alternates = null;
242:                /**
243:                 *
244:                 */
245:                protected int current = 0;
246:
247:                /**
248:                 * Constructor takes an array of Strings.
249:                 *
250:                 * @param alternates A String[].
251:                 */
252:                public VelocityAlternator(String[] alternates) {
253:                    this .alternates = alternates;
254:                }
255:
256:                /**
257:                 * Alternates to the next in the list.
258:                 *
259:                 * @return The current alternate in the sequence.
260:                 */
261:                public String alternate() {
262:                    current++;
263:                    current %= alternates.length;
264:                    return "";
265:                }
266:
267:                /**
268:                 * Returns the current alternate.
269:                 *
270:                 * @return A String.
271:                 */
272:                public String toString() {
273:                    return alternates[current];
274:                }
275:            }
276:
277:            /**
278:             * As VelocityAlternator, but calls <code>alternate()</code>
279:             * automatically on rendering in a template.
280:             */
281:            public class VelocityAutoAlternator extends VelocityAlternator {
282:                /**
283:                 * Constructor takes an array of Strings.
284:                 *
285:                 * @param alternates A String[].
286:                 */
287:                public VelocityAutoAlternator(String[] alternates) {
288:                    super (alternates);
289:                }
290:
291:                /**
292:                 * Returns the current alternate, and automatically alternates
293:                 * to the next alternate in its sequence (trigged upon
294:                 * rendering).
295:                 *
296:                 * @return The current alternate in the sequence.
297:                 */
298:                public final String toString() {
299:                    String s = alternates[current];
300:                    alternate();
301:                    return s;
302:                }
303:            }
304:
305:            /**
306:             * Makes an alternator object that alternates between two values.
307:             *
308:             * <p>Example usage in a Velocity template:
309:             *
310:             * <code><pre>
311:             * &lt;table&gt;
312:             * $formatter.makeAlternator("rowcolor", "#c0c0c0", "#e0e0e0")
313:             * #foreach $item in $items
314:             * #begin
315:             * &lt;tr&gt;&lt;td bgcolor="$rowcolor"&gt;$item.Name&lt;/td&gt;&lt;/tr&gt;
316:             * $rowcolor.alternate()
317:             * #end
318:             * &lt;/table&gt;
319:             * </pre></code>
320:             *
321:             * @param name The name for the alternator int the context.
322:             * @param alt1 The first alternate.
323:             * @param alt2 The second alternate.
324:             * @return The newly created instance.
325:             */
326:            public String makeAlternator(String name, String alt1, String alt2) {
327:                String[] alternates = { alt1, alt2 };
328:                context.put(name, new VelocityAlternator(alternates));
329:                return "";
330:            }
331:
332:            /**
333:             * Makes an alternator object that alternates between three
334:             * values.
335:             * @param name
336:             * @param alt1
337:             * @param alt2
338:             * @param alt3
339:             * @return alternated object.
340:             *
341:             * @see #makeAlternator(String name, String alt1, String alt2)
342:             */
343:            public String makeAlternator(String name, String alt1, String alt2,
344:                    String alt3) {
345:                String[] alternates = { alt1, alt2, alt3 };
346:                context.put(name, new VelocityAlternator(alternates));
347:                return "";
348:            }
349:
350:            /**
351:             * Makes an alternator object that alternates between four values.
352:             * @param name
353:             * @param alt1
354:             * @param alt2
355:             * @param alt3
356:             * @param alt4
357:             * @return Alternated object.
358:             *
359:             * @see #makeAlternator(String name, String alt1, String alt2)
360:             */
361:            public String makeAlternator(String name, String alt1, String alt2,
362:                    String alt3, String alt4) {
363:                String[] alternates = { alt1, alt2, alt3, alt4 };
364:                context.put(name, new VelocityAlternator(alternates));
365:                return "";
366:            }
367:
368:            /**
369:             * Makes an alternator object that alternates between two values
370:             * automatically.
371:             * @param name
372:             * @param alt1
373:             * @param alt2
374:             * @return Alternated object.
375:             *
376:             * @see #makeAlternator(String name, String alt1, String alt2)
377:             */
378:            public String makeAutoAlternator(String name, String alt1,
379:                    String alt2) {
380:                String[] alternates = { alt1, alt2 };
381:                context.put(name, new VelocityAutoAlternator(alternates));
382:                return "";
383:            }
384:
385:            /**
386:             * Returns a default value if the object passed is null.
387:             * @param o
388:             * @param dflt
389:             * @return Object or default value when object is null.
390:             */
391:            public Object isNull(Object o, Object dflt) {
392:                if (o == null) {
393:                    return dflt;
394:                } else {
395:                    return o;
396:                }
397:            }
398:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.