1 20 package org.objectweb.modfact.corba.provider; 21 22 import org.objectweb.modfact.corba.helper.IDLCommon; 23 import org.objectweb.modfact.corba.helper.JavaCommon; 24 import org.objectweb.modfact.corba.helper.MOFCommon; 25 import org.objectweb.modfact.corba.helper.XMIImportCommon; 26 27 31 public class XMIImportProvider implements XMIImportCommon { 32 33 34 private IDLCommon idlHelper; 35 private MOFCommon mofHelper; 36 private JavaCommon javaHelper; 37 38 41 public XMIImportProvider () { 42 } 43 44 50 public XMIImportProvider (IDLCommon idlHelper, MOFCommon mofHelper, JavaCommon javaHelper) { 51 this.idlHelper = idlHelper; 52 this.mofHelper = mofHelper; 53 this.javaHelper = javaHelper; 54 } 55 56 61 public String typeCode2Java(org.omg.CORBA.TypeCode _type) { 62 switch (_type.kind().value()) { 63 case org.omg.CORBA.TCKind._tk_boolean : 64 return "boolean"; 65 case org.omg.CORBA.TCKind._tk_octet : 66 return "byte"; 67 case org.omg.CORBA.TCKind._tk_char : 68 case org.omg.CORBA.TCKind._tk_wchar : 69 return "char"; 70 case org.omg.CORBA.TCKind._tk_short : 71 case org.omg.CORBA.TCKind._tk_ushort : 72 return "short"; 73 case org.omg.CORBA.TCKind._tk_long : 74 case org.omg.CORBA.TCKind._tk_ulong : 75 return "int"; 76 case org.omg.CORBA.TCKind._tk_longlong : 77 case org.omg.CORBA.TCKind._tk_ulonglong : 78 return "long"; 79 case org.omg.CORBA.TCKind._tk_double : 80 return "double"; 81 case org.omg.CORBA.TCKind._tk_float : 82 return "float"; 83 case org.omg.CORBA.TCKind._tk_string : 84 case org.omg.CORBA.TCKind._tk_wstring : 85 return "String"; 86 default : 87 return "Object"; 88 } 89 } 90 91 98 public String replaceFirst(String original, String to_replace, String new_value) { 99 int index = original.indexOf(to_replace); 100 if (index != -1) { 101 return original.substring(0, index) + new_value + original.substring(index + to_replace.length()); 102 } else { 103 return original; 104 } 105 } 106 107 public String format1FirstMin(String toFormat) { 108 String format1 = idlHelper.format1(toFormat); 109 return format1.substring(0, 1).toLowerCase() + format1.substring(1); 110 } 111 112 119 public boolean matchesValues(String to_parse, String value) { 120 java.util.StringTokenizer token = new java.util.StringTokenizer (value, "|"); 121 while (token.hasMoreTokens()) { 122 if (to_parse.equals(token.nextToken())) 123 return true; 124 } 125 return false; 126 } 127 128 135 public boolean endsWithValues(String to_parse, String value) { 136 java.util.StringTokenizer token = new java.util.StringTokenizer (value, "|"); 137 while (token.hasMoreTokens()) { 138 if (to_parse.endsWith(token.nextToken())) 139 return true; 140 } 141 return false; 142 } 143 144 150 public void addHashtableValue(java.util.Hashtable hashtable, Object key, Object object) { 151 if (hashtable.containsKey(key)) { 152 java.util.Vector vector = (java.util.Vector ) hashtable.get(key); 153 if (!vector.contains(object)) 154 vector.add(object); 155 } else { 156 java.util.Vector vector = new java.util.Vector (); 157 vector.add(object); 158 hashtable.put(key, vector); 159 } 160 } 161 162 168 public boolean vectorContains(java.util.Vector _vector, org.omg.mof.Reflective.RefBaseObject _object) { 169 if (_vector.size() == 0) 170 return false; 171 int i = 0; 172 boolean _is_contained = false; 173 while (!_is_contained && (i < _vector.size())) { 174 org.omg.mof.Reflective.RefBaseObject _current = (org.omg.mof.Reflective.RefBaseObject) _vector.elementAt(i); 175 if (_current.ref_itself(_object)) 176 _is_contained = true; 177 else 178 i++; 179 } 180 return _is_contained; 181 } 182 183 189 public boolean arrayContains(org.omg.CORBA.Object [] _array, org.omg.mof.Reflective.RefBaseObject _object) { 190 for (int i = 0; i < _array.length; i++) { 191 if (_array[i]._is_equivalent(_object)) 192 return true; 193 } 194 return false; 195 } 196 197 202 public String defaultAttributeValue(org.omg.CORBA.TypeCode _type) throws org.omg.mof.Reflective.MofError { 203 return defaultAttributeValue(_type, null); 204 } 205 206 212 public String defaultAttributeValue(org.omg.CORBA.TypeCode _type, org.omg.mof.Model.Classifier _classifier) 213 throws org.omg.mof.Reflective.MofError { 214 try { 215 switch (_type.kind().value()) { 216 case org.omg.CORBA.TCKind._tk_boolean : 217 return "false"; 218 case org.omg.CORBA.TCKind._tk_octet : 219 case org.omg.CORBA.TCKind._tk_short : 220 case org.omg.CORBA.TCKind._tk_ushort : 221 case org.omg.CORBA.TCKind._tk_long : 222 case org.omg.CORBA.TCKind._tk_ulong : 223 case org.omg.CORBA.TCKind._tk_longlong : 224 case org.omg.CORBA.TCKind._tk_ulonglong : 225 return "0"; 226 case org.omg.CORBA.TCKind._tk_double : 227 case org.omg.CORBA.TCKind._tk_float : 228 return "0.0"; 229 case org.omg.CORBA.TCKind._tk_char : 230 case org.omg.CORBA.TCKind._tk_wchar : 231 return "''"; 232 case org.omg.CORBA.TCKind._tk_string : 233 case org.omg.CORBA.TCKind._tk_wstring : 234 return "\"\""; 235 case org.omg.CORBA.TCKind._tk_alias : 236 return defaultAttributeValue(_type.content_type(), _classifier); 237 case org.omg.CORBA.TCKind._tk_enum : 238 if (_type.member_count() > 0) 239 return javaHelper.javaQualifiedName(_classifier) + "." + _type.member_name(0); 240 else { 241 return "null"; 242 } 243 case org.omg.CORBA.TCKind._tk_TypeCode : 244 return "_orb.create_any().type()"; 245 default : 246 String classifierName = _classifier.name(); 247 if (isPrimitiveType(classifierName)) { 248 if (classifierName.equalsIgnoreCase("String")) 249 return "new String()"; 250 if (classifierName.equalsIgnoreCase("Boolean")) 251 return "false"; 252 if (classifierName.equalsIgnoreCase("Integer") || classifierName.equalsIgnoreCase("Long")) 253 return "0"; 254 if (classifierName.equalsIgnoreCase("Float") || classifierName.equalsIgnoreCase("Double")) 255 return "0.0"; 256 return null; 257 } else { 258 return "null"; 259 } 260 } 261 } catch (Exception e) { 262 return "null"; 263 } 264 } 265 266 271 public String defaultAttributeValue(org.omg.mof.Model.Classifier _classifier) throws org.omg.mof.Reflective.MofError { 272 if (_classifier._is_a(org.omg.mof.Model.DataTypeHelper.id())) { 273 org.omg.mof.Model.DataType dataType = org.omg.mof.Model.DataTypeHelper.narrow(_classifier); 274 return defaultAttributeValue(dataType.type_code(), _classifier); 275 } else { 276 return "null"; 277 } 278 } 279 280 284 public boolean isPrimitiveType(String datatype_name) { 285 if (datatype_name.startsWith("*")) 286 return isPrimitiveType(datatype_name.substring(1)); 287 String tmp = datatype_name.toLowerCase(); 288 return ( 289 tmp.equalsIgnoreCase("string") 290 || tmp.equalsIgnoreCase("boolean") 291 || tmp.equalsIgnoreCase("integer") 292 || tmp.equalsIgnoreCase("long") 293 || tmp.equalsIgnoreCase("float") 294 || tmp.equalsIgnoreCase("double")); 295 } 296 297 302 public org.omg.mof.Model.Reference[] getReferences(org.omg.mof.Model.Association _association) 303 throws org.omg.mof.Reflective.MofError, org.omg.mof.Reflective.NotSet { 304 java.util.Vector _temp = new java.util.Vector (); 305 org.omg.mof.Model.Package _package = org.omg.mof.Model.PackageHelper.narrow(_association.container()); 307 org.omg.mof.Model.Class _classes_of_package []; 309 _classes_of_package = mofHelper.classesOfPackage(_package); 310 org.omg.mof.Model.AssociationEnd[] _association_ends = mofHelper.associationEndsOfAssociation(_association); 312 for (int i = 0; i < _classes_of_package.length; i++) { 313 org.omg.mof.Model.Class _class = (org.omg.mof.Model.Class) _classes_of_package[i]; 314 org.omg.mof.Model.Reference _references []; 316 _references = mofHelper.referencesOfClass(_class); 317 for (int j = 0; j < _references.length; j++) { 318 org.omg.mof.Model.Reference _reference = (org.omg.mof.Model.Reference) _references[j]; 319 org.omg.mof.Model.AssociationEnd _association_end = _reference.referenced_end(); 320 if (_association_end._is_equivalent(_association_ends[0]) || _association_end._is_equivalent(_association_ends[1])) { 321 _temp.addElement(_reference); 322 } 323 } 324 } 325 org.omg.mof.Model.Reference[] _referencees = new org.omg.mof.Model.Reference[_temp.size()]; 326 for (int i = 0; i < _referencees.length; i++) 327 _referencees[i] = (org.omg.mof.Model.Reference) _temp.elementAt(i); 328 return _referencees; 329 } 330 331 } 332 | Popular Tags |