Source Code Cross Referenced for Conventions.java in  » J2EE » spring-framework-2.0.6 » org » springframework » core » 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 » J2EE » spring framework 2.0.6 » org.springframework.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2007 the original author or authors.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.springframework.core;
018:
019:        import java.io.Externalizable;
020:        import java.io.Serializable;
021:        import java.lang.reflect.Proxy;
022:        import java.util.Collection;
023:        import java.util.HashSet;
024:        import java.util.Iterator;
025:        import java.util.Set;
026:
027:        import org.springframework.util.Assert;
028:        import org.springframework.util.ClassUtils;
029:        import org.springframework.util.StringUtils;
030:
031:        /**
032:         * Provides methods to support various naming and other conventions used
033:         * throughout the framework. Mainly for internal use within the framework.
034:         *
035:         * @author Rob Harrop
036:         * @author Juergen Hoeller
037:         * @since 2.0
038:         */
039:        public abstract class Conventions {
040:
041:            /**
042:             * Suffix added to names when using arrays.
043:             */
044:            private static final String PLURAL_SUFFIX = "List";
045:
046:            /**
047:             * Set of interfaces that are supposed to be ignored
048:             * when searching for the 'primary' interface of a proxy.
049:             */
050:            private static final Set ignoredInterfaces = new HashSet();
051:
052:            static {
053:                ignoredInterfaces.add(Serializable.class);
054:                ignoredInterfaces.add(Externalizable.class);
055:                ignoredInterfaces.add(Cloneable.class);
056:                ignoredInterfaces.add(Comparable.class);
057:            }
058:
059:            /**
060:             * Determine the conventional variable name for the supplied
061:             * code>Object</code> based on its concrete type. The convention
062:             * used is to return the uncapitalized short name of the <code>Class</code>.
063:             * So, <code>com.myapp.Product</code> becomes <code>product</code>.
064:             * <p>For arrays, we use the pluralized version of the array component type.
065:             * For <code>Collection</code>s we attempt to 'peek ahead' in the
066:             * <code>Collection</code> to determine the component type and
067:             * return the pluralized version of that component type.
068:             */
069:            public static String getVariableName(Object value) {
070:                Assert.notNull(value, "Value must not be null");
071:                Class valueClass = null;
072:                boolean pluralize = false;
073:
074:                if (value.getClass().isArray()) {
075:                    valueClass = value.getClass().getComponentType();
076:                    pluralize = true;
077:                } else if (value instanceof  Collection) {
078:                    Collection collection = (Collection) value;
079:                    if (collection.isEmpty()) {
080:                        throw new IllegalArgumentException(
081:                                "Cannot generate variable name for an empty Collection");
082:                    }
083:                    Object valueToCheck = peekAhead(collection);
084:                    valueClass = getClassForValue(valueToCheck);
085:                    pluralize = true;
086:                } else {
087:                    valueClass = getClassForValue(value);
088:                }
089:
090:                String name = StringUtils
091:                        .uncapitalize(getShortName(valueClass));
092:                return (pluralize ? pluralize(name) : name);
093:            }
094:
095:            /**
096:             * Convert <code>String</code>s in attribute name format (lowercase, hyphens separating words)
097:             * into property name format (camel-cased). For example, <code>transaction-manager</code> is
098:             * converted into <code>transactionManager</code>.
099:             */
100:            public static String attributeNameToPropertyName(
101:                    String attributeName) {
102:                Assert.notNull(attributeName,
103:                        "'attributeName' must not be null");
104:                if (attributeName.indexOf("-") == -1) {
105:                    return attributeName;
106:                }
107:                char[] chars = attributeName.toCharArray();
108:                char[] result = new char[chars.length - 1]; // not completely accurate but good guess
109:                int currPos = 0;
110:                boolean upperCaseNext = false;
111:                for (int i = 0; i < chars.length; i++) {
112:                    char c = chars[i];
113:                    if (c == '-') {
114:                        upperCaseNext = true;
115:                    } else if (upperCaseNext) {
116:                        result[currPos++] = Character.toUpperCase(c);
117:                        upperCaseNext = false;
118:                    } else {
119:                        result[currPos++] = c;
120:                    }
121:                }
122:                return new String(result, 0, currPos);
123:            }
124:
125:            /**
126:             * Determine the class to use for naming a variable that contains
127:             * the given value.
128:             * <p>Will return the class of the given value, except when
129:             * encountering a JDK proxy, in which case it will determine
130:             * the 'primary' interface implemented by that proxy.
131:             * @param value the value to check
132:             * @return the class to use for naming a variable
133:             */
134:            private static Class getClassForValue(Object value) {
135:                if (Proxy.isProxyClass(value.getClass())) {
136:                    Class[] ifcs = value.getClass().getInterfaces();
137:                    for (int i = 0; i < ifcs.length; i++) {
138:                        Class ifc = ifcs[i];
139:                        if (!ignoredInterfaces.contains(ifc)) {
140:                            return ifc;
141:                        }
142:                    }
143:                }
144:                return value.getClass();
145:            }
146:
147:            /**
148:             * Determine the short name of the given class: without package qualification,
149:             * and without the outer class name in case of an inner class.
150:             * @param valueClass the class to determine a name for
151:             * @return the short name
152:             */
153:            private static String getShortName(Class valueClass) {
154:                String shortName = ClassUtils.getShortName(valueClass);
155:                int dotIndex = shortName.lastIndexOf('.');
156:                return (dotIndex != -1 ? shortName.substring(dotIndex + 1)
157:                        : shortName);
158:            }
159:
160:            /**
161:             * Pluralize the given name.
162:             */
163:            private static String pluralize(String name) {
164:                return name + PLURAL_SUFFIX;
165:            }
166:
167:            /**
168:             * Retrieve the <code>Class</code> of an element in the <code>Collection</code>.
169:             * The exact element for which the <code>Class</code> is retreived will depend
170:             * on the concrete <code>Collection</code> implementation.
171:             */
172:            private static Object peekAhead(Collection collection) {
173:                Iterator it = collection.iterator();
174:                if (!it.hasNext()) {
175:                    throw new IllegalStateException(
176:                            "Unable to peek ahead in non-empty collection - no element found");
177:                }
178:                Object value = it.next();
179:                if (value == null) {
180:                    throw new IllegalStateException(
181:                            "Unable to peek ahead in non-empty collection - only null element found");
182:                }
183:                return value;
184:            }
185:
186:            /**
187:             * Return an attribute name qualified by the supplied enclosing {@link Class}. For example,
188:             * the attribute name '<code>foo</code>' qualified by {@link Class} '<code>com.myapp.SomeClass</code>'
189:             * would be '<code>com.myapp.SomeClass.foo</code>'
190:             */
191:            public static String getQualifiedAttributeName(
192:                    Class enclosingClass, String attributeName) {
193:                Assert.notNull(enclosingClass,
194:                        "'enclosingClass' must not be null");
195:                Assert.notNull(attributeName,
196:                        "'attributeName' must not be null");
197:                return enclosingClass.getName() + "." + attributeName;
198:            }
199:
200:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.