1 19 20 package org.netbeans.lib.jmi.mapping; 21 22 import org.netbeans.lib.jmi.util.TagProvider; 23 import javax.jmi.model.*; 24 import javax.jmi.reflect.*; 25 import java.io.IOException ; 26 import java.util.*; 27 28 33 public abstract class GenericMapper { 34 protected static String PACKAGE_POSTFIX = "Package"; protected static String CLASS_POSTFIX = "Class"; protected static String ENUM_POSTFIX = "Enum"; 38 protected static final String DT_EXCEPTION = "javax.jmi.reflect.RefException"; protected static final String DT_PACKAGE = "javax.jmi.reflect.RefPackage"; protected static final String DT_ASSOCIATION = "javax.jmi.reflect.RefAssociation"; protected static final String DT_CLASS = "javax.jmi.reflect.RefClass"; protected static final String DT_INSTANCE = "javax.jmi.reflect.RefObject"; protected static final String DT_STRUCTURE = "javax.jmi.reflect.RefStruct"; protected static final String DT_ENUMERATION = "javax.jmi.reflect.RefEnum"; protected static final String DT_MULTIVALUED = "java.util.Collection"; protected static final String DT_ORDERED = "java.util.List"; protected static final String DT_ANY = "java.lang.Object"; 59 protected final TagProvider tagProvider = new TagProvider(); 60 61 protected final Set visited = new HashSet(); 64 65 protected static final HashMap objectToPrimitive = new HashMap(7); 67 static { 68 objectToPrimitive.put("java.lang.Short", "short"); objectToPrimitive.put("java.lang.Integer", "int"); objectToPrimitive.put("java.lang.Float", "float"); objectToPrimitive.put("java.lang.Double", "double"); objectToPrimitive.put("java.lang.Boolean", "boolean"); objectToPrimitive.put("java.lang.Character", "char"); objectToPrimitive.put("java.lang.Long", "long"); } 76 77 protected static String firstUpper(String text) { 79 if (text == null) return null; 80 if (text.length() < 2) return text.toUpperCase(Locale.US); 81 return text.substring(0, 1).toUpperCase(Locale.US) + text.substring(1); 82 } 83 84 protected static String firstLower(String text) { 86 if (text == null) return null; 87 if (text.length() < 2) return text.toLowerCase(Locale.US); 88 return text.substring(0, 1).toLowerCase(Locale.US) + text.substring(1); 89 } 90 91 protected static String removeUnderscores(String text) { 93 StringBuffer result = new StringBuffer (text.length()); 94 int pos = 0; 95 int oldPos = 0; 96 String firstChar; 97 98 while (oldPos <= text.length()) { 99 if ((pos = text.indexOf('_', oldPos)) == -1) { 100 pos = text.length() + 1; 101 } 102 if (pos - oldPos > 1) { 103 firstChar = text.substring(oldPos, oldPos + 1); 104 if (oldPos > 0) { 107 firstChar = firstChar.toUpperCase(Locale.US); 108 } 109 result.append(firstChar); 111 if (pos > text.length()) { 113 result.append(text.substring(oldPos + 1)); 114 } else { 115 result.append(text.substring(oldPos + 1, pos)); 116 } 117 } 118 oldPos = pos + 1; 119 } 120 121 return result.toString(); 122 } 123 124 private static List parseDots(String packageName) { 126 int position; 127 String name = packageName; 128 ArrayList result = new ArrayList(); 129 130 while ((position = name.indexOf('.')) > 0) { 131 result.add(name.substring(0, position)); 132 name = name.substring(position + 1); 133 } 134 135 if (position != 0) { 136 result.add(name); 137 } 138 139 return result; 140 } 141 142 protected Classifier getAttrType(TypedElement attr) { 144 Classifier curType = attr.getType(); 145 Classifier attrType = (Classifier) aliasesCache.get(curType); 146 147 if (attrType == null) { 148 attrType = curType; 149 if (curType instanceof AliasType) { 150 attrType = getAttrType((TypedElement) curType); 151 } 152 aliasesCache.put(curType, attrType); 154 } 155 156 return attrType; 157 } 158 159 protected static final int GENERIC_STYLE_NONE = 0; 160 protected static final int GENERIC_STYLE_FAKE = 1; 161 protected static final int GENERIC_STYLE_REAL = 2; 162 163 protected String getTypeName(StructuralFeature f) { 165 return getTypeName(f, GENERIC_STYLE_NONE); 166 } 167 protected String getTypeName(StructuralFeature f, int genericStyle) { 168 return getTypeName(f, f.getMultiplicity(), genericStyle); 169 } 170 protected String getTypeName(Parameter p) { 171 return getTypeName(p, GENERIC_STYLE_NONE); 172 } 173 protected String getTypeName(Parameter p, int genericStyle) { 174 return getTypeName(p, p.getMultiplicity(), genericStyle); 175 } 176 private String getTypeName(TypedElement te, MultiplicityType mp, int genericStyle) { 177 String n = getTypeName(getAttrType(te)); 178 if (n == null || n.equals("null")) { throw new IllegalStateException ("No type name for " + te); } 181 if (mp.getUpper() > 1 || mp.getUpper() == -1) { 182 String rawname = mp.isOrdered() ? DT_ORDERED : DT_MULTIVALUED; 183 switch (genericStyle) { 184 case GENERIC_STYLE_NONE: 185 return rawname; 186 case GENERIC_STYLE_FAKE: 187 return rawname + "/*<" + n + ">*/"; case GENERIC_STYLE_REAL: 189 return rawname + "<" + n + ">"; default: 191 throw new IllegalArgumentException ("Bad genericStyle: " + genericStyle); } 193 } else if (mp.getLower() < 1) { 194 return n; 195 } else { 196 return getPrimitiveName(n); 197 } 198 } 199 200 202 protected String getTypeName2(TypedElement te) { 203 return getPrimitiveName(getTypeName(getAttrType(te))); 204 } 205 206 protected String getTypeName(Classifier type) { 208 String result = (String ) typeNameCache.get(type); 209 210 if (result == null) { 211 result = tagProvider.getDataTypeName(type); 212 typeNameCache.put(type, result); 213 } 214 215 return result; 216 } 217 218 protected String getPrimitiveName(String typeName) { 220 String name = (String ) objectToPrimitive.get(typeName); 221 return name == null ? typeName : name; 222 } 223 224 protected static boolean canBePrimitive(String typeName) { 226 return (objectToPrimitive.get(typeName) != null); 227 } 228 229 protected final Hashtable aliasesCache = new Hashtable(100); 231 protected final Hashtable typeNameCache = new Hashtable(100); 233 234 236 protected abstract void classInstanceTemplate(javax.jmi.model.MofClass objClass) throws IOException ; 238 protected abstract void classProxyTemplate(javax.jmi.model.MofClass objClass) throws IOException ; 240 241 protected abstract void associationTemplate(Association objAssociation) throws IOException ; 243 244 protected abstract void packageTemplate(MofPackage objPackage) throws IOException ; 245 246 protected abstract void exceptionTemplate(MofException objException) throws IOException ; 248 249 protected abstract void enumerationInterfaceTemplate(EnumerationType objEnumeration) throws IOException ; 251 protected abstract void enumerationClassTemplate(EnumerationType objEnumeration) throws IOException ; 253 254 protected abstract void structureTemplate(StructureType objStructure) throws IOException ; 256 257 259 public GenericMapper() { } 260 261 263 268 protected abstract boolean createStream(List pkg, String filename) throws IOException ; 269 273 protected abstract void closeStream() throws IOException ; 274 275 private boolean streamOpen = false; 276 private boolean switchStream(ModelElement me, String suffix) throws IOException { 277 if (streamOpen) { 278 streamOpen = false; 279 closeStream(); 280 } 281 streamOpen = createStream(parseDots(tagProvider.getTypePrefix(me)), 282 tagProvider.getSubstName(me) + suffix); 283 return streamOpen; 284 } 285 286 288 293 public void visitRefAssociation(RefAssociation refAssociation) throws IOException { 294 visitRefObject(refAssociation.refMetaObject()); 296 } 297 298 303 public void visitRefClass(RefClass refClass) throws IOException { 304 306 if (refClass.refImmediatePackage() instanceof ModelPackage) { 307 for (Iterator it = refClass.refAllOfClass().iterator(); it.hasNext();) { 308 visitRefObject((RefObject) it.next()); 309 } 310 } 311 } 312 313 318 public void visitRefObject(RefObject refObject) throws IOException { 319 if (visited.contains(refObject) || !(refObject instanceof Namespace)) { 320 return; 321 } 322 Namespace outermost = (Namespace) refObject; 323 for (;;) { 324 Namespace container = outermost.getContainer(); 325 if (container == null) 326 break; 327 outermost = container; 328 } 329 if (!(outermost instanceof MofPackage)) { 330 return; 331 } 332 boolean ignoreLifecycle = false; 333 String ignoreLifecycleString = tagProvider.getTagValue(outermost, TagProvider.TAGID_IGNORE_LIFECYCLE); 334 if (ignoreLifecycleString != null) { 335 if (ignoreLifecycleString.equals("true")) ignoreLifecycle = true; 337 } 339 340 try { 341 if (refObject instanceof Association) { 342 if (!ignoreLifecycle && switchStream((Association)refObject, "")) associationTemplate((Association)refObject); 345 } else if (refObject instanceof MofClass || refObject instanceof MofPackage) { 346 GeneralizableElement elm = (GeneralizableElement) refObject; 347 visited.add(elm); 348 for (Iterator it = elm.getSupertypes().iterator(); it.hasNext();) { 349 visitRefObject((RefObject) it.next()); 350 } 351 Collection contents = elm.getContents(); 352 for (Iterator it = contents.iterator(); it.hasNext();) { 353 visitRefObject((RefObject) it.next()); 354 } 355 if (elm instanceof MofPackage) { 356 if (!ignoreLifecycle && switchStream(elm, PACKAGE_POSTFIX)) 358 packageTemplate((MofPackage) elm); 359 } else { 360 if (switchStream(elm, "")) classInstanceTemplate((MofClass) elm); 363 if (!ignoreLifecycle && switchStream(elm, CLASS_POSTFIX)) 364 classProxyTemplate((MofClass) elm); 365 } 366 } else if (refObject instanceof EnumerationType) { 367 final EnumerationType et = (EnumerationType) refObject; 369 if (switchStream(et, "")) enumerationInterfaceTemplate(et); 371 if (switchStream(et, ENUM_POSTFIX)) 372 enumerationClassTemplate(et); 373 } else if (refObject instanceof StructureType) { 374 if (switchStream((StructureType)refObject, "")) structureTemplate((StructureType)refObject); 377 } else if (refObject instanceof MofException) { 378 if (switchStream((MofException)refObject, "")) exceptionTemplate((MofException)refObject); 381 } else { 382 } 384 } finally { 385 if (streamOpen) { 386 streamOpen = false; 387 closeStream(); 388 } 389 } 390 } 391 392 397 public void visitRefPackage(RefPackage refPackage) throws IOException { 398 400 if (refPackage instanceof ModelPackage) { 401 ModelPackage pkg = (ModelPackage) refPackage; 402 ModelElement element; 403 404 for (Iterator it = pkg.getMofPackage().refAllOfClass().iterator(); it.hasNext();) { 405 element = (ModelElement) it.next(); 406 if (element.getContainer() == null) { 407 visitRefObject(element); 408 } 409 } 410 } 411 } 412 413 public void visitRefBaseObject(RefBaseObject object) throws IOException { 414 if (object instanceof RefPackage) { 415 visitRefPackage((RefPackage) object); 416 } else if (object instanceof RefObject) { 417 visitRefObject((RefObject) object); 418 } else if (object instanceof RefAssociation) { 419 visitRefAssociation((RefAssociation) object); 420 } else if (object instanceof RefClass) { 421 visitRefClass((RefClass) object); 422 } 423 } 424 } 425 | Popular Tags |