KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > corba > provider > XMIImportProvider


1 /**
2  * copyright 2002 2003 Laboratoire d'Informatique Paris 6 (LIP6)
3  *
4  * This file is part of ModFact.
5  *
6  * ModFact is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * at your option) any later version.
10  *
11  * ModFact is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with ModFact; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package org.objectweb.modfact.corba.provider;
21
22 import org.objectweb.modfact.corba.helper.IDLCommon;
23 import org.objectweb.modfact.corba.helper.JavaCommon;
24 import org.objectweb.modfact.corba.helper.MOFCommon;
25 import org.objectweb.modfact.corba.helper.XMIImportCommon;
26
27 /**
28  * @author Pierre Carpentier
29  *
30  */

31 public class XMIImportProvider implements XMIImportCommon {
32
33     /** The Helper. */
34     private IDLCommon idlHelper;
35     private MOFCommon mofHelper;
36     private JavaCommon javaHelper;
37     
38     /**
39      * Default Constructor of the XMIImport Provider
40      */

41     public XMIImportProvider () {
42     }
43     
44     /**
45      * Constructor of the XMIImport Provider
46      * @param idlHelper The IDL helper.
47      * @param mofHelper The MOF helper.
48      * @param javaHelper The Java helper.
49      */

50     public XMIImportProvider (IDLCommon idlHelper, MOFCommon mofHelper, JavaCommon javaHelper) {
51         this.idlHelper = idlHelper;
52         this.mofHelper = mofHelper;
53         this.javaHelper = javaHelper;
54     }
55
56     /**
57      * Pass from a TypeCode to the corresponding Java type code
58      * @param _type The CORBA Type Code.
59      * @return A String representing the corresponding Java type.
60      */

61     public String JavaDoc typeCode2Java(org.omg.CORBA.TypeCode JavaDoc _type) {
62         switch (_type.kind().value()) {
63             case org.omg.CORBA.TCKind._tk_boolean :
64                 return "boolean";
65             case org.omg.CORBA.TCKind._tk_octet :
66                 return "byte";
67             case org.omg.CORBA.TCKind._tk_char :
68             case org.omg.CORBA.TCKind._tk_wchar :
69                 return "char";
70             case org.omg.CORBA.TCKind._tk_short :
71             case org.omg.CORBA.TCKind._tk_ushort :
72                 return "short";
73             case org.omg.CORBA.TCKind._tk_long :
74             case org.omg.CORBA.TCKind._tk_ulong :
75                 return "int";
76             case org.omg.CORBA.TCKind._tk_longlong :
77             case org.omg.CORBA.TCKind._tk_ulonglong :
78                 return "long";
79             case org.omg.CORBA.TCKind._tk_double :
80                 return "double";
81             case org.omg.CORBA.TCKind._tk_float :
82                 return "float";
83             case org.omg.CORBA.TCKind._tk_string :
84             case org.omg.CORBA.TCKind._tk_wstring :
85                 return "String";
86             default :
87                 return "Object";
88         }
89     }
90
91     /**
92      * Replace the first occurence of the given string with the given replacement.
93      * @param original The original String to modify.
94      * @param to_replace The substring of the string to search and replace.
95      * @param new_value The replacement String.
96      * @return The modified String.
97      */

98     public String JavaDoc replaceFirst(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
99         int index = original.indexOf(to_replace);
100         if (index != -1) {
101             return original.substring(0, index) + new_value + original.substring(index + to_replace.length());
102         } else {
103             return original;
104         }
105     }
106
107     public String JavaDoc format1FirstMin(String JavaDoc toFormat) {
108         String JavaDoc format1 = idlHelper.format1(toFormat);
109         return format1.substring(0, 1).toLowerCase() + format1.substring(1);
110     }
111
112     /**
113      * Parse a String and test if it corresponds to the value. This value can be multiple and separate by '|', for example,
114      * matchesValues(to_parse, new String("value1|value2|value3")).
115      * @param to_parse The substring of the string to search and replace.
116      * @param value The replacement String.
117      * @return TRUE if the parsed string corresponds to one of the values.
118      */

119     public boolean matchesValues(String JavaDoc to_parse, String JavaDoc value) {
120         java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(value, "|");
121         while (token.hasMoreTokens()) {
122             if (to_parse.equals(token.nextToken()))
123                 return true;
124         }
125         return false;
126     }
127
128     /**
129      * Parse a String and test if it ends with the value. This value can be multiple and separate by '|', for example,
130      * matchesValues(to_parse, new String("end1|end2|end3")).
131      * @param to_parse The substring of the string to search and replace.
132      * @param value The replacement String.
133      * @return TRUE if the parsed string ends with on of the values.
134      */

135     public boolean endsWithValues(String JavaDoc to_parse, String JavaDoc value) {
136         java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(value, "|");
137         while (token.hasMoreTokens()) {
138             if (to_parse.endsWith(token.nextToken()))
139                 return true;
140         }
141         return false;
142     }
143
144     /**
145      * Add a value to a hashtable composed by an object as key (a model) and a vector as value (models keys).
146      * @param hashtable The hashtable.
147      * @param key The key in the hastable.
148      * @param object The referenced object to add to the vector corresponding to the key of the hashtable.
149      */

150     public void addHashtableValue(java.util.Hashtable JavaDoc hashtable, Object JavaDoc key, Object JavaDoc object) {
151         if (hashtable.containsKey(key)) {
152             java.util.Vector JavaDoc vector = (java.util.Vector JavaDoc) hashtable.get(key);
153             if (!vector.contains(object))
154                 vector.add(object);
155         } else {
156             java.util.Vector JavaDoc vector = new java.util.Vector JavaDoc();
157             vector.add(object);
158             hashtable.put(key, vector);
159         }
160     }
161
162     /**
163      * Tests if a MOF Object is contained in a vector.
164      * @param _vector The vector to parse.
165      * @param _object The MOF Object.
166      * @return TRUE if the vector contains the MOF Object.
167      */

168     public boolean vectorContains(java.util.Vector JavaDoc _vector, org.omg.mof.Reflective.RefBaseObject _object) {
169         if (_vector.size() == 0)
170             return false;
171         int i = 0;
172         boolean _is_contained = false;
173         while (!_is_contained && (i < _vector.size())) {
174             org.omg.mof.Reflective.RefBaseObject _current = (org.omg.mof.Reflective.RefBaseObject) _vector.elementAt(i);
175             if (_current.ref_itself(_object))
176                 _is_contained = true;
177             else
178                 i++;
179         }
180         return _is_contained;
181     }
182
183     /**
184      * Tests if a MOF Object is contained in a vector.
185      * @param _array The array to parse.
186      * @param _object The MOF Object.
187      * @return TRUE if the vector contains the MOF Object.
188      */

189     public boolean arrayContains(org.omg.CORBA.Object JavaDoc[] _array, org.omg.mof.Reflective.RefBaseObject _object) {
190         for (int i = 0; i < _array.length; i++) {
191             if (_array[i]._is_equivalent(_object))
192                 return true;
193         }
194         return false;
195     }
196
197     /**
198      * Gets the default value of an Attribute.
199      * @param _type The CORBA Type Code of the attribute.
200      * @return The default value of the attribute.
201      */

202     public String JavaDoc defaultAttributeValue(org.omg.CORBA.TypeCode JavaDoc _type) throws org.omg.mof.Reflective.MofError {
203         return defaultAttributeValue(_type, null);
204     }
205
206     /**
207      * Gets the default value of an Attribute.
208      * @param _type The CORBA Type Code of the attribute.
209      * @param _classifier The Object type of the attribute.
210      * @return The default value of the attribute.
211      */

212     public String JavaDoc defaultAttributeValue(org.omg.CORBA.TypeCode JavaDoc _type, org.omg.mof.Model.Classifier _classifier)
213         throws org.omg.mof.Reflective.MofError {
214         try {
215             switch (_type.kind().value()) {
216                 case org.omg.CORBA.TCKind._tk_boolean :
217                     return "false";
218                 case org.omg.CORBA.TCKind._tk_octet :
219                 case org.omg.CORBA.TCKind._tk_short :
220                 case org.omg.CORBA.TCKind._tk_ushort :
221                 case org.omg.CORBA.TCKind._tk_long :
222                 case org.omg.CORBA.TCKind._tk_ulong :
223                 case org.omg.CORBA.TCKind._tk_longlong :
224                 case org.omg.CORBA.TCKind._tk_ulonglong :
225                     return "0";
226                 case org.omg.CORBA.TCKind._tk_double :
227                 case org.omg.CORBA.TCKind._tk_float :
228                     return "0.0";
229                 case org.omg.CORBA.TCKind._tk_char :
230                 case org.omg.CORBA.TCKind._tk_wchar :
231                     return "''";
232                 case org.omg.CORBA.TCKind._tk_string :
233                 case org.omg.CORBA.TCKind._tk_wstring :
234                     return "\"\"";
235                 case org.omg.CORBA.TCKind._tk_alias :
236                     return defaultAttributeValue(_type.content_type(), _classifier);
237                 case org.omg.CORBA.TCKind._tk_enum :
238                     if (_type.member_count() > 0)
239                         return javaHelper.javaQualifiedName(_classifier) + "." + _type.member_name(0);
240                     else {
241                         return "null";
242                     }
243                 case org.omg.CORBA.TCKind._tk_TypeCode :
244                     return "_orb.create_any().type()";
245                 default :
246                     String JavaDoc classifierName = _classifier.name();
247                     if (isPrimitiveType(classifierName)) {
248                         if (classifierName.equalsIgnoreCase("String"))
249                             return "new String()";
250                         if (classifierName.equalsIgnoreCase("Boolean"))
251                             return "false";
252                         if (classifierName.equalsIgnoreCase("Integer") || classifierName.equalsIgnoreCase("Long"))
253                             return "0";
254                         if (classifierName.equalsIgnoreCase("Float") || classifierName.equalsIgnoreCase("Double"))
255                             return "0.0";
256                         return null;
257                     } else {
258                         return "null";
259                     }
260             }
261         } catch (Exception JavaDoc e) {
262             return "null";
263         }
264     }
265
266     /**
267      * Gets the default value of an Attribute.
268      * @param _classifier The Object type of the attribute.
269      * @return The default value of the attribute.
270      */

271     public String JavaDoc defaultAttributeValue(org.omg.mof.Model.Classifier _classifier) throws org.omg.mof.Reflective.MofError {
272         if (_classifier._is_a(org.omg.mof.Model.DataTypeHelper.id())) {
273             org.omg.mof.Model.DataType dataType = org.omg.mof.Model.DataTypeHelper.narrow(_classifier);
274             return defaultAttributeValue(dataType.type_code(), _classifier);
275         } else {
276             return "null";
277         }
278     }
279
280     /**
281      * Tests if the DataType Name represents a PrimitiveType.
282      * @param datatype_name The name of the datatype.
283      */

284     public boolean isPrimitiveType(String JavaDoc datatype_name) {
285         if (datatype_name.startsWith("*"))
286             return isPrimitiveType(datatype_name.substring(1));
287         String JavaDoc tmp = datatype_name.toLowerCase();
288         return (
289             tmp.equalsIgnoreCase("string")
290                 || tmp.equalsIgnoreCase("boolean")
291                 || tmp.equalsIgnoreCase("integer")
292                 || tmp.equalsIgnoreCase("long")
293                 || tmp.equalsIgnoreCase("float")
294                 || tmp.equalsIgnoreCase("double"));
295     }
296
297     /**
298      * Gets the references of an association.
299      * @param _association The association.
300      * @return The references.
301      */

302     public org.omg.mof.Model.Reference[] getReferences(org.omg.mof.Model.Association _association)
303         throws org.omg.mof.Reflective.MofError, org.omg.mof.Reflective.NotSet {
304         java.util.Vector JavaDoc _temp = new java.util.Vector JavaDoc();
305         // Get the package which contains the association
306
org.omg.mof.Model.Package _package = org.omg.mof.Model.PackageHelper.narrow(_association.container());
307         // Get all classes
308
org.omg.mof.Model.Class _classes_of_package [];
309         _classes_of_package = mofHelper.classesOfPackage(_package);
310         // Get the AssociationEnd
311
org.omg.mof.Model.AssociationEnd[] _association_ends = mofHelper.associationEndsOfAssociation(_association);
312         for (int i = 0; i < _classes_of_package.length; i++) {
313             org.omg.mof.Model.Class _class = (org.omg.mof.Model.Class) _classes_of_package[i];
314             // already processed
315
org.omg.mof.Model.Reference _references [];
316             _references = mofHelper.referencesOfClass(_class);
317             for (int j = 0; j < _references.length; j++) {
318                 org.omg.mof.Model.Reference _reference = (org.omg.mof.Model.Reference) _references[j];
319                 org.omg.mof.Model.AssociationEnd _association_end = _reference.referenced_end();
320                 if (_association_end._is_equivalent(_association_ends[0]) || _association_end._is_equivalent(_association_ends[1])) {
321                     _temp.addElement(_reference);
322                 }
323             }
324         }
325         org.omg.mof.Model.Reference[] _referencees = new org.omg.mof.Model.Reference[_temp.size()];
326         for (int i = 0; i < _referencees.length; i++)
327             _referencees[i] = (org.omg.mof.Model.Reference) _temp.elementAt(i);
328         return _referencees;
329     }
330
331 }
332
Popular Tags