KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > handlers > gen > TagSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.handlers.gen;
20
21 import java.util.Locale JavaDoc;
22 import org.netbeans.mdr.persistence.MOFID;
23 import org.netbeans.mdr.storagemodel.StorableObject;
24 import org.netbeans.mdr.util.DebugException;
25 import org.netbeans.mdr.util.Logger;
26 import org.netbeans.mdr.util.MOFConstants;
27 import javax.jmi.model.MultiplicityType;
28 import java.util.Collection JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 /**
33  *
34  * @author Martin Matula, Dusan Balek
35  * @version
36  */

37 public class TagSupport extends Object JavaDoc {
38     public static final String JavaDoc TAGID_PACKAGE_PREFIX = "javax.jmi.packagePrefix"; //NOI18N
39
public static final String JavaDoc TAGID_SUBSTITUTE_NAME = "javax.jmi.substituteName"; //NOI18N
40
public static final String JavaDoc TAGID_IMPL_PACKAGE_PREFIX = "org.netbeans.implPackagePrefix"; //NOI18N
41

42     public static final int ASSOCIATION = 0;
43     public static final int INSTANCE = 1;
44     public static final int CLASS = 2;
45     public static final int PACKAGE = 3;
46     
47     private static final Hashtable JavaDoc valueCache = new Hashtable JavaDoc();
48     
49     private TagSupport() {
50     }
51     
52     /** Returns the full class-name of the JMI interface to be implemented
53      * by a handler for repository object <code>type</code>.
54      *
55      * @param type a repository object (not: meta-object) looking for a handler
56      * @return fully qualified class-name of the JMI interface to be
57      * implemented by a handler for <code>type</code>
58      **/

59     public static String JavaDoc getTypeFullName(StorableObject type) {
60         return getTypeFullName(type, getSubstName(type));
61     }
62     
63     public static String JavaDoc getTypeFullName(StorableObject type, String JavaDoc substName) {
64         return getTypePrefix(type, new StringBuffer JavaDoc(50)).append('.').append(substName).toString();
65     }
66     
67     public static String JavaDoc getTypeFullName(StorableObject storable, int type) {
68         return getTypePrefix(storable, new StringBuffer JavaDoc(50)).append('.').append(getSubstName(storable)).append((type == CLASS ? "Class" : (type == PACKAGE ? "Package" : ""))).toString(); //NOI18N
69
}
70     
71     /** Returns the full class-name of an optional implementation class
72      * for repository object <code>type</code>. If a class of the returned
73      * name is available in the class-path it is used as super-class for the
74      * generated handler class. The returned class-name is:
75      *
76      * <pre>mdrjmiimpl.<i>package</i>.<i>name</i>Impl</pre>
77      * where
78      * <pre><i>package</i>.<i>name</i>.</pre>
79      * is the fully qualified name of the JMI interface.
80      *
81      * @param storable a repository object (not: meta-object) looking for a handler
82      * @param type kind of repository object
83      * @return fully qualified class-name of optional implementation class
84      * */

85     public static String JavaDoc getImplFullName(StorableObject storable, int type) {
86         return getImplPrefix(storable, new StringBuffer JavaDoc(50)).append('.').append(getSubstName(storable)).append(type == CLASS ? "Class" : (type == PACKAGE ? "Package" : "")).append("Impl").toString(); //NOI18N
87
}
88     
89     public static StorableObject getTag(StorableObject storable, String JavaDoc tagID) {
90         StorableObject tag = null;
91         
92         try {
93             Collection JavaDoc tags = (Collection JavaDoc) storable.getImmediatePackage().getAssociation(MOFConstants.SH_MODEL_ATTACHES_TO).queryObjects(MOFConstants.SH_MODEL_ATTACHES_TO_MODEL_ELEMENT, storable.getMofId());
94             StorableObject temp;
95             for (Iterator JavaDoc it = tags.iterator(); it.hasNext();) {
96                 temp = (StorableObject) it.next();
97                 if (tagID.equals(temp.getAttribute(MOFConstants.SH_MODEL_TAG_TAG_ID))) {
98                     tag = temp;
99                     break;
100                 }
101             }
102         } catch (Exception JavaDoc e) {
103             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
104         }
105         
106         return tag;
107     }
108     
109     public static Collection JavaDoc getTagValues(StorableObject storable, String JavaDoc tagID) {
110         StorableObject tag = getTag(storable, tagID);
111         
112         try {
113             if (tag == null) {
114                 return null;
115             } else {
116                 return (Collection JavaDoc) tag.getAttribute(MOFConstants.SH_MODEL_TAG_VALUES);
117             }
118         } catch (Exception JavaDoc e) {
119             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
120         }
121     }
122     
123     public static String JavaDoc getTagValue(StorableObject storable, String JavaDoc tagID) {
124         if (storable == null)
125             return null;
126         Object JavaDoc tagKey = new CacheKey(storable.getMofId(), tagID);
127         String JavaDoc value = (String JavaDoc) valueCache.get(tagKey);
128         
129         if (value == null) {
130             try {
131                 Collection JavaDoc values = getTagValues(storable, tagID);
132                 
133                 if (values != null && values.size() > 0) {
134                     value = (String JavaDoc) values.iterator().next();
135                     valueCache.put(tagKey, value);
136                 }
137             } catch (Exception JavaDoc e) {
138                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
139             }
140         }
141         
142         return value;
143     }
144     
145     public static String JavaDoc getTagValue(StorableObject storable, String JavaDoc tagID, String JavaDoc defaultValue) {
146         String JavaDoc result = getTagValue(storable, tagID);
147         if (result == null) {
148             result = defaultValue;
149         }
150         
151         return result;
152     }
153     
154     public static String JavaDoc getDataTypeName(StorableObject type) {
155         try {
156             String JavaDoc typeName = (String JavaDoc) type.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
157             if (typeName.equals(MOFConstants.SH_MODEL_PRIMITIVE_TYPE))
158                 return "java.lang." + (String JavaDoc) type.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); //NOI18N
159
else if (typeName.equals(MOFConstants.SH_MODEL_ALIAS_TYPE))
160                 return getDataTypeName((StorableObject) type.getReference(MOFConstants.SH_MODEL_TYPED_ELEMENT_TYPE));
161             else if (typeName.equals(MOFConstants.SH_MODEL_COLLECTION_TYPE)) {
162                 MultiplicityType multiplicity = (MultiplicityType) type.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_MULTIPLICITY);
163                 if (multiplicity.isOrdered())
164                     return "java.util.List"; //NOI18N
165
else
166                     return "java.util.Collection"; //NOI18N
167
} else
168                 return getTypeFullName(type);
169         } catch (Exception JavaDoc e) {
170             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
171         }
172         
173     }
174     
175     public static String JavaDoc getSubstName(StorableObject storable) {
176         return getSubstName(storable, null, null);
177     }
178     
179     public static String JavaDoc getSubstName(StorableObject storable, String JavaDoc name, String JavaDoc meta) {
180         String JavaDoc result = getTagValue(storable, TAGID_SUBSTITUTE_NAME);
181         try {
182             if (result == null) {
183                 if (name == null)
184                     result = (String JavaDoc) storable.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
185                 else
186                     result = name;
187             }
188             if (meta == null)
189                 meta = (String JavaDoc) storable.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
190         } catch (Exception JavaDoc e) {
191             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
192         }
193         if (MOFConstants.SH_MODEL_CONSTANT.equals(meta))
194             result = mapName(result, true, false);
195         else {
196             boolean flag = MOFConstants.SH_MODEL_CLASS.equals(meta) ||
197             MOFConstants.SH_MODEL_PACKAGE.equals(meta) ||
198             MOFConstants.SH_MODEL_ASSOCIATION.equals(meta) ||
199             MOFConstants.SH_MODEL_EXCEPTION.equals(meta) ||
200             MOFConstants.SH_MODEL_IMPORT.equals(meta) ||
201             MOFConstants.SH_MODEL_STRUCTURE_TYPE.equals(meta) ||
202             MOFConstants.SH_MODEL_ENUMERATION_TYPE.equals(meta) ||
203             MOFConstants.SH_MODEL_COLLECTION_TYPE.equals(meta);
204             result = mapName(result, false, !flag);
205             if (MOFConstants.SH_MODEL_EXCEPTION.equals(meta) && !result.endsWith("Exception")) //NOI18N
206
result = result + "Exception"; //NOI18N
207
}
208         return result;
209     }
210     
211     private static String JavaDoc mapName(String JavaDoc name, boolean toLiteral, boolean firstLower) {
212         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(32);
213         boolean wordRead = false;
214         boolean lowerCharDetected = false;
215         for (int x = 0; x < name.length(); x++) {
216             char c = name.charAt(x);
217             if (c == '-' || c == '_' || Character.isWhitespace(c)) {
218                 if (wordRead) {
219                     // first whitespace following end of word reached
220
if (toLiteral)
221                         buffer.append('_');
222                     wordRead = false;
223                     lowerCharDetected = false;
224                 }
225             } else {
226                 if (lowerCharDetected && Character.isUpperCase(c)) {
227                     // a next word started ...
228
if (toLiteral)
229                         buffer.append('_');
230                     wordRead = false;
231                     lowerCharDetected = false;
232                 } else if (Character.isLowerCase(c))
233                     lowerCharDetected = true;
234                 if (!wordRead || toLiteral)
235                     buffer.append(Character.toUpperCase(c));
236                 else
237                     buffer.append(Character.toLowerCase(c));
238                 wordRead = true;
239             } // if
240
} // for
241
if (buffer.length() > 0) {
242             if (toLiteral && !wordRead)
243                 buffer.deleteCharAt(buffer.length() - 1);
244             if (firstLower)
245                 buffer.replace(0, 1, new String JavaDoc(new char [] {(Character.toLowerCase(buffer.charAt(0)))}));
246         }
247         return buffer.toString();
248     }
249     
250     public static String JavaDoc mapEnumLiteral(String JavaDoc name) {
251         return mapName(name, true, false);
252     }
253     
254     private static StringBuffer JavaDoc getImplPrefix(StorableObject storable, StringBuffer JavaDoc sb) {
255         try {
256             StorableObject pckg = storable;
257
258             while (!MOFConstants.SH_MODEL_PACKAGE.equals(pckg.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME))) {
259                 pckg = (StorableObject) pckg.getReference(MOFConstants.SH_MODEL_MODEL_ELEMENT_CONTAINER);
260             }
261             
262             StorableObject container = (StorableObject) pckg.getReference(MOFConstants.SH_MODEL_MODEL_ELEMENT_CONTAINER);
263             if (container == null) {
264                 String JavaDoc result = getTagValue(pckg, TAGID_IMPL_PACKAGE_PREFIX);
265                 if (result == null) {
266                     result = getTagValue(pckg, TAGID_PACKAGE_PREFIX);
267                     if (result == null) {
268                         sb.append("impl"); //NOI18N
269
} else {
270                         result = result.toLowerCase(Locale.US);
271                         if ("javax.jmi".equals(result)) { //NOI18N
272
sb.append("org.netbeans.jmiimpl.mof"); //NOI18N
273
} else if (result.startsWith("org.netbeans.jmi.")) { //NOI18N
274
sb.append("org.netbeans.jmiimpl").append(result.substring(16)); //NOI18N
275
} else if (result.startsWith("org.omg")) { //NOI18N
276
if (result.length() == 7) {
277                                 sb.append("org.netbeans.jmiimpl.omg"); //NOI18N
278
} else if (result.charAt(7) == '.') {
279                                 sb.append("org.netbeans.jmiimpl.omg").append(result.substring(7)); //NOI18N
280
} else {
281                                 sb.append(result).append(".impl"); //NOI18N
282
}
283                         } else {
284                             sb.append(result).append(".impl"); //NOI18N
285
}
286                     }
287                 } else {
288                     sb.append(result.toLowerCase(Locale.US));
289                 }
290             } else {
291                 getImplPrefix(container, sb);
292             }
293
294             String JavaDoc packageName = getTagValue(pckg, TAGID_SUBSTITUTE_NAME);
295             if (packageName == null) {
296                 packageName = mapName((String JavaDoc) pckg.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME), false, true);
297             }
298
299             sb.append('.').append(packageName.toLowerCase(Locale.US));
300             
301             return sb;
302         } catch (Exception JavaDoc e) {
303             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
304         }
305     }
306     
307     public static String JavaDoc getTypePrefix(StorableObject storable) {
308         return getTypePrefix(storable, new StringBuffer JavaDoc(50)).toString();
309     }
310     
311     public static StringBuffer JavaDoc getTypePrefix(StorableObject storable, StringBuffer JavaDoc sb) {
312         try {
313             StorableObject pckg = storable;
314
315             while (!MOFConstants.SH_MODEL_PACKAGE.equals(pckg.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME))) {
316                 pckg = (StorableObject) pckg.getReference(MOFConstants.SH_MODEL_MODEL_ELEMENT_CONTAINER);
317             }
318             
319             StorableObject container = (StorableObject) pckg.getReference(MOFConstants.SH_MODEL_MODEL_ELEMENT_CONTAINER);
320             if (container == null) {
321                 String JavaDoc result = getTagValue(pckg, TAGID_PACKAGE_PREFIX);
322                 if (result != null) {
323                     sb.append(result.toLowerCase(Locale.US)).append('.');
324                 }
325             } else {
326                 getTypePrefix(container, sb).append('.');
327             }
328
329             String JavaDoc packageName = getTagValue(pckg, TAGID_SUBSTITUTE_NAME);
330             if (packageName == null) {
331                 packageName = mapName((String JavaDoc) pckg.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME), false, true);
332             }
333             
334             return sb.append(packageName.toLowerCase(Locale.US));
335         } catch (Exception JavaDoc e) {
336             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
337         }
338     }
339     
340     private static class CacheKey {
341         private final MOFID mofId;
342         private final String JavaDoc tagId;
343         
344         public CacheKey(MOFID mofId, String JavaDoc tagId) {
345             if (mofId == null) throw new IllegalArgumentException JavaDoc();
346             this.mofId = mofId;
347             this.tagId = tagId == null ? "null" : tagId;
348         }
349
350         public boolean equals(Object JavaDoc o) {
351             return (o instanceof CacheKey) && ((CacheKey) o).mofId.equals(mofId) && ((CacheKey) o).tagId.equals(tagId);
352         }
353         
354         public int hashCode() {
355             return mofId.hashCode() * 31 + tagId.hashCode();
356         }
357     }
358 }
359
Popular Tags