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


001:        /*******************************************************************************
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements.  See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership.  The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License.  You may obtain a copy of the License at
009:         * 
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         * 
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied.  See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         *******************************************************************************/package org.ofbiz.base.util;
019:
020:        import java.text.DateFormat;
021:        import java.text.DecimalFormat;
022:        import java.text.ParseException;
023:        import java.util.Locale;
024:
025:        /**
026:         * General output formatting functions - mainly for helping in JSPs
027:         */
028:        public class UtilFormatOut {
029:
030:            public static final String module = UtilFormatOut.class.getName();
031:
032:            public static String safeToString(Object obj) {
033:                if (obj != null) {
034:                    return obj.toString();
035:                } else {
036:                    return "";
037:                }
038:            }
039:
040:            // ------------------- price format handlers -------------------
041:            static DecimalFormat priceDecimalFormat = new DecimalFormat(
042:                    "#,##0.00");
043:            static DecimalFormat priceNumberFormat = new DecimalFormat("##0.00");
044:
045:            /** Formats a Double representing a price into a string
046:             * @param price The price Double to be formatted
047:             * @return A String with the formatted price
048:             */
049:            public static String formatPrice(Double price) {
050:                if (price == null)
051:                    return "";
052:                return formatPrice(price.doubleValue());
053:            }
054:
055:            /** Formats a double representing a price into a string
056:             * @param price The price double to be formatted
057:             * @return A String with the formatted price
058:             */
059:            public static String formatPrice(double price) {
060:                return priceDecimalFormat.format(price);
061:            }
062:
063:            public static Double formatPriceNumber(double price) {
064:                try {
065:                    return new Double(priceDecimalFormat.parse(
066:                            formatPrice(price)).doubleValue());
067:                } catch (ParseException e) {
068:                    Debug.logError(e, module);
069:                    return new Double(price);
070:                }
071:            }
072:
073:            /** Formats a double into a properly formatted currency string based on isoCode and Locale
074:             * @param price The price double to be formatted
075:             * @param isoCode the currency ISO code
076:             * @param locale The Locale used to format the number
077:             * @param maximumFractionDigits The maximum number of fraction digits used; if set to -1 than the default value for the locale is used
078:             * @return A String with the formatted price
079:             */
080:            public static String formatCurrency(double price, String isoCode,
081:                    Locale locale, int maximumFractionDigits) {
082:                //Debug.logInfo("formatting currency: " + price + ", isoCode: " + isoCode + ", locale: " + locale, module);
083:                com.ibm.icu.text.NumberFormat nf = com.ibm.icu.text.NumberFormat
084:                        .getCurrencyInstance(locale);
085:                if (isoCode != null && isoCode.length() > 1) {
086:                    nf.setCurrency(com.ibm.icu.util.Currency
087:                            .getInstance(isoCode));
088:                } else {
089:                    Debug.logWarning(
090:                            "No isoCode specified to format currency value:"
091:                                    + price, module);
092:                }
093:                if (maximumFractionDigits >= 0) {
094:                    nf.setMaximumFractionDigits(maximumFractionDigits);
095:                }
096:                return nf.format(price);
097:            }
098:
099:            /** Formats a double into a properly formatted currency string based on isoCode and Locale
100:             * @param price The price double to be formatted
101:             * @param isoCode the currency ISO code
102:             * @param locale The Locale used to format the number
103:             * @return A String with the formatted price
104:             */
105:            public static String formatCurrency(double price, String isoCode,
106:                    Locale locale) {
107:                return formatCurrency(price, isoCode, locale, -1);
108:            }
109:
110:            /** Formats a double into a properly formatted currency string based on isoCode and Locale
111:             * @param price The price Double to be formatted
112:             * @param isoCode the currency ISO code
113:             * @param locale The Locale used to format the number
114:             * @param maximumFractionDigits The maximum number of fraction digits used; if set to -1 than the default value for the locale is used
115:             * @return A String with the formatted price
116:             */
117:            public static String formatCurrency(Double price, String isoCode,
118:                    Locale locale, int maximumFractionDigits) {
119:                return formatCurrency(price.doubleValue(), isoCode, locale,
120:                        maximumFractionDigits);
121:            }
122:
123:            /** Formats a double into a properly formatted currency string based on isoCode and Locale
124:             * @param price The price Double to be formatted
125:             * @param isoCode the currency ISO code
126:             * @param locale The Locale used to format the number
127:             * @return A String with the formatted price
128:             */
129:            public static String formatCurrency(Double price, String isoCode,
130:                    Locale locale) {
131:                return formatCurrency(price.doubleValue(), isoCode, locale, -1);
132:            }
133:
134:            /** Formats a Double into a properly spelled out number string based on Locale
135:             * @param amount The amount Double to be formatted
136:             * @param locale The Locale used to format the number
137:             * @return A String with the formatted number
138:             */
139:            public static String formatSpelledOutAmount(Double amount,
140:                    Locale locale) {
141:                return formatSpelledOutAmount(amount.doubleValue(), locale);
142:            }
143:
144:            /** Formats a double into a properly spelled out number string based on Locale
145:             * @param amount The amount double to be formatted
146:             * @param locale The Locale used to format the number
147:             * @return A String with the formatted number
148:             */
149:            public static String formatSpelledOutAmount(double amount,
150:                    Locale locale) {
151:                //Debug.logInfo("formatting currency: " + price + ", isoCode: " + isoCode + ", locale: " + locale, module);
152:                com.ibm.icu.text.NumberFormat nf = new com.ibm.icu.text.RuleBasedNumberFormat(
153:                        locale, com.ibm.icu.text.RuleBasedNumberFormat.SPELLOUT);
154:                return nf.format(amount);
155:            }
156:
157:            /** Formats a double into a properly formatted string, with two decimals, based on Locale
158:             * @param amount The amount double to be formatted
159:             * @param locale The Locale used to format the number
160:             * @return A String with the formatted amount
161:             */
162:            // This method should be used in place of formatPrice because it is locale aware.
163:            public static String formatAmount(double amount, Locale locale) {
164:                com.ibm.icu.text.NumberFormat nf = com.ibm.icu.text.NumberFormat
165:                        .getInstance(locale);
166:                nf.setMinimumFractionDigits(2);
167:                nf.setMaximumFractionDigits(2);
168:                return nf.format(amount);
169:            }
170:
171:            // ------------------- percentage format handlers -------------------
172:            static DecimalFormat percentageDecimalFormat = new DecimalFormat(
173:                    "##0.##%");
174:
175:            /** Formats a Double representing a percentage into a string
176:             * @param percentage The percentage Double to be formatted
177:             * @return A String with the formatted percentage
178:             */
179:            public static String formatPercentage(Double percentage) {
180:                if (percentage == null)
181:                    return "";
182:                return formatPercentage(percentage.doubleValue());
183:            }
184:
185:            /** Formats a double representing a percentage into a string
186:             * @param percentage The percentage double to be formatted
187:             * @return A String with the formatted percentage
188:             */
189:            public static String formatPercentage(double percentage) {
190:                return percentageDecimalFormat.format(percentage);
191:            }
192:
193:            // ------------------- quantity format handlers -------------------
194:            static DecimalFormat quantityDecimalFormat = new DecimalFormat(
195:                    "#,##0.###");
196:
197:            /** Formats an Long representing a quantity into a string
198:             * @param quantity The quantity Long to be formatted
199:             * @return A String with the formatted quantity
200:             */
201:            public static String formatQuantity(Long quantity) {
202:                if (quantity == null)
203:                    return "";
204:                else
205:                    return formatQuantity(quantity.doubleValue());
206:            }
207:
208:            /** Formats an int representing a quantity into a string
209:             * @param quantity The quantity long to be formatted
210:             * @return A String with the formatted quantity
211:             */
212:            public static String formatQuantity(long quantity) {
213:                return formatQuantity((double) quantity);
214:            }
215:
216:            /** Formats an Integer representing a quantity into a string
217:             * @param quantity The quantity Integer to be formatted
218:             * @return A String with the formatted quantity
219:             */
220:            public static String formatQuantity(Integer quantity) {
221:                if (quantity == null)
222:                    return "";
223:                else
224:                    return formatQuantity(quantity.doubleValue());
225:            }
226:
227:            /** Formats an int representing a quantity into a string
228:             * @param quantity The quantity int to be formatted
229:             * @return A String with the formatted quantity
230:             */
231:            public static String formatQuantity(int quantity) {
232:                return formatQuantity((double) quantity);
233:            }
234:
235:            /** Formats a Float representing a quantity into a string
236:             * @param quantity The quantity Float to be formatted
237:             * @return A String with the formatted quantity
238:             */
239:            public static String formatQuantity(Float quantity) {
240:                if (quantity == null)
241:                    return "";
242:                else
243:                    return formatQuantity(quantity.doubleValue());
244:            }
245:
246:            /** Formats a float representing a quantity into a string
247:             * @param quantity The quantity float to be formatted
248:             * @return A String with the formatted quantity
249:             */
250:            public static String formatQuantity(float quantity) {
251:                return formatQuantity((double) quantity);
252:            }
253:
254:            /** Formats an Double representing a quantity into a string
255:             * @param quantity The quantity Double to be formatted
256:             * @return A String with the formatted quantity
257:             */
258:            public static String formatQuantity(Double quantity) {
259:                if (quantity == null)
260:                    return "";
261:                else
262:                    return formatQuantity(quantity.doubleValue());
263:            }
264:
265:            /** Formats an double representing a quantity into a string
266:             * @param quantity The quantity double to be formatted
267:             * @return A String with the formatted quantity
268:             */
269:            public static String formatQuantity(double quantity) {
270:                return quantityDecimalFormat.format(quantity);
271:            }
272:
273:            public static String formatPaddedNumber(long number,
274:                    int numericPadding) {
275:                StringBuffer outStrBfr = new StringBuffer(Long.toString(number));
276:                while (numericPadding > outStrBfr.length()) {
277:                    outStrBfr.insert(0, '0');
278:                }
279:                return outStrBfr.toString();
280:            }
281:
282:            public static String formatPaddingRemove(String original) {
283:                if (original == null)
284:                    return null;
285:                StringBuffer orgBuf = new StringBuffer(original);
286:                while (orgBuf.length() > 0 && orgBuf.charAt(0) == '0') {
287:                    orgBuf.deleteCharAt(0);
288:                }
289:                return orgBuf.toString();
290:            }
291:
292:            // ------------------- date handlers -------------------          
293:            /** Formats a String timestamp into a nice string
294:             * @param timestamp String timestamp to be formatted
295:             * @return A String with the formatted date/time
296:             */
297:            public static String formatDate(java.sql.Timestamp timestamp) {
298:                if (timestamp == null)
299:                    return "";
300:                DateFormat df = DateFormat.getDateTimeInstance(DateFormat.LONG,
301:                        DateFormat.FULL);
302:                java.util.Date date = timestamp;
303:                return df.format(date);
304:            }
305:
306:            // ------------------- null string handlers -------------------
307:            /** 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
308:             * @param obj1 The passed Object
309:             * @return The toString() of the passed Object if not null, otherwise an empty non-null String
310:             */
311:            public static String makeString(Object obj1) {
312:                if (obj1 != null)
313:                    return obj1.toString();
314:                else
315:                    return "";
316:            }
317:
318:            /** Checks to see if the passed string is null, if it is returns an empty but non-null string.
319:             * @param string1 The passed String
320:             * @return The passed String if not null, otherwise an empty non-null String
321:             */
322:            public static String checkNull(String string1) {
323:                if (string1 != null)
324:                    return string1;
325:                else
326:                    return "";
327:            }
328:
329:            /** Returns the first passed String if not null, otherwise the second if not null, 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 null, otherwise the second if not null, otherwise an empty but non-null String
333:             */
334:            public static String checkNull(String string1, String string2) {
335:                if (string1 != null)
336:                    return string1;
337:                else if (string2 != null)
338:                    return string2;
339:                else
340:                    return "";
341:            }
342:
343:            /** 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.
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 null, otherwise the second if not null, otherwise the third if not null, otherwise an empty but non-null String
348:             */
349:            public static String checkNull(String string1, String string2,
350:                    String string3) {
351:                if (string1 != null)
352:                    return string1;
353:                else if (string2 != null)
354:                    return string2;
355:                else if (string3 != null)
356:                    return string3;
357:                else
358:                    return "";
359:            }
360:
361:            /** 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.
362:             * @param string1 The first passed String
363:             * @param string2 The second passed String
364:             * @param string3 The third passed String
365:             * @param string4 The fourth passed String
366:             * @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
367:             */
368:            public static String checkNull(String string1, String string2,
369:                    String string3, String string4) {
370:                if (string1 != null)
371:                    return string1;
372:                else if (string2 != null)
373:                    return string2;
374:                else if (string3 != null)
375:                    return string3;
376:                else if (string4 != null)
377:                    return string4;
378:                else
379:                    return "";
380:            }
381:
382:            /** Returns <code>pre + base + post</code> if base String is not null or empty, otherwise an empty but non-null String.
383:             * @param base The base String
384:             * @param pre The pre String
385:             * @param post The post String
386:             * @return <code>pre + base + post</code> if base String is not null or empty, otherwise an empty but non-null String.
387:             */
388:            public static String ifNotEmpty(String base, String pre, String post) {
389:                if (base != null && base.length() > 0)
390:                    return pre + base + post;
391:                else
392:                    return "";
393:            }
394:
395:            /** Returns the first passed String if not empty, otherwise the second if not empty, otherwise an empty but non-null String.
396:             * @param string1 The first passed String
397:             * @param string2 The second passed String
398:             * @return The first passed String if not empty, otherwise the second if not empty, otherwise an empty but non-null String
399:             */
400:            public static String checkEmpty(String string1, String string2) {
401:                if (string1 != null && string1.length() > 0)
402:                    return string1;
403:                else if (string2 != null && string2.length() > 0)
404:                    return string2;
405:                else
406:                    return "";
407:            }
408:
409:            /** 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.
410:             * @param string1 The first passed String
411:             * @param string2 The second passed String
412:             * @param string3 The third passed String
413:             * @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
414:             */
415:            public static String checkEmpty(String string1, String string2,
416:                    String string3) {
417:                if (string1 != null && string1.length() > 0)
418:                    return string1;
419:                else if (string2 != null && string2.length() > 0)
420:                    return string2;
421:                else if (string3 != null && string3.length() > 0)
422:                    return string3;
423:                else
424:                    return "";
425:            }
426:
427:            // ------------------- web encode handlers -------------------
428:            /** Encodes an HTTP URL query String, replacing characters used for other things in HTTP URL query strings, but not touching the separator characters '?', '=', and '&'
429:             * @param query The plain query String
430:             * @return The encoded String
431:             */
432:            public static String encodeQuery(String query) {
433:                String retString;
434:
435:                retString = replaceString(query, "%", "%25");
436:                retString = replaceString(retString, " ", "%20");
437:                return retString;
438:            }
439:
440:            /** Encodes a single HTTP URL query value, replacing characters used for other things in HTTP URL query strings
441:             * @param query The plain query value String
442:             * @return The encoded String
443:             */
444:            public static String encodeQueryValue(String query) {
445:                String retString;
446:
447:                retString = replaceString(query, "%", "%25");
448:                retString = replaceString(retString, " ", "%20");
449:                retString = replaceString(retString, "&", "%26");
450:                retString = replaceString(retString, "?", "%3F");
451:                retString = replaceString(retString, "=", "%3D");
452:                return retString;
453:            }
454:
455:            /** Replaces all occurances of oldString in mainString with newString
456:             * @param mainString The original string
457:             * @param oldString The string to replace
458:             * @param newString The string to insert in place of the old
459:             * @return mainString with all occurances of oldString replaced by newString
460:             */
461:            public static String replaceString(String mainString,
462:                    String oldString, String newString) {
463:                return StringUtil.replaceString(mainString, oldString,
464:                        newString);
465:            }
466:
467:            /** Decodes a single query value from an HTTP URL parameter, replacing %ASCII values with characters
468:             * @param query The encoded query value String
469:             * @return The plain, decoded String
470:             */
471:            public static String decodeQueryValue(String query) {
472:                String retString;
473:
474:                retString = replaceString(query, "%25", "%");
475:                retString = replaceString(retString, "%20", " ");
476:                retString = replaceString(retString, "%26", "&");
477:                retString = replaceString(retString, "%3F", "?");
478:                retString = replaceString(retString, "%3D", "=");
479:                return retString;
480:            }
481:
482:            // ------------------- web encode handlers -------------------
483:            /** Encodes an XML string replacing the characters '<', '>', '"', ''', '&'
484:             * @param inString The plain value String
485:             * @return The encoded String
486:             */
487:            public static String encodeXmlValue(String inString) {
488:                String retString = inString;
489:
490:                retString = StringUtil.replaceString(retString, "&", "&amp;");
491:                retString = StringUtil.replaceString(retString, "<", "&lt;");
492:                retString = StringUtil.replaceString(retString, ">", "&gt;");
493:                retString = StringUtil.replaceString(retString, "\"", "&quot;");
494:                retString = StringUtil.replaceString(retString, "'", "&apos;");
495:                return retString;
496:            }
497:
498:            public static String padString(String str, int setLen,
499:                    boolean padEnd, char padChar) {
500:                if (str == null) {
501:                    return null;
502:                }
503:                if (setLen == 0) {
504:                    return str;
505:                }
506:                int stringLen = str.length();
507:                int diff = setLen - stringLen;
508:                if (diff < 0) {
509:                    return str.substring(0, setLen);
510:                } else {
511:                    String newString = new String();
512:                    if (padEnd) {
513:                        newString = newString + str;
514:                    }
515:                    for (int i = 0; i < diff; i++) {
516:                        newString = newString + padChar;
517:                    }
518:                    if (!padEnd) {
519:                        newString = newString + str;
520:                    }
521:                    return newString;
522:                }
523:            }
524:
525:            public static String makeSqlSafe(String unsafeString) {
526:                return unsafeString.replaceAll("'", "''");
527:            }
528:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.