Source Code Cross Referenced for BaseLocaleConverter.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » locale » 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 
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;
019:
020:        import org.apache.commons.beanutils.ConversionException;
021:        import org.apache.commons.logging.Log;
022:        import org.apache.commons.logging.LogFactory;
023:
024:        import java.text.ParseException;
025:        import java.util.Locale;
026:
027:        /**
028:         * <p>The base class for all standart type locale-sensitive converters.
029:         * It has {@link LocaleConverter} and {@link org.apache.commons.beanutils.Converter} implementations,
030:         * that convert an incoming locale-sensitive Object into an object of correspond type,
031:         * optionally using a default value or throwing a {@link ConversionException}
032:         * if a conversion error occurs.</p>
033:         *
034:         * @author Yauheny Mikulski
035:         */
036:
037:        public abstract class BaseLocaleConverter implements  LocaleConverter {
038:
039:            // ----------------------------------------------------- Instance Variables
040:
041:            /** All logging goes through this logger */
042:            private Log log = LogFactory.getLog(BaseLocaleConverter.class);
043:
044:            /** The default value specified to our Constructor, if any. */
045:            private Object defaultValue = null;
046:
047:            /** Should we return the default value on conversion errors? */
048:            protected boolean useDefault = false;
049:
050:            /** The locale specified to our Constructor, by default - system locale. */
051:            protected Locale locale = Locale.getDefault();
052:
053:            /** The default pattern specified to our Constructor, if any. */
054:            protected String pattern = null;
055:
056:            /** The flag indicating whether the given pattern string is localized or not. */
057:            protected boolean locPattern = false;
058:
059:            // ----------------------------------------------------------- Constructors
060:
061:            /**
062:             * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
063:             * if a conversion error occurs.
064:             * An unlocalized pattern is used for the convertion.
065:             *
066:             * @param locale        The locale
067:             * @param pattern       The convertion pattern
068:             */
069:            protected BaseLocaleConverter(Locale locale, String pattern) {
070:
071:                this (null, locale, pattern, false, false);
072:            }
073:
074:            /**
075:             * Create a {@link LocaleConverter} that will throw a {@link ConversionException}
076:             * if a conversion error occurs.
077:             *
078:             * @param locale        The locale
079:             * @param pattern       The convertion pattern
080:             * @param locPattern    Indicate whether the pattern is localized or not
081:             */
082:            protected BaseLocaleConverter(Locale locale, String pattern,
083:                    boolean locPattern) {
084:
085:                this (null, locale, pattern, false, locPattern);
086:            }
087:
088:            /**
089:             * Create a {@link LocaleConverter} that will return the specified default value
090:             * if a conversion error occurs.
091:             * An unlocalized pattern is used for the convertion.
092:             *
093:             * @param defaultValue  The default value to be returned
094:             * @param locale        The locale
095:             * @param pattern       The convertion pattern
096:             */
097:            protected BaseLocaleConverter(Object defaultValue, Locale locale,
098:                    String pattern) {
099:
100:                this (defaultValue, locale, pattern, false);
101:            }
102:
103:            /**
104:             * Create a {@link LocaleConverter} that will return the specified default value
105:             * if a conversion error occurs.
106:             *
107:             * @param defaultValue  The default value to be returned
108:             * @param locale        The locale
109:             * @param pattern       The convertion pattern
110:             * @param locPattern    Indicate whether the pattern is localized or not
111:             */
112:            protected BaseLocaleConverter(Object defaultValue, Locale locale,
113:                    String pattern, boolean locPattern) {
114:
115:                this (defaultValue, locale, pattern, true, locPattern);
116:            }
117:
118:            /**
119:             * Create a {@link LocaleConverter} that will return the specified default value
120:             * or throw a {@link ConversionException} if a conversion error occurs.
121:             *
122:             * @param defaultValue  The default value to be returned
123:             * @param locale        The locale
124:             * @param pattern       The convertion pattern
125:             * @param useDefault    Indicate whether the default value is used or not
126:             * @param locPattern    Indicate whether the pattern is localized or not
127:             */
128:            private BaseLocaleConverter(Object defaultValue, Locale locale,
129:                    String pattern, boolean useDefault, boolean locPattern) {
130:
131:                if (useDefault) {
132:                    this .defaultValue = defaultValue;
133:                    this .useDefault = true;
134:                }
135:
136:                if (locale != null) {
137:                    this .locale = locale;
138:                }
139:
140:                this .pattern = pattern;
141:                this .locPattern = locPattern;
142:            }
143:
144:            // --------------------------------------------------------- Methods
145:
146:            /**
147:             * Convert the specified locale-sensitive input object into an output object of the
148:             * specified type.
149:             *
150:             * @param value The input object to be converted
151:             * @param pattern The pattern is used for the convertion
152:             * @return The converted value
153:             *
154:             * @exception ParseException if conversion cannot be performed
155:             *  successfully
156:             */
157:
158:            abstract protected Object parse(Object value, String pattern)
159:                    throws ParseException;
160:
161:            /**
162:             * Convert the specified locale-sensitive input object into an output object.
163:             * The default pattern is used for the convertion.
164:             *
165:             * @param value The input object to be converted
166:             * @return The converted value
167:             *
168:             * @exception ConversionException if conversion cannot be performed
169:             *  successfully
170:             */
171:            public Object convert(Object value) {
172:                return convert(value, null);
173:            }
174:
175:            /**
176:             * Convert the specified locale-sensitive input object into an output object.
177:             *
178:             * @param value The input object to be converted
179:             * @param pattern The pattern is used for the convertion
180:             * @return The converted value
181:             *
182:             * @exception ConversionException if conversion cannot be performed
183:             *  successfully
184:             */
185:            public Object convert(Object value, String pattern) {
186:                return convert(null, value, pattern);
187:            }
188:
189:            /**
190:             * Convert the specified locale-sensitive input object into an output object of the
191:             * specified type. The default pattern is used for the convertion.
192:             *
193:             * @param type Data type to which this value should be converted
194:             * @param value The input object to be converted
195:             * @return The converted value
196:             *
197:             * @exception ConversionException if conversion cannot be performed
198:             *  successfully
199:             */
200:            public Object convert(Class type, Object value) {
201:                return convert(type, value, null);
202:            }
203:
204:            /**
205:             * Convert the specified locale-sensitive input object into an output object of the
206:             * specified type.
207:             *
208:             * @param type Data is type to which this value should be converted
209:             * @param value is the input object to be converted
210:             * @param pattern is the pattern is used for the conversion; if null is
211:             * passed then the default pattern associated with the converter object
212:             * will be used.
213:             * @return The converted value
214:             *
215:             * @exception ConversionException if conversion cannot be performed
216:             *  successfully
217:             */
218:            public Object convert(Class type, Object value, String pattern) {
219:                if (value == null) {
220:                    if (useDefault) {
221:                        return (defaultValue);
222:                    } else {
223:                        // symmetric beanutils function allows null
224:                        // so do not: throw new ConversionException("No value specified");
225:                        log
226:                                .debug("Null value specified for conversion, returing null");
227:                        return null;
228:                    }
229:                }
230:
231:                try {
232:                    if (pattern != null) {
233:                        return parse(value, pattern);
234:                    } else {
235:                        return parse(value, this .pattern);
236:                    }
237:                } catch (Exception e) {
238:                    if (useDefault) {
239:                        return (defaultValue);
240:                    } else {
241:                        if (e instanceof  ConversionException) {
242:                            throw (ConversionException) e;
243:                        }
244:                        throw new ConversionException(e);
245:                    }
246:                }
247:            }
248:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.