1 26 27 package org.objectweb.openccm.generator.translator.ast2idl.lib; 28 29 import org.objectweb.openccm.ast.api.TypeKind; 30 import org.objectweb.openccm.ast.api.Declaration; 31 32 40 41 public class AST_IDLTranslator 42 implements org.objectweb.openccm.generator.translator.ast2idl.api.AST_IDLTranslator 43 { 44 50 53 protected java.util.Set keywords_; 54 55 61 64 public 65 AST_IDLTranslator() 66 { 67 keywords_ = new java.util.HashSet (); 68 keywords_.add("abstract"); 69 keywords_.add("any"); 70 keywords_.add("attribute"); 71 keywords_.add("boolean"); 72 keywords_.add("case"); 73 keywords_.add("char"); 74 keywords_.add("const"); 75 keywords_.add("context"); 76 keywords_.add("custom"); 77 keywords_.add("default"); 78 keywords_.add("double"); 79 keywords_.add("exception"); 80 keywords_.add("enum"); 81 keywords_.add("factory"); 82 keywords_.add("FALSE"); 83 keywords_.add("fixed"); 84 keywords_.add("float"); 85 keywords_.add("in"); 86 keywords_.add("inout"); 87 keywords_.add("interface"); 88 keywords_.add("local"); 89 keywords_.add("long"); 90 keywords_.add("module"); 91 keywords_.add("native"); 92 keywords_.add("Object"); 93 keywords_.add("octet"); 94 keywords_.add("oneway"); 95 keywords_.add("out"); 96 keywords_.add("private"); 97 keywords_.add("public"); 98 keywords_.add("raises"); 99 keywords_.add("readonly"); 100 keywords_.add("sequence"); 101 keywords_.add("short"); 102 keywords_.add("string"); 103 keywords_.add("struct"); 104 keywords_.add("supports"); 105 keywords_.add("switch"); 106 keywords_.add("TRUE"); 107 keywords_.add("truncatable"); 108 keywords_.add("typedef"); 109 keywords_.add("unsigned"); 110 keywords_.add("union"); 111 keywords_.add("ValueBase"); 112 keywords_.add("valuetype"); 113 keywords_.add("void"); 114 keywords_.add("wchar"); 115 keywords_.add("wstring"); 116 } 117 118 124 130 137 public String 138 toIDL(org.objectweb.openccm.ast.api.TypeRef typeref) 139 { 140 TypeKind tk = typeref.getTypeKind(); 141 142 if (tk == TypeKind.tk_null) 143 return "null"; 144 else if (tk == TypeKind.tk_void) 145 return "void"; 146 else if (tk == TypeKind.tk_short) 147 return "short"; 148 else if (tk == TypeKind.tk_ushort) 149 return "unsigned short"; 150 else if (tk == TypeKind.tk_long) 151 return "long"; 152 else if (tk == TypeKind.tk_ulong) 153 return "unsigned long"; 154 else if (tk == TypeKind.tk_longlong) 155 return "long long"; 156 else if (tk == TypeKind.tk_ulonglong) 157 return "unsigned long long"; 158 else if (tk == TypeKind.tk_float) 159 return "float"; 160 else if (tk == TypeKind.tk_double) 161 return "double"; 162 else if (tk == TypeKind.tk_longdouble) 163 return "long double"; 164 else if (tk == TypeKind.tk_boolean) 165 return "boolean"; 166 else if (tk == TypeKind.tk_char) 167 return "char"; 168 else if (tk == TypeKind.tk_wchar) 169 return "wchar"; 170 else if (tk == TypeKind.tk_octet) 171 return "octet"; 172 else if (tk == TypeKind.tk_any) 173 return "any"; 174 else if (tk == TypeKind.tk_Principal) 175 return "::CORBA::Principal"; 176 else if (tk == TypeKind.tk_TypeCode) 177 return "::CORBA::TypeCode"; 178 179 else if (tk == TypeKind.tk_objref) 180 return "Object"; 181 182 else if ( (tk == TypeKind.tk_struct) || 183 (tk == TypeKind.tk_union) || 184 (tk == TypeKind.tk_enum) || 185 (tk == TypeKind.tk_exception) || 186 (tk == TypeKind.tk_alias) || 187 (tk == TypeKind.tk_value_box) || 188 (tk == TypeKind.tk_value) || 189 (tk == TypeKind.tk_native) || 190 (tk == TypeKind.tk_abstract_interface) || 191 (tk == TypeKind.tk_local_interface) || 192 (tk == TypeKind.tk_interface) || 193 (tk == TypeKind.tk_component) || 194 (tk == TypeKind.tk_event) || 195 (tk == TypeKind.tk_home) ) 196 { 197 Declaration decl = (Declaration)typeref; 198 return getAbsoluteName(decl); 199 } 200 else if (tk == TypeKind.tk_string) 201 { 202 int l = ((org.objectweb.openccm.ast.api.TypeRefWithLength)typeref).getLength(); 203 if (l==0) 204 return "string"; 205 return "string<"+l+">"; 206 } 207 else if (tk == TypeKind.tk_wstring) 208 { 209 int l = ((org.objectweb.openccm.ast.api.TypeRefWithLength)typeref).getLength(); 210 if (l==0) 211 return "wstring"; 212 return "wstring<"+l+">"; 213 } 214 else if (tk == TypeKind.tk_sequence) 215 { 216 org.objectweb.openccm.ast.api.TypeRefWithContent type = (org.objectweb.openccm.ast.api.TypeRefWithContent)typeref; 217 int l = type.getLength(); 218 String str = toIDL(type.getContentType()); 219 if (l==0) 220 return "sequence<"+str+">"; 221 return "sequence<"+str+","+l+">"; 222 } 223 else if (tk == TypeKind.tk_array) 224 { 225 org.objectweb.openccm.ast.api.TypeRefWithContent type = (org.objectweb.openccm.ast.api.TypeRefWithContent)typeref; 226 String str = toIDL(type.getContentType()); 227 return str; 228 } 229 else if (tk == TypeKind.tk_fixed) 230 { 231 org.objectweb.openccm.ast.api.FixedTypeRef type = (org.objectweb.openccm.ast.api.FixedTypeRef)typeref; 232 return "fixed<"+type.getDigits()+","+type.getScale()+">"; 233 } 234 else 235 return ""; 236 } 237 238 245 public String 246 toIDLPostfix(org.objectweb.openccm.ast.api.TypeRef typeref) 247 { 248 String res = ""; 249 250 if ( typeref.getTypeKind() == TypeKind.tk_array ) 251 { 252 org.objectweb.openccm.ast.api.TypeRefWithContent type = null; 253 type = (org.objectweb.openccm.ast.api.TypeRefWithContent)typeref; 254 res = toIDLPostfix(type.getContentType())+"["+type.getLength()+"]"; 255 } 256 return res; 257 } 258 259 267 public String 268 toString(org.objectweb.openccm.ast.api.AnyValue anyvalue, 269 org.objectweb.openccm.ast.api.TypeRef typeref) 270 { 271 if (anyvalue.isInteger()) 275 return java.lang.String.valueOf(anyvalue.getAsInteger()); 276 else if (anyvalue.isString()) 277 return "\""+anyvalue.getAsString()+"\""; 278 else if (anyvalue.isWString()) 279 return "L\""+anyvalue.getAsWString()+"\""; 280 else if (anyvalue.isChar()) 281 return "\'"+java.lang.String.valueOf(anyvalue.getAsChar())+"\'"; 282 else if (anyvalue.isWChar()) 283 return "L\'"+java.lang.String.valueOf(anyvalue.getAsWChar())+"\'"; 284 else if (anyvalue.isFloating()) 285 return java.lang.String.valueOf(anyvalue.getAsFloating()); 286 else if (anyvalue.isFixed()) 287 return anyvalue.getAsFixed(); 288 else if (anyvalue.isBoolean()) 289 { 290 if (anyvalue.getAsBoolean()) 291 return "TRUE"; 292 else 293 return "FALSE"; 294 } 295 else if (anyvalue.isEnum()) 296 { 297 String abs_name = getAbsoluteName( ((org.objectweb.openccm.ast.api.EnumDecl)typeref).getParent() ); 299 return abs_name + "::" + anyvalue.getAsEnum(); 300 } 301 else 302 return ""; 303 } 304 305 312 public java.lang.String 313 checkKeywords(String id) 314 { 315 if (keywords_.contains( id.toLowerCase() )) 316 return "_"+id; 317 318 return id; 319 } 320 321 329 public String 330 getAbsoluteName(Declaration decl) 331 { 332 String result = ""; 333 334 if (decl.getParent() != null) 335 result = getAbsoluteName( decl.getParent() ); 336 337 if ( (decl.getName() != null) && 338 !(decl.getName().equals("")) ) 339 result = result + "::" + checkKeywords(decl.getName()); 340 341 return result; 342 } 343 344 } 345 | Popular Tags |