001: /*
002: * ====================================================================
003: * The JRefactory License, Version 1.0
004: *
005: * Copyright (c) 2001 JRefactory. All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "JRefactory" must not be used to endorse or promote
027: * products derived from this software without prior written
028: * permission. For written permission, please contact seguin@acm.org.
029: *
030: * 5. Products derived from this software may not be called "JRefactory",
031: * nor may "JRefactory" appear in their name, without prior written
032: * permission of Chris Seguin.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
038: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: * SUCH DAMAGE.
046: * ====================================================================
047: *
048: * This software consists of voluntary contributions made by many
049: * individuals on behalf of JRefactory. For more information on
050: * JRefactory, please see
051: * <http://www.sourceforge.org/projects/jrefactory>.
052: */
053: package org.acm.seguin.uml.refactor;
054:
055: import org.acm.seguin.summary.VariableSummary;
056:
057: /**
058: * Names the variable based on hungarian notation
059: *
060: *@author Chris Seguin
061: *@created July 16, 2002
062: */
063: public class HungarianNamer {
064: /**
065: * Sets the suggested name of this parameter
066: *
067: *@param initVariable The new defaultName value
068: *@param prefix Description of the Parameter
069: *@return The defaultName value
070: */
071: public String getDefaultName(VariableSummary initVariable,
072: String prefix) {
073: String name = initVariable.getName();
074: if ((name.length() > 1) && (name.charAt(1) == '_')) {
075: return name;
076: }
077:
078: if (isAllCaps(name)) {
079: return name;
080: }
081:
082: StringBuffer buffer = new StringBuffer(prefix);
083: String type = initVariable.getType();
084: if (type.equals("String")) {
085: buffer.append("sz");
086: } else {
087: useCapitalLettersFromType(type, buffer);
088: }
089:
090: if (buffer.length() == 2) {
091: buffer.append(type.charAt(0));
092: } else if (buffer.length() == 3) {
093: insureMinimumLettersInTypeCode(buffer, type);
094: }
095:
096: int first = 0;
097: if (name.charAt(0) == '_') {
098: first++;
099: }
100:
101: buffer.append(Character.toUpperCase(name.charAt(first)));
102: if (name.length() > first + 1) {
103: buffer.append(name.substring(first + 1));
104: }
105:
106: return buffer.toString();
107: }
108:
109: /**
110: * Gets the allCaps attribute of the HungarianNamer object
111: *
112: *@param name Description of the Parameter
113: *@return The allCaps value
114: */
115: private boolean isAllCaps(String name) {
116: for (int ndx = 0; ndx < name.length(); ndx++) {
117: char ch = name.charAt(ndx);
118: if (ch == '_') {
119: // OK
120: } else if (Character.isUpperCase(ch)) {
121: // OK
122: } else {
123: return false;
124: }
125: }
126:
127: return true;
128: }
129:
130: /**
131: * Determines if the character is a vowel (a, e, i, o, or u)
132: *
133: *@param ch Description of the Parameter
134: *@return Description of the Return Value
135: */
136: private boolean isVowel(char ch) {
137: ch = Character.toLowerCase(ch);
138: return (ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o')
139: || (ch == 'u');
140: }
141:
142: /**
143: * Insures that we have the appropriate number of characters
144: *
145: *@param buffer Description of the Parameter
146: *@param type Description of the Parameter
147: */
148: private void insureMinimumLettersInTypeCode(StringBuffer buffer,
149: String type) {
150: buffer.append(type.charAt(1));
151: int ndx = 2;
152: char ch = type.charAt(ndx);
153: while (isVowel(ch)) {
154: buffer.append(ch);
155: ndx++;
156: ch = type.charAt(ndx);
157: }
158: buffer.append(ch);
159: }
160:
161: /**
162: * Selects the capital letters from the type
163: *
164: *@param type Description of the Parameter
165: *@param buffer Description of the Parameter
166: */
167: private void useCapitalLettersFromType(String type,
168: StringBuffer buffer) {
169: for (int ndx = 0; ndx < type.length(); ndx++) {
170: char ch = type.charAt(ndx);
171: if (Character.isUpperCase(ch)) {
172: buffer.append(Character.toLowerCase(ch));
173: }
174: }
175: }
176: }
|