Source Code Cross Referenced for ValidatorUtils.java in  » Library » Apache-commons-validator-1.3.1-src » org » apache » commons » validator » 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 » Library » Apache commons validator 1.3.1 src » org.apache.commons.validator.util 
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:        package org.apache.commons.validator.util;
018:
019:        import java.lang.reflect.InvocationTargetException;
020:        import java.util.Collection;
021:        import java.util.HashMap;
022:        import java.util.Iterator;
023:        import java.util.Map;
024:
025:        import org.apache.commons.beanutils.PropertyUtils;
026:        import org.apache.commons.collections.FastHashMap;
027:        import org.apache.commons.logging.Log;
028:        import org.apache.commons.logging.LogFactory;
029:        import org.apache.commons.validator.Arg;
030:        import org.apache.commons.validator.Msg;
031:        import org.apache.commons.validator.Var;
032:
033:        /**
034:         * Basic utility methods.
035:         * <p>
036:         * The use of FastHashMap is deprecated and will be replaced in a future
037:         * release.
038:         * </p>
039:         *
040:         * @version $Revision: 478334 $ $Date: 2006-11-22 21:31:54 +0000 (Wed, 22 Nov 2006) $
041:         */
042:        public class ValidatorUtils {
043:
044:            /**
045:             * <p>Replace part of a <code>String</code> with another value.</p>
046:             *
047:             * @param value <code>String</code> to perform the replacement on.
048:             * @param key The name of the constant.
049:             * @param replaceValue The value of the constant.
050:             *
051:             * @return The modified value.
052:             */
053:            public static String replace(String value, String key,
054:                    String replaceValue) {
055:
056:                if (value == null || key == null || replaceValue == null) {
057:                    return value;
058:                }
059:
060:                int pos = value.indexOf(key);
061:
062:                if (pos < 0) {
063:                    return value;
064:                }
065:
066:                int length = value.length();
067:                int start = pos;
068:                int end = pos + key.length();
069:
070:                if (length == key.length()) {
071:                    value = replaceValue;
072:
073:                } else if (end == length) {
074:                    value = value.substring(0, start) + replaceValue;
075:
076:                } else {
077:                    value = value.substring(0, start) + replaceValue
078:                            + replace(value.substring(end), key, replaceValue);
079:                }
080:
081:                return value;
082:            }
083:
084:            /**
085:             * Convenience method for getting a value from a bean property as a
086:             * <code>String</code>.  If the property is a <code>String[]</code> or
087:             * <code>Collection</code> and it is empty, an empty <code>String</code>
088:             * "" is returned.  Otherwise, property.toString() is returned.  This method
089:             * may return <code>null</code> if there was an error retrieving the
090:             * property.
091:             *
092:             * @param bean The bean object.
093:             * @param property The name of the property to access.
094:             *
095:             * @return The value of the property.
096:             */
097:            public static String getValueAsString(Object bean, String property) {
098:                Object value = null;
099:
100:                try {
101:                    value = PropertyUtils.getProperty(bean, property);
102:
103:                } catch (IllegalAccessException e) {
104:                    Log log = LogFactory.getLog(ValidatorUtils.class);
105:                    log.error(e.getMessage(), e);
106:                } catch (InvocationTargetException e) {
107:                    Log log = LogFactory.getLog(ValidatorUtils.class);
108:                    log.error(e.getMessage(), e);
109:                } catch (NoSuchMethodException e) {
110:                    Log log = LogFactory.getLog(ValidatorUtils.class);
111:                    log.error(e.getMessage(), e);
112:                }
113:
114:                if (value == null) {
115:                    return null;
116:                }
117:
118:                if (value instanceof  String[]) {
119:                    return ((String[]) value).length > 0 ? value.toString()
120:                            : "";
121:
122:                } else if (value instanceof  Collection) {
123:                    return ((Collection) value).isEmpty() ? "" : value
124:                            .toString();
125:
126:                } else {
127:                    return value.toString();
128:                }
129:
130:            }
131:
132:            /**
133:             * Makes a deep copy of a <code>FastHashMap</code> if the values
134:             * are <code>Msg</code>, <code>Arg</code>,
135:             * or <code>Var</code>.  Otherwise it is a shallow copy.
136:             *
137:             * @param map <code>FastHashMap</code> to copy.
138:             * @return FastHashMap A copy of the <code>FastHashMap</code> that was
139:             * passed in.
140:             * @deprecated This method is not part of Validator's public API.  Validator
141:             * will use it internally until FastHashMap references are removed.  Use
142:             * copyMap() instead.
143:             */
144:            public static FastHashMap copyFastHashMap(FastHashMap map) {
145:                FastHashMap results = new FastHashMap();
146:
147:                Iterator i = map.keySet().iterator();
148:                while (i.hasNext()) {
149:                    String key = (String) i.next();
150:                    Object value = map.get(key);
151:
152:                    if (value instanceof  Msg) {
153:                        results.put(key, ((Msg) value).clone());
154:                    } else if (value instanceof  Arg) {
155:                        results.put(key, ((Arg) value).clone());
156:                    } else if (value instanceof  Var) {
157:                        results.put(key, ((Var) value).clone());
158:                    } else {
159:                        results.put(key, value);
160:                    }
161:                }
162:
163:                results.setFast(true);
164:                return results;
165:            }
166:
167:            /**
168:             * Makes a deep copy of a <code>Map</code> if the values are 
169:             * <code>Msg</code>, <code>Arg</code>, or <code>Var</code>.  Otherwise, 
170:             * it is a shallow copy.
171:             *
172:             * @param map The source Map to copy.
173:             *
174:             * @return A copy of the <code>Map</code> that was passed in.
175:             */
176:            public static Map copyMap(Map map) {
177:                Map results = new HashMap();
178:
179:                Iterator iter = map.keySet().iterator();
180:                while (iter.hasNext()) {
181:                    String key = (String) iter.next();
182:                    Object value = map.get(key);
183:
184:                    if (value instanceof  Msg) {
185:                        results.put(key, ((Msg) value).clone());
186:                    } else if (value instanceof  Arg) {
187:                        results.put(key, ((Arg) value).clone());
188:                    } else if (value instanceof  Var) {
189:                        results.put(key, ((Var) value).clone());
190:                    } else {
191:                        results.put(key, value);
192:                    }
193:                }
194:                return results;
195:            }
196:
197:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.