1 20 21 package org.jacorb.idl; 22 23 28 29 import java.io.File ; 30 import java.io.PrintWriter ; 31 import java.util.*; 32 33 class ConstDecl extends Declaration 34 { 35 private static Hashtable values = new Hashtable(); 36 private static Hashtable declarations = new Hashtable(); 37 38 private ScopedName t = new ScopedName(new_num()); 39 private int pos_int_const = 0; 40 private boolean int_const_set = false; 41 42 public ConstExpr const_expr; 43 public ConstType const_type; 44 45 public ConstDecl(int num) 46 { 47 super(num); 48 } 49 50 public static void init() 51 { 52 values.clear(); 53 declarations.clear(); 54 } 55 56 public static String namedValue(ScopedName sn) 57 { 58 String resolvedName = sn.resolvedName(); 59 if (values.containsKey(resolvedName)) 60 { 61 return (String )values.get(resolvedName); 62 } 63 else 64 { 65 return resolvedName; 66 } 67 } 68 69 public void setPackage(String s) 70 { 71 s = parser.pack_replace(s); 72 super.setPackage(s); 73 const_type.setPackage(s); 74 const_expr.setPackage(s); 75 t.typeName = name; 76 t.setPackage(s); 77 } 78 79 public void parse() 80 { 81 const_expr.setDeclaration(this); 82 try 83 { 84 NameTable.define(full_name(), "constant"); 85 } 86 catch (NameAlreadyDefined p) 87 { 88 parser.error("Constant " + full_name() + 89 " already defined", token); 90 } 91 const_type.parse(); 92 const_expr.parse(); 93 t.typeName = name; 94 values.put(t.resolvedName() + 95 (contained() ? "" : ".value"), 96 const_expr.value()); 97 98 if (logger.isDebugEnabled()) 99 { 100 logger.debug("ConstDecl.parse, put value: " + t.resolvedName() + 101 (contained() ? "" : ".value") + " , " + 102 const_expr.value()); 103 } 104 105 declarations.put(t.resolvedName(), this); 106 } 107 108 static ConstDecl getDeclaration(String resolvedName) 109 { 110 return (ConstDecl)declarations.get(resolvedName); 111 } 112 113 int pos_int_const() 114 { 115 if (!int_const_set) 116 { 117 pos_int_const = const_expr.pos_int_const(); 118 int_const_set = true; 119 } 120 return pos_int_const; 121 } 122 123 127 128 public void printContained(PrintWriter ps) 129 { 130 TypeSpec ts = const_type.symbol.typeSpec(); 131 boolean alias = ts instanceof AliasTypeSpec; 132 133 if (ts instanceof AliasTypeSpec) 134 { 135 ts = ((AliasTypeSpec)ts).originalType(); 136 } 137 138 ps.print("\t" + const_type + " " + name + " = "); 139 if (ts instanceof IntType && ((IntType)ts).type_spec instanceof ShortType) 140 { 141 ps.print("(short)("); 143 const_expr.print(ps); 144 ps.println(");"); 145 } 146 else if (ts instanceof LongLongType) 147 { 148 ps.print (const_expr.toString () + ';'); 149 } 150 else if (ts instanceof FloatType) 151 { 152 ps.print("(float)("); 154 const_expr.print(ps); 155 ps.println(");"); 156 } 157 else if (ts instanceof FixedPointConstType) 158 { 159 ps.print("new java.math.BigDecimal("); 161 const_expr.print(ps); 162 ps.println(");"); 163 } 164 else if (ts instanceof OctetType) 165 { 166 ps.print("(byte)("); 168 const_expr.print(ps); 169 ps.println(");"); 170 } 171 else 172 { 173 const_expr.print(ps); 174 ps.println(";"); 175 } 176 } 177 178 boolean contained() 179 { 180 181 boolean result = false; 182 IdlSymbol enc = getEnclosingSymbol(); 183 184 while(enc != null) 185 { 186 if (enc instanceof Interface) 187 { 188 result = true; 189 break; 190 } 191 enc = enc.getEnclosingSymbol(); 192 } 193 if (logger.isDebugEnabled()) 194 logger.debug("ConstDecl.contained()? " + full_name() 195 + " returns " + result); 196 return result; 197 } 198 199 202 203 public void print(PrintWriter ps) 204 { 205 if (contained() || (included && !generateIncluded())) 206 return; 207 208 try 209 { 210 String fullName = ScopedName.unPseudoName(full_name()); 211 String className; 212 if (fullName.indexOf('.') > 0) 213 { 214 pack_name = fullName.substring(0, fullName.lastIndexOf('.')); 215 className = fullName.substring(fullName.lastIndexOf('.') + 1); 216 } 217 else 218 { 219 pack_name = ""; 220 className = fullName; 221 } 222 223 String path = parser.out_dir + fileSeparator + 224 pack_name.replace('.', fileSeparator); 225 File dir = new File (path); 226 if (!dir.exists()) 227 { 228 if (!dir.mkdirs()) 229 { 230 org.jacorb.idl.parser.fatal_error("Unable to create " + path, null); 231 } 232 } 233 234 String fname = className + ".java"; 235 File f = new File (dir, fname); 236 237 if (GlobalInputStream.isMoreRecentThan(f)) 238 { 239 PrintWriter pw = new PrintWriter (new java.io.FileWriter (f)); 240 241 if (logger.isDebugEnabled()) 242 logger.debug("ConstDecl.print " + fname); 243 244 if (Environment.JAVA14 && pack_name.equals("")) 245 lexer.emit_warn 246 ("No package defined for " + className + " - illegal in JDK1.4", token); 247 if (!pack_name.equals("")) 248 pw.println("package " + pack_name + ";"); 249 250 pw.println("/**"); 251 pw.println(" * Automatically generated from IDL const definition "); 252 pw.println(" * @author JacORB IDL compiler "); 253 pw.println(" */\n"); 254 255 pw.println("public interface " + className); 256 pw.println("{"); 257 258 TypeSpec ts = const_type.symbol.typeSpec(); 259 if (ts instanceof AliasTypeSpec) 260 { 261 ts = ((AliasTypeSpec)ts).originalType(); 262 } 263 264 pw.print("\t" + const_type.toString() + " value = "); 265 266 if (logger.isWarnEnabled()) 267 logger.warn("ConstDecl, ts " + 268 const_type.toString() + " " + ts.getClass()); 269 270 if (ts instanceof ShortType) 271 { 272 pw.print("(short)(" + const_expr.toString() + ");"); 274 } 275 else if (ts instanceof LongLongType) 276 { 277 pw.print (const_expr.toString () + ';'); 278 } 279 else if (ts instanceof FloatType) 280 { 281 pw.println("(float)(" + const_expr.toString() + ");"); 283 } 284 else if (ts instanceof OctetType) 285 { 286 pw.println("(byte)(" + const_expr.toString() + ");"); 288 } 289 else if (ts instanceof FixedPointConstType || 290 ts instanceof FixedPointType) 291 { 292 pw.println("new java.math.BigDecimal (" + const_expr.toString() + ");"); 293 } 294 else 295 { 296 pw.println(const_expr.toString() + ";"); 297 } 298 pw.println("}"); 299 pw.close(); 300 } 301 } 302 catch (java.io.IOException i) 303 { 304 throw new RuntimeException ("File IO error" + i); 305 } 306 } 307 } 308 | Popular Tags |