Source Code Cross Referenced for UtilFormatOut.java in  » ERP-CRM-Financial » SourceTap-CRM » org » ofbiz » base » util » 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 » ERP CRM Financial » SourceTap CRM » org.ofbiz.base.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: UtilFormatOut.java,v 1.6 2004/02/07 09:42:52 jonesde Exp $
003:         *
004:         *  Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005:         *
006:         *  Permission is hereby granted, free of charge, to any person obtaining a
007:         *  copy of this software and associated documentation files (the "Software"),
008:         *  to deal in the Software without restriction, including without limitation
009:         *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
010:         *  and/or sell copies of the Software, and to permit persons to whom the
011:         *  Software is furnished to do so, subject to the following conditions:
012:         *
013:         *  The above copyright notice and this permission notice shall be included
014:         *  in all copies or substantial portions of the Software.
015:         *
016:         *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017:         *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018:         *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019:         *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020:         *  CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021:         *  OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022:         *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023:         */
024:        package org.ofbiz.base.util;
025:
026:        import java.text.DateFormat;
027:        import java.text.DecimalFormat;
028:        import java.text.NumberFormat;
029:        import java.util.Locale;
030:        import java.util.Currency;
031:
032:        /**
033:         * General output formatting functions - mainly for helping in JSPs
034:         *
035:         * @author     <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
036:         * @author     <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
037:         * @version    $Revision: 1.6 $
038:         * @since      2.0
039:         */
040:        public class UtilFormatOut {
041:
042:            public static final String module = UtilFormatOut.class.getName();
043:
044:            public static String safeToString(Object obj) {
045:                if (obj != null) {
046:                    return obj.toString();
047:                } else {
048:                    return "";
049:                }
050:            }
051:
052:            // ------------------- price format handlers -------------------
053:            static DecimalFormat priceDecimalFormat = new DecimalFormat(
054:                    "#,##0.00");
055:
056:            /** Formats a Double representing a price into a string
057:             * @param price The price Double to be formatted
058:             * @return A String with the formatted price
059:             */
060:            public static String formatPrice(Double price) {
061:                if (price == null)
062:                    return "";
063:                return formatPrice(price.doubleValue());
064:            }
065:
066:            /** Formats a double representing a price into a string
067:             * @param price The price double to be formatted
068:             * @return A String with the formatted price
069:             */
070:            public static String formatPrice(double price) {
071:                return priceDecimalFormat.format(price);
072:            }
073:
074:            /** Formats a double into a properly formatted currency string based on isoCode and Locale
075:             * @param price The price double to be formatted
076:             * @param isoCode the currency ISO code
077:             * @param locale The Locale used to format the number
078:             * @return A String with the formatted price
079:             */
080:            public static String formatCurrency(double price, String isoCode,
081:                    Locale locale) {
082:                //Debug.logInfo("formatting currency: " + price + ", isoCode: " + isoCode + ", locale: " + locale, module);
083:                NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
084:                if (isoCode != null && isoCode.length() > 1) {
085:                    nf.setCurrency(Currency.getInstance(isoCode));
086:                } else {
087:                    Debug.logWarning(
088:                            "No isoCode specified to format currency value:"
089:                                    + price, module);
090:                }
091:                return nf.format(price);
092:            }
093:
094:            /** Formats a double into a properly formatted currency string based on isoCode and Locale
095:             * @param price The price Double to be formatted
096:             * @param isoCode the currency ISO code
097:             * @param locale The Locale used to format the number
098:             * @return A String with the formatted price
099:             */
100:            public static String formatCurrency(Double price, String isoCode,
101:                    Locale locale) {
102:                return formatCurrency(price.doubleValue(), isoCode, locale);
103:            }
104:
105:            // ------------------- percentage format handlers -------------------
106:            static DecimalFormat percentageDecimalFormat = new DecimalFormat(
107:                    "##0.##%");
108:
109:            /** Formats a Double representing a percentage into a string
110:             * @param percentage The percentage Double to be formatted
111:             * @return A String with the formatted percentage
112:             */
113:            public static String formatPercentage(Double percentage) {
114:                if (percentage == null)
115:                    return "";
116:                return formatPercentage(percentage.doubleValue());
117:            }
118:
119:            /** Formats a double representing a percentage into a string
120:             * @param percentage The percentage double to be formatted
121:             * @return A String with the formatted percentage
122:             */
123:            public static String formatPercentage(double percentage) {
124:                return percentageDecimalFormat.format(percentage);
125:            }
126:
127:            // ------------------- quantity format handlers -------------------
128:            static DecimalFormat quantityDecimalFormat = new DecimalFormat(
129:                    "#,##0.###");
130:
131:            /** Formats an Long representing a quantity into a string
132:             * @param quantity The quantity Long to be formatted
133:             * @return A String with the formatted quantity
134:             */
135:            public static String formatQuantity(Long quantity) {
136:                if (quantity == null)
137:                    return "";
138:                else
139:                    return formatQuantity(quantity.doubleValue());
140:            }
141:
142:            /** Formats an int representing a quantity into a string
143:             * @param quantity The quantity long to be formatted
144:             * @return A String with the formatted quantity
145:             */
146:            public static String formatQuantity(long quantity) {
147:                return formatQuantity((double) quantity);
148:            }
149:
150:            /** Formats an Integer representing a quantity into a string
151:             * @param quantity The quantity Integer to be formatted
152:             * @return A String with the formatted quantity
153:             */
154:            public static String formatQuantity(Integer quantity) {
155:                if (quantity == null)
156:                    return "";
157:                else
158:                    return formatQuantity(quantity.doubleValue());
159:            }
160:
161:            /** Formats an int representing a quantity into a string
162:             * @param quantity The quantity int to be formatted
163:             * @return A String with the formatted quantity
164:             */
165:            public static String formatQuantity(int quantity) {
166:                return formatQuantity((double) quantity);
167:            }
168:
169:            /** Formats a Float representing a quantity into a string
170:             * @param quantity The quantity Float to be formatted
171:             * @return A String with the formatted quantity
172:             */
173:            public static String formatQuantity(Float quantity) {
174:                if (quantity == null)
175:                    return "";
176:                else
177:                    return formatQuantity(quantity.doubleValue());
178:            }
179:
180:            /** Formats a float representing a quantity into a string
181:             * @param quantity The quantity float to be formatted
182:             * @return A String with the formatted quantity
183:             */
184:            public static String formatQuantity(float quantity) {
185:                return formatQuantity((double) quantity);
186:            }
187:
188:            /** Formats an Double representing a quantity into a string
189:             * @param quantity The quantity Double to be formatted
190:             * @return A String with the formatted quantity
191:             */
192:            public static String formatQuantity(Double quantity) {
193:                if (quantity == null)
194:                    return "";
195:                else
196:                    return formatQuantity(quantity.doubleValue());
197:            }
198:
199:            /** Formats an double representing a quantity into a string
200:             * @param quantity The quantity double to be formatted
201:             * @return A String with the formatted quantity
202:             */
203:            public static String formatQuantity(double quantity) {
204:                return quantityDecimalFormat.format(quantity);
205:            }
206:
207:            public static String formatPaddedNumber(long number,
208:                    int numericPadding) {
209:                StringBuffer outStrBfr = new StringBuffer(Long.toString(number));
210:                while (numericPadding > outStrBfr.length()) {
211:                    outStrBfr.insert(0, '0');
212:                }
213:                return outStrBfr.toString();
214:            }
215:
216:            public static String formatPaddingRemove(String original) {
217:                if (original == null)
218:                    return null;
219:                StringBuffer orgBuf = new StringBuffer(original);
220:                while (orgBuf.length() > 0 && orgBuf.charAt(0) == '0') {
221:                    orgBuf.deleteCharAt(0);
222:                }
223:                return orgBuf.toString();
224:            }
225:
226:            // ------------------- date handlers -------------------          
227:            /** Formats a String timestamp into a nice string
228:             * @param timestamp String timestamp to be formatted
229:             * @return A String with the formatted date/time
230:             */
231:            public static String formatDate(java.sql.Timestamp timestamp) {
232:                if (timestamp == null)
233:                    return "";
234:                DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,
235:                        DateFormat.FULL);
236:                java.util.Date date = (java.util.Date) timestamp;
237:                return df.format(date);
238:            }
239:
240:            // ------------------- null string handlers -------------------
241:            /** Checks to see if the passed Object is null, if it is returns an empty but non-null string, otherwise calls toString() on the object
242:             * @param obj1 The passed Object
243:             * @return The toString() of the passed Object if not null, otherwise an empty non-null String
244:             */
245:            public static String makeString(Object obj1) {
246:                if (obj1 != null)
247:                    return obj1.toString();
248:                else
249:                    return "";
250:            }
251:
252:            /** Checks to see if the passed string is null, if it is returns an empty but non-null string.
253:             * @param string1 The passed String
254:             * @return The passed String if not null, otherwise an empty non-null String
255:             */
256:            public static String checkNull(String string1) {
257:                if (string1 != null)
258:                    return string1;
259:                else
260:                    return "";
261:            }
262:
263:            /** Returns the first passed String if not null, otherwise the second if not null, otherwise an empty but non-null String.
264:             * @param string1 The first passed String
265:             * @param string2 The second passed String
266:             * @return The first passed String if not null, otherwise the second if not null, otherwise an empty but non-null String
267:             */
268:            public static String checkNull(String string1, String string2) {
269:                if (string1 != null)
270:                    return string1;
271:                else if (string2 != null)
272:                    return string2;
273:                else
274:                    return "";
275:            }
276:
277:            /** Returns the first passed String if not null, otherwise the second if not null, otherwise the third if not null, otherwise an empty but non-null String.
278:             * @param string1 The first passed String
279:             * @param string2 The second passed String
280:             * @param string3 The third passed String
281:             * @return The first passed String if not null, otherwise the second if not null, otherwise the third if not null, otherwise an empty but non-null String
282:             */
283:            public static String checkNull(String string1, String string2,
284:                    String string3) {
285:                if (string1 != null)
286:                    return string1;
287:                else if (string2 != null)
288:                    return string2;
289:                else if (string3 != null)
290:                    return string3;
291:                else
292:                    return "";
293:            }
294:
295:            /** Returns the first passed String if not null, otherwise the second if not null, otherwise the third if not null, otherwise the fourth if not null, otherwise an empty but non-null String.
296:             * @param string1 The first passed String
297:             * @param string2 The second passed String
298:             * @param string3 The third passed String
299:             * @param string4 The fourth passed String
300:             * @return The first passed String if not null, otherwise the second if not null, otherwise the third if not null, otherwise the fourth if not null, otherwise an empty but non-null String
301:             */
302:            public static String checkNull(String string1, String string2,
303:                    String string3, String string4) {
304:                if (string1 != null)
305:                    return string1;
306:                else if (string2 != null)
307:                    return string2;
308:                else if (string3 != null)
309:                    return string3;
310:                else if (string4 != null)
311:                    return string4;
312:                else
313:                    return "";
314:            }
315:
316:            /** Returns <code>pre + base + post</code> if base String is not null or empty, otherwise an empty but non-null String.
317:             * @param base The base String
318:             * @param pre The pre String
319:             * @param post The post String
320:             * @return <code>pre + base + post</code> if base String is not null or empty, otherwise an empty but non-null String.
321:             */
322:            public static String ifNotEmpty(String base, String pre, String post) {
323:                if (base != null && base.length() > 0)
324:                    return pre + base + post;
325:                else
326:                    return "";
327:            }
328:
329:            /** Returns the first passed String if not empty, otherwise the second if not empty, otherwise an empty but non-null String.
330:             * @param string1 The first passed String
331:             * @param string2 The second passed String
332:             * @return The first passed String if not empty, otherwise the second if not empty, otherwise an empty but non-null String
333:             */
334:            public static String checkEmpty(String string1, String string2) {
335:                if (string1 != null && string1.length() > 0)
336:                    return string1;
337:                else if (string2 != null && string2.length() > 0)
338:                    return string2;
339:                else
340:                    return "";
341:            }
342:
343:            /** Returns the first passed String if not empty, otherwise the second if not empty, otherwise the third if not empty, otherwise an empty but non-null String.
344:             * @param string1 The first passed String
345:             * @param string2 The second passed String
346:             * @param string3 The third passed String
347:             * @return The first passed String if not empty, otherwise the second if not empty, otherwise the third if not empty, otherwise an empty but non-null String
348:             */
349:            public static String checkEmpty(String string1, String string2,
350:                    String string3) {
351:                if (string1 != null && string1.length() > 0)
352:                    return string1;
353:                else if (string2 != null && string2.length() > 0)
354:                    return string2;
355:                else if (string3 != null && string3.length() > 0)
356:                    return string3;
357:                else
358:                    return "";
359:            }
360:
361:            // ------------------- web encode handlers -------------------
362:            /** Encodes an HTTP URL query String, replacing characters used for other things in HTTP URL query strings, but not touching the separator characters '?', '=', and '&'
363:             * @param query The plain query String
364:             * @return The encoded String
365:             */
366:            public static String encodeQuery(String query) {
367:                String retString;
368:
369:                retString = replaceString(query, "%", "%25");
370:                retString = replaceString(retString, " ", "%20");
371:                return retString;
372:            }
373:
374:            /** Encodes a single HTTP URL query value, replacing characters used for other things in HTTP URL query strings
375:             * @param query The plain query value String
376:             * @return The encoded String
377:             */
378:            public static String encodeQueryValue(String query) {
379:                String retString;
380:
381:                retString = replaceString(query, "%", "%25");
382:                retString = replaceString(retString, " ", "%20");
383:                retString = replaceString(retString, "&", "%26");
384:                retString = replaceString(retString, "?", "%3F");
385:                retString = replaceString(retString, "=", "%3D");
386:                return retString;
387:            }
388:
389:            /** Replaces all occurances of oldString in mainString with newString
390:             * @param mainString The original string
391:             * @param oldString The string to replace
392:             * @param newString The string to insert in place of the old
393:             * @return mainString with all occurances of oldString replaced by newString
394:             */
395:            public static String replaceString(String mainString,
396:                    String oldString, String newString) {
397:                return StringUtil.replaceString(mainString, oldString,
398:                        newString);
399:            }
400:
401:            /** Decodes a single query value from an HTTP URL parameter, replacing %ASCII values with characters
402:             * @param query The encoded query value String
403:             * @return The plain, decoded String
404:             */
405:            public static String decodeQueryValue(String query) {
406:                String retString;
407:
408:                retString = replaceString(query, "%25", "%");
409:                retString = replaceString(retString, "%20", " ");
410:                retString = replaceString(retString, "%26", "&");
411:                retString = replaceString(retString, "%3F", "?");
412:                retString = replaceString(retString, "%3D", "=");
413:                return retString;
414:            }
415:
416:            // ------------------- web encode handlers -------------------
417:            /** Encodes an XML string replacing the characters '<', '>', '"', ''', '&'
418:             * @param inString The plain value String
419:             * @return The encoded String
420:             */
421:            public static String encodeXmlValue(String inString) {
422:                String retString = inString;
423:
424:                retString = StringUtil.replaceString(retString, "&", "&amp;");
425:                retString = StringUtil.replaceString(retString, "<", "&lt;");
426:                retString = StringUtil.replaceString(retString, ">", "&gt;");
427:                retString = StringUtil.replaceString(retString, "\"", "&quot;");
428:                retString = StringUtil.replaceString(retString, "'", "&apos;");
429:                return retString;
430:            }
431:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.