KickJava   Java API By Example, From Geeks To Geeks.

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


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.jmi.helper;
21
22 import javax.jmi.model.*;
23
24 import java.util.*;
25 import java.util.regex.Matcher JavaDoc;
26 import java.util.regex.Pattern JavaDoc;
27
28 /**
29  *
30  */

31 /**
32  *
33  */

34 /**
35  * @author Xavier, Prawee
36  *
37  */

38 public class JMIProvider {
39
40     public static boolean considers_idl_substitute_name = true;
41
42     // ignore package prefix
43
public static String JavaDoc shortQualifierOf(ModelElement _elem) {
44         Namespace ns = null;
45
46         if (_elem instanceof Namespace) {
47             ns = (Namespace) _elem;
48         } else {
49             ns = _elem.getContainer();
50         }
51         if (ns == null)
52             return "";
53
54         String JavaDoc res = null;
55
56         while (true) {
57             if (ns instanceof MofPackage) {
58                 // ignore classes that contain datatypes
59
res =
60                      packageNameFormat(ns.getName())
61                         + ((res == null) ? "" : ("." + res));
62             }
63             Namespace ns2 = ns.getContainer();
64             if (ns2 == null)
65                 break;
66             ns = ns2;
67         }
68         return res;
69     }
70
71     // include package prefix
72
public static String JavaDoc qualifierOf(ModelElement _elem) {
73
74         return packagePrefixOf(_elem) + shortQualifierOf(_elem);
75
76     }
77
78     // return package prefix with dot
79
public static String JavaDoc packagePrefixOf(ModelElement _elem) {
80         Namespace ns = null;
81
82         if (_elem instanceof MofPackage) {
83             ns = (Namespace) _elem;
84         } else {
85             ns = _elem.getContainer();
86         }
87         if (ns == null)
88             return "";
89
90         while (true) {
91             Namespace ns2 = ns.getContainer();
92             if (ns2 == null)
93                 break;
94             ns = ns2;
95         }
96
97         Iterator it =
98             ((ModelPackage) ns.refImmediatePackage())
99                 .getTag()
100                 .refAllOfClass()
101                 .iterator();
102         while (it.hasNext()) {
103             Tag t = (Tag) it.next();
104             if ((t.getTagId().equals("javax.jmi.packagePrefix")
105                 || t.getTagId().equals("com.urep.jcr.java_prefix"))
106                 && t.getElements().contains(ns)) {
107                 return t.getValues().get(0) + ".";
108             }
109         }
110         return "";
111     }
112
113     public static String JavaDoc jmiPackageExtentName(MofPackage p) {
114         String JavaDoc stName = findSubstituteName(p);
115         if (stName != null)
116             return stName;
117         return classNameFormat(p.getName());
118     }
119     
120     /*
121      * Alias for PackageExtentName
122      * */

123     public static String JavaDoc jmiPackageName(MofPackage p) {
124         return jmiPackageExtentName(p);
125     }
126
127     public static String JavaDoc jmiPackageQualifiedName(MofPackage p) {
128         return qualifierOf(p) + "." + jmiPackageExtentName(p);
129     }
130
131     public static String JavaDoc jmiClassName(MofClass clazz) {
132         String JavaDoc stName = findSubstituteName(clazz);
133         if (stName != null)
134             return stName;
135         return classNameFormat(clazz.getName());
136     }
137
138     // if not found, return null
139
public static String JavaDoc findSubstituteName(ModelElement m) {
140         Iterator it =
141             ((ModelPackage) m.refImmediatePackage())
142                 .getTag()
143                 .refAllOfClass()
144                 .iterator();
145         while (it.hasNext()) {
146             Tag t = (Tag) it.next();
147             if ((t.getTagId().equals("javax.jmi.substituteName")
148                 || t.getTagId().equals("com.urep.jcr.java_substitute_name")
149                 || (considers_idl_substitute_name
150                     && t.getTagId().equals("org.omg.mof.idl_substitute_name")))
151                 && t.getElements().contains(m)) {
152                 return (String JavaDoc) t.getValues().get(0);
153             }
154         }
155         return null;
156     }
157
158     public static String JavaDoc jmiClassQualifiedName(MofClass clazz) {
159         return qualifierOf(clazz) + "." + jmiClassName(clazz);
160     }
161
162     public static String JavaDoc jmiAssociationName(Association a) {
163         String JavaDoc stName = findSubstituteName(a);
164         if (stName != null)
165             return stName;
166         return classNameFormat(a.getName());
167     }
168
169     public static String JavaDoc jmiAssociationQualifiedName(Association association) {
170         return qualifierOf(association) + "." + jmiAssociationName(association);
171     }
172
173     public static String JavaDoc jmiPrimitiveTypeName(PrimitiveType _type) {
174         String JavaDoc typeName = _type.getName().toLowerCase();
175         if (typeName.equals("string")) {
176             return "String";
177         } else if (typeName.equals("boolean")) {
178             return "boolean";
179         } else if (typeName.equals("integer") || typeName.equals("long")) {
180             return "int";
181         } else if (typeName.equals("float")) {
182             return "float";
183         } else if (typeName.equals("double")) {
184             return "double";
185         } else {
186             throw new RuntimeException JavaDoc(
187                 "unknown primitive type: " + _type.getName());
188         }
189     }
190
191     public static String JavaDoc jmiNullablePrimitiveTypeName(PrimitiveType _type) {
192         String JavaDoc typeName = _type.getName().toLowerCase();
193         if (typeName.equals("string")) {
194             return "java.lang.String";
195         } else if (typeName.equals("boolean")) {
196             return "java.lang.Boolean";
197         } else if (typeName.equals("integer") || typeName.equals("long")) {
198             return "java.lang.Integer";
199         } else if (typeName.equals("float")) {
200             return "java.lang.Float";
201         } else if (typeName.equals("double")) {
202             return "java.lang.Double";
203         } else {
204             throw new RuntimeException JavaDoc(
205                 "unknown primitive type: " + _type.getName());
206         }
207     }
208
209     public static String JavaDoc jmiDataTypeQualifiedName(DataType _type) {
210         if (_type instanceof PrimitiveType) {
211             return jmiNullablePrimitiveTypeName((PrimitiveType) _type);
212         }
213         return qualifierOf(_type) + "." + _type.getName();
214     }
215
216     public static String JavaDoc jmiImportName(Import _import) {
217         MofPackage p = (MofPackage) _import.getImportedNamespace();
218         return jmiPackageExtentName(p);
219     }
220
221     public static String JavaDoc jmiClassifierQualifiedName(Classifier _classifier) {
222         if (_classifier instanceof MofClass)
223             return jmiClassQualifiedName((MofClass) _classifier);
224
225         if (_classifier instanceof DataType)
226             return jmiDataTypeQualifiedName((DataType) _classifier);
227
228         throw new RuntimeException JavaDoc("invalide parameter:" + _classifier);
229     }
230
231     public static String JavaDoc jmiFormat1(String JavaDoc name) {
232         return noUnderscoreName(name, true);
233     }
234     
235     public static java.util.Vector JavaDoc splitAndDelete(String JavaDoc _input) {
236         java.util.Vector JavaDoc parts = new java.util.Vector JavaDoc();
237         String JavaDoc delim = "-_ .";
238         java.util.StringTokenizer JavaDoc _sstring = new java.util.StringTokenizer JavaDoc(_input, delim);
239         while (_sstring.hasMoreTokens()) {
240             String JavaDoc part = _sstring.nextToken();
241             int i = 0;
242             int j = 0;
243             boolean isPart = false;
244             boolean beginPart = true;
245             while (i < part.length()) {
246                 if (beginPart) {
247                     if ((Character.isDigit(part.charAt(i))) || (Character.isUpperCase(part.charAt(i))))
248                         i++;
249                     else
250                         beginPart = false;
251                 } else {
252                     if ((Character.isDigit(part.charAt(i))) || (Character.isLowerCase(part.charAt(i))))
253                         i++;
254                     else
255                         isPart = true;
256                 }
257                 if (isPart) {
258                     parts.addElement(part.substring(j, i));
259                     j = i;
260                     isPart = false;
261                     beginPart = true;
262                 }
263                 if (i == j)
264                     i++; // if the character is not specified in the filter
265
}
266             if (i != j)
267                 parts.addElement(part.substring(j, i));
268         }
269         return parts;
270     }
271     
272     public static String JavaDoc jmiFormat2(String JavaDoc name) {
273         java.util.Vector JavaDoc s1 = splitAndDelete(name);
274         String JavaDoc buffer = " ";
275         for (int i = 0; i < s1.size(); i++) {
276             String JavaDoc s2 = s1.elementAt(i).toString().toLowerCase();
277             buffer = buffer.concat(s2);
278             if (i != (s1.size() - 1))
279                 buffer = buffer.concat("_");
280         }
281         return buffer.trim();
282     }
283
284     // StructureField, Attribute, or Reference
285
public static String JavaDoc jmiAccessorName(TypedElement attribute) {
286
287         String JavaDoc accessorName = jmiFormat1(attribute.getName());
288         Classifier t = attribute.getType();
289
290         if ((t instanceof PrimitiveType)
291             && t.getName().equalsIgnoreCase("boolean")
292             && ((attribute instanceof StructureField)
293                 || ((Attribute) attribute).getMultiplicity().getUpper() == 1)) {
294
295             if (attribute.getName().toLowerCase().startsWith("is"))
296                 return attribute.getName();
297             else
298                 return "is" + accessorName;
299         } else
300             return "get" + accessorName;
301     }
302
303     // Attribute or Reference
304
public static String JavaDoc jmiMutatorName(StructuralFeature attribute) {
305
306         Classifier t = attribute.getType();
307
308         if ((t instanceof PrimitiveType)
309             && t.getName().equalsIgnoreCase("boolean")
310             && attribute.getMultiplicity().getUpper() == 1
311             && attribute.getName().toLowerCase().startsWith("is")) {
312
313             return "set" + jmiFormat1(attribute.getName().substring(2));
314
315         } else
316             return "set" + jmiFormat1(attribute.getName());
317     }
318
319     public static String JavaDoc typeTemplate(TypedElement elem) {
320         String JavaDoc javaType = null;
321         Classifier type = elem.getType();
322
323         if (MofHelper.isSinglePrimitive(elem)) {
324             javaType = JMIProvider.jmiPrimitiveTypeName((PrimitiveType) type);
325
326         } else if (
327             (elem instanceof StructuralFeature)
328                 && ((StructuralFeature) elem).getMultiplicity().getUpper() != 1) {
329
330             javaType =
331                 (((StructuralFeature) elem).getMultiplicity().isOrdered())
332                     ? "java.util.List"
333                     : "java.util.Collection";
334
335         } else if (
336             (elem instanceof Parameter)
337                 && ((Parameter) elem).getMultiplicity().getUpper() != 1) {
338
339             javaType =
340                 (((Parameter) elem).getMultiplicity().isOrdered())
341                     ? "java.util.List"
342                     : "java.util.Collection";
343
344         } else {
345             javaType = JMIProvider.jmiClassifierQualifiedName(type);
346         }
347
348         if ((elem instanceof Parameter)
349             && (((Parameter) elem)
350                 .getDirection()
351                 .equals(DirectionKindEnum.INOUT_DIR)
352                 || ((Parameter) elem).getDirection().equals(
353                     DirectionKindEnum.OUT_DIR))) {
354
355             javaType = javaType + "[]";
356         }
357         return javaType;
358     }
359
360     /**
361      * 4.7.1 rule for generating identifier
362      *
363      * @param name
364      * @return
365      */

366     public static String JavaDoc packageNameFormat(String JavaDoc name) {
367         return name.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
368     }
369
370     /**
371      * 4.7.1 rule for generating identifier
372      *
373      * @param name
374      * @return
375      */

376     public static String JavaDoc classNameFormat(String JavaDoc name) {
377         return noUnderscoreName(name, true);
378     }
379     
380
381     /**
382      * 4.7.1 rule for generating identifier
383      *
384      * @param name
385      * @return
386      */

387     public static String JavaDoc attributNameFormat(String JavaDoc name) {
388         return noUnderscoreName(name, false);
389     }
390
391     /**
392      * 4.7.1 rule for generating identifier
393      *
394      * @param name
395      * @return
396      */

397     public static String JavaDoc operationNameFormat(String JavaDoc name) {
398         return noUnderscoreName(name, false);
399     }
400
401     /**
402      * 4.7.1 rule for generating identifier
403      *
404      * @param name
405      * @return
406      */

407     public static String JavaDoc constantNameFormat(String JavaDoc name) {
408         return underscoreName(name);
409     }
410
411     /**
412      * 4.7.1 rule for generating identifier
413      *
414      * @param name
415      * @return
416      */

417     public static String JavaDoc enumerationNameFormat(String JavaDoc name) {
418         return underscoreName(name);
419     }
420     /*
421      * example
422      * input: "This is a CAT!!!"
423      * output: "THIS_IS_A_CAT"
424      * */

425     public static String JavaDoc underscoreName(String JavaDoc name) {
426         String JavaDoc res = "";
427         String JavaDoc decomposed = decompose(name);
428         StringTokenizer st = new StringTokenizer(decomposed);
429         if(st.countTokens()==0) {
430             throw new RuntimeException JavaDoc("Invalide name '" +name +"'");
431         }
432         res = st.nextToken().toUpperCase();
433         while (st.hasMoreElements()) {
434             res = res +"_" +st.nextToken().toUpperCase();
435         }
436         return res;
437     }
438     
439     public static String JavaDoc noUnderscoreName(String JavaDoc name, boolean beginsWithUpper) {
440         String JavaDoc res = "";
441         String JavaDoc decomposed = decompose(name);
442         StringTokenizer st = new StringTokenizer(decomposed);
443         boolean isFirstToken = true;
444         while (st.hasMoreElements()) {
445             char[] tmp = st.nextToken()
446                               .toLowerCase()
447                               .toCharArray();
448             if ( (!isFirstToken)
449                 || beginsWithUpper ) {
450                 tmp[0] = Character.toUpperCase(tmp[0]);
451                 isFirstToken = false;
452             }
453             res = res + new String JavaDoc(tmp);
454         }
455         if(res.length()==0) {
456             throw new RuntimeException JavaDoc("Invalide name '" +name +"'");
457         }
458         return res;
459     }
460
461     /* example
462         input: "abcDEFGhi23JKL45"
463         output: "abc DEF Ghi23 JKL45"
464      * */

465     public static String JavaDoc decompose(String JavaDoc input) {
466         // eliminate special chars
467
String JavaDoc r = input.replaceAll("[^a-zA-Z0-9]", " ");
468         // detect upper-lower transition : beginning of word
469
r = r.replaceAll("[A-Z][a-z]", " $0");
470         // detect lower-upper transition : beginning of ALL-CAPITAL word
471
Matcher JavaDoc m = Pattern.compile("[a-z0-9][A-Z]").matcher(r);
472         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
473         while (m.find()) {
474             String JavaDoc matched = m.group();
475             m.appendReplacement(sb
476                 , matched.charAt(0) +" " +matched.charAt(1) );
477         }
478         m.appendTail(sb);
479         r = sb.toString();
480         return r;
481     }
482     
483     
484 }
485
Popular Tags