KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > ast > lib > AnyValueImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Mathieu Vadet.
23 Contributor(s): Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.ast.lib;
28
29 /** Used to report errors. */
30 import org.objectweb.openccm.ast.api.ASTError;
31
32 /** Used to create CORBA Any. */
33 import org.objectweb.openccm.corba.TheORB;
34
35 /** Used to create CORBA DynamicAny::DynAny. */
36 import org.objectweb.openccm.corba.TheDynamicAnyFactory;
37
38 /** Used to get original TypeCodes. */
39 import org.objectweb.openccm.corba.TypeCodeUtils;
40
41 /** Used to wrap CORBA user exceptions. */
42 import org.objectweb.openccm.corba.UserExceptionWrapper;
43
44 /** Used to manipulate CORBA TypeCode. */
45 import org.omg.CORBA.TypeCode JavaDoc;
46
47 /** Used to manipulate CORBA TypeCode kind. */
48 import org.omg.CORBA.TCKind JavaDoc;
49
50 /** Used to manipulate CORBA Any. */
51 import org.omg.CORBA.Any JavaDoc;
52
53 /** Used to manipulate CORBA DynamicAny::DynAny. */
54 import org.omg.DynamicAny.DynAny JavaDoc;
55
56 /** Used to manipulate CORBA DynamicAny::DynFixed. */
57 import org.omg.DynamicAny.DynFixed JavaDoc;
58 import org.omg.DynamicAny.DynFixedHelper JavaDoc;
59
60 /** Used to manipulate CORBA DynamicAny::DynEnum. */
61 import org.omg.DynamicAny.DynEnum JavaDoc;
62 import org.omg.DynamicAny.DynEnumHelper JavaDoc;
63
64 /**
65  * AnyValueImpl is a wrapper class to manipulate CORBA::Any objects
66  * more easily.
67  *
68  * This provides getting, setting, and testing operations
69  * for basic IDL data types.
70  *
71  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
72  * <a HREF="mailto:Mathieu.Vadet@lifl.fr">Mathieu Vadet</a>
73  *
74  * @version 0.3
75  */

76
77 public class AnyValueImpl
78        extends ObjectBase
79        implements org.objectweb.openccm.ast.api.AnyValue
80 {
81     // ==================================================================
82
//
83
// Kind of type.
84
//
85
// ==================================================================
86

87     /** Represent unknown type for the Any value. */
88     final private int NONE = 0;
89
90     /** Represent integer type for the Any value. */
91     final private int INTEGER = 1;
92
93     /** Represent string type for the Any value. */
94     final private int STRING = 2;
95
96     /** Represent wide string type for the Any value. */
97     final private int WSTRING = 3;
98
99     /** Represent character type for the Any value. */
100     final private int CHAR = 4;
101
102     /** Represent wide character type for the Any value. */
103     final private int WCHAR = 5;
104
105     /** Represent fixed type for the Any value. */
106     final private int FIXED = 6;
107
108     /** Represent float type for the Any value. */
109     final private int FLOATING = 7;
110
111     /** Represent boolean type for the Any value. */
112     final private int BOOLEAN = 8;
113
114     /** Represent enumerate type for the Any value. */
115     final private int ENUM = 9;
116
117     // ==================================================================
118
//
119
// Internal state.
120
//
121
// ==================================================================
122

123     /** The type of the constant. */
124     private int type_;
125
126     /** The value when type_ == INTEGER. */
127     private long integer_;
128
129     /** The value when type_ == STRING or WSTRING. */
130     private String JavaDoc string_;
131
132     /** The value when type_ == CHAR or WCHAR. */
133     private char char_;
134
135     /** The value when type_ == FIXED. */
136     private String JavaDoc fixed_;
137
138     /** The value when type_ == FLOATING. */
139     private double floating_;
140
141     /** The value when type_ == BOOLEAN. */
142     private boolean boolean_;
143
144     /** The TypeCode when type_ == ENUM. */
145     private TypeCode JavaDoc tc_;
146
147     /** The enumerator when type_ == ENUM. */
148     private int enumerator_;
149
150     // ==================================================================
151
//
152
// Constructor.
153
//
154
// ==================================================================
155

156     /** The default constructor. */
157     public
158     AnyValueImpl()
159     {
160         // Init as empty value.
161
type_ = NONE;
162     }
163
164     // ==================================================================
165
//
166
// Internal methods.
167
//
168
// ==================================================================
169

170     /**
171      * Obtain the any value as a CORBA::Any.
172      *
173      * @param tc The TypeCode of the desired any value.
174      * Note that one of the set* method should have been used
175      * to fix the value before using this method.
176      * If the TypeCode and the set* method are not consistent,
177      * an ASTError is thrown.
178      *
179      * @return An Any created from the TypeCode.
180      */

181     protected Any JavaDoc
182     computeAny(TypeCode JavaDoc tc)
183     {
184         if(type_ == NONE) {
185             Any JavaDoc any = TheORB.create_any();
186             any.insert_octet((byte)0);
187             return any;
188         }
189
190         TypeCode JavaDoc origTC = TypeCodeUtils.getOrigType(tc);
191
192         // TODO: Why using DynAny in all cases?
193
// TODO: Why creating DynAny from the unaliased TypeCode?
194
DynAny JavaDoc dynAny = TheDynamicAnyFactory.
195                         create_dyn_any_from_type_code(origTC);
196
197         try {
198             switch(origTC.kind().value()) {
199             case TCKind._tk_short:
200                 if(type_ != INTEGER)
201                     throw new ASTError("Invalid constant value!");
202                 dynAny.insert_short((short)integer_);
203                 break;
204
205             case TCKind._tk_long:
206                 if(type_ != INTEGER)
207                     throw new ASTError("Invalid constant value!");
208                 dynAny.insert_long((int)integer_);
209                 break;
210
211             case TCKind._tk_ushort:
212                 if(type_ != INTEGER)
213                     throw new ASTError("Invalid constant value!");
214                 dynAny.insert_ushort((short)integer_);
215                 break;
216
217             case TCKind._tk_ulong:
218                 if(type_ != INTEGER)
219                     throw new ASTError("Invalid constant value!");
220                 dynAny.insert_ulong((int)integer_);
221                 break;
222
223             case TCKind._tk_float:
224                 if(type_ != FLOATING)
225                     throw new ASTError("Invalid constant value!");
226                 dynAny.insert_float((float)floating_);
227                 break;
228
229             case TCKind._tk_double:
230                 if(type_ != FLOATING)
231                     throw new ASTError("Invalid constant value!");
232                 dynAny.insert_double(floating_);
233                 break;
234
235             case TCKind._tk_boolean:
236                 if(type_ != BOOLEAN)
237                     throw new ASTError("Invalid constant value!");
238                 dynAny.insert_boolean(boolean_);
239                 break;
240
241             case TCKind._tk_char:
242                 if(type_ != CHAR)
243                     throw new ASTError("Invalid constant value!");
244                 dynAny.insert_char(char_);
245                 break;
246
247             case TCKind._tk_string:
248                 if(type_ != STRING)
249                     throw new ASTError("Invalid constant value!");
250                 dynAny.insert_string(string_);
251                 break;
252
253             case TCKind._tk_longlong:
254                 if(type_ != INTEGER)
255                     throw new ASTError("Invalid constant value!");
256                 dynAny.insert_longlong(integer_);
257                 break;
258
259             case TCKind._tk_ulonglong:
260                 if(type_ != INTEGER)
261                     throw new ASTError("Invalid constant value!");
262                 dynAny.insert_ulonglong(integer_);
263                 break;
264
265             case TCKind._tk_wchar:
266                 if(type_ != WCHAR)
267                     throw new ASTError("Invalid constant value!");
268                 dynAny.insert_wchar(char_);
269                 break;
270
271             case TCKind._tk_wstring:
272                 if(type_ != WSTRING)
273                     throw new ASTError("Invalid constant value!");
274                 dynAny.insert_wstring(string_);
275                 break;
276
277             case TCKind._tk_fixed:
278                 if(type_ != FIXED)
279                     throw new ASTError("Invalid constant value!");
280                 DynFixedHelper.narrow(dynAny).set_value(fixed_);
281                 break;
282
283             case TCKind._tk_enum:
284                 if(type_ != ENUM)
285                     throw new ASTError("Invalid constant value!");
286                 if(!tc_.equal(origTC))
287                     throw new ASTError("Invalid constant type!");
288                 DynEnumHelper.narrow(dynAny).set_as_ulong(enumerator_);
289                 break;
290
291             default:
292                 throw new ASTError("Invalid constant type!");
293             }
294             return dynAny.to_any();
295         }
296         catch(org.omg.DynamicAny.DynAnyPackage.InvalidValue JavaDoc exc)
297         {
298             // Wrap the DynamicAny::InvalidValue exception.
299
throw new UserExceptionWrapper(exc);
300         }
301         catch(org.omg.DynamicAny.DynAnyPackage.TypeMismatch JavaDoc exc)
302         {
303             // Wrap the DynamicAny::TypeMismatch exception.
304
throw new UserExceptionWrapper(exc);
305         }
306     }
307
308     /**
309      * Set the Any value from a CORBA::Any.
310      *
311      * @param any The CORBA::Any.
312      */

313     protected void
314     loadAny(Any JavaDoc any)
315     {
316         TypeCode JavaDoc tc = any.type();
317         try
318         {
319             switch(tc.kind().value())
320             {
321             case TCKind._tk_short:
322                 setAsInteger(any.extract_short());
323                 break;
324             case TCKind._tk_long:
325                 setAsInteger(any.extract_long());
326                 break;
327             case TCKind._tk_ushort:
328                 setAsInteger(any.extract_ushort());
329                 break;
330             case TCKind._tk_ulong:
331                 setAsInteger(any.extract_ulong());
332                 break;
333             case TCKind._tk_float:
334                 setAsFloating(any.extract_float());
335                 break;
336             case TCKind._tk_double:
337                 setAsFloating(any.extract_double());
338                 break;
339             case TCKind._tk_boolean:
340                 setAsBoolean(any.extract_boolean());
341                 break;
342             case TCKind._tk_char:
343                 setAsChar(any.extract_char());
344                 break;
345             case TCKind._tk_wchar:
346                 setAsWChar(any.extract_wchar());
347                 break;
348             case TCKind._tk_string:
349                 setAsString(any.extract_string());
350                 break;
351             case TCKind._tk_wstring:
352                 setAsWString(any.extract_wstring());
353                 break;
354             case TCKind._tk_longlong:
355                 setAsInteger(any.extract_longlong());
356                 break;
357             case TCKind._tk_ulonglong:
358                 setAsInteger(any.extract_ulonglong());
359                 break;
360             case TCKind._tk_fixed:
361                 DynFixed JavaDoc fixed = TheDynamicAnyFactory.create_dyn_fixed(any);
362                 setAsFixed(fixed.get_value());
363                 break;
364             case TCKind._tk_enum:
365                 DynEnum JavaDoc dynEnum = TheDynamicAnyFactory.create_dyn_enum(any);
366                 setAsEnum(tc, dynEnum.get_as_ulong(), dynEnum.get_as_string());
367                 break;
368             default:
369                 break;
370             }
371
372         } catch(org.omg.CORBA.BAD_OPERATION JavaDoc ex) {
373             // Wrap the CORBA::BAD_OPERATION exception.
374
throw new ASTError(ex.getMessage());
375         }
376     }
377
378     // ==================================================================
379
//
380
// Public methods.
381
//
382
// ==================================================================
383

384     // ==================================================================
385
//
386
// Methods for OMG IDL org.objectweb.openccm.ast.api.AnyValue
387
//
388
// ==================================================================
389

390     /**
391      * Does it contain an integer value?
392      *
393      * @return True if it's an integer or false otherwise.
394      */

395     public boolean
396     isInteger()
397     {
398         return type_ == INTEGER;
399     }
400
401     /**
402      * Set with an integer value.
403      *
404      * @param value The integer to set as value.
405      */

406     public void
407     setAsInteger(long value)
408     {
409         type_ = INTEGER;
410         integer_ = value;
411     }
412
413     /**
414      * Get as an integer value.
415      *
416      * @return The integer that must have been previously set.
417      */

418     public long
419     getAsInteger()
420     {
421         return integer_;
422     }
423
424     /**
425      * Does it contain a string value?
426      *
427      * @return True if it's a string or false otherwise.
428      */

429     public boolean
430     isString()
431     {
432         return type_ == STRING;
433     }
434
435     /**
436      * Set with a string value.
437      *
438      * @param value The string to set as value.
439      */

440     public void
441     setAsString(String JavaDoc value)
442     {
443         type_ = STRING;
444         string_ = value;
445     }
446
447     /**
448      * Get as a string value.
449      *
450      * @return The string that must have been previously set.
451      */

452     public String JavaDoc
453     getAsString()
454     {
455         return string_;
456     }
457
458     /**
459      * Does it contain a wstring value?
460      *
461      * @return True if it's a wstring or false otherwise.
462      */

463     public boolean
464     isWString()
465     {
466         return type_ == WSTRING;
467     }
468
469     /**
470      * Set with a wstring value.
471      *
472      * @param value The wstring to set as value.
473      */

474     public void
475     setAsWString(String JavaDoc value)
476     {
477         type_ = WSTRING;
478         string_ = value;
479     }
480
481     /**
482      * Get as a wstring value.
483      *
484      * @return The wstring that must have been previously set.
485      */

486     public String JavaDoc
487     getAsWString()
488     {
489         return string_;
490     }
491
492     /**
493      * Does it contain a char value?
494      *
495      * @return True if it's a char or false otherwise.
496      */

497     public boolean
498     isChar()
499     {
500         return type_ == CHAR;
501     }
502
503     /**
504      * Set with a char value.
505      *
506      * @param value The char to set as value.
507      */

508     public void
509     setAsChar(char value)
510     {
511         type_ = CHAR;
512         char_ = value;
513     }
514
515     /**
516      * Get as a char value.
517      *
518      * @return The char that must have been previously set.
519      */

520     public char
521     getAsChar()
522     {
523         return char_;
524     }
525
526     /**
527      * Does it contain a wchar value?
528      *
529      * @return True if it's a wchar or false otherwise.
530      */

531     public boolean
532     isWChar()
533     {
534         return type_ == WCHAR;
535     }
536
537     /**
538      * Set with a wchar value.
539      *
540      * @param value The char to set as value.
541      */

542     public void
543     setAsWChar(char value)
544     {
545         type_ = WCHAR;
546         char_ = value;
547     }
548
549     /**
550      * Get as a wchar value.
551      *
552      * @return The wchar that must have been previously set.
553      */

554     public char
555     getAsWChar()
556     {
557         return char_;
558     }
559
560     /**
561      * Does it contain a fixed value?
562      *
563      * @return True if it's a fixed or false otherwise.
564      */

565     public boolean
566     isFixed()
567     {
568         return type_ == FIXED;
569     }
570
571     /**
572      * Set with a fixed value.
573      *
574      * @param value The fixed string to set as value.
575      */

576     public void
577     setAsFixed(String JavaDoc value)
578     {
579         type_ = FIXED;
580         fixed_ = value;
581     }
582
583     /**
584      * Get as a fixed value.
585      *
586      * @return The fixed string that must have been previously set.
587      */

588     public String JavaDoc
589     getAsFixed()
590     {
591         return fixed_;
592     }
593
594     /**
595      * Does it contain a floating value?
596      *
597      * @return True if it's a float or false otherwise.
598      */

599     public boolean
600     isFloating()
601     {
602         return type_ == FLOATING;
603     }
604
605     /**
606      * Set with a floating value.
607      *
608      * @param value The double to set as value.
609      */

610     public void
611     setAsFloating(double value)
612     {
613         type_ = FLOATING;
614         floating_ = value;
615     }
616
617     /**
618      * Get as a floating value.
619      *
620      * @return The double that must have been previously set.
621      */

622     public double
623     getAsFloating()
624     {
625         return floating_;
626     }
627
628     /**
629      * Does it contain a boolean value?
630      *
631      * @return True if it's a boolean or false otherwise.
632      */

633     public boolean
634     isBoolean()
635     {
636         return type_ == BOOLEAN;
637     }
638
639     /**
640      * Set with a boolean value.
641      *
642      * @param value The boolean to set as value.
643      */

644     public void
645     setAsBoolean(boolean value)
646     {
647         type_ = BOOLEAN;
648         boolean_ = value;
649     }
650
651     /**
652      * Get as a boolean value.
653      *
654      * @return The boolean that must have been previously set.
655      */

656     public boolean
657     getAsBoolean()
658     {
659         return boolean_;
660     }
661
662     /**
663      * Does it contain an enum value?
664      *
665      * @return True if it's a enum or false otherwise.
666      */

667     public boolean
668     isEnum()
669     {
670         return type_ == ENUM;
671     }
672
673     /**
674      * Set with an enum value.
675      *
676      * @param tc The associated enum TypeCode.
677      * @param value The enumerator value.
678      * @param name The enumerator name.
679      */

680     public void
681     setAsEnum(TypeCode JavaDoc tc,
682               int value,
683               String JavaDoc name)
684     {
685         type_ = ENUM;
686         tc_ = tc;
687         enumerator_ = value;
688         string_ = name;
689     }
690
691     /**
692      * Get as an enum value.
693      *
694      * @return The string that must have been previously set.
695      */

696     public String JavaDoc
697     getAsEnum()
698     {
699         return string_;
700     }
701 }
702
Popular Tags