001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.util;
043:
044: /**
045: *
046: *
047: * @author Todd Fast, todd.fast@sun.com
048: */
049: public class NameUtil {
050: /**
051: *
052: *
053: */
054: private NameUtil() {
055: super ();
056: }
057:
058: /**
059: *
060: *
061: */
062: public static String capitalize(String value) {
063: if (value == null || value.length() == 0)
064: return value;
065:
066: char nameChars[] = value.toCharArray();
067: nameChars[0] = Character.toUpperCase(nameChars[0]);
068: return new String(nameChars);
069: }
070:
071: /**
072: *
073: *
074: */
075: public static String decapitalize(String value) {
076: if (value == null || value.length() == 0)
077: return value;
078:
079: char nameChars[] = value.toCharArray();
080: nameChars[0] = Character.toLowerCase(nameChars[0]);
081: return new String(nameChars);
082: }
083:
084: /**
085: *
086: *
087: */
088: public static boolean isAllUpperCase(String value) {
089: if (value == null || value.length() == 0)
090: return false;
091:
092: for (int i = 0; i < value.length(); i++) {
093: char currentChar = value.charAt(i);
094:
095: if (Character.isWhitespace(currentChar)
096: || Character.isDigit(currentChar)) {
097: continue;
098: }
099:
100: if (!Character.isUpperCase(currentChar))
101: return false;
102: }
103:
104: return true;
105: }
106:
107: /**
108: *
109: *
110: */
111: public static String toJavaIdentifier(String value) {
112: if (value == null || value.length() == 0)
113: return value;
114:
115: String result = java.beans.Introspector.decapitalize(value);
116: if (isAllUpperCase(result))
117: result = value.toLowerCase();
118:
119: return result;
120: }
121:
122: /**
123: *
124: *
125: */
126: public static String toDisplayName(String value) {
127: if (value == null || value.length() == 0)
128: return value;
129:
130: value = capitalize(value);
131: StringBuffer javaConstant = new StringBuffer(value.length());
132:
133: char curChar;
134: boolean upperFound = false;
135: boolean digitFound = false;
136: boolean specialFound = false;
137: boolean nonUpperFound = false;
138:
139: for (int i = 0; i < value.length(); i++) {
140: curChar = value.charAt(i);
141:
142: if (Character.isUpperCase(curChar)) {
143: if ((nonUpperFound || digitFound) && i > 0) {
144: javaConstant.append(" "); // NOI18N
145: javaConstant.append(curChar);
146: }
147:
148: else
149: javaConstant.append(curChar);
150:
151: upperFound = true;
152: digitFound = false;
153: specialFound = false;
154: nonUpperFound = false;
155: }
156:
157: else if (Character.isDigit(curChar)) {
158: if (nonUpperFound && i > 0) {
159: javaConstant.append(" "); // NOI18N
160: }
161:
162: javaConstant.append(curChar);
163:
164: upperFound = false;
165: digitFound = true;
166: specialFound = false;
167: nonUpperFound = false;
168: }
169:
170: else if (curChar == CHAR_UNDERSCORE || curChar == CHAR_DASH) {
171: if ((upperFound || digitFound) && i > 0) {
172: javaConstant.append(" "); // NOI18N
173: }
174:
175: else
176: javaConstant.append(curChar); // NOI18N
177:
178: upperFound = false;
179: digitFound = false;
180: specialFound = true;
181: nonUpperFound = false;
182: }
183:
184: else {
185: upperFound = false;
186: digitFound = false;
187: specialFound = false;
188: nonUpperFound = true;
189:
190: javaConstant.append(curChar);
191: }
192:
193: }
194:
195: return javaConstant.toString();
196: }
197:
198: /**
199: *
200: *
201: */
202: public static String toJavaConstant(String value) {
203: if (value == null || value.length() == 0)
204: return value;
205:
206: StringBuffer javaConstant = new StringBuffer(value.length());
207: char curChar;
208: boolean nonUpperFound = false;
209:
210: for (int i = 0; i < value.length(); i++) {
211: curChar = value.charAt(i);
212: if (curChar == ' ' || curChar == '_') {
213: curChar = '_';
214: } else if (Character.isUpperCase(curChar)) {
215: if (nonUpperFound && i > 0)
216: javaConstant.append("_"); // NOI18N
217:
218: nonUpperFound = false;
219: curChar = Character.toUpperCase(curChar);
220: } else {
221: nonUpperFound = true;
222: curChar = Character.toUpperCase(curChar);
223: }
224:
225: javaConstant.append(curChar);
226: }
227:
228: return javaConstant.toString();
229: }
230:
231: /** Test whether a given string is comprised of only ascii chars.
232: * @param id string which should be checked
233: * @return <code>true</code> if all chars are ascii
234: * Also returns true if String is zero length
235: */
236: public static boolean isAscii(String value) {
237: if (value == null)
238: throw new IllegalArgumentException(
239: "Null passed into isAscii"); // NOI18N
240:
241: if (value.length() == 0)
242: return true; // Arbitrary semantic decision.
243:
244: for (int i = 0; i < value.length(); i++) {
245: char nextChar = value.charAt(i);
246: if (!((nextChar & 0xff80) == 0))
247: return false;
248: }
249: return true;
250: }
251:
252: public final static char CHAR_UNDERSCORE = '_';
253: public final static char CHAR_DASH = '-';
254: }
|