1 20 package org.objectweb.modfact.jmi.helper; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Vector ; 25 26 import javax.jmi.model.MofClass; 27 import javax.jmi.model.MofPackage; 28 29 33 public class XMIIOHelper { 34 35 36 public static java.util.Hashtable _classes_sub_classes = new java.util.Hashtable (); 37 38 43 public static javax.jmi.model.MofClass[] subClasses(javax.jmi.model.Classifier _class) { 44 if (_classes_sub_classes.size() == 0) { 45 searchAllSubClasses(((MofPackage) _class.getContainer()).refClass().refAllOfType()); 46 } 47 if (_classes_sub_classes.containsKey(JMIProvider.jmiClassifierQualifiedName(_class))) { 48 java.util.Vector vector = 49 (java.util.Vector ) _classes_sub_classes.get(JMIProvider.jmiClassifierQualifiedName(_class)); 50 javax.jmi.model.MofClass[] cls = new javax.jmi.model.MofClass[vector.size()]; 51 for (int i = 0; i < cls.length; i++) 52 cls[i] = (javax.jmi.model.MofClass) vector.elementAt(i); 53 return cls; 54 } else { 55 return new javax.jmi.model.MofClass[0]; 56 } 57 } 58 59 65 protected static void searchAllSubClasses(java.util.Collection _packs) { 66 Iterator it = _packs.iterator(); 68 while (it.hasNext()) { 69 javax.jmi.model.MofClass[] classes = MofHelper.classesOfPackage((MofPackage) it.next()); 70 for (int j = 0; j < classes.length; j++) { 72 javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes[j]; 73 java.util.List _sup_classes = allSupertypes(cls); 74 Iterator itSup = _sup_classes.iterator(); 76 while (itSup.hasNext()) { 77 javax.jmi.model.Classifier sup = (javax.jmi.model.Classifier) itSup.next(); 78 addHashtableValue(_classes_sub_classes, JMIProvider.jmiClassifierQualifiedName(sup), cls); 79 } 80 } 81 } 82 } 83 84 89 public static javax.jmi.model.Reference[] getReferences(javax.jmi.model.Association _association) { 90 java.util.Vector _temp = new java.util.Vector (); 91 MofPackage _package = (MofPackage) _association.getContainer(); 93 javax.jmi.model.MofClass _classes_of_package[] = MofHelper.classesOfPackage(_package); 95 javax.jmi.model.AssociationEnd[] _association_ends = MofHelper.associationEndsOfAssociation(_association); 97 for (int i = 0; i < _classes_of_package.length; i++) { 98 javax.jmi.model.MofClass _class = (javax.jmi.model.MofClass) _classes_of_package[i]; 99 javax.jmi.model.Reference _references[] = MofHelper.referencesOfClass(_class, false); 101 for (int j = 0; j < _references.length; j++) { 102 javax.jmi.model.Reference _reference = (javax.jmi.model.Reference) _references[j]; 103 javax.jmi.model.AssociationEnd _association_end = _reference.getReferencedEnd(); 104 if (_association_end == _association_ends[0] || _association_end == _association_ends[1]) { 105 _temp.addElement(_reference); 106 } 107 } 108 } 109 javax.jmi.model.Reference[] _referencees = new javax.jmi.model.Reference[_temp.size()]; 110 for (int i = 0; i < _referencees.length; i++) 111 _referencees[i] = (javax.jmi.model.Reference) _temp.elementAt(i); 112 return _referencees; 113 } 114 115 120 public static javax.jmi.model.AssociationEnd otherAssociationEnd(javax.jmi.model.AssociationEnd _association_end) { 121 javax.jmi.model.Association asso = (javax.jmi.model.Association) (_association_end.getContainer()); 122 javax.jmi.model.AssociationEnd[] ends = MofHelper.associationEndsOfAssociation(asso); 123 if (ends[0] == _association_end) 124 return ends[1]; 125 else 126 return ends[0]; 127 } 128 129 134 public static java.util.List allSupertypes(javax.jmi.model.Classifier classifier) { 135 java.util.List _supertypes = classifier.getSupertypes(); 136 java.util.List _all_supertypes = new java.util.ArrayList (_supertypes); 137 java.util.Iterator it = _supertypes.iterator(); 138 while (it.hasNext()) { 139 javax.jmi.model.Classifier supertype = (javax.jmi.model.Classifier) it.next(); 140 java.util.List _super_supertypes = allSupertypes(supertype); 141 java.util.Iterator itSuper = _super_supertypes.iterator(); 142 while (itSuper.hasNext()) { 143 Object s = itSuper.next(); 144 if (!_all_supertypes.contains(s)) 145 _all_supertypes.add(s); 146 } 147 } 148 return _all_supertypes; 149 } 150 151 158 public static String replaceFirst(String original, String to_replace, String new_value) { 159 int index = original.indexOf(to_replace); 160 if (index != -1) { 161 return original.substring(0, index) + new_value + original.substring(index + to_replace.length()); 162 } else { 163 return original; 164 } 165 } 166 167 public static String format1FirstMin(String toFormat) { 168 String format1 = JMIProvider.jmiFormat1(toFormat); 169 return format1.substring(0, 1).toLowerCase() + format1.substring(1); 170 } 171 172 177 public static String testJavaConflict(String _input) { 178 if (endsWithValues(_input, "Package|Helper|Holder|Operations|POA|POATie") 179 || matchesValues( 180 _input, 181 "abstract|default|if|private|throw|boolean|do|implements|protected|throws|break|double|import|public|transient|byte|else|instanceof|return|try|case|extends|int|short|void||catch|final|interface|static|volatilechar|finally|long|super|while|class|float|native|switch|const|for|new|synchronized|continue|goto|package|this|true|false|null|clone|equals|finalize|getClass|hashCode|notify|notifyAll|toString|wait")) 182 return "_" + _input; 183 else 184 return _input; 185 } 186 187 194 public static boolean matchesValues(String to_parse, String value) { 195 java.util.StringTokenizer token = new java.util.StringTokenizer (value, "|"); 196 while (token.hasMoreTokens()) { 197 if (to_parse.equals(token.nextToken())) 198 return true; 199 } 200 return false; 201 } 202 203 210 public static boolean endsWithValues(String to_parse, String value) { 211 java.util.StringTokenizer token = new java.util.StringTokenizer (value, "|"); 212 while (token.hasMoreTokens()) { 213 if (to_parse.endsWith(token.nextToken())) 214 return true; 215 } 216 return false; 217 } 218 219 225 public static void addHashtableValue(java.util.Hashtable hashtable, Object key, Object object) { 226 if (hashtable.containsKey(key)) { 227 java.util.Vector vector = (java.util.Vector ) hashtable.get(key); 228 if (!vector.contains(object)) 229 vector.add(object); 230 } else { 231 java.util.Vector vector = new java.util.Vector (); 232 vector.add(object); 233 hashtable.put(key, vector); 234 } 235 } 236 237 240 public static MofClass[] allClassesOfPackageOrderedByInheritance(MofPackage _package) { 241 List classesUnordered = new java.util.ArrayList (); 242 classesUnordered = MofHelper.filterContentsByClass(_package, MofClass.class, true); 243 List packages = MofHelper.filterContentsByClass(_package, MofPackage.class, false); 244 for (int i=0 ; i<packages.size() ; i++) { 245 List classes = MofHelper.filterContentsByClass((MofPackage)packages.get(i), MofClass.class, true); 246 classesUnordered.addAll(classes); 247 } 248 249 Vector classesUnorderedV = new Vector (); 250 Vector classesOrderedV = new Vector (); 251 for (int i = 0; i < classesUnordered.size(); i++) 252 classesUnorderedV.addElement(classesUnordered.get(i)); 253 254 int courant = 0; 255 while (classesUnorderedV.size() != 0) { 256 MofClass _courant = null; 257 _courant = (MofClass) classesUnorderedV.elementAt(courant); 258 List supertypes = _courant.getSupertypes(); 259 boolean _is_top = true; 260 int j = 0; 261 while (_is_top && j < supertypes.size()) { 262 if (classesUnorderedV.contains(supertypes.get(j))) 263 _is_top = false; 264 j++; 265 } 266 if (_is_top) { 267 classesOrderedV.addElement(_courant); 268 classesUnorderedV.removeElement(_courant); 269 courant = 0; 270 } else 271 courant++; 272 } 273 MofClass[] resu = new MofClass[classesOrderedV.size()]; 274 for (int i = 0; i < resu.length; i++) 275 resu[i] = (MofClass) classesOrderedV.elementAt(i); 276 return resu; 277 } 278 279 282 public static String format1Idl2JavaConflict(String _input) { 283 Vector s1 = JMIProvider.splitAndDelete(_input); 284 String buffer = " "; 285 for (int i = 0; i < s1.size(); i++) { 286 char[] s2 = s1.elementAt(i).toString().toCharArray(); 287 s2[0] = Character.toUpperCase(s1.elementAt(i).toString().charAt(0)); 288 String s3 = String.valueOf(s2); 289 buffer = buffer.concat(s3); 290 } 291 return testJavaConflict(buffer.trim()); 292 } 293 294 297 public static String format2Idl2JavaConflict(String _input) { 298 Vector s1 = JMIProvider.splitAndDelete(_input); 299 String buffer = " "; 300 for (int i = 0; i < s1.size(); i++) { 301 String s2 = s1.elementAt(i).toString().toLowerCase(); 302 buffer = buffer.concat(s2); 303 if (i != (s1.size() - 1)) 304 buffer = buffer.concat("_"); 305 } 306 return testJavaConflict(buffer.trim()); 307 } 308 309 } 310 | Popular Tags |