KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > modfact > jmi > helper > XMIIOHelper


1 /**
2  * copyright 2002 2004 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.jmi.helper;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import javax.jmi.model.MofClass;
27 import javax.jmi.model.MofPackage;
28
29 /**
30  * @author Pierre Carpentier
31  *
32  */

33 public class XMIIOHelper {
34
35     /** The hashtable which contains the subclasses for each class. */
36     public static java.util.Hashtable JavaDoc _classes_sub_classes = new java.util.Hashtable JavaDoc();
37
38     /**
39      * Gets all the subclasses of a class.
40      * @param _class The super class.
41      * @return An array which contains all the subclasses of the class.
42      */

43     public static javax.jmi.model.MofClass[] subClasses(javax.jmi.model.Classifier _class) {
44         if (_classes_sub_classes.size() == 0) {
45             searchAllSubClasses(((MofPackage) _class.getContainer()).refClass().refAllOfType());
46         }
47         if (_classes_sub_classes.containsKey(JMIProvider.jmiClassifierQualifiedName(_class))) {
48             java.util.Vector JavaDoc vector =
49                 (java.util.Vector JavaDoc) _classes_sub_classes.get(JMIProvider.jmiClassifierQualifiedName(_class));
50             javax.jmi.model.MofClass[] cls = new javax.jmi.model.MofClass[vector.size()];
51             for (int i = 0; i < cls.length; i++)
52                 cls[i] = (javax.jmi.model.MofClass) vector.elementAt(i);
53             return cls;
54         } else {
55             return new javax.jmi.model.MofClass[0];
56         }
57     }
58
59     /**
60      * Search all subclasses of a package.
61      * Parses all the classes of a package, and build an hashtable which contains the subclasses for each class.
62      * This vector is used by the subClasses method.
63      * @param _packs The packages.
64      */

65     protected static void searchAllSubClasses(java.util.Collection JavaDoc _packs) {
66         // For all packages
67
Iterator JavaDoc it = _packs.iterator();
68         while (it.hasNext()) {
69             javax.jmi.model.MofClass[] classes = MofHelper.classesOfPackage((MofPackage) it.next());
70             // For all classes of the package
71
for (int j = 0; j < classes.length; j++) {
72                 javax.jmi.model.MofClass cls = (javax.jmi.model.MofClass) classes[j];
73                 java.util.List JavaDoc _sup_classes = allSupertypes(cls);
74                 // For all supertypes of the class
75
Iterator JavaDoc itSup = _sup_classes.iterator();
76                 while (itSup.hasNext()) {
77                     javax.jmi.model.Classifier sup = (javax.jmi.model.Classifier) itSup.next();
78                     addHashtableValue(_classes_sub_classes, JMIProvider.jmiClassifierQualifiedName(sup), cls);
79                 }
80             }
81         }
82     }
83
84     /**
85      * Gets the references of an association.
86      * @param _association The association.
87      * @return The references.
88      */

89     public static javax.jmi.model.Reference[] getReferences(javax.jmi.model.Association _association) {
90         java.util.Vector JavaDoc _temp = new java.util.Vector JavaDoc();
91         // Get the package which contains the association
92
MofPackage _package = (MofPackage) _association.getContainer();
93         // Get all classes
94
javax.jmi.model.MofClass _classes_of_package[] = MofHelper.classesOfPackage(_package);
95         // Get the AssociationEnd
96
javax.jmi.model.AssociationEnd[] _association_ends = MofHelper.associationEndsOfAssociation(_association);
97         for (int i = 0; i < _classes_of_package.length; i++) {
98             javax.jmi.model.MofClass _class = (javax.jmi.model.MofClass) _classes_of_package[i];
99             // already processed
100
javax.jmi.model.Reference _references[] = MofHelper.referencesOfClass(_class, false);
101             for (int j = 0; j < _references.length; j++) {
102                 javax.jmi.model.Reference _reference = (javax.jmi.model.Reference) _references[j];
103                 javax.jmi.model.AssociationEnd _association_end = _reference.getReferencedEnd();
104                 if (_association_end == _association_ends[0] || _association_end == _association_ends[1]) {
105                     _temp.addElement(_reference);
106                 }
107             }
108         }
109         javax.jmi.model.Reference[] _referencees = new javax.jmi.model.Reference[_temp.size()];
110         for (int i = 0; i < _referencees.length; i++)
111             _referencees[i] = (javax.jmi.model.Reference) _temp.elementAt(i);
112         return _referencees;
113     }
114
115     /**
116      * Gets the opposite association end of an association end.
117      * @param _association_end The association end.
118      * @return The opposite association end.
119      */

120     public static javax.jmi.model.AssociationEnd otherAssociationEnd(javax.jmi.model.AssociationEnd _association_end) {
121         javax.jmi.model.Association asso = (javax.jmi.model.Association) (_association_end.getContainer());
122         javax.jmi.model.AssociationEnd[] ends = MofHelper.associationEndsOfAssociation(asso);
123         if (ends[0] == _association_end)
124             return ends[1];
125         else
126             return ends[0];
127     }
128
129     /**
130      * Gets all the supertype of a classifier.
131      * @param classifier The classifier.
132      * @return The supertypes.
133      */

134     public static java.util.List JavaDoc allSupertypes(javax.jmi.model.Classifier classifier) {
135         java.util.List JavaDoc _supertypes = classifier.getSupertypes();
136         java.util.List JavaDoc _all_supertypes = new java.util.ArrayList JavaDoc(_supertypes);
137         java.util.Iterator JavaDoc it = _supertypes.iterator();
138         while (it.hasNext()) {
139             javax.jmi.model.Classifier supertype = (javax.jmi.model.Classifier) it.next();
140             java.util.List JavaDoc _super_supertypes = allSupertypes(supertype);
141             java.util.Iterator JavaDoc itSuper = _super_supertypes.iterator();
142             while (itSuper.hasNext()) {
143                 Object JavaDoc s = itSuper.next();
144                 if (!_all_supertypes.contains(s))
145                     _all_supertypes.add(s);
146             }
147         }
148         return _all_supertypes;
149     }
150
151     /**
152      * Replace the first occurence of the given string with the given replacement.
153      * @param original The original String to modify.
154      * @param to_replace The substring of the string to search and replace.
155      * @param new_value The replacement String.
156      * @return The modified String.
157      */

158     public static String JavaDoc replaceFirst(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
159         int index = original.indexOf(to_replace);
160         if (index != -1) {
161             return original.substring(0, index) + new_value + original.substring(index + to_replace.length());
162         } else {
163             return original;
164         }
165     }
166
167     public static String JavaDoc format1FirstMin(String JavaDoc toFormat) {
168         String JavaDoc format1 = JMIProvider.jmiFormat1(toFormat);
169         return format1.substring(0, 1).toLowerCase() + format1.substring(1);
170     }
171
172     /**
173      * Tests if the _input is a Java keyword.
174      * @param _input The word to test.
175      * @return The correct word to prevent Java conflicts.
176      */

177     public static String JavaDoc testJavaConflict(String JavaDoc _input) {
178         if (endsWithValues(_input, "Package|Helper|Holder|Operations|POA|POATie")
179             || matchesValues(
180                 _input,
181                 "abstract|default|if|private|throw|boolean|do|implements|protected|throws|break|double|import|public|transient|byte|else|instanceof|return|try|case|extends|int|short|void||catch|final|interface|static|volatilechar|finally|long|super|while|class|float|native|switch|const|for|new|synchronized|continue|goto|package|this|true|false|null|clone|equals|finalize|getClass|hashCode|notify|notifyAll|toString|wait"))
182             return "_" + _input;
183         else
184             return _input;
185     }
186
187     /**
188      * Parse a String and test if it corresponds to the value. This value can be multiple and separate by '|', for example,
189      * matchesValues(to_parse, new String("value1|value2|value3")).
190      * @param to_parse The substring of the string to search and replace.
191      * @param value The replacement String.
192      * @return TRUE if the parsed string corresponds to one of the values.
193      */

194     public static boolean matchesValues(String JavaDoc to_parse, String JavaDoc value) {
195         java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(value, "|");
196         while (token.hasMoreTokens()) {
197             if (to_parse.equals(token.nextToken()))
198                 return true;
199         }
200         return false;
201     }
202
203     /**
204      * Parse a String and test if it ends with the value. This value can be multiple and separate by '|', for example,
205      * matchesValues(to_parse, new String("end1|end2|end3")).
206      * @param to_parse The substring of the string to search and replace.
207      * @param value The replacement String.
208      * @return TRUE if the parsed string ends with on of the values.
209      */

210     public static boolean endsWithValues(String JavaDoc to_parse, String JavaDoc value) {
211         java.util.StringTokenizer JavaDoc token = new java.util.StringTokenizer JavaDoc(value, "|");
212         while (token.hasMoreTokens()) {
213             if (to_parse.endsWith(token.nextToken()))
214                 return true;
215         }
216         return false;
217     }
218
219     /**
220      * Add a value to a hashtable composed by an object as key (a model) and a vector as value (models keys).
221      * @param hashtable The hashtable.
222      * @param key The key in the hastable.
223      * @param object The referenced object to add to the vector corresponding to the key of the hashtable.
224      */

225     public static void addHashtableValue(java.util.Hashtable JavaDoc hashtable, Object JavaDoc key, Object JavaDoc object) {
226         if (hashtable.containsKey(key)) {
227             java.util.Vector JavaDoc vector = (java.util.Vector JavaDoc) hashtable.get(key);
228             if (!vector.contains(object))
229                 vector.add(object);
230         } else {
231             java.util.Vector JavaDoc vector = new java.util.Vector JavaDoc();
232             vector.add(object);
233             hashtable.put(key, vector);
234         }
235     }
236     
237     /**
238      * Give the all classes (ordered by inheritance) of a Package.
239      */

240     public static MofClass[] allClassesOfPackageOrderedByInheritance(MofPackage _package) {
241         List JavaDoc classesUnordered = new java.util.ArrayList JavaDoc();
242         classesUnordered = MofHelper.filterContentsByClass(_package, MofClass.class, true);
243         List JavaDoc packages = MofHelper.filterContentsByClass(_package, MofPackage.class, false);
244         for (int i=0 ; i<packages.size() ; i++) {
245             List JavaDoc classes = MofHelper.filterContentsByClass((MofPackage)packages.get(i), MofClass.class, true);
246             classesUnordered.addAll(classes);
247         }
248
249         Vector JavaDoc classesUnorderedV = new Vector JavaDoc();
250         Vector JavaDoc classesOrderedV = new Vector JavaDoc();
251         for (int i = 0; i < classesUnordered.size(); i++)
252             classesUnorderedV.addElement(classesUnordered.get(i));
253
254         int courant = 0;
255         while (classesUnorderedV.size() != 0) {
256             MofClass _courant = null;
257             _courant = (MofClass) classesUnorderedV.elementAt(courant);
258             List JavaDoc supertypes = _courant.getSupertypes();
259             boolean _is_top = true;
260             int j = 0;
261             while (_is_top && j < supertypes.size()) {
262                 if (classesUnorderedV.contains(supertypes.get(j)))
263                    _is_top = false;
264                 j++;
265             }
266             if (_is_top) {
267                 classesOrderedV.addElement(_courant);
268                 classesUnorderedV.removeElement(_courant);
269                 courant = 0;
270             } else
271                 courant++;
272         }
273         MofClass[] resu = new MofClass[classesOrderedV.size()];
274         for (int i = 0; i < resu.length; i++)
275             resu[i] = (MofClass) classesOrderedV.elementAt(i);
276         return resu;
277     }
278
279     /**
280      * @see fr.lip6.modfact.helper.JavaCommon#format1Idl2JavaConflict(String)
281      */

282     public static String JavaDoc format1Idl2JavaConflict(String JavaDoc _input) {
283         Vector JavaDoc s1 = JMIProvider.splitAndDelete(_input);
284         String JavaDoc buffer = " ";
285         for (int i = 0; i < s1.size(); i++) {
286             char[] s2 = s1.elementAt(i).toString().toCharArray();
287             s2[0] = Character.toUpperCase(s1.elementAt(i).toString().charAt(0));
288             String JavaDoc s3 = String.valueOf(s2);
289             buffer = buffer.concat(s3);
290         }
291         return testJavaConflict(buffer.trim());
292     }
293     
294     /**
295      * @see fr.lip6.modfact.helper.JavaCommon#format2Idl2JavaConflict(String)
296      */

297     public static String JavaDoc format2Idl2JavaConflict(String JavaDoc _input) {
298         Vector JavaDoc s1 = JMIProvider.splitAndDelete(_input);
299         String JavaDoc buffer = " ";
300         for (int i = 0; i < s1.size(); i++) {
301             String JavaDoc s2 = s1.elementAt(i).toString().toLowerCase();
302             buffer = buffer.concat(s2);
303             if (i != (s1.size() - 1))
304                 buffer = buffer.concat("_");
305         }
306         return testJavaConflict(buffer.trim());
307     }
308     
309 }
310
Popular Tags