Source Code Cross Referenced for StringLocaleConverter.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » locale » converters » 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 » Library » Apache commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils.locale.converters 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.commons.beanutils.locale.converters;
019:
020:        import org.apache.commons.beanutils.locale.BaseLocaleConverter;
021:        import org.apache.commons.logging.Log;
022:        import org.apache.commons.logging.LogFactory;
023:
024:        import java.math.BigDecimal;
025:        import java.math.BigInteger;
026:        import java.text.DecimalFormat;
027:        import java.text.NumberFormat;
028:        import java.text.ParseException;
029:        import java.text.SimpleDateFormat;
030:        import java.util.Date;
031:        import java.util.Locale;
032:
033:        /**
034:         * <p>Standard {@link org.apache.commons.beanutils.locale.LocaleConverter}
035:         * implementation that converts an incoming
036:         * locale-sensitive object into a <code>java.lang.String</code> object,
037:         * optionally using a default value or throwing a
038:         * {@link org.apache.commons.beanutils.ConversionException}
039:         * if a conversion error occurs.</p>
040:         *
041:         * @author Yauheny Mikulski
042:         */
043:
044:        public class StringLocaleConverter extends BaseLocaleConverter {
045:
046:            // ----------------------------------------------------- Instance Variables
047:
048:            /** All logging goes through this logger */
049:            private Log log = LogFactory.getLog(StringLocaleConverter.class); //msz fix
050:
051:            // ----------------------------------------------------------- Constructors
052:
053:            /**
054:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
055:             * that will throw a {@link org.apache.commons.beanutils.ConversionException}
056:             * if a conversion error occurs. The locale is the default locale for
057:             * this instance of the Java Virtual Machine and an unlocalized pattern is used
058:             * for the convertion.
059:             *
060:             */
061:            public StringLocaleConverter() {
062:
063:                this (false);
064:            }
065:
066:            /**
067:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
068:             * that will throw a {@link org.apache.commons.beanutils.ConversionException}
069:             * if a conversion error occurs. The locale is the default locale for
070:             * this instance of the Java Virtual Machine.
071:             *
072:             * @param locPattern    Indicate whether the pattern is localized or not
073:             */
074:            public StringLocaleConverter(boolean locPattern) {
075:
076:                this (Locale.getDefault(), locPattern);
077:            }
078:
079:            /**
080:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
081:             * that will throw a {@link org.apache.commons.beanutils.ConversionException}
082:             * if a conversion error occurs. An unlocalized pattern is used for the convertion.
083:             *
084:             * @param locale        The locale
085:             */
086:            public StringLocaleConverter(Locale locale) {
087:
088:                this (locale, false);
089:            }
090:
091:            /**
092:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
093:             * that will throw a {@link org.apache.commons.beanutils.ConversionException}
094:             * if a conversion error occurs.
095:             *
096:             * @param locale        The locale
097:             * @param locPattern    Indicate whether the pattern is localized or not
098:             */
099:            public StringLocaleConverter(Locale locale, boolean locPattern) {
100:
101:                this (locale, (String) null, locPattern);
102:            }
103:
104:            /**
105:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
106:             * that will throw a {@link org.apache.commons.beanutils.ConversionException}
107:             * if a conversion error occurs. An unlocalized pattern is used for the convertion.
108:             *
109:             * @param locale        The locale
110:             * @param pattern       The convertion pattern
111:             */
112:            public StringLocaleConverter(Locale locale, String pattern) {
113:
114:                this (locale, pattern, false);
115:            }
116:
117:            /**
118:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
119:             * that will throw a {@link org.apache.commons.beanutils.ConversionException}
120:             * if a conversion error occurs.
121:             *
122:             * @param locale        The locale
123:             * @param pattern       The convertion pattern
124:             * @param locPattern    Indicate whether the pattern is localized or not
125:             */
126:            public StringLocaleConverter(Locale locale, String pattern,
127:                    boolean locPattern) {
128:
129:                super (locale, pattern, locPattern);
130:            }
131:
132:            /**
133:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
134:             * that will return the specified default value
135:             * if a conversion error occurs. The locale is the default locale for
136:             * this instance of the Java Virtual Machine and an unlocalized pattern is used
137:             * for the convertion.
138:             *
139:             * @param defaultValue  The default value to be returned
140:             */
141:            public StringLocaleConverter(Object defaultValue) {
142:
143:                this (defaultValue, false);
144:            }
145:
146:            /**
147:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
148:             * that will return the specified default value
149:             * if a conversion error occurs. The locale is the default locale for
150:             * this instance of the Java Virtual Machine.
151:             *
152:             * @param defaultValue  The default value to be returned
153:             * @param locPattern    Indicate whether the pattern is localized or not
154:             */
155:            public StringLocaleConverter(Object defaultValue, boolean locPattern) {
156:
157:                this (defaultValue, Locale.getDefault(), locPattern);
158:            }
159:
160:            /**
161:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
162:             * that will return the specified default value
163:             * if a conversion error occurs. An unlocalized pattern is used for the convertion.
164:             *
165:             * @param defaultValue  The default value to be returned
166:             * @param locale        The locale
167:             */
168:            public StringLocaleConverter(Object defaultValue, Locale locale) {
169:
170:                this (defaultValue, locale, false);
171:            }
172:
173:            /**
174:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
175:             * that will return the specified default value
176:             * if a conversion error occurs.
177:             *
178:             * @param defaultValue  The default value to be returned
179:             * @param locale        The locale
180:             * @param locPattern    Indicate whether the pattern is localized or not
181:             */
182:            public StringLocaleConverter(Object defaultValue, Locale locale,
183:                    boolean locPattern) {
184:
185:                this (defaultValue, locale, null, locPattern);
186:            }
187:
188:            /**
189:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
190:             * that will return the specified default value
191:             * if a conversion error occurs. An unlocalized pattern is used for the convertion.
192:             *
193:             * @param defaultValue  The default value to be returned
194:             * @param locale        The locale
195:             * @param pattern       The convertion pattern
196:             */
197:            public StringLocaleConverter(Object defaultValue, Locale locale,
198:                    String pattern) {
199:
200:                this (defaultValue, locale, pattern, false);
201:            }
202:
203:            /**
204:             * Create a {@link org.apache.commons.beanutils.locale.LocaleConverter}
205:             * that will return the specified default value
206:             * if a conversion error occurs.
207:             *
208:             * @param defaultValue  The default value to be returned
209:             * @param locale        The locale
210:             * @param pattern       The convertion pattern
211:             * @param locPattern    Indicate whether the pattern is localized or not
212:             */
213:            public StringLocaleConverter(Object defaultValue, Locale locale,
214:                    String pattern, boolean locPattern) {
215:
216:                super (defaultValue, locale, pattern, locPattern);
217:            }
218:
219:            // --------------------------------------------------------- Methods
220:
221:            /**
222:             * Convert the specified locale-sensitive input object into an output object of the
223:             * specified type.
224:             *
225:             * @param value The input object to be converted
226:             * @param pattern The pattern is used for the convertion
227:             * @return The converted value
228:             *
229:             * @exception org.apache.commons.beanutils.ConversionException if conversion
230:             * cannot be performed successfully
231:             * @throws ParseException if an error occurs
232:             */
233:            protected Object parse(Object value, String pattern)
234:                    throws ParseException {
235:
236:                String result = null;
237:
238:                if ((value instanceof  Integer) || (value instanceof  Long)
239:                        || (value instanceof  BigInteger)
240:                        || (value instanceof  Byte) || (value instanceof  Short)) {
241:
242:                    result = getDecimalFormat(locale, pattern).format(
243:                            ((Number) value).longValue());
244:                } else if ((value instanceof  Double)
245:                        || (value instanceof  BigDecimal)
246:                        || (value instanceof  Float)) {
247:
248:                    result = getDecimalFormat(locale, pattern).format(
249:                            ((Number) value).doubleValue());
250:                } else if (value instanceof  Date) { // java.util.Date, java.sql.Date, java.sql.Time, java.sql.Timestamp
251:
252:                    SimpleDateFormat dateFormat = new SimpleDateFormat(pattern,
253:                            locale);
254:
255:                    result = dateFormat.format(value);
256:                } else {
257:                    result = value.toString();
258:                }
259:
260:                return result;
261:            }
262:
263:            /**
264:             * Make an instance of DecimalFormat.
265:             *
266:             * @param locale The locale
267:             * @param pattern The pattern is used for the convertion
268:             * @return The format for the locale and pattern
269:             *
270:             * @exception ConversionException if conversion cannot be performed
271:             *  successfully
272:             * @throws ParseException if an error occurs parsing a String to a Number
273:             */
274:            private DecimalFormat getDecimalFormat(Locale locale, String pattern) {
275:
276:                DecimalFormat numberFormat = (DecimalFormat) NumberFormat
277:                        .getInstance(locale);
278:
279:                // if some constructors default pattern to null, it makes only sense to handle null pattern gracefully
280:                if (pattern != null) {
281:                    if (locPattern) {
282:                        numberFormat.applyLocalizedPattern(pattern);
283:                    } else {
284:                        numberFormat.applyPattern(pattern);
285:                    }
286:                } else {
287:                    log.info("No pattern provided, using default.");
288:                }
289:
290:                return numberFormat;
291:            }
292:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.