KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
22
23 import javax.jmi.reflect.*;
24 import javax.jmi.model.MultiplicityType;
25
26 import org.netbeans.mdr.handlers.*;
27 import org.netbeans.mdr.storagemodel.*;
28 import org.netbeans.mdr.util.*;
29
30 /**
31  *
32  * @author Martin Matula
33  * @version
34  */

35 public class ContentsFinder extends Object JavaDoc {
36     private final HashMap snameToObject = new HashMap();
37     private final Class JavaDoc superClass;
38     private final String JavaDoc substName;
39     private final boolean derived;
40
41     private static String JavaDoc firstUpper(String JavaDoc text) {
42         if (text == null) return null;
43         if (text.length() < 2) return text.toUpperCase(Locale.US);
44         return text.substring(0, 1).toUpperCase(Locale.US) + text.substring(1);
45     }
46
47     private void retrieveContents(StorableBaseObject storable, StorableObject meta) throws Exception JavaDoc {
48         List contents = (List) meta.getReference(MOFConstants.SH_MODEL_NAMESPACE_CONTENTS);
49         StorableObject element;
50         String JavaDoc substName;
51
52         for (Iterator it = contents.iterator(); it.hasNext();) {
53             element = (StorableObject) it.next();
54             substName = TagSupport.getSubstName(element);
55             snameToObject.put(firstUpper(substName), new ElementDescriptor(storable, element));
56         }
57
58         List supertypes = (List) meta.getReference(MOFConstants.SH_MODEL_GENERALIZABLE_ELEMENT_SUPERTYPES);
59
60         for (Iterator it = supertypes.iterator(); it.hasNext();) {
61             element = (StorableObject) it.next();
62             retrieveContents(storable, element);
63         }
64     }
65 /*
66     public void dump() {
67         Logger.getDefault().log("Contents finder dump:");
68         for (Iterator it = snameToObject.keySet().iterator(); it.hasNext();) {
69             Logger.getDefault().log(" sname: " + it.next());
70         }
71     }
72 */

73     /** Creates new ContentsFinder */
74     public ContentsFinder(StorableBaseObject storable) {
75         try {
76             boolean derived = false;
77             StorableObject meta = storable.getMetaObject();
78             retrieveContents(storable, meta);
79             substName = TagSupport.getSubstName(meta);
80             if (storable instanceof StorableAssociation) {
81                 derived = ((Boolean JavaDoc) meta.getAttribute(MOFConstants.SH_MODEL_ASSOCIATION_IS_DERIVED)).booleanValue();
82                 Class JavaDoc temp = null;
83                 if (derived) try {
84                     temp = BaseObjectHandler.resolveImplementation(TagSupport.getImplFullName(meta, TagSupport.ASSOCIATION));
85                 } catch (ClassNotFoundException JavaDoc e) {
86                     temp = null;
87                 }
88                 if (temp == null) {
89                     temp = AssociationHandler.class;
90                 }
91                 superClass = temp;
92             } else if (storable instanceof StorablePackage) {
93                 superClass = PackageProxyHandler.class;
94             } else if (storable instanceof StorableObject) {
95                 superClass = ((StorableObject) storable).getClassProxy().getInstanceSuperclass();
96             } else if (storable instanceof StorableClass) {
97                 superClass = ((StorableClass) storable).getClassSuperclass();
98             } else {
99                 superClass = null;
100             }
101             this.derived = derived;
102         } catch (Exception JavaDoc e) {
103             throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
104         }
105     }
106     
107     public Class JavaDoc getHandlerClass() {
108         return superClass;
109     }
110     
111     public String JavaDoc getSubstName() {
112         return substName;
113     }
114     
115     public boolean isDerived() {
116         return derived;
117     }
118
119     public ElementDescriptor lookup(String JavaDoc substName) {
120         return (ElementDescriptor) snameToObject.get(firstUpper(substName));
121     }
122     
123     public class ElementDescriptor {
124         public static final int ATTRIBUTE = 1;
125         public static final int REFERENCE = 2;
126         public static final int OPERATION = 4;
127         public static final int CLASS = 8;
128         public static final int PACKAGE = 16;
129         public static final int ASSOCIATION = 32;
130         public static final int DATATYPE = 64;
131         public static final int ASSOCIATION_END = 128;
132         public static final int IMPORT = 256;
133         
134         private final String JavaDoc name;
135         private final int type;
136         private final boolean multivalued;
137         private final boolean ordered;
138         private final boolean writable;
139         private final boolean derived;
140         private final int index;
141         
142         private ElementDescriptor(StorableBaseObject parent, StorableObject storable) {
143             try {
144                 name = (String JavaDoc) storable.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
145                 String JavaDoc typeName = (String JavaDoc) storable.getMetaObject().getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME);
146
147                 boolean multivalued = false;
148                 boolean ordered = false;
149                 boolean writable = false;
150                 boolean derived = false;
151                 int index = 0;
152
153                 if (typeName.equals(MOFConstants.SH_MODEL_ATTRIBUTE) || typeName.equals(MOFConstants.SH_MODEL_REFERENCE)) {
154                     MultiplicityType multiplicity = (MultiplicityType) storable.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_MULTIPLICITY);
155                     multivalued = !((multiplicity.getUpper() == 1) && (multiplicity.getLower() == 1));
156                     ordered = multiplicity.isOrdered() && multivalued;
157                     writable = ((Boolean JavaDoc) storable.getAttribute(MOFConstants.SH_MODEL_STRUCTURAL_FEATURE_IS_CHANGEABLE)).booleanValue();
158                     if (typeName.equals(MOFConstants.SH_MODEL_REFERENCE)) {
159                         type = 2;
160                     } else {
161                         type = 1;
162                         derived = ((Boolean JavaDoc) storable.getAttribute(MOFConstants.SH_MODEL_ASSOCIATION_IS_DERIVED)).booleanValue();
163                         if (!derived) {
164                             index = ((StorableFeatured) parent).getClassProxy().getAttrIndex(name);
165                         }
166                     }
167                 } else if (typeName.equals(MOFConstants.SH_MODEL_OPERATION)) {
168                     type = 4;
169                     derived = true;
170                 } else if (typeName.equals(MOFConstants.SH_MODEL_CLASS)) {
171                     type = 8;
172                 } else if (typeName.equals(MOFConstants.SH_MODEL_PACKAGE)) {
173                     type = 16;
174                 } else if (typeName.equals(MOFConstants.SH_MODEL_ASSOCIATION)) {
175                     type = 32;
176                 } else if (typeName.equals(MOFConstants.SH_MODEL_ENUMERATION_TYPE) || typeName.equals(MOFConstants.SH_MODEL_STRUCTURE_TYPE)) {
177                     type = 64;
178                 } else if (typeName.equals(MOFConstants.SH_MODEL_ASSOCIATION_END)) {
179                     type = 128;
180                 } else if (typeName.equals(MOFConstants.SH_MODEL_IMPORT)) {
181                     type = 256;
182                 } else {
183                     type = 0;
184                 }
185
186                 this.multivalued = multivalued;
187                 this.ordered = ordered;
188                 this.writable = writable;
189                 this.derived = derived;
190                 this.index = index;
191             } catch (Exception JavaDoc e) {
192                 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e);
193             }
194         }
195         
196         public String JavaDoc getName() {
197             return name;
198         }
199         
200         public int getIndex() {
201             return index;
202         }
203         
204         public boolean isOfType(int type) {
205             return (this.type & type) != 0;
206         }
207         
208         public boolean isMultivalued() {
209             return multivalued;
210         }
211         
212         public boolean isOrdered() {
213             return ordered;
214         }
215         
216         public boolean canModify() {
217             return writable;
218         }
219         
220         public boolean isDerived() {
221             return derived;
222         }
223     }
224 }
225
Popular Tags