1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.javagen.agile.oo.util;
17
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Set;
21
22 import org.javagen.agile.core.model.Model;
23 import org.javagen.agile.core.util.StringUtil;
24 import org.javagen.agile.oo.model.Class;
25
26 public class OOUtil {
27
28 public static final String[] JAVA_RESERVED_WORDS = {
29 "abstract", "assert", "boolean", "break", "byte", "byvalue", "case", "cast",
30 "catch", "char", "class", "const", "continue", "default", "do", "double",
31 "else", "extends", "false", "final", "finally", "float", "for", "future",
32 "generic", "goto", "if", "implements", "import", "inner", "instanceof",
33 "int", "interface", "long", "native", "new", "null", "operator", "outer",
34 "package", "private", "protected", "public", "rest", "return", "short",
35 "static", "super", "switch", "synchronized", "this", "throw", "throws",
36 "transient", "true", "try", "var", "void", "volatile", "while", "enum",
37 };
38
39 private static Set<String> reservedTable;
40
41 /***
42 * Check for Java reserved words.
43 */
44 public static boolean isReserved(String ident) {
45 if (reservedTable==null) {
46 reservedTable = new HashSet<String>();
47 for(int i=0;i<JAVA_RESERVED_WORDS.length;i++)
48 reservedTable.add(JAVA_RESERVED_WORDS[i]);
49 }
50 return reservedTable.contains(ident);
51 }
52
53 public static String varFromClassName(String className) {
54 String varName = StringUtil.makeFirstLetterLowerCase( className.substring(className.lastIndexOf('.')+1) );
55 return varName;
56 }
57
58 public static String camelBackJavaVar(String anyString) {
59 String varName = legalJavaName( StringUtil.camelBackName(anyString) );
60 return varName;
61 }
62
63 public static String camelBackJavaClass(String anyString) {
64 String className = legalJavaName( StringUtil.makeFirstLetterUpperCase(StringUtil.camelBackName(anyString)) );
65 return className;
66 }
67
68 public static String shortClassName(String className) {
69 int pos = className.lastIndexOf('.');
70 return pos==-1 ? className : className.substring(pos+1);
71 }
72
73 /***
74 * Make sure identifier is a legal Java name and modify it if necisary.
75 */
76 public static String legalJavaName(String identifier)
77 {
78 if (identifier==null || identifier.trim().length()==0)
79 return identifier;
80 boolean legal = !Character.isDigit(identifier.charAt(0)) && !isReserved(identifier);
81 return legal ? identifier : "_"+identifier;
82 }
83
84 /***
85 * Generate a legal uppercase Java enum name given an arbitrary string.
86 */
87 public static String javaEnumName(String anyString)
88 {
89 if (anyString==null || anyString.trim().length()==0)
90 return null;
91 return legalJavaName(StringUtil.camelBackName(anyString)).toUpperCase();
92 }
93
94 /***
95 * Convert arbitrary stirng to legal Java constant name. All non-legal
96 * identifier characters are converted to '_'.
97 */
98 public static String javaConstName(String anyString)
99 {
100 if (anyString == null)
101 return null;
102 StringBuilder javaConst = new StringBuilder();
103 int strlen = anyString.length();
104 char lastChar = '\0';
105 for(int i = 0; i < strlen; i++) {
106 char c = Character.toUpperCase( anyString.charAt(i) );
107 boolean validId = Character.isJavaIdentifierPart(c) && (c < 128);
108 if (validId || lastChar != '_')
109 javaConst.append( validId ? c : '_');
110 lastChar = validId ? c : '_';
111 }
112 return legalJavaName( javaConst.toString() );
113 }
114
115 public static String trimJavaExt(String javaFileName) {
116 if (javaFileName==null)
117 return null;
118 return javaFileName.endsWith(".java") ? javaFileName.substring(0,javaFileName.length()-5) : javaFileName;
119 }
120
121 public static String packageFromPath(String filePath) {
122 if (filePath==null || filePath.length()==0)
123 return "";
124 filePath = filePath.replace('//','/');
125 filePath = filePath.replace('/','.');
126 int startPos = filePath.startsWith(".") ? 1 : 0;
127 int endPos = filePath.endsWith(".") ? filePath.length()-1 : filePath.length();
128 return filePath.substring(startPos, endPos);
129 }
130
131
132
133
134
135 /***
136 * Search for a Class in arbitrary hierarchy.
137 * @param parentFolder
138 * @param className
139 * @return
140 */
141 public static Class getClass(Model model, String className) {
142 if (model instanceof Class) {
143 Class _class = (Class)model;
144 if (className.equals(_class.getName()))
145 return _class;
146 } else {
147 List<Model> children = model.getChildModels();
148 if (children!=null)
149 for(Model child : children) {
150 Class _class = getClass(child, className);
151 if (_class!=null)
152 return _class;
153 }
154 }
155 return null;
156 }
157
158 /***
159 // * Search for a Class in arbitrary hierarchy.
160 // * @param modelList
161 // * @param className
162 // * @return
163 // */
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 }