1 19 package org.netbeans.mdr.storagemodel; 20 21 import org.netbeans.mdr.NBMDRepositoryImpl; 22 import org.netbeans.mdr.handlers.EnumResolver; 23 import org.netbeans.mdr.handlers.StructImpl; 24 import org.netbeans.mdr.persistence.MOFID; 25 import org.netbeans.mdr.persistence.StorageException; 26 import org.netbeans.mdr.util.*; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.NodeList ; 30 import org.xml.sax.EntityResolver ; 31 import org.xml.sax.InputSource ; 32 import org.xml.sax.SAXException ; 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import java.io.IOException ; 37 import java.io.StringReader ; 38 import java.net.URL ; 39 import java.util.*; 40 41 47 public class BootReader extends Object { 48 49 private static final String JMI_PACKAGE_PREFIX = "javax.jmi.model."; 50 51 52 private final MdrStorage mdrStorage; 53 54 private final URL docURL; 55 56 private final Hashtable odByName = new Hashtable(100); 57 58 private final Hashtable odByXmiId = new Hashtable(100); 59 60 private final Hashtable aliases = new Hashtable(10); 61 62 private final List excludeTypes = new ArrayList(3); 63 64 private final Hashtable objectsByName = new Hashtable(100); 65 66 private final Hashtable proxyIdByName = new Hashtable(11); 67 68 69 private final Hashtable proxyById = new Hashtable(); 70 71 private final ArrayList superclassByClass = new ArrayList(); 72 73 private final Hashtable proxyIdByXmiId = new Hashtable(); 74 75 private final HashSet proxyReferences = new HashSet(); 76 77 private final HashMap otherEnds = new HashMap(); 78 79 80 private Document document = null; 81 82 private StorableAssociation containsAssociation = null; 83 84 private static class DummyER implements EntityResolver { 85 private static EntityResolver instance = new DummyER(); 86 87 public static EntityResolver getInstance() { 88 return instance; 89 } 90 91 public InputSource resolveEntity(String publicID, String systemID) { 92 Logger.getDefault().log("resolving reference: " + publicID + ", " + systemID); 93 return new InputSource (new StringReader ("")); 94 } 95 } 96 97 101 public BootReader(MdrStorage storage, URL docURL) { 102 mdrStorage = storage; 103 this.docURL = docURL; 104 } 105 106 113 protected Document parse(URL docURL) throws IOException , SAXException , ParserConfigurationException { 114 Document result = null; 115 DocumentBuilderFactory bfact = DocumentBuilderFactory.newInstance(); 116 bfact.setValidating(false); 117 DocumentBuilder docBuilder = bfact.newDocumentBuilder(); 118 docBuilder.setEntityResolver(DummyER.getInstance()); 119 InputSource is = new InputSource (docURL.toString()); 120 result = docBuilder.parse(is); 121 return result; 122 } 123 124 130 public Collection read() throws IOException , SAXException { 131 try { 132 document = parse( docURL ); 133 Node xmiContent = document.getElementsByTagName(XmiConstants.XMI_CONTENT).item(0); 134 createProxies(xmiContent); 135 Collection result = createInstances(xmiContent); 136 rebuildMetas(); 137 initSuperClasses(); 138 return result; 139 } catch (ParserConfigurationException e) { 140 Logger.getDefault().log("Unable to parse document. "+e.getMessage() ); 141 return null; 142 } 143 } 144 145 private void initSuperClasses() { 146 Object sc; 147 Class iface[] = new Class [1]; 148 for (Iterator it = proxyById.values().iterator(); it.hasNext();) { 149 sc = it.next(); 150 if (sc instanceof StorableClass) { 151 try { 152 iface[0] = null; 153 ((StorableClass) sc).initClassSuperclass(iface); 154 iface[0] = null; 155 ((StorableClass) sc).initInstanceSuperclass(iface); 156 } catch (Exception e) { 157 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 158 } 159 } 160 } 161 } 162 163 167 private void createProxies(Node xmiContent) { 168 NodeList childNodes = xmiContent.getChildNodes(); 169 170 for (int i = 0; i < childNodes.getLength(); i++) { 171 Node packageNode = childNodes.item(i); 172 if (XmiUtils.isTextNode(packageNode)) continue; 173 if (XmiUtils.getXmiAttrValueAsString(packageNode, MOFConstants.MODEL_MODEL_ELEMENT_NAME).equals(MOFConstants.MODEL)) { 174 String logicalMOFName = ""; createProxiesInPackage(null, new MOFID (bootSequenceNumber(logicalMOFName),logicalMOFName), packageNode); 176 } 177 } 178 } 179 180 184 private Collection createInstances(Node xmiContent) { 185 XmiUtils.XmiNodeIterator classes = new XmiUtils.XmiNodeIterator(xmiContent); 186 Collection result = createInstances(classes, null, "", null); 187 resolveSuperClasses(); 188 resolveReferences(); 189 setValues(); 190 return result; 191 } 192 193 195 private void rebuildMetas() { 196 try { 197 StorablePackage sp = mdrStorage.getContextOutermostPackage(NBMDRepositoryImpl.BOOT_MOF); 198 sp.replaceValues(objectsByName); 199 200 StorableClass sc; 201 StorableObject so; 202 StorableAssociation sa; 203 204 for (Iterator classes = sp.getAllClasses().iterator(); classes.hasNext();) { 206 sc = (StorableClass) classes.next(); 207 sc.replaceValues(objectsByName); 208 for (Iterator instances = sc.allObjects(false).iterator(); instances.hasNext();) { 210 so = (StorableObject) instances.next(); 211 so.replaceValues(objectsByName); 212 } 213 } 214 215 for (Iterator associations = sp.getAllAssociations().iterator(); associations.hasNext();) { 216 sa = (StorableAssociation) associations.next(); 217 sa.replaceValues(objectsByName); 218 } 219 220 mdrStorage.rebuildMetas(NBMDRepositoryImpl.BOOT_MOF, objectsByName); 221 mdrStorage.setProperty(MdrStorage.MOF_TOP_PACKAGE, sp.getMofId()); 222 } catch (Exception e) { 223 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 224 } 225 } 226 227 232 private StorablePackage createProxiesInPackage(StorablePackage immediatePackage, MOFID fullPackageName, Node packageNode) { 233 String stringifiedFullPackageName = fullPackageName.getStorageID (); 235 String packagePrefix = stringifiedFullPackageName.length() > 0 ? stringifiedFullPackageName + "." : ""; 236 String elementName = XmiUtils.getXmiAttrValueAsString(packageNode, MdrStorage.MODEL_MODEL_ELEMENT_NAME); 237 String logicalMOFName = packagePrefix + elementName; 238 MOFID packageName = new MOFID (bootSequenceNumber(logicalMOFName), logicalMOFName); 239 240 StorablePackage currentPackage = createPackage(immediatePackage, packageName, elementName); 241 242 Node contents = XmiUtils.getChildNode(packageNode, MdrStorage.MODEL_NAMESPACE_CONTENTS); 243 XmiUtils.XmiNodeIterator ni = new XmiUtils.XmiNodeIterator(contents); 244 245 MOFID name = null; 246 String nodeName = null; 247 List attrDescs = null; 248 List clAttrDescs = null; 249 Node classContents = null; 250 XmiUtils.XmiNodeIterator features = null; 251 String storableXmiId = null; 252 StorableClass sc = null; 253 254 for (; ni.hasNext();) { 255 Node nonAbstract = ni.next(); 256 elementName = XmiUtils.getXmiAttrValueAsString(nonAbstract, MdrStorage.MODEL_MODEL_ELEMENT_NAME); 257 logicalMOFName = packageName.getStorageID() + "." + elementName; 258 name = new MOFID (bootSequenceNumber(logicalMOFName), logicalMOFName); 259 nodeName = XmiUtils.resolveFullName(nonAbstract); 260 261 if (nodeName.equals(MdrStorage.MODEL_CLASS)) { 262 attrDescs = new ArrayList(); 264 clAttrDescs = new ArrayList(); 265 boolean classDerived = false; 266 boolean instanceDerived = false; 267 boolean isSingleton = "true".equalsIgnoreCase(XmiUtils.getXmiAttrValueAsString(nonAbstract, MdrStorage.MODEL_CLASS_IS_SINGLETON)); 268 boolean isAbstract = "true".equalsIgnoreCase(XmiUtils.getXmiAttrValueAsString(nonAbstract, MdrStorage.MODEL_GENERALIZABLE_ELEMENT_IS_ABSTRACT)); 269 270 classContents = XmiUtils.getChildNode(nonAbstract, MdrStorage.MODEL_NAMESPACE_CONTENTS); 271 if (classContents != null) { 272 features = new XmiUtils.XmiNodeIterator(classContents); 274 if (features != null) { 275 Node featureNode = null; 276 String attName = null; 277 boolean isDerived = false; 278 for (; features.hasNext();) { 279 featureNode = features.next(); 280 if (XmiUtils.resolveFullName(featureNode).equals(MdrStorage.MODEL_ATTRIBUTE)) { 281 attName = XmiUtils.getXmiAttrValueAsString(featureNode, MdrStorage.MODEL_MODEL_ELEMENT_NAME); 282 isDerived = "true".equalsIgnoreCase(XmiUtils.getXmiAttrValueAsString(featureNode, MdrStorage.MODEL_ATTRIBUTE_IS_DERIVED)); 283 boolean classifier = MOFConstants.SCOPE_CLASSIFIER.equals(XmiUtils.getXmiAttrValueAsString(featureNode, MOFConstants.MODEL_FEATURE_SCOPE)); 284 if (!isDerived && attName != null) { 285 Node multiplicity = XmiUtils.getChildNode(featureNode, MdrStorage.MODEL_STRUCTURAL_FEATURE_MULTIPLICITY); 286 Node fields = XmiUtils.getChildNode(multiplicity, MdrStorage.MODEL_MULTIPLICITY_TYPE); 287 int maxSize; 288 String upper; 289 if (fields == null) { 290 XmiUtils.XmiNodeIterator fieldVals = new XmiUtils.XmiNodeIterator(multiplicity, XmiConstants.XMI_FIELD); 292 fieldVals.next(); upper = fieldVals.next().getFirstChild().getNodeValue(); 294 } else { 295 upper = XmiUtils.getXmiAttrValueAsString(fields, MdrStorage.MODEL_MULTIPLICITY_TYPE_UPPER); 297 } 298 try { 299 maxSize = Integer.parseInt(upper); 300 } catch (NumberFormatException e) { 301 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 302 } 303 logicalMOFName = name.getStorageID() + "." + attName; 304 StorableClass.AttributeDescriptor desc = new StorableClass.AttributeDescriptor(mdrStorage, new MOFID (bootSequenceNumber(logicalMOFName), logicalMOFName), attName, Object .class, 0, maxSize, false, true, true, null); 305 if (classifier) { 306 clAttrDescs.add(desc); 307 } else { 308 attrDescs.add(desc); 309 } 310 } 311 } else if (XmiUtils.resolveFullName(featureNode).equals(MdrStorage.MODEL_OPERATION)) { 312 isDerived = true; 313 } else { 314 isDerived = false; 315 } 316 317 if (isDerived) { 318 if ("classifier_level".equalsIgnoreCase(XmiUtils.getXmiAttrValueAsString(featureNode, MdrStorage.MODEL_FEATURE_SCOPE))) { 319 classDerived = true; 320 } else { 321 instanceDerived = true; 322 } 323 } 324 } 325 } 326 } 327 sc = createClass(currentPackage, name, elementName, attrDescs, clAttrDescs, classDerived, instanceDerived, isSingleton, isAbstract); 328 storableXmiId = XmiUtils.getAttributeValueAsString(nonAbstract, XmiConstants.XMI_ID); 329 if ((sc != null) && (storableXmiId != null)) { 330 proxyIdByXmiId.put(storableXmiId, sc.getMofId()); } else { 332 Logger.getDefault().log( "Unable to set proxy by XmiId: "+nonAbstract.getNodeName() ); 333 } 334 335 } else if (nodeName.equals(MdrStorage.MODEL_ASSOCIATION)) { 336 Node asocContents = XmiUtils.getChildNode(nonAbstract, MdrStorage.MODEL_NAMESPACE_CONTENTS); 338 XmiUtils.XmiNodeIterator asocEnds = new XmiUtils.XmiNodeIterator(asocContents, MdrStorage.MODEL_ASSOCIATION_END); 339 Node asocEndA = asocEnds.next (); 341 String endAName = XmiUtils.getXmiAttrValueAsString(asocEndA, MdrStorage.MODEL_MODEL_ELEMENT_NAME); 342 Node multiplicityA = XmiUtils.getChildNode(asocEndA, MdrStorage.MODEL_ASSOCIATION_END_MULTIPLICITY); 344 Node fieldsA = XmiUtils.getChildNode(multiplicityA, MdrStorage.MODEL_MULTIPLICITY_TYPE); 345 String lowerA; 346 String upperA; 347 String orderedA; 348 String uniqueA; 349 if (fieldsA == null) { 350 XmiUtils.XmiNodeIterator fields = new XmiUtils.XmiNodeIterator(multiplicityA, XmiConstants.XMI_FIELD); 352 lowerA = fields.next().getFirstChild().getNodeValue(); 353 upperA = fields.next().getFirstChild().getNodeValue(); 354 orderedA = fields.next().getFirstChild().getNodeValue(); 355 uniqueA = fields.next().getFirstChild().getNodeValue(); 356 } else { 357 lowerA = XmiUtils.getXmiAttrValueAsString(fieldsA, MdrStorage.MODEL_MULTIPLICITY_TYPE_LOWER); 359 upperA = XmiUtils.getXmiAttrValueAsString(fieldsA, MdrStorage.MODEL_MULTIPLICITY_TYPE_UPPER); 360 orderedA = XmiUtils.getXmiAttrValueAsString(fieldsA, MdrStorage.MODEL_MULTIPLICITY_TYPE_IS_ORDERED); 361 uniqueA = XmiUtils.getXmiAttrValueAsString(fieldsA, MdrStorage.MODEL_MULTIPLICITY_TYPE_IS_UNIQUE); 362 } 363 boolean aggrA = !XmiUtils.getXmiAttrValueAsString(asocEndA, MdrStorage.MODEL_ASSOCIATION_END_AGGREGATION).equals("none"); 364 365 Node asocEndB = asocEnds.next(); 367 String endBName = XmiUtils.getXmiAttrValueAsString (asocEndB, MdrStorage.MODEL_MODEL_ELEMENT_NAME); 368 Node multiplicityB = XmiUtils.getChildNode(asocEndB, MdrStorage.MODEL_ASSOCIATION_END_MULTIPLICITY); 370 Node fieldsB = XmiUtils.getChildNode(multiplicityB, MdrStorage.MODEL_MULTIPLICITY_TYPE); 371 String lowerB; 372 String upperB; 373 String orderedB; 374 String uniqueB; 375 if (fieldsB == null) { 376 XmiUtils.XmiNodeIterator fields = new XmiUtils.XmiNodeIterator(multiplicityB, XmiConstants.XMI_FIELD); 378 lowerB = fields.next().getFirstChild().getNodeValue(); 379 upperB = fields.next().getFirstChild().getNodeValue(); 380 orderedB = fields.next().getFirstChild().getNodeValue(); 381 uniqueB = fields.next().getFirstChild().getNodeValue(); 382 } else { 383 lowerB = XmiUtils.getXmiAttrValueAsString(fieldsB, MdrStorage.MODEL_MULTIPLICITY_TYPE_LOWER); 385 upperB = XmiUtils.getXmiAttrValueAsString(fieldsB, MdrStorage.MODEL_MULTIPLICITY_TYPE_UPPER); 386 orderedB = XmiUtils.getXmiAttrValueAsString(fieldsB, MdrStorage.MODEL_MULTIPLICITY_TYPE_IS_ORDERED); 387 uniqueB = XmiUtils.getXmiAttrValueAsString(fieldsB, MdrStorage.MODEL_MULTIPLICITY_TYPE_IS_UNIQUE); 388 } 389 boolean aggrB = !XmiUtils.getXmiAttrValueAsString(asocEndB, MdrStorage.MODEL_ASSOCIATION_END_AGGREGATION).equals("none"); 390 391 StorableAssociation storableAssociation; 392 try { 393 storableAssociation = createAssociation(currentPackage, name, elementName, endAName, endBName, Integer.parseInt(lowerA), Integer.parseInt(upperA), Integer.parseInt(lowerB), Integer.parseInt(upperB), orderedA.equals("true"), orderedB.equals("true"), uniqueA.equals("true"), uniqueB.equals("true"), aggrA, aggrB); 394 } catch (NumberFormatException e) { 395 throw new DebugException("Wrong format of multiplicity"); 396 } 397 String endALogicalName = name.getStorageID() + "." + endAName; 398 String endBLogicalName = name.getStorageID() + "." + endBName; 399 long asn = bootSequenceNumber (endALogicalName); 400 long bsn = bootSequenceNumber (endBLogicalName); 401 otherEnds.put(new MOFID (asn, endALogicalName), new MOFID (bsn, endBLogicalName)); 402 otherEnds.put(new MOFID (bsn, endBLogicalName), new MOFID (asn, endALogicalName)); 403 404 if (name.getStorageID().equals(MdrStorage.MODEL_CONTAINS)) { 405 containsAssociation = storableAssociation; 406 } 407 } else if (nodeName.equals(MdrStorage.MODEL_PACKAGE)) { 408 createProxiesInPackage(currentPackage, packageName, nonAbstract); 409 } else if (nodeName.equals(MdrStorage.MODEL_PRIMITIVE_TYPE)) { 410 if (!name.getStorageID().startsWith(MdrStorage.DATATYPES + ".")) { 411 String xmiId = XmiUtils.getAttributeValueAsString(nonAbstract, XmiConstants.XMI_ID); 412 excludeTypes.add(xmiId); 413 } 414 } else if (nodeName.equals(MdrStorage.MODEL_ALIAS_TYPE)) { 415 String xmiId = XmiUtils.getAttributeValueAsString(nonAbstract, XmiConstants.XMI_ID); 416 aliases.put(xmiId, XmiUtils.getXmiRefValue(nonAbstract, MdrStorage.MODEL_TYPED_ELEMENT_TYPE).get(0)); 417 } 418 } 419 return currentPackage; 420 } 421 422 428 private Collection createInstances(XmiUtils.XmiNodeIterator items, MOFID containerMofId, String namePrefix, ObjectDescriptor parent) { 429 ArrayList instances = new ArrayList(); 430 for (;items.hasNext();) { 431 Node classNode = items.next(); 432 433 if (XmiUtils.isTextNode(classNode)) continue; 434 435 String logicalMOFName = namePrefix + XmiUtils.getXmiAttrValueAsString(classNode, MdrStorage.MODEL_MODEL_ELEMENT_NAME); 437 MOFID name = new MOFID (bootSequenceNumber(logicalMOFName), logicalMOFName); 438 439 String xmiId = XmiUtils.getAttributeValueAsString(classNode, XmiConstants.XMI_ID); 440 logicalMOFName = XmiUtils.resolveFullName (classNode); 441 MOFID mofClassName = new MOFID (bootSequenceNumber(logicalMOFName), logicalMOFName); 442 MOFID mofPackageName = getPackageName (mofClassName); 443 MOFID classProxy = (MOFID) proxyIdByName.get(mofClassName); 444 MOFID packageProxy = (MOFID) proxyIdByName.get(mofPackageName); 445 String dataType = null; 446 447 if (xmiId != null && excludeTypes.contains(resolveTypeId(xmiId))) continue; 448 449 if (mofClassName.getStorageID().equals(MdrStorage.MODEL_ATTRIBUTE) || mofClassName.getStorageID().equals(MdrStorage.MODEL_STRUCTURE_FIELD)) { 450 dataType = resolveTypeId((String ) XmiUtils.getXmiRefValue(classNode, MdrStorage.MODEL_TYPED_ELEMENT_TYPE).get(0)); 451 if (excludeTypes.contains(dataType)) continue; 452 } 453 454 StorableObject instance = createObject(mofClassName, packageProxy, classProxy); 456 instances.add(instance); 457 458 objectsByName.put(name, instance.getMofId()); 460 ObjectDescriptor od = new ObjectDescriptor(classNode, instance, name); 461 if (odByName.put(name, od) != null) { 462 Logger.getDefault().log("duplicate for: " + name); 463 } 464 if (xmiId != null) { 465 odByXmiId.put(xmiId, od); 466 } 467 468 if (containerMofId != null) { 470 try { 471 containsAssociation.addLink(containerMofId, instance.getMofId()); 472 } catch (StorageException e) { 473 throw new DebugException("Storage exception: " + e); 474 } 475 } 476 477 if (mofClassName.getStorageID().equals(MdrStorage.MODEL_ATTRIBUTE)) { 479 if (XmiUtils.getXmiAttrValueAsString(classNode, MdrStorage.MODEL_ATTRIBUTE_IS_DERIVED).equals("false")) { 480 String upper = XmiUtils.getXmiAttrValueAsString(XmiUtils.getChildNode(XmiUtils.getChildNode(classNode, MdrStorage.MODEL_STRUCTURAL_FEATURE_MULTIPLICITY), MdrStorage.MODEL_MULTIPLICITY_TYPE), MdrStorage.MODEL_MULTIPLICITY_TYPE_UPPER); 481 if (upper == null) { 482 XmiUtils.XmiNodeIterator fields = new XmiUtils.XmiNodeIterator(XmiUtils.getChildNode(classNode, MdrStorage.MODEL_STRUCTURAL_FEATURE_MULTIPLICITY), XmiConstants.XMI_FIELD); 483 fields.next(); 484 upper = fields.next().getFirstChild().getNodeValue(); 485 } 486 parent.addFeature(new Feature(name, Feature.ATTRIBUTE, dataType, null, !upper.equals("1"))); 487 } 488 } else if (mofClassName.getStorageID().equals(MdrStorage.MODEL_STRUCTURE_FIELD)) { 489 parent.addFeature(new Feature(name, Feature.FIELD, dataType, null, false)); 490 } else if (mofClassName.getStorageID().equals(MdrStorage.MODEL_REFERENCE)) { 491 String referencedEnd = (String ) XmiUtils.getXmiRefValue(classNode, MdrStorage.MODEL_REFERENCE_REFERENCED_END).get(0); 492 parent.addFeature(new Feature(name, Feature.REFERENCE, null, referencedEnd, true)); 493 proxyReferences.add(parent); } else if (mofClassName.getStorageID().equals(MdrStorage.MODEL_CLASS)) { 496 List superTypes = XmiUtils.getXmiRefValue(classNode, MdrStorage.MODEL_GENERALIZABLE_ELEMENT_SUPERTYPES); 497 for (Iterator st = superTypes.iterator(); st.hasNext();) { 498 String superTypeXmiId = (String ) st.next(); 499 od.addSupertype(superTypeXmiId); 500 MOFID superProxyId = (MOFID) proxyIdByXmiId.get(superTypeXmiId); 501 if (superProxyId != null) { superclassByClass.add(new SuperClassDescriptor((MOFID) proxyIdByXmiId.get(xmiId), superProxyId)); } else { 504 throw new DebugException("Unable to get MofId for proxy woth XmiId : " + superTypeXmiId); 505 } 506 } 507 } 508 509 Node classContents = XmiUtils.getChildNode(classNode, MdrStorage.MODEL_NAMESPACE_CONTENTS); 511 if (classContents != null) { 512 XmiUtils.XmiNodeIterator subnodes = new XmiUtils.XmiNodeIterator(classContents); 513 createInstances(subnodes, instance.getMofId(), name.getStorageID () + ".", od); 514 } 515 } 516 return instances; 517 } 518 519 private void setValues() { 520 ObjectDescriptor od; 521 for (Iterator it = odByName.values().iterator(); it.hasNext();) { 522 od = (ObjectDescriptor) it.next(); 523 readFeatureValues(od, (ObjectDescriptor) odByName.get(od.getStorable().getMetaObjectId())); 524 533 } 534 } 535 536 539 private void readFeatureValues(ObjectDescriptor object, ObjectDescriptor od) { 540 Feature feature; 541 for (Iterator it = od.getFeatures().iterator(); it.hasNext();) { 542 feature = (Feature) it.next(); 543 try { 544 switch (feature.getType()) { 545 case Feature.ATTRIBUTE: 546 Object attrValue = getXmiAttrValue(object.getNode(), feature); 547 String featureName = getObjectName (feature.getName()); 548 object.getStorable().setAttribute(featureName, attrValue); 549 break; 550 case Feature.REFERENCE: 551 if (!feature.getName().getStorageID().equals(MdrStorage.MODEL_NAMESPACE_CONTENTS)) { 552 List value = XmiUtils.getXmiRefValue(object.getNode(), feature.getName().getStorageID()); 553 for (Iterator refs = value.iterator(); refs.hasNext();) { 554 String ref = (String ) refs.next(); 556 object.getStorable().addReference(getObjectName(feature.getName()), ((ObjectDescriptor) odByXmiId.get(ref)).getStorable().getMofId()); 557 } 560 break; 561 } 562 } 563 } catch (StorageException e) { 564 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 565 } 566 } 567 568 for (Iterator it = od.getSupertypes().iterator(); it.hasNext();) { 569 readFeatureValues(object, (ObjectDescriptor) odByXmiId.get(it.next())); 570 } 571 } 572 573 577 private Object getXmiAttrValue(Node node, Feature feature) { 578 ObjectDescriptor dataType = feature.getDataType(); 579 MOFID metaType = dataType.getStorable().getMetaObjectId(); 580 Object value = null; 581 582 if (metaType.getStorageID().equals(MdrStorage.MODEL_STRUCTURE_TYPE)) { 583 Map fields = new HashMap(); 584 Feature field; 585 586 Node attrNode = XmiUtils.getChildNode(node, feature.getName().getStorageID()); 587 Node structNode = XmiUtils.getChildNode(attrNode, dataType.getName().getStorageID()); 588 589 if (structNode == null) { 590 XmiUtils.XmiNodeIterator fieldNodes = new XmiUtils.XmiNodeIterator(attrNode, XmiConstants.XMI_FIELD); 592 for (Iterator it = dataType.getFeatures().iterator(); it.hasNext();) { 593 Node fieldNode = fieldNodes.next(); 594 field = (Feature) it.next(); 595 fields.put(getObjectName(field.getName()), decodeStringValue(fieldNode.getFirstChild().getNodeValue(), field.getDataType().getStorable().getMetaObjectId(), field.getDataType())); 596 } 597 } else { 598 for (Iterator it = dataType.getFeatures().iterator(); it.hasNext();) { 600 field = (Feature) it.next(); 601 fields.put(getObjectName(field.getName()), getXmiAttrValue(structNode, field)); 602 } 603 } 604 605 try { 606 value = StructImpl.newInstance(Class.forName(JMI_PACKAGE_PREFIX + getObjectName(dataType.getName())), new ArrayList(fields.keySet()), fields, parseDots(dataType.getName())); 607 } catch (ClassNotFoundException e) { 608 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 609 } 610 } else { 611 if (feature.isMultivalued()) { 612 List vals = XmiUtils.getXmiMultiValueAsString(node, feature.getName().getStorageID()); 613 List x = new ArrayList(vals.size()); 614 for (Iterator it = vals.iterator(); it.hasNext(); ) { 615 x.add(decodeStringValue((String )it.next(), metaType, dataType)); 616 } 617 value = x; 618 } else { 619 value = decodeStringValue(XmiUtils.getXmiAttrValueAsString(node, feature.getName().getStorageID()), metaType, dataType); 620 } 622 } 623 624 return value; 625 } 626 627 private Object decodeStringValue(String stringValue, MOFID metaType, ObjectDescriptor dataType) { 628 Object value = null; 629 630 if (metaType.getStorageID().equals(MdrStorage.MODEL_ENUMERATION_TYPE)) { 631 value = EnumResolver.resolveEnum(JMI_PACKAGE_PREFIX + getObjectName (dataType.getName()), stringValue); 632 } else if (metaType.getStorageID().equals(MdrStorage.MODEL_PRIMITIVE_TYPE)) { 633 String typeName = dataType.getName().getStorageID(); 634 if (typeName.equals(MdrStorage.DATATYPES_STRING)) { 635 value = stringValue; 636 } else if (typeName.equals(MdrStorage.DATATYPES_BOOLEAN)) { 637 value = Boolean.valueOf(stringValue); 638 } else if (typeName.equals(MdrStorage.DATATYPES_INTEGER)) { 639 value = new Integer (stringValue); 640 } else if (typeName.equals(MdrStorage.DATATYPES_DOUBLE)) { 641 value = new Double (stringValue); 642 } else if (typeName.equals(MdrStorage.DATATYPES_FLOAT)) { 643 value = new Float (stringValue); 644 } else if (typeName.equals(MdrStorage.DATATYPES_LONG)) { 645 value = new Long (stringValue); 646 } else { 647 Logger.getDefault().log("unrecognized type: " + typeName); 648 } 649 } else { 650 Logger.getDefault().log("unrecognized metatype: " + metaType); 651 } 652 653 return value; 654 } 655 656 659 private String resolveTypeId(String xmiId) { 660 String result = xmiId; 661 String temp; 662 663 while ((temp = (String ) aliases.get(result)) != null) { 664 result = temp; 665 } 666 667 return result; 668 } 669 670 674 private StorablePackage createPackage(StorablePackage immediatePackage, MOFID metaObjectId, String metaObjectName) { 675 try { 676 StorablePackage sp = new StorablePackage(mdrStorage, immediatePackage == null ? null : immediatePackage.getMofId(), metaObjectId, NBMDRepositoryImpl.BOOT_MOF, new HashMap()); 677 MdrStorage storage = mdrStorage; 678 storage.addBootObject(sp.getMofId()); 679 proxyIdByName.put(metaObjectId, sp.getMofId()); 680 if (immediatePackage != null) { 681 immediatePackage.addPackage(metaObjectName, sp.getMofId()); 682 } 683 return sp; 684 } catch ( StorageException e ) { 685 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 686 } 687 } 688 689 693 private StorableClass createClass(StorablePackage immediatePackage, MOFID metaMofId, 694 String metaObjectName, List attrDescs, List clAttrDescs, boolean classDerived, boolean instanceDerived, 695 boolean isSingleton, boolean isAbstract) { 696 697 try { 698 StorableClass sc = new StorableClass(mdrStorage, immediatePackage.getMofId(), 699 metaMofId, attrDescs, clAttrDescs, new HashMap(), classDerived, instanceDerived, 700 isSingleton, isAbstract 701 ); 702 MdrStorage storage = mdrStorage; 703 storage.addBootClass(sc.getMofId()); 704 proxyIdByName.put(metaMofId, sc.getMofId()); 705 706 proxyById.put( sc.getMofId(), sc ); 708 710 immediatePackage.addClass(metaObjectName, sc.getMofId()); 711 return sc; 712 } catch (StorageException e) { 713 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 714 } 715 } 716 717 731 private StorableAssociation createAssociation(StorablePackage immediatePackage,MOFID metaMofId,String metaObjectName, String endA,String endB,int minA, int maxA, int minB, int maxB,boolean orderedA,boolean orderedB,boolean uniqueA,boolean uniqueB,boolean aggrA, boolean aggrB) { 732 try { 733 String endALogicalName = metaMofId.getStorageID () + "." + endA; 734 String endBLogicalName = metaMofId.getStorageID() + "." + endB; 735 736 StorableAssociation sa = new StorableAssociation( 737 mdrStorage, immediatePackage.getMofId(), metaMofId, endA, new MOFID (bootSequenceNumber(endALogicalName), endALogicalName), 738 endB, new MOFID(bootSequenceNumber(endBLogicalName), endBLogicalName), Object .class, Object .class, minA, maxA, minB, maxB, 739 orderedA, orderedB, uniqueA, uniqueB, aggrA, aggrB, false, false 740 ); 741 MdrStorage storage = mdrStorage; 742 storage.addBootAssociation(sa.getMofId()); 743 proxyIdByName.put(metaMofId, sa.getMofId()); 744 745 proxyById.put( sa.getMofId(), sa ); 747 749 750 immediatePackage.addAssociation(metaObjectName, sa.getMofId()); 751 return sa; 752 } catch (StorageException e) { 753 throw new DebugException("Storage exception: " + e); 754 } 755 } 756 757 762 private StorableObject createObject(MOFID metaObject, MOFID immediatePackage, MOFID classProxy) { 763 try { 764 StorableObject so = new StorableObject(mdrStorage, immediatePackage, metaObject, classProxy); 765 MdrStorage storage = mdrStorage; 766 storage.addBootObject(so.getMofId()); 767 return so; 768 } catch (StorageException e) { 769 throw (DebugException) Logger.getDefault().annotate(new DebugException(), e); 770 } 771 } 772 773 776 private List parseDots(MOFID mofid) { 777 List value = new ArrayList(); 778 if (!(mofid instanceof MOFID)) 779 throw new IllegalArgumentException (); 780 String attr = mofid.getStorageID(); 781 int pos; 782 while ((pos = attr.indexOf('.')) > -1) { 783 if (pos > 0) { 784 value.add(attr.substring(0, pos)); 785 } 786 attr = attr.substring(pos + 1); 787 } 788 value.add(attr); 789 return value; 790 } 791 792 794 private void resolveReferences() { 795 ObjectDescriptor od = null; 796 Feature feature = null; 797 Object sc = null; 798 for (Iterator it = proxyReferences.iterator(); it.hasNext(); ) { 799 od = (ObjectDescriptor) it.next(); 800 sc = proxyById.get(proxyIdByName.get(od.getName())); 801 if (sc instanceof StorableClass) { 802 for (Iterator features = od.getFeatures().iterator(); features.hasNext(); ) { 803 feature = (Feature) features.next(); 804 if (feature.getType() == feature.REFERENCE) { 805 MOFID endName = (MOFID) otherEnds.get(feature.getAssocEndName()); 806 ((StorableClass)sc).addReferenceDescriptor(feature.getName(), getObjectName(feature.getName()), (MOFID) proxyIdByName.get(feature.getAssocName()), getObjectName(endName)); 808 } 809 } 810 } 811 } 812 } 813 814 815 private void resolveSuperClasses() { 816 MOFID classId = null; 817 ArrayList superclasses = new ArrayList(); 818 ArrayList resolved = new ArrayList(); 819 SuperClassDescriptor scd = null; 820 Object sc = null; 821 for (Iterator it = superclassByClass.iterator(); it.hasNext(); ) { 822 superclasses.clear(); 823 scd = (SuperClassDescriptor)it.next(); 824 classId = scd.getClassId(); 825 sc = proxyById.get( classId ); 826 if ((!(resolved.contains( classId ))) && (sc instanceof StorableClass) ) { 827 resolved.add( classId ); 828 superclasses.addAll( getAllSuperClasses( classId ) ); 829 StorableClass storable = (StorableClass) sc; 830 for (Iterator foundSuperClasses = superclasses.iterator(); foundSuperClasses.hasNext(); ) { 831 storable.addSuperclass( (MOFID)foundSuperClasses.next() ); 832 } 833 } 834 } 835 } 836 837 840 private List getAllSuperClasses(MOFID classId) { 841 ArrayList result = new ArrayList(); 842 MOFID superClassId = null; 843 List superClassIds = new ArrayList(); 844 superClassIds.addAll( getSuperClassByClass(classId) ); 845 846 for (Iterator it = superClassIds.iterator(); it.hasNext(); ) { 847 superClassId = (MOFID)it.next(); 848 if ( (superClassId != null) && (!result.contains( superClassId ) ) ) { 849 result.add( superClassId ); 850 result.addAll( getAllSuperClasses( superClassId ) ); 851 } 852 } 853 return result; 854 } 855 856 859 private List getSuperClassByClass(MOFID classId) { 860 ArrayList result = new ArrayList(); 861 SuperClassDescriptor scd = null; 862 if (classId != null) { 863 for (Iterator it = superclassByClass.iterator(); it.hasNext(); ) { 864 scd = (SuperClassDescriptor)it.next(); 865 if (classId.equals( scd.getClassId() ) ) { 866 result.add( scd.getSuperClassId() ); 867 } 868 } 869 } 870 return result; 871 } 872 873 874 private MOFID getPackageName (MOFID bootMOFID) { 875 String storageId = bootMOFID.getStorageID (); 876 String logicalMOFName = storageId.substring(0, storageId.lastIndexOf('.')); 877 return new MOFID (bootSequenceNumber (logicalMOFName), logicalMOFName); 878 } 879 880 private static String getObjectName (MOFID bootMOFID) { 881 String storageId = bootMOFID.getStorageID(); 882 return storageId.substring(storageId.lastIndexOf('.') + 1); 883 } 884 885 private long bootSequenceNumber (String logicalMOFName) { 886 return logicalMOFName.hashCode (); 887 } 888 889 892 private class SuperClassDescriptor { 893 private MOFID classId = null; 894 private MOFID superClassId = null; 895 public SuperClassDescriptor(MOFID classId, MOFID superClassId) { 896 this.classId = classId; 897 this.superClassId = superClassId; 898 } 899 public MOFID getClassId() { 900 return classId; 901 } 902 public MOFID getSuperClassId() { 903 return superClassId; 904 } 905 } 906 908 private class ObjectDescriptor { 909 private final Node node; 910 private final StorableObject storable; 911 private final MOFID name; 912 private MOFID parentName; 913 private List features = null; 914 private List supertypes = null; 915 916 public ObjectDescriptor(Node node, StorableObject storable, MOFID name) { 917 this.node = node; 918 this.storable = storable; 919 this.name = name; 920 } 921 922 924 public MOFID getName() { 925 return name; 926 } 927 928 930 public MOFID getParentName() { 931 if (parentName == null) { 932 String sname = name.getStorageID(); 933 String logicalMOFName = sname.substring(0, sname.lastIndexOf('.')); 934 parentName = new MOFID (bootSequenceNumber(logicalMOFName), logicalMOFName); 935 } 936 return parentName; 937 } 938 939 941 public Node getNode() { 942 return node; 943 } 944 945 947 public StorableObject getStorable() { 948 return storable; 949 } 950 951 953 public List getSupertypes() { 954 if (supertypes == null) { 955 supertypes = new ArrayList(); 956 } 957 958 return supertypes; 959 } 960 961 963 public List getFeatures() { 964 if (features == null) { 965 features = new ArrayList(); 966 } 967 return features; 968 } 969 970 972 public void addSupertype(String supertypeXmiId) { 973 getSupertypes().add(supertypeXmiId); 974 } 975 976 978 public void addFeature(Feature feature) { 979 getFeatures().add(feature); 980 } 981 } 982 983 private class Feature { 984 public static final int ATTRIBUTE = 0; 985 public static final int REFERENCE = 1; 986 public static final int FIELD = 2; 987 988 private final MOFID fullName; 989 private final int type; 990 private final String dataTypeXmiId; 991 private final String assocEndXmiId; 992 private final boolean multivalued; 993 994 private ObjectDescriptor dataType = null; 995 private MOFID assocName = null; 996 private MOFID assocEndName = null; 997 998 private void resolveNames() { 999 ObjectDescriptor od = (ObjectDescriptor) odByXmiId.get(assocEndXmiId); 1000 assocEndName = od.getName(); 1001 assocName = od.getParentName(); 1002 } 1003 1004 public Feature(MOFID fullName, int type, String dataTypeXmiId, String assocEndXmiId, boolean multivalued) { 1005 this.fullName = fullName; 1006 this.type = type; 1007 this.dataTypeXmiId = dataTypeXmiId; 1008 this.assocEndXmiId = assocEndXmiId; 1009 this.multivalued = multivalued; 1010 } 1011 1012 1014 public MOFID getName() { 1015 return fullName; 1016 } 1017 1018 1020 1021 1022 public MOFID getAssocName() { 1023 if (assocName == null) { 1024 resolveNames(); 1025 } 1026 return assocName; 1027 } 1028 1031 public MOFID getAssocEndName() { 1032 if (assocEndName == null) { 1033 resolveNames(); 1034 } 1035 return assocEndName; 1036 } 1037 1038 1040 public ObjectDescriptor getDataType() { 1041 if (dataType == null) { 1042 dataType = (ObjectDescriptor) odByXmiId.get(dataTypeXmiId); 1043 } 1044 return dataType; 1045 } 1046 1047 1049 public boolean isMultivalued() { 1050 return multivalued; 1051 } 1052 1053 1055 public int getType() { 1056 return type; 1057 } 1058 } 1059 1060} 1061 | Popular Tags |