KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > examples > jet > article2 > model > NameUtil


1 package org.eclipse.emf.examples.jet.article2.model;
2
3
4 /**
5  * Utility class for formatting names of java model objects.
6  *
7  * @author Remko Popma
8  * @version $Revision: 1.1 $ ($Date: 2004/05/31 21:35:35 $)
9  */

10 public class NameUtil
11 {
12
13   /**
14    * Returns the specified string with the first character converted to lower
15    * case.
16    *
17    * @param name
18    * the string to convert
19    * @return the specified string with the first character converted to lower
20    * case
21    */

22   public static String JavaDoc uncapName(String JavaDoc name)
23   {
24     if (name.length() == 0)
25     {
26       return name;
27     }
28     return Character.toLowerCase(name.charAt(0)) + name.substring(1);
29   }
30
31   /**
32    * Returns the specified string with the first character converted to upper
33    * case.
34    *
35    * @param name
36    * the string to convert
37    * @return the specified string with the first character converted to upper
38    * case
39    */

40   public static String JavaDoc capName(String JavaDoc name)
41   {
42     if (name.length() == 0)
43     {
44       return name;
45     }
46     return Character.toUpperCase(name.charAt(0)) + name.substring(1);
47   }
48
49   /**
50    * Converts the specified constant name to a Java class name: the returned
51    * string will have an initial upper case character, followed by all lower
52    * case characters, except for characters following an underscore '_' or a
53    * whitespace character: characters following a character that will be removed
54    * from the identifier are converted to upper case.
55    *
56    * @param code
57    * the name to convert to a Java class name
58    * @return a Java class name for the specified name
59    */

60   public static String JavaDoc constantToJavaClassName(String JavaDoc code)
61   {
62     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
63
64     boolean upperNext = true;
65     char[] chars = code.toCharArray();
66     for (int i = 0; i < chars.length; i++)
67     {
68       char c = chars[i];
69
70       boolean valid = (i == 0) ? Character.isJavaIdentifierStart(c) : Character.isJavaIdentifierPart(c);
71
72       if (!valid)
73       {
74         upperNext = true;
75         continue;
76       }
77       if (c == '_')
78       {
79         upperNext = true;
80         continue;
81       }
82
83       if (upperNext)
84       {
85         result.append(Character.toUpperCase(c));
86         upperNext = false;
87       }
88       else
89       {
90         result.append(Character.toLowerCase(c));
91       }
92     }
93     return result.toString();
94   }
95
96   /**
97    * Returns whether the specified string is a valid Java identifier.
98    *
99    * @param name
100    * the string to check
101    * @return whether the specified string is a valid Java identifier
102    */

103   public static boolean isValidIdentifier(String JavaDoc name)
104   {
105     char[] chars = name.toCharArray();
106     for (int i = 0; i < chars.length; i++)
107     {
108       char c = chars[i];
109
110       boolean valid = (i == 0) ? Character.isJavaIdentifierStart(c) : Character.isJavaIdentifierPart(c);
111
112       if (!valid)
113       {
114         return false;
115       }
116     }
117     return true;
118   }
119
120   /**
121    * Private constructor: this class is not intended to be instantiated.
122    */

123   private NameUtil()
124   {
125   }
126 }
Popular Tags