1 2 5 14 package org.jacorb.trading.impl; 15 16 import java.util.*; 17 import org.omg.CORBA.*; 18 import org.omg.CosTrading.*; 19 import org.omg.CosTradingDynamic.*; 20 import org.jacorb.trading.util.PropUtil; 21 22 25 26 public class Recipe 27 { 28 29 private Recipe() 30 { 31 } 32 33 37 public static boolean validate(String recipe, Property[] props) 38 { 39 boolean result = true; 40 41 int pos = 0; 42 int len = recipe.length(); 43 44 Hashtable propTable = new Hashtable(); 46 for (int i = 0; i < props.length; i++) 47 propTable.put(props[i].name, props[i].value); 48 49 boolean seenDollar = false; 50 51 while (pos < len && result) { 52 char ch = recipe.charAt(pos); 53 54 switch (ch) { 55 case '$': 56 if (seenDollar) 57 seenDollar = false; 58 else 59 seenDollar = true; 60 break; 61 62 case '*': 63 if (seenDollar) 64 seenDollar = false; 65 break; 66 67 case '(': 68 if (seenDollar) { 69 int rparen = recipe.indexOf(')', pos); 70 if (rparen < 0) 71 result = false; else { 73 String propName = recipe.substring(pos + 1, rparen); 75 76 if (! propTable.containsKey(propName)) 78 result = false; 79 else { Any any = (Any)propTable.get(propName); 81 TypeCode tc = any.type(); 82 if (PropUtil.isDynamicProperty(tc)) { 83 DynamicProp dp = DynamicPropHelper.extract(any); 84 tc = dp.returned_type; 85 } 86 87 result = checkPropertyType(tc); 88 } 89 90 pos = rparen; 91 } 92 93 seenDollar = false; 94 } 95 break; 96 } 98 pos++; 99 } 101 if (seenDollar) 102 result = false; 103 104 return result; 105 } 106 107 108 113 public static String rewrite(String recipe, SourceAdapter src, String primary) 114 { 115 StringBuffer result = new StringBuffer (); 116 117 boolean seenDollar = false; 118 int pos = 0; 119 int len = recipe.length(); 120 121 while (pos < len) { 122 char ch = recipe.charAt(pos); 123 124 switch (ch) { 125 case '$': 126 if (seenDollar) { 127 result.append('$'); 128 seenDollar = false; 129 } 130 else 131 seenDollar = true; 132 break; 133 134 case '*': 135 if (seenDollar) { 136 seenDollar = false; 137 result.append(primary); 138 } 139 else 140 result.append(ch); 141 break; 142 143 case '(': 144 if (seenDollar) { 145 int rparen = recipe.indexOf(')', pos); 146 String propName = recipe.substring(pos + 1, rparen); 148 Any value = src.getPropertyValue(propName); 151 if (value == null) 152 return null; 153 result.append(convertValue(value)); 154 pos = rparen; 155 seenDollar = false; 156 } 157 else 158 result.append(ch); 159 break; 160 161 default: 162 result.append(ch); 163 } 165 pos++; 166 } 168 return result.toString(); 169 } 170 171 172 173 protected static boolean checkPropertyType(TypeCode tc) 174 { 175 boolean result = false; 176 177 TCKind kind = tc.kind(); 178 switch (kind.value()) { 179 case TCKind._tk_short: 181 case TCKind._tk_long: 182 case TCKind._tk_ushort: 183 case TCKind._tk_ulong: 184 case TCKind._tk_float: 185 case TCKind._tk_double: 186 case TCKind._tk_boolean: 187 case TCKind._tk_char: 188 case TCKind._tk_string: 189 result = true; 190 break; 191 } 192 193 return result; 194 } 195 196 197 198 protected static String convertValue(Any val) 199 { 200 String result = "<unknown>"; 201 202 TCKind kind = val.type().kind(); 203 204 try { 205 switch (kind.value()) { 206 case TCKind._tk_short: { 207 int s = val.extract_short(); 208 result = "" + s; 209 } 210 break; 211 212 case TCKind._tk_long: { 213 int l = val.extract_long(); 214 result = "" + l; 215 } 216 break; 217 218 case TCKind._tk_ushort: { 219 int i = val.extract_ushort(); 220 result = "" + i; 221 } 222 break; 223 224 case TCKind._tk_ulong: { 225 long l = val.extract_ulong(); 226 result = "" + l; 227 } 228 break; 229 230 case TCKind._tk_float: { 231 float f = val.extract_float(); 232 result = "" + f; 233 } 234 break; 235 236 case TCKind._tk_double: { 237 double d = val.extract_double(); 238 result = "" + d; 239 } 240 break; 241 242 case TCKind._tk_boolean: { 243 boolean b = val.extract_boolean(); 244 result = "" + (b ? "TRUE" : "FALSE"); 245 } 246 break; 247 248 case TCKind._tk_char: { 249 char c = val.extract_char(); 250 result = "'" + c + "'"; 251 } 252 break; 253 254 case TCKind._tk_string: { 255 String s = val.extract_string(); 256 result = "'" + s + "'"; 257 } 258 break; 259 } 260 } 261 catch (BAD_OPERATION e) { 262 e.printStackTrace(); 263 } 264 265 return result; 266 } 267 268 269 298 } 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | Popular Tags |