KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > trading > impl > Recipe


1
2 // Copyright (C) 1998-1999
3
// Object Oriented Concepts, Inc.
4

5 // **********************************************************************
6
//
7
// Copyright (c) 1997
8
// Mark Spruiell (mark@intellisoft.com)
9
//
10
// See the COPYING file for more information
11
//
12
// **********************************************************************
13

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 /**
23  * Used for validating and rewriting proxy constraint recipes
24  */

25
26 public class Recipe
27 {
28
29   private Recipe()
30   {
31   }
32
33   /**
34    * Validates a proxy recipe; requires properties mentioned in the
35    * recipe to be present in props
36    */

37   public static boolean validate(String JavaDoc recipe, Property[] props)
38   {
39     boolean result = true;
40
41     int pos = 0;
42     int len = recipe.length();
43
44       // make a hashtable of properties for quicker lookup
45
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; // missing right paren
72
else {
73                 // extract the property name
74
String JavaDoc propName = recipe.substring(pos + 1, rparen);
75
76                 // make sure the property exists
77
if (! propTable.containsKey(propName))
78                 result = false;
79               else { // make sure we support the property's type
80
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       } // switch (ch)
97

98       pos++;
99     } // while
100

101     if (seenDollar)
102       result = false;
103
104     return result;
105   }
106
107
108   /**
109    * Returns the secondary constraint by following the recipe; assumes
110    * the recipe has previously been validated; returns null if a value
111    * for a required property could not be obtained
112    */

113   public static String JavaDoc rewrite(String JavaDoc recipe, SourceAdapter src, String JavaDoc primary)
114   {
115     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
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               // extract the property name
147
String JavaDoc propName = recipe.substring(pos + 1, rparen);
148               // use the source to get the property's value, which
149
// automatically handles dynamic properties
150
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       } // switch (ch)
164

165       pos++;
166     } // while
167

168     return result.toString();
169   }
170
171
172   /** Determines if the given property type is allowable in a recipe */
173   protected static boolean checkPropertyType(TypeCode tc)
174   {
175     boolean result = false;
176
177     TCKind kind = tc.kind();
178     switch (kind.value()) {
179         // these are the types of properties supported in a recipe
180
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   /** Converts a property value to a string */
198   protected static String JavaDoc convertValue(Any val)
199   {
200     String JavaDoc 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 JavaDoc 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   /**************** comment out this line to enable main()
270   public static void main(String[] args)
271   {
272     if (args.length != 1) {
273       System.out.println("Usage: Recipe <recipe>");
274       return;
275     }
276
277     ORB orb = ORB.init();
278     Any any;
279
280     Property[] props = new Property[2];
281     any = orb.create_any();
282     any.insert_long(2050023013);
283     props[0] = new Property("prop1", any);
284     any = orb.create_any();
285     any.insert_char('z');
286     props[1] = new Property("prop2", any);
287
288     if (! Recipe.validate(args[0], props)) {
289       System.out.println("Invalid recipe");
290       return;
291     }
292
293     SourceAdapter source = new SourceAdapter(null, props);
294     String result = Recipe.rewrite(args[0], source, "primary constraint");
295     System.out.println(result);
296   }
297   /**************** comment out this line to enable main() */

298 }
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
Popular Tags