1 19 package org.netbeans.lib.jmi.util; 20 21 import java.util.*; 22 23 import javax.jmi.model.*; 24 25 30 public class TagProvider extends Object { 31 public static final String TAGID_PACKAGE_PREFIX = "javax.jmi.packagePrefix"; public static final String TAGID_SUBSTITUTE_NAME = "javax.jmi.substituteName"; public static final String TAGID_IGNORE_LIFECYCLE = "javax.jmi.ignoreLifecycle"; public static final String TAGID_IMPL_PACKAGE_PREFIX = "org.netbeans.implPackagePrefix"; public static final String TAGID_XMI_NAMESPACE = "org.omg.xmi.namespace"; 37 public static final int ASSOCIATION = 0; 38 public static final int INSTANCE = 1; 39 public static final int CLASS = 2; 40 public static final int PACKAGE = 3; 41 42 private static final String DT_ORDERED = "java.util.List"; private static final String DT_MULTIVALUED = "java.util.Collection"; 45 private final HashMap valueCache = new HashMap(); 46 47 48 public TagProvider() { 49 } 50 51 public Tag getTag(ModelElement element, String tagID) { 52 Tag tag = null; 53 54 Collection tags = ((ModelPackage) element.refImmediatePackage()).getAttachesTo().getTag(element); 55 Tag temp; 56 for (Iterator it = tags.iterator(); it.hasNext();) { 57 temp = (Tag) it.next(); 58 if (tagID.equals(temp.getTagId())) { 59 tag = temp; 60 break; 61 } 62 } 63 64 return tag; 65 } 66 67 public Collection getTagValues(ModelElement element, String tagID) { 68 Tag tag = getTag(element, tagID); 69 70 if (tag == null) { 71 return null; 72 } 73 else { 74 return tag.getValues(); 75 } 76 } 77 78 public String getTagValue(ModelElement element, String tagID) { 79 if (element == null) return null; 80 String tagKey = element.refMofId() + ":" + tagID; String value = (String ) valueCache.get(tagKey); 82 83 if (value == null) { 84 Collection values = getTagValues(element, tagID); 85 86 if (values != null && values.size() > 0) { 87 value = (String ) values.iterator().next(); 88 valueCache.put(tagKey, value); 89 } 90 } 91 92 return value; 93 } 94 95 public String getTagValue(ModelElement element, String tagID, String defaultValue) { 96 String result = getTagValue(element, tagID); 97 if (result == null) { 98 result = defaultValue; 99 } 100 101 return result; 102 } 103 104 public String getTypeFullName(ModelElement type) { 105 return getTypePrefix(type, new StringBuffer (50)).append('.').append(getSubstName(type)).toString(); 106 } 107 108 public String getDataTypeName(Parameter prm) { 109 MultiplicityType mp = prm.getMultiplicity(); 110 111 if (mp.getUpper() > 1 || mp.getUpper() == -1) { 112 return (mp.isOrdered() ? DT_ORDERED : DT_MULTIVALUED); 113 } else { 114 return getDataTypeName(prm.getType()); 115 } 116 } 117 118 public String getDataTypeName(Classifier type) { 119 String result; 120 121 if (type instanceof PrimitiveType) { 122 result = "java.lang." + type.getName(); } else if (type instanceof AliasType) { 124 result = getDataTypeName(((AliasType) type).getType()); 125 } else if (type instanceof CollectionType) { 126 result = (((CollectionType) type).getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED); 127 } else { 128 result = getTypeFullName(type); 129 } 130 131 return result; 132 } 133 134 public String getSubstName(ModelElement element) { 135 String result = getTagValue(element, TAGID_SUBSTITUTE_NAME); 136 if (result == null) 137 result = element.getName(); 138 if (element instanceof Constant) { 139 result = mapName (result, true, false); 140 } else { 141 boolean flag = element instanceof MofClass || element instanceof MofPackage || 142 element instanceof Association || element instanceof MofException || 143 element instanceof StructureType || element instanceof EnumerationType || 144 element instanceof CollectionType || element instanceof Import; 145 result = mapName (result, false, !flag); 146 } if (element instanceof MofException && !result.endsWith("Exception")) result = result + "Exception"; return result; 150 } 151 152 private static String mapName(String name, boolean toLiteral, boolean firstLower) { 153 StringBuffer buffer = new StringBuffer (32); 154 boolean wordRead = false; 155 boolean lowerCharDetected = false; 156 for (int x = 0; x < name.length (); x++) { 157 char c = name.charAt (x); 158 if (c == '-' || c == '_' || Character.isWhitespace (c)) { 159 if (wordRead) { 160 if (toLiteral) 162 buffer.append ('_'); 163 wordRead = false; 164 lowerCharDetected = false; 165 } 166 } else { 167 if (lowerCharDetected && Character.isUpperCase (c)) { 168 if (toLiteral) 170 buffer.append ('_'); 171 wordRead = false; 172 lowerCharDetected = false; 173 } else if (Character.isLowerCase (c)) 174 lowerCharDetected = true; 175 if (!wordRead || toLiteral) 176 buffer.append (Character.toUpperCase (c)); 177 else 178 buffer.append (Character.toLowerCase (c)); 179 wordRead = true; 180 } } if (buffer.length () > 0) { 183 if (toLiteral && !wordRead) 184 buffer.deleteCharAt (buffer.length () - 1); 185 if (firstLower) 186 buffer.replace (0, 1, new String (new char [] {(Character.toLowerCase (buffer.charAt(0)))})); 187 } 188 return buffer.toString (); 189 } 190 191 public static String mapEnumLiteral (String name) { 192 return mapName (name, true, false); 193 } 194 195 public String getNamespaceName(MofPackage type) { 196 String result = getTagValue(type, TAGID_XMI_NAMESPACE); 197 if (result == null) { 198 result = type.getName(); 199 } 200 return result; 201 } 202 203 public String getTypePrefix(ModelElement metaObject) { 204 return getTypePrefix(metaObject, new StringBuffer (50)).toString(); 205 } 206 207 public String getImplFullName(ModelElement object, int type) { 208 return getImplPrefix(object, new StringBuffer (50)).append('.').append(getSubstName(object)).append(type == CLASS ? "Class" : (type == PACKAGE ? "Package" : "")).append("Impl").toString(); } 210 211 private StringBuffer getImplPrefix(ModelElement object, StringBuffer sb) { 212 try { 213 ModelElement pckg = object; 214 215 while (!(pckg instanceof MofPackage)) { 216 pckg = pckg.getContainer(); 217 } 218 219 Namespace container = pckg.getContainer(); 220 if (container == null) { 221 String result = getTagValue(pckg, TAGID_IMPL_PACKAGE_PREFIX); 222 if (result == null) { 223 result = getTagValue(pckg, TAGID_PACKAGE_PREFIX); 224 if (result == null) { 225 sb.append("impl"); } else { 227 result = result.toLowerCase(Locale.US); 228 if ("javax.jmi".equals(result)) { sb.append("org.netbeans.jmiimpl.mof"); } else if (result.startsWith("org.netbeans.jmi.")) { sb.append("org.netbeans.jmiimpl").append(result.substring(16)); } else if (result.startsWith("org.omg")) { if (result.length() == 7) { 234 sb.append("org.netbeans.jmiimpl.omg"); } else if (result.charAt(7) == '.') { 236 sb.append("org.netbeans.jmiimpl.omg").append(result.substring(7)); } else { 238 sb.append(result).append(".impl"); } 240 } else { 241 sb.append(result).append(".impl"); } 243 } 244 } else { 245 sb.append(result.toLowerCase(Locale.US)); 246 } 247 } else { 248 getImplPrefix(container, sb); 249 } 250 251 String packageName = getTagValue(pckg, TAGID_SUBSTITUTE_NAME); 252 if (packageName == null) { 253 packageName = mapName(pckg.getName(), false, true); 254 } 255 256 sb.append('.').append(packageName.toLowerCase(Locale.US)); 257 258 return sb; 259 } catch (Exception e) { 260 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 261 } 262 } 263 264 private StringBuffer getTypePrefix(ModelElement metaObject, StringBuffer sb) { 265 ModelElement pckg = metaObject; 266 267 while (!(pckg instanceof MofPackage)) { 268 pckg = pckg.getContainer(); 269 } 270 271 Namespace container = pckg.getContainer(); 272 if (container == null) { 273 String result = getTagValue(pckg, TagProvider.TAGID_PACKAGE_PREFIX); 274 if (result != null) { 275 sb.append(result.toLowerCase(Locale.US)).append('.'); 276 } 277 } else { 278 getTypePrefix(container, sb).append('.'); 279 } 280 281 String packageName = getTagValue(pckg, TagProvider.TAGID_SUBSTITUTE_NAME); 282 if (packageName == null) { 283 packageName = mapName(pckg.getName(), false, true); 284 } 285 286 return sb.append(packageName.toLowerCase(Locale.US)); 287 } 288 } 289 | Popular Tags |