KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > util > TagProvider


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.lib.jmi.util;
20
21 import java.util.*;
22
23 import javax.jmi.model.*;
24
25 /**
26  *
27  * @author mmatula
28  * @version
29  */

30 public class TagProvider extends Object JavaDoc {
31     public static final String JavaDoc TAGID_PACKAGE_PREFIX = "javax.jmi.packagePrefix"; //NOI18N
32
public static final String JavaDoc TAGID_SUBSTITUTE_NAME = "javax.jmi.substituteName"; //NOI18N
33
public static final String JavaDoc TAGID_IGNORE_LIFECYCLE = "javax.jmi.ignoreLifecycle"; //NOI18N
34
public static final String JavaDoc TAGID_IMPL_PACKAGE_PREFIX = "org.netbeans.implPackagePrefix"; //NOI18N
35
public static final String JavaDoc TAGID_XMI_NAMESPACE = "org.omg.xmi.namespace"; //NOI18N
36

37     public static final int ASSOCIATION = 0;
38     public static final int INSTANCE = 1;
39     public static final int CLASS = 2;
40     public static final int PACKAGE = 3;
41     
42     private static final String JavaDoc DT_ORDERED = "java.util.List"; //NOI18N
43
private static final String JavaDoc DT_MULTIVALUED = "java.util.Collection"; //NOI18N
44

45     private final HashMap valueCache = new HashMap();
46
47     /** Creates new TagProvider */
48     public TagProvider() {
49     }
50
51     public Tag getTag(ModelElement element, String JavaDoc tagID) {
52         Tag tag = null;
53
54         Collection tags = ((ModelPackage) element.refImmediatePackage()).getAttachesTo().getTag(element);
55         Tag temp;
56         for (Iterator it = tags.iterator(); it.hasNext();) {
57             temp = (Tag) it.next();
58             if (tagID.equals(temp.getTagId())) {
59                 tag = temp;
60                 break;
61             }
62         }
63
64         return tag;
65     }
66
67     public Collection getTagValues(ModelElement element, String JavaDoc tagID) {
68         Tag tag = getTag(element, tagID);
69
70         if (tag == null) {
71             return null;
72         }
73         else {
74             return tag.getValues();
75         }
76     }
77
78     public String JavaDoc getTagValue(ModelElement element, String JavaDoc tagID) {
79         if (element == null) return null;
80         String JavaDoc tagKey = element.refMofId() + ":" + tagID; //NOI18N
81
String JavaDoc value = (String JavaDoc) valueCache.get(tagKey);
82
83         if (value == null) {
84             Collection values = getTagValues(element, tagID);
85
86             if (values != null && values.size() > 0) {
87                 value = (String JavaDoc) values.iterator().next();
88                 valueCache.put(tagKey, value);
89             }
90         }
91
92         return value;
93     }
94
95     public String JavaDoc getTagValue(ModelElement element, String JavaDoc tagID, String JavaDoc defaultValue) {
96         String JavaDoc result = getTagValue(element, tagID);
97         if (result == null) {
98             result = defaultValue;
99         }
100
101         return result;
102     }
103
104     public String JavaDoc getTypeFullName(ModelElement type) {
105         return getTypePrefix(type, new StringBuffer JavaDoc(50)).append('.').append(getSubstName(type)).toString();
106     }
107
108     public String JavaDoc getDataTypeName(Parameter prm) {
109         MultiplicityType mp = prm.getMultiplicity();
110
111         if (mp.getUpper() > 1 || mp.getUpper() == -1) {
112             return (mp.isOrdered() ? DT_ORDERED : DT_MULTIVALUED);
113         } else {
114             return getDataTypeName(prm.getType());
115         }
116     }
117
118     public String JavaDoc getDataTypeName(Classifier type) {
119         String JavaDoc result;
120
121         if (type instanceof PrimitiveType) {
122             result = "java.lang." + type.getName(); //NOI18N
123
} else if (type instanceof AliasType) {
124             result = getDataTypeName(((AliasType) type).getType());
125         } else if (type instanceof CollectionType) {
126             result = (((CollectionType) type).getMultiplicity().isOrdered() ? DT_ORDERED : DT_MULTIVALUED);
127         } else {
128             result = getTypeFullName(type);
129         }
130
131         return result;
132     }
133
134     public String JavaDoc getSubstName(ModelElement element) {
135         String JavaDoc result = getTagValue(element, TAGID_SUBSTITUTE_NAME);
136         if (result == null)
137             result = element.getName();
138         if (element instanceof Constant) {
139             result = mapName (result, true, false);
140         } else {
141             boolean flag = element instanceof MofClass || element instanceof MofPackage ||
142                 element instanceof Association || element instanceof MofException ||
143                 element instanceof StructureType || element instanceof EnumerationType ||
144                 element instanceof CollectionType || element instanceof Import;
145             result = mapName (result, false, !flag);
146         } // else
147
if (element instanceof MofException && !result.endsWith("Exception")) //NOI18N
148
result = result + "Exception"; //NOI18N
149
return result;
150     }
151
152     private static String JavaDoc mapName(String JavaDoc name, boolean toLiteral, boolean firstLower) {
153         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc (32);
154         boolean wordRead = false;
155         boolean lowerCharDetected = false;
156         for (int x = 0; x < name.length (); x++) {
157             char c = name.charAt (x);
158             if (c == '-' || c == '_' || Character.isWhitespace (c)) {
159                 if (wordRead) {
160                     // first whitespace following end of word reached
161
if (toLiteral)
162                         buffer.append ('_');
163                     wordRead = false;
164                     lowerCharDetected = false;
165                 }
166             } else {
167                 if (lowerCharDetected && Character.isUpperCase (c)) {
168                     // a next word started ...
169
if (toLiteral)
170                         buffer.append ('_');
171                     wordRead = false;
172                     lowerCharDetected = false;
173                 } else if (Character.isLowerCase (c))
174                     lowerCharDetected = true;
175                 if (!wordRead || toLiteral)
176                     buffer.append (Character.toUpperCase (c));
177                 else
178                     buffer.append (Character.toLowerCase (c));
179                 wordRead = true;
180             } // if
181
} // for
182
if (buffer.length () > 0) {
183             if (toLiteral && !wordRead)
184                 buffer.deleteCharAt (buffer.length () - 1);
185             if (firstLower)
186                 buffer.replace (0, 1, new String JavaDoc (new char [] {(Character.toLowerCase (buffer.charAt(0)))}));
187         }
188         return buffer.toString ();
189     }
190
191     public static String JavaDoc mapEnumLiteral (String JavaDoc name) {
192         return mapName (name, true, false);
193     }
194
195     public String JavaDoc getNamespaceName(MofPackage type) {
196         String JavaDoc result = getTagValue(type, TAGID_XMI_NAMESPACE);
197         if (result == null) {
198             result = type.getName();
199         }
200         return result;
201     }
202
203     public String JavaDoc getTypePrefix(ModelElement metaObject) {
204         return getTypePrefix(metaObject, new StringBuffer JavaDoc(50)).toString();
205     }
206
207     public String JavaDoc getImplFullName(ModelElement object, int type) {
208         return getImplPrefix(object, new StringBuffer JavaDoc(50)).append('.').append(getSubstName(object)).append(type == CLASS ? "Class" : (type == PACKAGE ? "Package" : "")).append("Impl").toString(); //NOI18N
209
}
210     
211     private StringBuffer JavaDoc getImplPrefix(ModelElement object, StringBuffer JavaDoc sb) {
212         try {
213             ModelElement pckg = object;
214
215             while (!(pckg instanceof MofPackage)) {
216                 pckg = pckg.getContainer();
217             }
218             
219             Namespace container = pckg.getContainer();
220             if (container == null) {
221                 String JavaDoc result = getTagValue(pckg, TAGID_IMPL_PACKAGE_PREFIX);
222                 if (result == null) {
223                     result = getTagValue(pckg, TAGID_PACKAGE_PREFIX);
224                     if (result == null) {
225                         sb.append("impl"); //NOI18N
226
} else {
227                         result = result.toLowerCase(Locale.US);
228                         if ("javax.jmi".equals(result)) { //NOI18N
229
sb.append("org.netbeans.jmiimpl.mof"); //NOI18N
230
} else if (result.startsWith("org.netbeans.jmi.")) { //NOI18N
231
sb.append("org.netbeans.jmiimpl").append(result.substring(16)); //NOI18N
232
} else if (result.startsWith("org.omg")) { //NOI18N
233
if (result.length() == 7) {
234                                 sb.append("org.netbeans.jmiimpl.omg"); //NOI18N
235
} else if (result.charAt(7) == '.') {
236                                 sb.append("org.netbeans.jmiimpl.omg").append(result.substring(7)); //NOI18N
237
} else {
238                                 sb.append(result).append(".impl"); //NOI18N
239
}
240                         } else {
241                             sb.append(result).append(".impl"); //NOI18N
242
}
243                     }
244                 } else {
245                     sb.append(result.toLowerCase(Locale.US));
246                 }
247             } else {
248                 getImplPrefix(container, sb);
249             }
250
251             String JavaDoc packageName = getTagValue(pckg, TAGID_SUBSTITUTE_NAME);
252             if (packageName == null) {
253                 packageName = mapName(pckg.getName(), false, true);
254             }
255
256             sb.append('.').append(packageName.toLowerCase(Locale.US));
257             
258             return sb;
259         } catch (Exception JavaDoc e) {
260             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
261         }
262     }
263
264     private StringBuffer JavaDoc getTypePrefix(ModelElement metaObject, StringBuffer JavaDoc sb) {
265         ModelElement pckg = metaObject;
266
267         while (!(pckg instanceof MofPackage)) {
268             pckg = pckg.getContainer();
269         }
270
271         Namespace container = pckg.getContainer();
272         if (container == null) {
273             String JavaDoc result = getTagValue(pckg, TagProvider.TAGID_PACKAGE_PREFIX);
274             if (result != null) {
275                 sb.append(result.toLowerCase(Locale.US)).append('.');
276             }
277         } else {
278             getTypePrefix(container, sb).append('.');
279         }
280
281         String JavaDoc packageName = getTagValue(pckg, TagProvider.TAGID_SUBSTITUTE_NAME);
282         if (packageName == null) {
283             packageName = mapName(pckg.getName(), false, true);
284         }
285
286         return sb.append(packageName.toLowerCase(Locale.US));
287     }
288 }
289
Popular Tags