1 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 35 public class ContentsFinder extends Object { 36 private final HashMap snameToObject = new HashMap(); 37 private final Class superClass; 38 private final String substName; 39 private final boolean derived; 40 41 private static String firstUpper(String 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 { 48 List contents = (List) meta.getReference(MOFConstants.SH_MODEL_NAMESPACE_CONTENTS); 49 StorableObject element; 50 String 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 73 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 ) meta.getAttribute(MOFConstants.SH_MODEL_ASSOCIATION_IS_DERIVED)).booleanValue(); 82 Class temp = null; 83 if (derived) try { 84 temp = BaseObjectHandler.resolveImplementation(TagSupport.getImplFullName(meta, TagSupport.ASSOCIATION)); 85 } catch (ClassNotFoundException 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 e) { 103 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 104 } 105 } 106 107 public Class getHandlerClass() { 108 return superClass; 109 } 110 111 public String getSubstName() { 112 return substName; 113 } 114 115 public boolean isDerived() { 116 return derived; 117 } 118 119 public ElementDescriptor lookup(String 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 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 ) storable.getAttribute(MOFConstants.SH_MODEL_MODEL_ELEMENT_NAME); 145 String typeName = (String ) 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 ) 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 ) 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 e) { 192 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 193 } 194 } 195 196 public String 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 |