KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > util > NameConverter


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.util;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 /**
27  * Utility class to convert from different naming styles to Java convention. For example
28  * names like "ABCD_EFG" can be converted to "abcdEfg".
29  *
30  * @author Andrus Adamchik
31  */

32 public class NameConverter {
33
34     private static final Map JavaDoc SPECIAL_CHAR_TO_JAVA_MAPPING = new HashMap JavaDoc();
35
36     static {
37         SPECIAL_CHAR_TO_JAVA_MAPPING.put("#", "pound");
38     }
39
40     /**
41      * Converts a String name to a String forllowing java convention for the static final
42      * variables. E.g. "abcXyz" will be converted to "ABC_XYZ".
43      *
44      * @since 1.0.3
45      */

46     public static String JavaDoc javaToUnderscored(String JavaDoc name) {
47         if (name == null) {
48             return null;
49         }
50
51         // clear of non-java chars. While the method name implies that a passed identifier
52
// is pure Java, it is used to build pk columns names and such, so extra safety
53
// check is a good idea
54
name = specialCharsToJava(name);
55
56         char charArray[] = name.toCharArray();
57         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
58
59         for (int i = 0; i < charArray.length; i++) {
60             if ((Character.isUpperCase(charArray[i])) && (i != 0)) {
61
62                 char prevChar = charArray[i - 1];
63                 if ((Character.isLowerCase(prevChar))) {
64                     buffer.append("_");
65                 }
66             }
67
68             buffer.append(Character.toUpperCase(charArray[i]));
69         }
70
71         return buffer.toString();
72     }
73
74     /**
75      * Converts names like "ABCD_EFG_123" to Java-style names like "abcdEfg123". If
76      * <code>capitalize</code> is true, returned name is capitalized (for instance if
77      * this is a class name).
78      *
79      * @since 1.2
80      */

81     public static String JavaDoc underscoredToJava(String JavaDoc name, boolean capitalize) {
82         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(name, "_");
83         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
84
85         boolean first = true;
86         while (st.hasMoreTokens()) {
87             String JavaDoc token = st.nextToken();
88
89             // clear of non-java chars
90
token = specialCharsToJava(token);
91
92             int len = token.length();
93             if (len == 0) {
94                 continue;
95             }
96
97             // sniff mixed case vs. single case styles
98
boolean hasLowerCase = false;
99             boolean hasUpperCase = false;
100             for (int i = 0; i < len && !(hasUpperCase && hasLowerCase); i++) {
101                 if (Character.isUpperCase(token.charAt(i))) {
102                     hasUpperCase = true;
103                 }
104                 else if (Character.isLowerCase(token.charAt(i))) {
105                     hasLowerCase = true;
106                 }
107             }
108
109             // if mixed case, preserve it, if all upper, convert to lower
110
if (hasUpperCase && !hasLowerCase) {
111                 token = token.toLowerCase();
112             }
113
114             if (first) {
115                 // apply explicit capitalization rules, if this is the first token
116
first = false;
117                 if (capitalize) {
118                     buf.append(Character.toUpperCase(token.charAt(0)));
119                 }
120                 else {
121                     buf.append(Character.toLowerCase(token.charAt(0)));
122                 }
123             }
124             else {
125                 buf.append(Character.toUpperCase(token.charAt(0)));
126             }
127
128             if (len > 1) {
129                 buf.append(token.substring(1, len));
130             }
131         }
132         return buf.toString();
133     }
134
135     /**
136      * Replaces special chars with human-readable and Java-id-compatible symbols.
137      */

138     static String JavaDoc specialCharsToJava(String JavaDoc string) {
139         int len = string.length();
140         if (len == 0) {
141             return string;
142         }
143
144         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(len);
145         for (int i = 0; i < len; i++) {
146
147             char c = string.charAt(i);
148             if (Character.isJavaIdentifierPart(c)) {
149                 buffer.append(c);
150             }
151             else {
152                 Object JavaDoc word = SPECIAL_CHAR_TO_JAVA_MAPPING.get(String.valueOf(c));
153                 buffer.append(word != null ? word : "_");
154             }
155         }
156
157         return buffer.toString();
158     }
159 }
160
Popular Tags