KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
23
24 import org.omg.CORBA.TCKind JavaDoc;
25 import org.omg.CORBA.TypeCodePackage.BadKind JavaDoc;
26 import org.omg.mof.Reflective.MofError;
27 import org.omg.mof.Reflective.NotSet;
28
29 import org.objectweb.modfact.corba.helper.IDLCommon;
30 import org.objectweb.modfact.corba.helper.MOFCommon;
31
32 /**
33  * @author Xavier Blanc
34  *
35  */

36 public class IDLProvider implements IDLCommon {
37
38     MOFCommon mofHelper;
39
40     public IDLProvider(MOFCommon mofHelper) {
41         this.mofHelper = mofHelper;
42     }
43
44     public IDLProvider() {
45     }
46
47     /**
48      * Format 1
49      */

50     public String JavaDoc format1(String JavaDoc input) {
51         Vector s1 = splitAndDelete(input);
52         String JavaDoc buffer = " ";
53         for (int i = 0; i < s1.size(); i++) {
54             char[] s2 = s1.elementAt(i).toString().toCharArray();
55             s2[0] = Character.toUpperCase(s1.elementAt(i).toString().charAt(0));
56             String JavaDoc s3 = String.valueOf(s2);
57             buffer = buffer.concat(s3);
58         }
59         return buffer.trim();
60     }
61     
62     /**
63      * Format 1 (with the first letter in lower case)
64      */

65     public String JavaDoc format1FirstMin(String JavaDoc _input) {
66         String JavaDoc format1FirstMin = format1(_input);
67         if (format1FirstMin.length() > 1) {
68             return format1FirstMin.substring(0, 1).toLowerCase() + format1FirstMin.substring(1);
69         } else {
70             return format1FirstMin.toLowerCase();
71         }
72     }
73
74     /**
75      * Format 2
76      */

77     public String JavaDoc format2(String JavaDoc input) {
78         Vector s1 = splitAndDelete(input);
79         String JavaDoc buffer = " ";
80         for (int i = 0; i < s1.size(); i++) {
81             String JavaDoc s2 = s1.elementAt(i).toString().toLowerCase();
82             buffer = buffer.concat(s2);
83             if (i != (s1.size() - 1))
84                 buffer = buffer.concat("_");
85         }
86
87         return buffer.trim();
88     }
89
90     /**
91      * Format 3
92      */

93     public String JavaDoc format3(String JavaDoc input) {
94         Vector s1 = splitAndDelete(input);
95         String JavaDoc buffer = " ";
96         int i;
97         for (i = 0; i < s1.size(); i++) {
98             String JavaDoc s2 = s1.elementAt(i).toString().toUpperCase();
99             buffer = buffer.concat(s2);
100             if (i != (s1.size() - 1))
101                 buffer = buffer.concat("_");
102         }
103         return buffer.trim();
104     }
105
106
107     /**
108      * QualfiedName
109      */

110     public String JavaDoc qualifiedName(org.omg.mof.Model.ModelElement modelElement)
111         throws org.omg.mof.Reflective.MofError , NotSet{
112         boolean bool = false;
113         String JavaDoc elementName = "";
114
115         if (modelElement._is_a(org.omg.mof.Model.ClassHelper.id())) {
116             elementName =
117                 className(org.omg.mof.Model.ClassHelper.narrow(modelElement));
118         } else {
119             elementName = modelElement.name();
120             if (elementName.startsWith("*"))
121                 elementName = elementName.substring(1);
122             // Use the format1 method to format the elementName
123
elementName = format1(elementName);
124         }
125         
126         String JavaDoc _namespacestring = elementName;
127
128         org.omg.mof.Model.ModelElement current = modelElement;
129         while (!bool) {
130             try {
131                 current = current.container();
132                 _namespacestring =
133                     format1(current.name()) + "::" + _namespacestring;
134             } catch (org.omg.mof.Reflective.NotSet ex) {
135                 bool = true;
136             }
137
138         }
139         return _namespacestring;
140     }
141
142     public String JavaDoc className(org.omg.mof.Model.Class clazz)
143         throws NotSet, MofError {
144         String JavaDoc idlSubstitute = mofHelper.idlSubstituteIdentifierForClass(clazz);
145         if (idlSubstitute.compareTo("") == 0)
146             return format1(clazz.name());
147         else return idlSubstitute;
148     }
149
150     public boolean isBaseType(org.omg.CORBA.TypeCode JavaDoc _typecode)
151         throws BadKind JavaDoc, org.omg.CORBA.TypeCodePackage.Bounds JavaDoc {
152         //logger.log(Level.FINE, "IDL typeCode2IDL");
153
int val = _typecode.kind().value();
154         if (val == TCKind._tk_alias
155             || val == TCKind._tk_struct
156             || val == TCKind._tk_enum
157             || val == TCKind._tk_sequence)
158             return false;
159         else
160             return true;
161     }
162
163     ////////////////////////////////////////////////////////////////
164
////////////////////////////////////////////////////////////////
165
// ADDED METHOD
166
////////////////////////////////////////////////////////////////
167
////////////////////////////////////////////////////////////////
168

169     Vector splitAndDelete(String JavaDoc _input) {
170         Vector parts = new Vector();
171         String JavaDoc delim = "-_ .";
172         StringTokenizer _sstring = new StringTokenizer(_input, delim);
173         while (_sstring.hasMoreTokens()) {
174             String JavaDoc part = _sstring.nextToken();
175             int i = 0;
176             int j = 0;
177             boolean isPart = false;
178             boolean beginPart = true;
179             while (i < part.length()) {
180                 if (beginPart) {
181                     if ((Character.isDigit(part.charAt(i)))
182                         || (Character.isUpperCase(part.charAt(i))))
183                         i++;
184                     else
185                         beginPart = false;
186                 } else {
187                     if ((Character.isDigit(part.charAt(i)))
188                         || (Character.isLowerCase(part.charAt(i))))
189                         i++;
190                     else
191                         isPart = true;
192                 }
193                 if (isPart) {
194                     parts.addElement(part.substring(j, i));
195                     j = i;
196                     isPart = false;
197                     beginPart = true;
198                 }
199                 if (i == j)
200                     i++; //cas de caractère non spécifé dans le filtre
201
}
202             if (i != j)
203                 parts.addElement(part.substring(j, i));
204         }
205         return parts;
206     }
207     
208     /**
209      * Tests if the _input is a IDL keyword
210      * @param _input The word to test.
211      * @return The correct word to prevent IDL conflicts.
212      */

213     public String JavaDoc testIdlConflict(String JavaDoc input) {
214         String JavaDoc keywordsIDL2 = "abstract|any|attribute|boolean|case|char|const|context|custom|default|double|exception|enum|factory|false|fixed|float|in|include|inout|interface|local|long|module|native|object|octet|oneway|out|private|public|raises|readonly|sequence|short|string|struct|supports|switch|true|truncatable|typedef|unsigned|union|valuebase|valuetype|void|wchar|wstring";
215         String JavaDoc keywordsIDL3 = "component|consumes|emits|eventtype|finder|getraises|home|import|multiple|primarykey|provides|publishes|setraises|typeid|typeprefix|uses";
216         if (input.toLowerCase().matches(keywordsIDL2 + "|" + keywordsIDL3))
217             return "_" + input;
218         else
219             return input;
220     }
221     
222     /**
223      * String standardization : MOF Format 2 to prevent IDL conflicts <br>
224      * Helper.format2("AbcDefGhi")= "abc_def_ghi"
225      */

226     public String JavaDoc format2IdlConflict(String JavaDoc input) {
227         return testIdlConflict(format2(input).trim());
228     }
229     
230 }
231
Popular Tags