1 20 package org.objectweb.modfact.jmi.helper; 21 22 import javax.jmi.model.*; 23 24 import java.util.*; 25 import java.util.regex.Matcher ; 26 import java.util.regex.Pattern ; 27 28 31 34 38 public class JMIProvider { 39 40 public static boolean considers_idl_substitute_name = true; 41 42 public static String shortQualifierOf(ModelElement _elem) { 44 Namespace ns = null; 45 46 if (_elem instanceof Namespace) { 47 ns = (Namespace) _elem; 48 } else { 49 ns = _elem.getContainer(); 50 } 51 if (ns == null) 52 return ""; 53 54 String res = null; 55 56 while (true) { 57 if (ns instanceof MofPackage) { 58 res = 60 packageNameFormat(ns.getName()) 61 + ((res == null) ? "" : ("." + res)); 62 } 63 Namespace ns2 = ns.getContainer(); 64 if (ns2 == null) 65 break; 66 ns = ns2; 67 } 68 return res; 69 } 70 71 public static String qualifierOf(ModelElement _elem) { 73 74 return packagePrefixOf(_elem) + shortQualifierOf(_elem); 75 76 } 77 78 public static String packagePrefixOf(ModelElement _elem) { 80 Namespace ns = null; 81 82 if (_elem instanceof MofPackage) { 83 ns = (Namespace) _elem; 84 } else { 85 ns = _elem.getContainer(); 86 } 87 if (ns == null) 88 return ""; 89 90 while (true) { 91 Namespace ns2 = ns.getContainer(); 92 if (ns2 == null) 93 break; 94 ns = ns2; 95 } 96 97 Iterator it = 98 ((ModelPackage) ns.refImmediatePackage()) 99 .getTag() 100 .refAllOfClass() 101 .iterator(); 102 while (it.hasNext()) { 103 Tag t = (Tag) it.next(); 104 if ((t.getTagId().equals("javax.jmi.packagePrefix") 105 || t.getTagId().equals("com.urep.jcr.java_prefix")) 106 && t.getElements().contains(ns)) { 107 return t.getValues().get(0) + "."; 108 } 109 } 110 return ""; 111 } 112 113 public static String jmiPackageExtentName(MofPackage p) { 114 String stName = findSubstituteName(p); 115 if (stName != null) 116 return stName; 117 return classNameFormat(p.getName()); 118 } 119 120 123 public static String jmiPackageName(MofPackage p) { 124 return jmiPackageExtentName(p); 125 } 126 127 public static String jmiPackageQualifiedName(MofPackage p) { 128 return qualifierOf(p) + "." + jmiPackageExtentName(p); 129 } 130 131 public static String jmiClassName(MofClass clazz) { 132 String stName = findSubstituteName(clazz); 133 if (stName != null) 134 return stName; 135 return classNameFormat(clazz.getName()); 136 } 137 138 public static String findSubstituteName(ModelElement m) { 140 Iterator it = 141 ((ModelPackage) m.refImmediatePackage()) 142 .getTag() 143 .refAllOfClass() 144 .iterator(); 145 while (it.hasNext()) { 146 Tag t = (Tag) it.next(); 147 if ((t.getTagId().equals("javax.jmi.substituteName") 148 || t.getTagId().equals("com.urep.jcr.java_substitute_name") 149 || (considers_idl_substitute_name 150 && t.getTagId().equals("org.omg.mof.idl_substitute_name"))) 151 && t.getElements().contains(m)) { 152 return (String ) t.getValues().get(0); 153 } 154 } 155 return null; 156 } 157 158 public static String jmiClassQualifiedName(MofClass clazz) { 159 return qualifierOf(clazz) + "." + jmiClassName(clazz); 160 } 161 162 public static String jmiAssociationName(Association a) { 163 String stName = findSubstituteName(a); 164 if (stName != null) 165 return stName; 166 return classNameFormat(a.getName()); 167 } 168 169 public static String jmiAssociationQualifiedName(Association association) { 170 return qualifierOf(association) + "." + jmiAssociationName(association); 171 } 172 173 public static String jmiPrimitiveTypeName(PrimitiveType _type) { 174 String typeName = _type.getName().toLowerCase(); 175 if (typeName.equals("string")) { 176 return "String"; 177 } else if (typeName.equals("boolean")) { 178 return "boolean"; 179 } else if (typeName.equals("integer") || typeName.equals("long")) { 180 return "int"; 181 } else if (typeName.equals("float")) { 182 return "float"; 183 } else if (typeName.equals("double")) { 184 return "double"; 185 } else { 186 throw new RuntimeException ( 187 "unknown primitive type: " + _type.getName()); 188 } 189 } 190 191 public static String jmiNullablePrimitiveTypeName(PrimitiveType _type) { 192 String typeName = _type.getName().toLowerCase(); 193 if (typeName.equals("string")) { 194 return "java.lang.String"; 195 } else if (typeName.equals("boolean")) { 196 return "java.lang.Boolean"; 197 } else if (typeName.equals("integer") || typeName.equals("long")) { 198 return "java.lang.Integer"; 199 } else if (typeName.equals("float")) { 200 return "java.lang.Float"; 201 } else if (typeName.equals("double")) { 202 return "java.lang.Double"; 203 } else { 204 throw new RuntimeException ( 205 "unknown primitive type: " + _type.getName()); 206 } 207 } 208 209 public static String jmiDataTypeQualifiedName(DataType _type) { 210 if (_type instanceof PrimitiveType) { 211 return jmiNullablePrimitiveTypeName((PrimitiveType) _type); 212 } 213 return qualifierOf(_type) + "." + _type.getName(); 214 } 215 216 public static String jmiImportName(Import _import) { 217 MofPackage p = (MofPackage) _import.getImportedNamespace(); 218 return jmiPackageExtentName(p); 219 } 220 221 public static String jmiClassifierQualifiedName(Classifier _classifier) { 222 if (_classifier instanceof MofClass) 223 return jmiClassQualifiedName((MofClass) _classifier); 224 225 if (_classifier instanceof DataType) 226 return jmiDataTypeQualifiedName((DataType) _classifier); 227 228 throw new RuntimeException ("invalide parameter:" + _classifier); 229 } 230 231 public static String jmiFormat1(String name) { 232 return noUnderscoreName(name, true); 233 } 234 235 public static java.util.Vector splitAndDelete(String _input) { 236 java.util.Vector parts = new java.util.Vector (); 237 String delim = "-_ ."; 238 java.util.StringTokenizer _sstring = new java.util.StringTokenizer (_input, delim); 239 while (_sstring.hasMoreTokens()) { 240 String part = _sstring.nextToken(); 241 int i = 0; 242 int j = 0; 243 boolean isPart = false; 244 boolean beginPart = true; 245 while (i < part.length()) { 246 if (beginPart) { 247 if ((Character.isDigit(part.charAt(i))) || (Character.isUpperCase(part.charAt(i)))) 248 i++; 249 else 250 beginPart = false; 251 } else { 252 if ((Character.isDigit(part.charAt(i))) || (Character.isLowerCase(part.charAt(i)))) 253 i++; 254 else 255 isPart = true; 256 } 257 if (isPart) { 258 parts.addElement(part.substring(j, i)); 259 j = i; 260 isPart = false; 261 beginPart = true; 262 } 263 if (i == j) 264 i++; } 266 if (i != j) 267 parts.addElement(part.substring(j, i)); 268 } 269 return parts; 270 } 271 272 public static String jmiFormat2(String name) { 273 java.util.Vector s1 = splitAndDelete(name); 274 String buffer = " "; 275 for (int i = 0; i < s1.size(); i++) { 276 String s2 = s1.elementAt(i).toString().toLowerCase(); 277 buffer = buffer.concat(s2); 278 if (i != (s1.size() - 1)) 279 buffer = buffer.concat("_"); 280 } 281 return buffer.trim(); 282 } 283 284 public static String jmiAccessorName(TypedElement attribute) { 286 287 String accessorName = jmiFormat1(attribute.getName()); 288 Classifier t = attribute.getType(); 289 290 if ((t instanceof PrimitiveType) 291 && t.getName().equalsIgnoreCase("boolean") 292 && ((attribute instanceof StructureField) 293 || ((Attribute) attribute).getMultiplicity().getUpper() == 1)) { 294 295 if (attribute.getName().toLowerCase().startsWith("is")) 296 return attribute.getName(); 297 else 298 return "is" + accessorName; 299 } else 300 return "get" + accessorName; 301 } 302 303 public static String jmiMutatorName(StructuralFeature attribute) { 305 306 Classifier t = attribute.getType(); 307 308 if ((t instanceof PrimitiveType) 309 && t.getName().equalsIgnoreCase("boolean") 310 && attribute.getMultiplicity().getUpper() == 1 311 && attribute.getName().toLowerCase().startsWith("is")) { 312 313 return "set" + jmiFormat1(attribute.getName().substring(2)); 314 315 } else 316 return "set" + jmiFormat1(attribute.getName()); 317 } 318 319 public static String typeTemplate(TypedElement elem) { 320 String javaType = null; 321 Classifier type = elem.getType(); 322 323 if (MofHelper.isSinglePrimitive(elem)) { 324 javaType = JMIProvider.jmiPrimitiveTypeName((PrimitiveType) type); 325 326 } else if ( 327 (elem instanceof StructuralFeature) 328 && ((StructuralFeature) elem).getMultiplicity().getUpper() != 1) { 329 330 javaType = 331 (((StructuralFeature) elem).getMultiplicity().isOrdered()) 332 ? "java.util.List" 333 : "java.util.Collection"; 334 335 } else if ( 336 (elem instanceof Parameter) 337 && ((Parameter) elem).getMultiplicity().getUpper() != 1) { 338 339 javaType = 340 (((Parameter) elem).getMultiplicity().isOrdered()) 341 ? "java.util.List" 342 : "java.util.Collection"; 343 344 } else { 345 javaType = JMIProvider.jmiClassifierQualifiedName(type); 346 } 347 348 if ((elem instanceof Parameter) 349 && (((Parameter) elem) 350 .getDirection() 351 .equals(DirectionKindEnum.INOUT_DIR) 352 || ((Parameter) elem).getDirection().equals( 353 DirectionKindEnum.OUT_DIR))) { 354 355 javaType = javaType + "[]"; 356 } 357 return javaType; 358 } 359 360 366 public static String packageNameFormat(String name) { 367 return name.replaceAll("[^a-zA-Z0-9]", "").toLowerCase(); 368 } 369 370 376 public static String classNameFormat(String name) { 377 return noUnderscoreName(name, true); 378 } 379 380 381 387 public static String attributNameFormat(String name) { 388 return noUnderscoreName(name, false); 389 } 390 391 397 public static String operationNameFormat(String name) { 398 return noUnderscoreName(name, false); 399 } 400 401 407 public static String constantNameFormat(String name) { 408 return underscoreName(name); 409 } 410 411 417 public static String enumerationNameFormat(String name) { 418 return underscoreName(name); 419 } 420 425 public static String underscoreName(String name) { 426 String res = ""; 427 String decomposed = decompose(name); 428 StringTokenizer st = new StringTokenizer(decomposed); 429 if(st.countTokens()==0) { 430 throw new RuntimeException ("Invalide name '" +name +"'"); 431 } 432 res = st.nextToken().toUpperCase(); 433 while (st.hasMoreElements()) { 434 res = res +"_" +st.nextToken().toUpperCase(); 435 } 436 return res; 437 } 438 439 public static String noUnderscoreName(String name, boolean beginsWithUpper) { 440 String res = ""; 441 String decomposed = decompose(name); 442 StringTokenizer st = new StringTokenizer(decomposed); 443 boolean isFirstToken = true; 444 while (st.hasMoreElements()) { 445 char[] tmp = st.nextToken() 446 .toLowerCase() 447 .toCharArray(); 448 if ( (!isFirstToken) 449 || beginsWithUpper ) { 450 tmp[0] = Character.toUpperCase(tmp[0]); 451 isFirstToken = false; 452 } 453 res = res + new String (tmp); 454 } 455 if(res.length()==0) { 456 throw new RuntimeException ("Invalide name '" +name +"'"); 457 } 458 return res; 459 } 460 461 465 public static String decompose(String input) { 466 String r = input.replaceAll("[^a-zA-Z0-9]", " "); 468 r = r.replaceAll("[A-Z][a-z]", " $0"); 470 Matcher m = Pattern.compile("[a-z0-9][A-Z]").matcher(r); 472 StringBuffer sb = new StringBuffer (); 473 while (m.find()) { 474 String matched = m.group(); 475 m.appendReplacement(sb 476 , matched.charAt(0) +" " +matched.charAt(1) ); 477 } 478 m.appendTail(sb); 479 r = sb.toString(); 480 return r; 481 } 482 483 484 } 485 | Popular Tags |