1 17 package org.eclipse.emf.importer.java.builder; 18 19 import java.io.BufferedInputStream ; 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.HashMap ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 import java.util.StringTokenizer ; 33 import java.util.TreeSet ; 34 import java.util.regex.Matcher ; 35 import java.util.regex.Pattern ; 36 37 import org.eclipse.core.resources.IContainer; 38 import org.eclipse.core.resources.IFile; 39 import org.eclipse.core.resources.IProject; 40 import org.eclipse.core.resources.IResource; 41 import org.eclipse.core.runtime.CoreException; 42 import org.eclipse.core.runtime.IPath; 43 import org.eclipse.core.runtime.IProgressMonitor; 44 import org.eclipse.core.runtime.IStatus; 45 import org.eclipse.core.runtime.MultiStatus; 46 import org.eclipse.core.runtime.Status; 47 import org.eclipse.jdt.core.Flags; 48 import org.eclipse.jdt.core.IJavaProject; 49 import org.eclipse.jdt.core.IPackageFragmentRoot; 50 import org.eclipse.jdt.core.JavaCore; 51 import org.eclipse.jdt.core.JavaModelException; 52 import org.eclipse.jdt.core.jdom.DOMFactory; 53 import org.eclipse.jdt.core.jdom.IDOMCompilationUnit; 54 import org.eclipse.jdt.core.jdom.IDOMField; 55 import org.eclipse.jdt.core.jdom.IDOMImport; 56 import org.eclipse.jdt.core.jdom.IDOMMethod; 57 import org.eclipse.jdt.core.jdom.IDOMNode; 58 import org.eclipse.jdt.core.jdom.IDOMPackage; 59 import org.eclipse.jdt.core.jdom.IDOMType; 60 61 import org.eclipse.emf.codegen.ecore.CodeGenEcorePlugin; 62 import org.eclipse.emf.codegen.ecore.genmodel.GenModel; 63 import org.eclipse.emf.codegen.ecore.genmodel.GenPackage; 64 import org.eclipse.emf.codegen.util.CodeGenUtil; 65 import org.eclipse.emf.common.util.EList; 66 import org.eclipse.emf.common.util.EMap; 67 import org.eclipse.emf.ecore.EAnnotation; 68 import org.eclipse.emf.ecore.EAttribute; 69 import org.eclipse.emf.ecore.EClass; 70 import org.eclipse.emf.ecore.EClassifier; 71 import org.eclipse.emf.ecore.EDataType; 72 import org.eclipse.emf.ecore.EEnum; 73 import org.eclipse.emf.ecore.EEnumLiteral; 74 import org.eclipse.emf.ecore.EModelElement; 75 import org.eclipse.emf.ecore.ENamedElement; 76 import org.eclipse.emf.ecore.EObject; 77 import org.eclipse.emf.ecore.EOperation; 78 import org.eclipse.emf.ecore.EPackage; 79 import org.eclipse.emf.ecore.EParameter; 80 import org.eclipse.emf.ecore.EReference; 81 import org.eclipse.emf.ecore.EStructuralFeature; 82 import org.eclipse.emf.ecore.ETypedElement; 83 import org.eclipse.emf.ecore.EcoreFactory; 84 import org.eclipse.emf.ecore.EcorePackage; 85 import org.eclipse.emf.ecore.plugin.EcorePlugin; 86 import org.eclipse.emf.ecore.resource.Resource; 87 import org.eclipse.emf.ecore.resource.ResourceSet; 88 import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; 89 import org.eclipse.emf.ecore.util.EcoreUtil; 90 import org.eclipse.emf.ecore.util.ExtendedMetaData; 91 import org.eclipse.emf.ecore.xml.namespace.XMLNamespacePackage; 92 import org.eclipse.emf.ecore.xml.type.XMLTypePackage; 93 import org.eclipse.emf.importer.ModelImporter; 94 import org.eclipse.emf.importer.java.JavaImporterPlugin; 95 96 97 public class JavaEcoreBuilder 98 { 99 102 protected static DOMFactory jdomFactory = new DOMFactory(); 103 104 107 protected IFile genModelFile; 108 109 112 protected GenModel genModel; 113 114 118 protected Map externalPackageNameToEPackageMap = new HashMap (); 119 120 124 protected Map packageNameToEPackageMap = new HashMap (); 125 126 130 protected Map ePackageToOrderingMap = new HashMap (); 131 132 136 protected Map ePackageToPrefixMap = new HashMap (); 137 138 142 protected Map eModelElementToIDOMNodeMap = new HashMap (); 143 144 149 protected Map eTypedElementToTypeNameMap = new HashMap (); 150 151 157 protected Map eTypedElementToInstanceTypeNameMap = new HashMap (); 158 159 162 protected Set demandCreatedEDataTypes = new HashSet (); 163 164 169 protected Map eClassToSuperTypeNamesMap = new HashMap (); 170 171 177 protected Map eReferenceToOppositeNameMap = new HashMap (); 178 179 182 protected Collection externalGenModels = new ArrayList (); 183 184 187 protected Collection usedGenPackages = new ArrayList (); 188 189 protected MultiStatus status; 190 191 194 protected GenModel oldGenModelVersion; 195 196 199 public JavaEcoreBuilder(IFile genModelFile) 200 { 201 this.genModelFile = genModelFile; 202 203 status = new MultiStatus( 204 JavaImporterPlugin.getPlugin().getBundle().getSymbolicName(), 205 0, 206 CodeGenEcorePlugin.INSTANCE.getString("_UI_ErrorsWereDetectedJava_message"), 207 null); 208 } 209 210 public JavaEcoreBuilder(IFile genModelFile, GenModel oldGenModelVersion) 211 { 212 this(genModelFile); 213 this.oldGenModelVersion = oldGenModelVersion; 214 } 215 216 public JavaEcoreBuilder(IFile genModelFile, GenModel oldGenModelVersion, GenModel genModel) 217 { 218 this(genModelFile, oldGenModelVersion); 219 this.genModel = genModel; 220 } 221 222 protected ResourceSet createResourceSet() 223 { 224 ResourceSet result = new ResourceSetImpl(); 225 result.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap()); 226 return result; 227 } 228 229 protected IPath analyseProject(IProject project) throws Exception 230 { 231 IJavaProject javaProject = JavaCore.create(project); 234 IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots(); 235 Set visited = new HashSet (); 236 for (int i = 0; i < packageFragmentRoots.length; ++i) 237 { 238 if (packageFragmentRoots[i].getKind() == IPackageFragmentRoot.K_SOURCE) 239 { 240 traverse((IContainer)packageFragmentRoots[i].getUnderlyingResource(), visited); 241 } 242 } 243 244 for (Iterator i = eTypedElementToTypeNameMap.entrySet().iterator(); i.hasNext();) 247 { 248 Map.Entry entry = (Map.Entry )i.next(); 249 ETypedElement eTypedElement = (ETypedElement)entry.getKey(); 250 String typeName = (String )entry.getValue(); 251 EClassifier eClassifier = resolve(eTypedElement, typeName); 252 253 if (eClassifier instanceof EClass && eTypedElement instanceof EAttribute) 256 { 257 EAttribute eAttribute = (EAttribute)eTypedElement; 258 EClass container = eAttribute.getEContainingClass(); 259 EReference eReference = EcoreFactory.eINSTANCE.createEReference(); 260 eReference.setChangeable(eAttribute.isChangeable()); 261 eReference.setVolatile(eAttribute.isVolatile()); 262 eReference.setTransient(eAttribute.isTransient()); 263 eReference.setLowerBound(eAttribute.getLowerBound()); 264 eReference.setUpperBound(eAttribute.getUpperBound()); 265 eReference.setName(eTypedElement.getName()); 266 eReference.getEAnnotations().addAll(eTypedElement.getEAnnotations()); 267 container.getEStructuralFeatures().add(container.getEStructuralFeatures().indexOf(eTypedElement), eReference); 268 container.getEStructuralFeatures().remove(eTypedElement); 269 eTypedElement = eReference; 270 } 271 else if (eClassifier instanceof EDataType && eTypedElement instanceof EReference) 272 { 273 EReference eReference = (EReference)eTypedElement; 274 EClass container = eReference.getEContainingClass(); 275 EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute(); 276 eAttribute.setChangeable(eReference.isChangeable()); 277 eAttribute.setVolatile(eReference.isVolatile()); 278 eAttribute.setTransient(eReference.isTransient()); 279 eAttribute.setLowerBound(eReference.getLowerBound()); 280 eAttribute.setUpperBound(eReference.getUpperBound()); 281 eAttribute.setName(eTypedElement.getName()); 282 eAttribute.getEAnnotations().addAll(eTypedElement.getEAnnotations()); 283 container.getEStructuralFeatures().remove(eTypedElement); 284 eTypedElement = eAttribute; 285 } 286 287 String instanceClassName = (String )eTypedElementToInstanceTypeNameMap.get(eTypedElement); 288 if (instanceClassName != null && demandCreatedEDataTypes.contains(eClassifier)) 289 { 290 demandCreatedEDataTypes.remove(eClassifier); 291 EClassifier resolvedInstanceClassName = resolve(eTypedElement, instanceClassName, false); 292 ((EDataType)eClassifier).setInstanceClassName(resolvedInstanceClassName.getInstanceClassName()); 293 } 294 295 if (eClassifier == null) 296 { 297 error(CodeGenEcorePlugin.INSTANCE.getString("_UI_TheTypeDoesNotResolveCorrectly_message", new Object []{ typeName })); 298 eClassifier = EcorePackage.eINSTANCE.getEObject(); 299 } 300 301 eTypedElement.setEType(eClassifier); 304 305 used(eClassifier); 306 } 307 308 for (Iterator i = eClassToSuperTypeNamesMap.entrySet().iterator(); i.hasNext();) 311 { 312 Map.Entry entry = (Map.Entry )i.next(); 313 EClass eClass = (EClass)entry.getKey(); 314 String [] superTypeNames = (String [])entry.getValue(); 315 if (superTypeNames != null) 316 { 317 for (int j = 0; j < superTypeNames.length; ++j) 318 { 319 EClassifier eClassifier = resolve(eClass, superTypeNames[j], false); 320 if (eClassifier.getEPackage() == null) 321 { 322 EPackage ePackage = (EPackage)EcoreUtil.getRootContainer(eClass); 323 EClass superEClass = EcoreFactory.eINSTANCE.createEClass(); 324 superEClass.setInstanceClassName(eClassifier.getInstanceClassName()); 325 superEClass.setName(eClassifier.getName()); 326 superEClass.setAbstract(true); 327 superEClass.setInterface(true); 328 ePackage.getEClassifiers().add(superEClass); 329 eClassifier = superEClass; 330 } 331 332 if (eClassifier instanceof EClass) 333 { 334 if (eClassifier != EcorePackage.eINSTANCE.getEObject()) 335 { 336 eClass.getESuperTypes().add(eClassifier); 337 used(eClassifier); 338 } 339 } 340 else 341 { 342 error(CodeGenEcorePlugin.INSTANCE.getString( 343 "_UI_TheSuperTypeDoesNotResolveCorrectly_message", 344 new Object []{ superTypeNames[j] })); 345 } 346 } 347 } 348 } 349 350 for (Iterator i = eReferenceToOppositeNameMap.entrySet().iterator(); i.hasNext();) 353 { 354 Map.Entry entry = (Map.Entry )i.next(); 355 EReference eReference = (EReference)entry.getKey(); 356 String oppositeName = (String )entry.getValue(); 357 EClass eClass = (EClass)eReference.getEType(); 358 EReference eOpposite = (EReference)eClass.getEStructuralFeature(oppositeName); 359 if (eOpposite == null) 360 { 361 error(CodeGenEcorePlugin.INSTANCE.getString("_UI_TheAttributeIsNotAMemberOf_message", new Object []{ 362 oppositeName, 363 eClass.getName() })); 364 } 365 else if (eOpposite.getEOpposite() != eReference && eOpposite.getEOpposite() != null) 366 { 367 error(CodeGenEcorePlugin.INSTANCE.getString("_UI_TheOppositeAlreadyHasOpposite_message", new Object []{ 368 oppositeName, 369 eOpposite.getEOpposite().getName(), 370 eOpposite.getEOpposite().getEContainingClass().getName() })); 371 } 372 else 373 { 374 eReference.setEOpposite(eOpposite); 375 eOpposite.setEOpposite(eReference); 376 377 used(eOpposite); 378 379 if (eOpposite.isContainment()) 382 { 383 eReference.setTransient(true); 384 } 385 } 386 } 387 388 for (Iterator i = ePackageToOrderingMap.entrySet().iterator(); i.hasNext();) 391 { 392 Map.Entry entry = (Map.Entry )i.next(); 393 EPackage ePackage = (EPackage)entry.getKey(); 394 Map nameToIDMap = (Map )entry.getValue(); 395 396 sort(ePackage.getEClassifiers(), nameToIDMap); 397 for (Iterator j = ePackage.getEClassifiers().iterator(); j.hasNext();) 398 { 399 EClassifier eClassifier = (EClassifier)j.next(); 400 if (eClassifier instanceof EClass) 401 { 402 EClass eClass = (EClass)eClassifier; 403 sort(eClass.getEStructuralFeatures(), nameToIDMap); 404 } 405 } 406 } 407 408 IPath targetFragmentRoot = project.getFullPath(); 411 for (int i = 0; i < packageFragmentRoots.length; ++i) 412 { 413 if (packageFragmentRoots[i].getKind() == IPackageFragmentRoot.K_SOURCE) 414 { 415 IPath path = packageFragmentRoots[i].getUnderlyingResource().getFullPath(); 416 if (targetFragmentRoot.isPrefixOf(path)) 417 { 418 targetFragmentRoot = path; 419 break; 420 } 421 } 422 } 423 424 return targetFragmentRoot; 425 } 426 427 public void computeEPackages(IProgressMonitor progressMonitor, ModelImporter modelImporter) throws Exception 428 { 429 IProject project = genModelFile.getProject(); 430 project.open(progressMonitor); 431 432 Collection allGenModelFiles = new ArrayList (); 433 Collection allReferencedProjects = new ArrayList (); 434 getAllReferencedProjects(allReferencedProjects, project.getDescription().getReferencedProjects()); 435 getAllReferencedProjects(allReferencedProjects, project.getDescription().getDynamicReferences()); 436 for (Iterator i = allReferencedProjects.iterator(); i.hasNext();) 437 { 438 getAllGenModelFiles(allGenModelFiles, (IProject)i.next()); 439 } 440 441 ResourceSet resourceSet = modelImporter.createResourceSet(); 442 for (Iterator i = allGenModelFiles.iterator(); i.hasNext();) 443 { 444 IFile file = (IFile)i.next(); 445 Resource resource = resourceSet.getResource(modelImporter.createFileURI(file.getFullPath().toString()), true); 446 GenModel genModel = (GenModel)resource.getContents().get(0); 447 externalGenModels.add(genModel); 448 for (Iterator j = genModel.getGenPackages().iterator(); j.hasNext();) 449 { 450 GenPackage genPackage = (GenPackage)j.next(); 451 determineExternalPackages(genPackage, modelImporter); 452 } 453 } 454 455 IPath targetFragmentRoot = analyseProject(project); 456 modelImporter.setModelPluginDirectory(targetFragmentRoot.toString()); 457 458 for (Iterator i = packageNameToEPackageMap.values().iterator(); i.hasNext();) 459 { 460 EPackage ePackage = (EPackage)i.next(); 461 modelImporter.getEPackages().add(ePackage); 462 463 ModelImporter.EPackageInfo ePackageInfo = modelImporter.getEPackageInfo(ePackage); 464 ePackageInfo.setPrefix((String )ePackageToPrefixMap.get(ePackage)); 465 for (Iterator entries = packageNameToEPackageMap.entrySet().iterator(); entries.hasNext();) 466 { 467 Map.Entry entry = (Map.Entry )entries.next(); 468 if (entry.getValue() == ePackage) 469 { 470 String qualifiedPackageName = (String )entry.getKey(); 471 int index = qualifiedPackageName == null ? -1 : qualifiedPackageName.lastIndexOf("."); 472 if (index != -1) 473 { 474 ePackageInfo.setBasePackage(qualifiedPackageName.substring(0, index)); 475 } 476 break; 477 } 478 } 479 } 480 } 481 482 public void used(EModelElement modelElement) 483 { 484 EPackage ePackage = (EPackage)EcoreUtil.getRootContainer(modelElement); 485 if (ePackage != EcorePackage.eINSTANCE) 486 { 487 for (Iterator i = externalGenModels.iterator(); i.hasNext();) 488 { 489 GenModel genModel = (GenModel)i.next(); 490 GenPackage genPackage = genModel.findGenPackage(ePackage); 491 if (genPackage != null) 492 { 493 if (!usedGenPackages.contains(genPackage)) 494 { 495 usedGenPackages.add(genPackage); 496 497 for (Iterator j = ePackage.eAllContents(); j.hasNext();) 500 { 501 EObject eObject = (EObject)j.next(); 502 for (Iterator k = eObject.eCrossReferences().iterator(); k.hasNext();) 503 { 504 Object o = k.next(); 505 if (o instanceof EModelElement) 506 { 507 used((EModelElement)o); 508 } 509 } 510 } 511 } 512 break; 513 } 514 } 515 } 516 } 517 518 public void determineExternalPackages(GenPackage genPackage) 519 { 520 determineExternalPackages(genPackage, null); 521 } 522 523 protected void determineExternalPackages(GenPackage genPackage, ModelImporter modelImporter) 524 { 525 if (modelImporter != null) 526 { 527 modelImporter.getReferencedGenPackages().add(genPackage); 528 } 529 EPackage ePackage = genPackage.getEcorePackage(); 530 externalPackageNameToEPackageMap.put(genPackage.getInterfacePackageName(), ePackage); 531 for (Iterator i = genPackage.getNestedGenPackages().iterator(); i.hasNext();) 532 { 533 determineExternalPackages((GenPackage)i.next()); 534 } 535 } 536 537 540 public void getAllReferencedProjects(Collection result, IProject[] projects) throws CoreException 541 { 542 for (int i = 0; i < projects.length; ++i) 543 { 544 IProject project = projects[i]; 545 if (!result.contains(project) && project.exists() && project.isOpen()) 546 { 547 result.add(project); 548 getAllReferencedProjects(result, project.getDescription().getReferencedProjects()); 549 getAllReferencedProjects(result, project.getDescription().getDynamicReferences()); 550 } 551 } 552 } 553 554 557 public void getAllGenModelFiles(Collection result, IContainer container) throws CoreException 558 { 559 IResource[] contents = container.members(); 560 for (int i = 0; i < contents.length; ++i) 561 { 562 IResource resource = contents[i]; 563 if (resource.getType() == IResource.FILE) 564 { 565 getAllGenModelFiles(result, (IFile)resource); 566 } 567 else 568 { 569 getAllGenModelFiles(result, (IContainer)resource); 570 } 571 } 572 } 573 574 577 public void getAllGenModelFiles(Collection result, IFile file) throws CoreException 578 { 579 if (file.getName().endsWith(".genmodel")) 580 { 581 IProject project = file.getProject(); 582 IJavaProject javaProject = JavaCore.create(project); 583 try 584 { 585 IPath outputLocation = javaProject.getOutputLocation(); 586 if (project == project.getWorkspace().getRoot().findMember(javaProject.getOutputLocation()) 587 || !outputLocation.isPrefixOf(file.getFullPath())) 588 { 589 result.add(file); 590 } 591 } 592 catch (JavaModelException exception) 593 { 594 JavaImporterPlugin.INSTANCE.log(exception); 595 } 596 } 597 } 598 599 602 public void traverse(IContainer container, Set visited) throws CoreException 603 { 604 IResource[] contents = container.members(); 605 for (int i = 0; i < contents.length; ++i) 606 { 607 IResource resource = contents[i]; 608 if (visited.add(resource)) 609 { 610 if (resource.getType() == IResource.FILE) 611 { 612 traverse((IFile)resource); 613 } 614 else 615 { 616 traverse((IContainer)resource, visited); 617 } 618 } 619 } 620 } 621 622 625 public void traverse(IFile file) throws CoreException 626 { 627 if ("java".equalsIgnoreCase(file.getProjectRelativePath().getFileExtension())) 628 { 629 try 630 { 631 BufferedInputStream bufferedInputStream = new BufferedInputStream (file.getContents(true)); 632 byte[] input = new byte [bufferedInputStream.available()]; 633 bufferedInputStream.read(input); 634 bufferedInputStream.close(); 635 636 String encoding = null; 643 try 644 { 645 encoding = file.getCharset(); 646 } 647 catch (CoreException ce) 648 { 649 } 651 String contents = encoding == null ? new String (input) : new String (input, encoding); 652 IDOMCompilationUnit jCompilationUnit = jdomFactory.createCompilationUnit(contents, "NAME"); 653 analyzeCompilationUnit(jCompilationUnit); 654 } 655 catch (IOException exception) 656 { 657 JavaImporterPlugin.INSTANCE.log(exception); 658 } 659 } 660 } 661 662 665 protected void analyzeCompilationUnit(IDOMCompilationUnit compilationUnit) 666 { 667 for (IDOMNode child = compilationUnit.getFirstChild(); child != null; child = child.getNextNode()) 668 { 669 if (child.getNodeType() == IDOMNode.TYPE) 670 { 671 analyzeType((IDOMType)child); 672 break; 673 } 674 } 675 } 676 677 680 protected void analyzeType(IDOMType type) 681 { 682 IDOMCompilationUnit compilationUnit = (IDOMCompilationUnit)type.getParent(); 683 String qualifiedPackageName = null; 684 if (compilationUnit.getFirstChild() instanceof IDOMPackage) 685 { 686 qualifiedPackageName = ((IDOMPackage)compilationUnit.getFirstChild()).getName(); 687 } 688 689 String modelAnnotation = getModelAnnotation(type.getComment()); 693 boolean isEClassifier = false; 694 String kind = null; 695 696 if (modelAnnotation != null) 697 { 698 kind = getModelAnnotationAttribute(modelAnnotation, "kind"); 699 isEClassifier = !"package".equals(kind); 700 } 701 702 if (isEClassifier) 703 { 704 EPackage ePackage = (EPackage)packageNameToEPackageMap.get(qualifiedPackageName); 707 if (ePackage == null) 708 { 709 ePackage = EcoreFactory.eINSTANCE.createEPackage(); 712 int index = qualifiedPackageName == null ? -1 : qualifiedPackageName.lastIndexOf("."); 713 String packageName = index == -1 ? qualifiedPackageName : qualifiedPackageName.substring(index + 1); 714 ePackage.setName(packageName); 715 ePackage.setNsURI("http:///" + (qualifiedPackageName == null ? "null" : qualifiedPackageName.replace('.', '/')) + ".ecore"); 716 ePackage.setNsPrefix(qualifiedPackageName == null ? "null" : qualifiedPackageName); 717 packageNameToEPackageMap.put(qualifiedPackageName, ePackage); 718 719 if (packageName != null) 720 { 721 String prefix = Character.toUpperCase(packageName.charAt(0)) + packageName.substring(1); 722 ePackageToPrefixMap.put(ePackage, prefix); 723 } 724 } 725 726 if ((type.getFlags() & Flags.AccInterface) != 0) 729 { 730 EClass eClass = EcoreFactory.eINSTANCE.createEClass(); 731 eModelElementToIDOMNodeMap.put(eClass, type); 732 eClass.setName(type.getName()); 733 ePackage.getEClassifiers().add(eClass); 734 eClass.getEAnnotations().addAll(extractEAnnotations(modelAnnotation)); 735 EcoreUtil.setDocumentation(eClass, getModelDocumentation(type.getComment())); 736 737 String [] superInterfaces = type.getSuperInterfaces(); 738 String extend = getExtendsAnnotation(type.getComment()); 739 if (extend != null && superInterfaces != null) 740 { 741 List superInterfaceList = new ArrayList (Arrays.asList(superInterfaces)); 742 for (StringTokenizer stringTokenizer = new StringTokenizer (extend, " ,\t\n\r\f"); stringTokenizer.hasMoreTokens();) 743 { 744 superInterfaceList.remove(stringTokenizer.nextToken()); 745 } 746 superInterfaces = new String [superInterfaceList.size()]; 747 superInterfaceList.toArray(superInterfaces); 748 } 749 eClassToSuperTypeNamesMap.put(eClass, superInterfaces); 750 751 String isAbstract = getModelAnnotationAttribute(modelAnnotation, "abstract"); 752 eClass.setAbstract("true".equals(isAbstract)); 753 754 String isInterface = getModelAnnotationAttribute(modelAnnotation, "interface"); 755 eClass.setInterface("true".equals(isInterface)); 756 757 for (IDOMNode child = type.getFirstChild(); child != null; child = child.getNextNode()) 760 { 761 if (child.getNodeType() == IDOMNode.METHOD) 762 { 763 analyzeMethod(eClass, (IDOMMethod)child); 764 } 765 } 766 767 String features = getModelAnnotationAttribute(modelAnnotation, "features"); 771 if (features != null) 772 { 773 for (StringTokenizer stringTokenizer = new StringTokenizer (features, " "); stringTokenizer.hasMoreTokens();) 774 { 775 String feature = stringTokenizer.nextToken(); 776 if (eClass.getEStructuralFeature(feature) == null) 777 { 778 analyzeMethod(eClass, getFilteredModelAnnotations(modelAnnotation, feature), "get" + Character.toUpperCase(feature.charAt(0)) 779 + feature.substring(1), "java.lang.Object", null, null); 780 } 781 else 782 { 783 warning(CodeGenEcorePlugin.INSTANCE.getString("_UI_DuplicateFeature_message", new Object []{ feature, eClass.getName() })); 784 } 785 } 786 } 787 } 788 else 791 { 792 EEnum eEnum = EcoreFactory.eINSTANCE.createEEnum(); 793 eModelElementToIDOMNodeMap.put(eEnum, type); 794 eEnum.setName(type.getName()); 795 ePackage.getEClassifiers().add(eEnum); 796 eEnum.getEAnnotations().addAll(extractEAnnotations(modelAnnotation)); 797 EcoreUtil.setDocumentation(eEnum, getModelDocumentation(type.getComment())); 798 799 for (IDOMNode child = type.getFirstChild(); child != null; child = child.getNextNode()) 802 { 803 if (child.getNodeType() == IDOMNode.FIELD) 804 { 805 analyzeField(eEnum, (IDOMField)child); 806 } 807 } 808 } 809 } 810 else 812 { 813 String typeName = type.getName(); 814 boolean isEPackage = false; 815 if (typeName.endsWith("Package") && typeName.length() > 7) 816 { 817 String packagePrefix = typeName.substring(0, typeName.length() - 7); 818 819 if ("package".equals(kind)) 822 { 823 isEPackage = true; 824 } 825 826 int index = qualifiedPackageName == null ? -1 : qualifiedPackageName.lastIndexOf("."); 827 String name = index == -1 ? qualifiedPackageName : qualifiedPackageName.substring(index + 1); 828 String nsURI = "http:///" + (qualifiedPackageName == null ? "null" : qualifiedPackageName.replace('.', '/')) + ".ecore"; 829 String nsPrefix = qualifiedPackageName == null ? "null" : qualifiedPackageName; 830 831 List eClasses = new ArrayList (); 832 List eDataTypes = new ArrayList (); 833 Map ordering = new HashMap (); 834 835 for (IDOMNode child = type.getFirstChild(); child != null; child = child.getNextNode()) 838 { 839 if (child.getNodeType() == IDOMNode.FIELD) 840 { 841 IDOMField field = (IDOMField)child; 842 String childName = child.getName(); 843 String childType = field.getType(); 844 if ("eNAME".equals(childName)) 845 { 846 isEPackage = true; 847 name = field.getInitializer(); 848 name = name.substring(2, name.length() - 1); 849 } 850 else if ("eNS_URI".equals(childName)) 851 { 852 isEPackage = true; 853 nsURI = field.getInitializer(); 854 nsURI = nsURI.substring(2, nsURI.length() - 1); 855 } 856 else if ("eNS_PREFIX".equals(childName)) 857 { 858 isEPackage = true; 859 nsPrefix = field.getInitializer(); 860 nsPrefix = nsPrefix.substring(2, nsPrefix.length() - 1); 861 } 862 else if ("int".equals(childType) && !childName.endsWith("FEATURE_COUNT")) 863 { 864 try 865 { 866 String initializer = field.getInitializer(); 867 int plusIndex = initializer.lastIndexOf("+"); 868 if (plusIndex != -1) 869 { 870 initializer = initializer.substring(plusIndex + 1); 871 } 872 initializer = initializer.trim(); 873 int value = Integer.parseInt(initializer); 874 ordering.put(childName, new Integer (value)); 875 } 876 catch (NumberFormatException exception) 877 { 878 } 880 } 881 } 882 else if (child.getNodeType() == IDOMNode.METHOD) 883 { 884 IDOMMethod method = (IDOMMethod)child; 885 String methodAnnotation = getModelAnnotation(method.getComment()); 886 if (methodAnnotation != null) 887 { 888 String returnType = method.getReturnType(); 889 if (returnType != null) 890 { 891 if (returnType.endsWith("EDataType")) 892 { 893 EDataType eDataType = EcoreFactory.eINSTANCE.createEDataType(); 894 eDataType.setInstanceClassName(getModelAnnotationAttribute(methodAnnotation, "instanceClass")); 895 eDataType.setName(method.getName().substring(3)); 896 String isSerializable = getModelAnnotationAttribute(methodAnnotation, "serializable"); 897 if ("false".equals(isSerializable)) 898 { 899 eDataType.setSerializable(false); 900 } 901 eDataTypes.add(eDataType); 902 eDataType.getEAnnotations().addAll(extractEAnnotations(methodAnnotation)); 903 EcoreUtil.setDocumentation(eDataType, getModelDocumentation(method.getComment())); 904 } 905 else if (returnType.endsWith("EClass")) 906 { 907 EClass eClass = EcoreFactory.eINSTANCE.createEClass(); 908 String instanceClass = getModelAnnotationAttribute(methodAnnotation, "instanceClass"); 909 if (instanceClass != null) 910 { 911 eClass.setInterface(true); 912 eClass.setAbstract(true); 913 eClass.setInstanceClassName(instanceClass); 914 eClass.setName(method.getName().substring(3)); 915 eClasses.add(eClass); 916 } 917 else 918 { 919 eClass.setInstanceClassName("java.util.Map$Entry"); 920 eClass.setName(method.getName().substring(3)); 921 eClasses.add(eClass); 922 923 String features = getModelAnnotationAttribute(methodAnnotation, "features"); 924 if (features != null) 925 { 926 for (StringTokenizer stringTokenizer = new StringTokenizer (features, " "); stringTokenizer.hasMoreTokens();) 927 { 928 String feature = stringTokenizer.nextToken(); 929 analyzeMethod(eClass, getFilteredModelAnnotations(methodAnnotation, feature), "get" 930 + Character.toUpperCase(feature.charAt(0)) + feature.substring(1), "java.lang.Object", null, null); 931 } 932 } 933 else 934 { 935 analyzeMethod(eClass, getFilteredModelAnnotations(methodAnnotation, "key"), "getKey", "java.lang.Object", null, null); 936 937 analyzeMethod( 938 eClass, 939 getFilteredModelAnnotations(methodAnnotation, "value"), 940 "getValue", 941 "java.lang.Object", 942 null, 943 null); 944 } 945 } 946 eClass.getEAnnotations().addAll(extractEAnnotations(methodAnnotation)); 947 EcoreUtil.setDocumentation(eClass, getModelDocumentation(method.getComment())); 948 } 949 } 950 } 951 } 952 } 953 954 if (isEPackage || !eClasses.isEmpty() || !eDataTypes.isEmpty()) 955 { 956 EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage(); 957 ePackageToOrderingMap.put(ePackage, ordering); 958 eModelElementToIDOMNodeMap.put(ePackage, type); 959 ePackage.setNsURI(nsURI); 960 ePackage.setNsPrefix(nsPrefix); 961 ePackage.setName(name); 962 ePackage.getEClassifiers().addAll(eClasses); 963 ePackage.getEClassifiers().addAll(eDataTypes); 964 if (modelAnnotation != null) 965 { 966 ePackage.getEAnnotations().addAll(extractEAnnotations(modelAnnotation)); 967 } 968 EcoreUtil.setDocumentation(ePackage, getModelDocumentation(type.getComment())); 969 970 ePackageToPrefixMap.put(ePackage, packagePrefix); 971 972 EPackage existingEPackage = (EPackage)packageNameToEPackageMap.get(qualifiedPackageName); 973 if (existingEPackage != null) 974 { 975 ePackage.getEClassifiers().addAll(existingEPackage.getEClassifiers()); 976 } 977 978 packageNameToEPackageMap.put(qualifiedPackageName, ePackage); 979 } 980 } 981 } 982 } 983 984 987 protected void analyzeMethod(EClass eClass, IDOMMethod method) 988 { 989 String modelAnnotation = getModelAnnotation(method.getComment()); 992 if (modelAnnotation != null) 993 { 994 String methodName = method.getName(); 995 String returnType = method.getReturnType(); 996 String [] parameterNames = method.getParameterNames(); 997 String [] parameterTypes = method.getParameterTypes(); 998 999 ETypedElement eTypedElement = analyzeMethod(eClass, modelAnnotation, methodName, returnType, parameterNames, parameterTypes); 1000 if (eTypedElement != null) 1001 { 1002 EcoreUtil.setDocumentation(eTypedElement, getModelDocumentation(method.getComment())); 1003 } 1004 1005 eModelElementToIDOMNodeMap.put(eTypedElement, method); 1006 if (eTypedElement instanceof EOperation) 1007 { 1008 EOperation eOperation = (EOperation)eTypedElement; 1009 for (Iterator i = eOperation.getEParameters().iterator(); i.hasNext();) 1010 { 1011 EParameter eParameter = (EParameter)i.next(); 1012 eModelElementToIDOMNodeMap.put(eParameter, method); 1013 } 1014 } 1015 } 1016 } 1017 1018 protected ETypedElement analyzeMethod( 1019 EClass eClass, 1020 String modelAnnotation, 1021 String methodName, 1022 String returnType, 1023 String [] parameterNames, 1024 String [] parameterTypes) 1025 { 1026 ETypedElement eTypedElement = null; 1029 String featureName = methodName; 1030 String parameters = getModelAnnotationAttribute(modelAnnotation, "parameters"); 1031 String kind = getModelAnnotationAttribute(modelAnnotation, "kind"); 1032 1033 boolean declaredEOperation = "operation".equals(kind) || 1037 (parameters != null && !"attribute".equals(kind) && !"reference".equals(kind)); 1038 1039 if (parameterNames == null && !declaredEOperation && methodName.startsWith("get") && methodName.length() > 3 && 1042 Character.isUpperCase(methodName.charAt(3)) && !"boolean".equals(returnType) && !"void".equals(returnType)) 1043 { 1044 featureName = CodeGenUtil.uncapName(methodName.substring(3)); 1047 } 1048 else if (parameterNames == null && !declaredEOperation && methodName.startsWith("is") && methodName.length() > 2 && 1049 Character.isUpperCase(methodName.charAt(2)) && "boolean".equals(returnType)) 1050 { 1051 featureName = CodeGenUtil.uncapName(methodName.substring(2)); 1054 } 1055 else 1056 { 1057 EOperation eOperation = EcoreFactory.eINSTANCE.createEOperation(); 1060 eClass.getEOperations().add(eTypedElement = eOperation); 1061 1062 handleETypedElement(eOperation, methodName, returnType, modelAnnotation, eClass.getName() + "." + methodName); 1063 1064 if (parameterTypes != null) 1065 { 1066 StringTokenizer stringTokenizer = new StringTokenizer (parameters == null ? "" : parameters); 1070 for (int i = 0; i < parameterNames.length; ++i) 1071 { 1072 EParameter eParameter = EcoreFactory.eINSTANCE.createEParameter(); 1073 eOperation.getEParameters().add(eParameter); 1074 String parameterName = parameterNames[i]; 1075 String parameterType = parameterTypes[i]; 1076 String parameterModelAnnotation = getFilteredModelAnnotations(modelAnnotation, parameterName); 1077 1078 if (stringTokenizer.hasMoreTokens()) 1079 { 1080 String dataType = stringTokenizer.nextToken(); 1081 if (!"-".equals(dataType)) 1082 { 1083 StringBuffer buffer = new StringBuffer (parameterModelAnnotation); 1084 buffer.append("dataType=\""); 1085 buffer.append(dataType); 1086 buffer.append("\" "); 1087 parameterModelAnnotation = buffer.toString(); 1088 } 1089 } 1090 1091 StringBuffer identifierName = new StringBuffer (eClass.getName()); 1092 identifierName.append('.'); 1093 identifierName.append(methodName); 1094 identifierName.append('('); 1095 identifierName.append(parameterName); 1096 identifierName.append(')'); 1097 handleETypedElement(eParameter, parameterName, parameterType, parameterModelAnnotation, identifierName.toString()); 1098 eParameter.getEAnnotations().addAll(extractEAnnotations(parameterModelAnnotation)); 1099 } 1100 } 1101 } 1102 1103 if (eTypedElement == null) 1106 { 1107 EStructuralFeature eStructuralFeature = null; 1110 1111 String opposite = getModelAnnotationAttribute(modelAnnotation, "opposite"); 1114 String containment = getModelAnnotationAttribute(modelAnnotation, "containment"); 1115 String resolveProxies = getModelAnnotationAttribute(modelAnnotation, "resolveProxies"); 1116 String mapType = getModelAnnotationAttribute(modelAnnotation, "mapType"); 1117 String keyType = getModelAnnotationAttribute(modelAnnotation, "keyType"); 1118 String valueType = getModelAnnotationAttribute(modelAnnotation, "valueType"); 1119 if (opposite != null || containment != null || resolveProxies != null || mapType != null || (keyType != null && valueType != null)) 1120 { 1121 EReference eReference = EcoreFactory.eINSTANCE.createEReference(); 1122 eClass.getEStructuralFeatures().add(eTypedElement = eStructuralFeature = eReference); 1123 1124 eReference.setContainment("true".equals(containment) || mapType != null && !returnType.endsWith("Entry") || keyType != null 1127 && valueType != null); 1128 eReference.setResolveProxies(!eReference.isContainment() && !"false".equals(resolveProxies)); 1129 eReference.setUnsettable("true".equals(getModelAnnotationAttribute(modelAnnotation, "unsettable"))); 1130 1131 if (opposite != null) 1134 { 1135 eReferenceToOppositeNameMap.put(eReference, opposite); 1136 } 1137 } 1138 else 1139 { 1140 EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute(); 1144 eClass.getEStructuralFeatures().add(eTypedElement = eStructuralFeature = eAttribute); 1145 1146 eAttribute.setUnsettable("true".equals(getModelAnnotationAttribute(modelAnnotation, "unsettable"))); 1149 eAttribute.setID("true".equals(getModelAnnotationAttribute(modelAnnotation, "id"))); 1150 String defaultValueLiteral = getModelAnnotationAttribute(modelAnnotation, "defaultValue"); 1151 if (defaultValueLiteral == null) 1152 { 1153 defaultValueLiteral = getModelAnnotationAttribute(modelAnnotation, "default"); 1154 } 1155 eStructuralFeature.setDefaultValueLiteral(defaultValueLiteral); 1156 } 1157 1158 handleETypedElement(eStructuralFeature, featureName, returnType, modelAnnotation,eClass.getName() + "." + methodName); 1161 1162 eStructuralFeature.setChangeable(!"false".equals(getModelAnnotationAttribute(modelAnnotation, "changeable"))); 1165 eStructuralFeature.setDerived("true".equals(getModelAnnotationAttribute(modelAnnotation, "derived"))); 1166 eStructuralFeature.setVolatile("true".equals(getModelAnnotationAttribute(modelAnnotation, "volatile"))); 1167 eStructuralFeature.setTransient("true".equals(getModelAnnotationAttribute(modelAnnotation, "transient"))); 1168 1169 EcoreUtil.setSuppressedVisibility 1172 (eStructuralFeature, EcoreUtil.GET, "true".equals(getModelAnnotationAttribute(modelAnnotation, "suppressedGetVisibility"))); 1173 EcoreUtil.setSuppressedVisibility 1174 (eStructuralFeature, EcoreUtil.SET, "true".equals(getModelAnnotationAttribute(modelAnnotation, "suppressedSetVisibility"))); 1175 EcoreUtil.setSuppressedVisibility 1176 (eStructuralFeature, EcoreUtil.IS_SET, "true".equals(getModelAnnotationAttribute(modelAnnotation, "suppressedIsSetVisibility"))); 1177 EcoreUtil.setSuppressedVisibility 1178 (eStructuralFeature, EcoreUtil.UNSET, "true".equals(getModelAnnotationAttribute(modelAnnotation, "suppressedUnsetVisibility"))); 1179 } 1180 1181 if (eTypedElement != null) 1182 { 1183 eTypedElement.getEAnnotations().addAll(extractEAnnotations(modelAnnotation)); 1186 } 1187 1188 return eTypedElement; 1189 } 1190 1191 protected void handleETypedElement(ETypedElement eTypedElement, String name, String type, String modelAnnotation, String identifierName) 1192 { 1193 eTypedElement.setName(name); 1194 if ("void".equals(type)) return; 1195 1196 String mapType = getModelAnnotationAttribute(modelAnnotation, "mapType"); 1197 String dataType = getModelAnnotationAttribute(modelAnnotation, "dataType"); 1198 String modelType = getModelAnnotationAttribute(modelAnnotation, "type"); 1199 String keyType = getModelAnnotationAttribute(modelAnnotation, "keyType"); 1200 String valueType = getModelAnnotationAttribute(modelAnnotation, "valueType"); 1201 String many = getModelAnnotationAttribute(modelAnnotation, "many"); 1202 1203 if (dataType == null || mapType != null) 1206 { 1207 if ("EList".equals(type) || "org.eclipse.emf.common.util.EList".equals(type)|| "List".equals(type) || "java.util.List".equals(type)) 1208 { 1209 eTypedElement.setUpperBound(-1); 1210 if (modelType == null && !"false".equals(many)) 1211 { 1212 error(CodeGenEcorePlugin.INSTANCE.getString("_UI_TheTypeMustBeSpecifiedFor_message", new Object [] { identifierName })); 1213 modelType = "java.lang.Object"; 1214 } 1215 } 1216 else if ("EMap".equals(type) || "org.eclipse.emf.common.util.EMap".equals(type) || 1217 "Map".equals(type) || "java.util.Map".equals(type) || 1218 mapType != null || (keyType != null && valueType != null) || 1219 "FeatureMap".equals(type) || "org.eclipse.emf.common.util.FeatureMap".equals(type)) 1220 { 1221 eTypedElement.setUpperBound(-1); 1222 } 1223 } 1224 1225 if (many != null) 1226 { 1227 eTypedElement.setUpperBound("true".equals(many) ? -1 : 1); 1228 } 1229 1230 eTypedElement.setLowerBound("true".equals(getModelAnnotationAttribute(modelAnnotation, "required")) ? 1 : 0); 1231 1232 String lowerBound = getModelAnnotationAttribute(modelAnnotation, "lowerBound"); 1233 if (lowerBound == null) 1234 { 1235 lowerBound = getModelAnnotationAttribute(modelAnnotation, "lower"); 1236 } 1237 if (lowerBound != null) 1238 { 1239 eTypedElement.setLowerBound(Integer.parseInt(lowerBound)); 1240 } 1241 String upperBound = getModelAnnotationAttribute(modelAnnotation, "upperBound"); 1242 if (upperBound == null) 1243 { 1244 upperBound = getModelAnnotationAttribute(modelAnnotation, "upper"); 1245 } 1246 if (upperBound != null) 1247 { 1248 eTypedElement.setUpperBound(Integer.parseInt(upperBound)); 1249 } 1250 1251 if (modelType != null) 1256 { 1257 type = modelType; 1258 } 1259 1260 if (mapType != null) 1261 { 1262 if (keyType != null && valueType != null) 1263 { 1264 type = mapType + "@" + keyType + "/" + valueType; 1265 } 1266 else 1267 { 1268 type = mapType; 1269 } 1270 } 1271 else if (dataType != null) 1272 { 1273 eTypedElementToInstanceTypeNameMap.put(eTypedElement, type); 1274 type = dataType; 1275 } 1276 else if (keyType != null && valueType != null) 1277 { 1278 type = keyType + "/" + valueType; 1281 } 1282 eTypedElementToTypeNameMap.put(eTypedElement, type); 1283 1284 eTypedElement.setUnique(!"false".equals(getModelAnnotationAttribute(modelAnnotation, "unique"))); 1285 eTypedElement.setOrdered(!"false".equals(getModelAnnotationAttribute(modelAnnotation, "ordered"))); 1286 } 1287 1288 protected EStructuralFeature createFeature(EClass eClass, String name, EClassifier eType) 1289 { 1290 if (eType instanceof EClass) 1291 { 1292 EReference eReference = EcoreFactory.eINSTANCE.createEReference(); 1293 eReference.setName(name); 1294 eReference.setEType(eType); 1295 eClass.getEStructuralFeatures().add(eReference); 1296 return eReference; 1297 } 1298 else 1299 { 1300 EAttribute eAttribute = EcoreFactory.eINSTANCE.createEAttribute(); 1301 eAttribute.setName(name); 1302 eAttribute.setEType(eType); 1303 eClass.getEStructuralFeatures().add(eAttribute); 1304 return eAttribute; 1305 } 1306 } 1307 1308 1311 protected void analyzeField(EEnum eEnum, IDOMField field) 1312 { 1313 String modelAnnotation = getModelAnnotation(field.getComment()); 1314 if (modelAnnotation != null) 1315 { 1316 String literalName = getModelAnnotationAttribute(modelAnnotation, "name"); 1319 if (literalName == null) 1320 { 1321 literalName = field.getName(); 1322 } 1323 1324 EEnumLiteral eEnumLiteral = EcoreFactory.eINSTANCE.createEEnumLiteral(); 1327 eModelElementToIDOMNodeMap.put(eEnumLiteral, field); 1328 eEnumLiteral.setName(literalName); 1329 eEnumLiteral.getEAnnotations().addAll(extractEAnnotations(modelAnnotation)); 1330 EcoreUtil.setDocumentation(eEnumLiteral, getModelDocumentation(field.getComment())); 1331 if (field.getInitializer() != null) 1332 { 1333 try 1334 { 1335 int value = Integer.parseInt(field.getInitializer().trim()); 1336 eEnumLiteral.setValue(value); 1337 } 1338 catch (NumberFormatException exception) 1339 { 1340 JavaImporterPlugin.INSTANCE.log(exception); 1341 eEnumLiteral.setValue(eEnum.getELiterals().size()); 1342 } 1343 } 1344 else 1345 { 1346 eEnumLiteral.setValue(eEnum.getELiterals().size()); 1347 } 1348 eEnum.getELiterals().add(eEnumLiteral); 1349 } 1350 } 1351 1352 1355 protected static Pattern modelDocExpression = Pattern.compile( 1356 "<!--\\s*begin-model-doc\\s*-->[ \\f\\n\\r\\t]*\\*\\s?(.*?)<!--\\s*end-model-doc\\s*-->", 1357 Pattern.MULTILINE | Pattern.DOTALL); 1358 1359 1362 protected String getModelDocumentation(String comment) 1363 { 1364 if (comment != null) 1365 { 1366 Matcher matcher = modelDocExpression.matcher(comment); 1367 if (matcher.find()) 1368 { 1369 return comment.substring(matcher.start(1), matcher.end(1)).replaceAll("[\\n\\r]*\\s*\\*[\\s]?", "\n").replaceAll("\\s*$", ""); 1370 } 1371 } 1372 1373 return null; 1374 } 1375 1376 1379 protected static Pattern modelAnnotationExpression = Pattern.compile( 1380 "@[ \\f\\n\\r\\t*]*model[ \\f\\n\\r\\t*]*((\\w*\\s*=\\s*(['\"])(?>\\\\.|.)*?\\3[ \\f\\n\\r\\t*]*)*)", 1381 Pattern.MULTILINE); 1382 1383 1386 protected String getModelAnnotation(String comment) 1387 { 1388 if (comment != null) 1389 { 1390 Matcher matcher = modelAnnotationExpression.matcher(comment); 1391 if (matcher.find()) 1392 { 1393 return comment.substring(matcher.start(1), matcher.end(1)); 1394 } 1395 } 1396 1397 return null; 1398 } 1399 1400 1403 protected static Pattern extendsAnnotationExpression = Pattern.compile("@\\s*extends\\s*(([.\\w]*\\s*,*\\s*)+)", Pattern.MULTILINE); 1404 1405 1408 protected static Pattern implementsAnnotationExpression = Pattern.compile("@\\s*implements\\s*(([.\\w]*\\s*,*\\s*)+)", Pattern.MULTILINE); 1409 1410 1413 protected String getExtendsAnnotation(String comment) 1414 { 1415 if (comment != null) 1416 { 1417 StringBuffer result = new StringBuffer (); 1418 Matcher extendsMatcher = extendsAnnotationExpression.matcher(comment); 1419 while (extendsMatcher.find()) 1420 { 1421 result.append(comment.substring(extendsMatcher.start(1), extendsMatcher.end(1))); 1422 result.append(' '); 1423 } 1424 1425 Matcher implementsMatcher = implementsAnnotationExpression.matcher(comment); 1426 while (implementsMatcher.find()) 1427 { 1428 result.append(comment.substring(implementsMatcher.start(1), implementsMatcher.end(1))); 1429 result.append(' '); 1430 } 1431 1432 return result.length() == 0 ? null : result.toString(); 1433 } 1434 1435 return null; 1436 } 1437 1438 1441 protected String getModelAnnotationAttribute(String modelAnnotation, String attributeName) 1442 { 1443 Pattern modelAnnotationAttributeExpressionDoubleQuote = Pattern.compile( 1444 "\\b" + attributeName + "\\s*=\\s*([\"'])((?>\\\\.|.)*?)\\1", 1445 Pattern.MULTILINE); 1446 Matcher matcher = modelAnnotationAttributeExpressionDoubleQuote.matcher(modelAnnotation); 1447 if (matcher.find()) 1448 { 1449 return modelAnnotation.substring(matcher.start(2), matcher.end(2)); 1450 } 1451 else 1452 { 1453 return null; 1454 } 1455 } 1456 1457 1460 protected String getModelAnnotationAttributes(String modelAnnotation, String attributeName) 1461 { 1462 StringBuffer result = null; 1463 Pattern modelAnnotationAttributeExpressionDoubleQuote = Pattern.compile( 1464 "\\b" + attributeName + "\\s*=\\s*([\"'])((?>\\\\.|.)*?)\\1", 1465 Pattern.MULTILINE); 1466 for (Matcher matcher = modelAnnotationAttributeExpressionDoubleQuote.matcher(modelAnnotation); matcher.find();) 1467 { 1468 if (result == null) 1469 { 1470 result = new StringBuffer (); 1471 } 1472 else 1473 { 1474 result.append(' '); 1475 } 1476 result.append(modelAnnotation.subSequence(matcher.start(2), matcher.end(2))); 1477 } 1478 1479 return result == null ? null : result.toString(); 1480 } 1481 1482 protected static Pattern eAnnotationExpression = Pattern.compile("\\G\\s*((?>\\\\.|\\S)+)((?:\\s+(?>\\\\.|\\S)+\\s*+=\\s*(['\"])((?>\\\\.|.)*?)\\3)*)"); 1483 1484 protected static Pattern eAnnotationDetailExpression = Pattern.compile("\\s+((?>\\\\.|\\S)+)\\s*+=\\s*((['\"])((?>\\\\.|.)*?)\\3)"); 1485 1486 protected List extractEAnnotations(String modelAnnotation) 1487 { 1488 List result = Collections.EMPTY_LIST; 1489 String annotations = getModelAnnotationAttributes(modelAnnotation, "annotation"); 1490 if (annotations != null) 1491 { 1492 for (Matcher matcher = eAnnotationExpression.matcher(annotations); matcher.find();) 1493 { 1494 if (result == Collections.EMPTY_LIST) 1495 { 1496 result = new ArrayList (); 1497 } 1498 EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); 1499 result.add(eAnnotation); 1500 eAnnotation.setSource(parseString(matcher.group(1))); 1501 EMap details = eAnnotation.getDetails(); 1502 for (Matcher detailMatcher = eAnnotationDetailExpression.matcher(matcher.group(2)); detailMatcher.find();) 1503 { 1504 details.put(parseString(detailMatcher.group(1)), parseString(detailMatcher.group(4))); 1505 } 1506 } 1507 } 1508 1509 String extendedMetaDataAnnotations = getModelAnnotationAttributes(modelAnnotation, "extendedMetaData"); 1510 if (extendedMetaDataAnnotations != null) 1511 { 1512 if (result == Collections.EMPTY_LIST) 1513 { 1514 result = new ArrayList (); 1515 } 1516 EAnnotation eAnnotation = EcoreFactory.eINSTANCE.createEAnnotation(); 1517 result.add(eAnnotation); 1518 eAnnotation.setSource(ExtendedMetaData.ANNOTATION_URI); 1519 EMap details = eAnnotation.getDetails(); 1520 for (Matcher detailMatcher = eAnnotationDetailExpression.matcher(" " + extendedMetaDataAnnotations); detailMatcher.find();) 1521 { 1522 details.put(parseString(detailMatcher.group(1)), parseString(detailMatcher.group(4))); 1523 } 1524 } 1525 1526 return result; 1527 } 1528 1529 private static String parseString(String stringLiteralBody) 1530 { 1531 return CodeGenUtil.parseString(stringLiteralBody); 1532 } 1533 1534 1537 protected String getFilteredModelAnnotations(String modelAnnotation, String filter) 1538 { 1539 StringBuffer result = new StringBuffer (); 1540 Pattern modelAnnotationAttributeExpressionDoubleQuote = Pattern.compile("\\b" + filter 1541 + "([A-Z]\\w*\\s*=\\s*([\"'])((?>\\\\.|.)*?)\\2)", Pattern.MULTILINE); 1542 int start = 0; 1543 int end = modelAnnotation.length(); 1544 Matcher matcher; 1545 while ((matcher = modelAnnotationAttributeExpressionDoubleQuote.matcher(modelAnnotation.subSequence(start, end))).find()) 1546 { 1547 result.append(modelAnnotation.substring(start + matcher.start(1), start + matcher.start(1) + 1).toLowerCase()); 1548 result.append(modelAnnotation.substring(start + matcher.start(1) + 1, start + matcher.end(1))); 1549 result.append(' '); 1550 start += matcher.end(0); 1551 } 1552 return result.toString(); 1553 } 1554 1555 protected EClassifier resolve(EModelElement eModelElement, String typeName) 1556 { 1557 return resolve(eModelElement, typeName, true); 1558 } 1559 1560 protected EClassifier resolve(EModelElement eModelElement, String typeName, boolean recordDemandCreatedEDataType) 1561 { 1562 EPackage ePackage = (EPackage)EcoreUtil.getRootContainer(eModelElement); 1563 1564 EClassifier eClassifier = null; 1567 1568 int indexOfSlash = typeName.indexOf("/"); 1572 if (indexOfSlash != -1) 1573 { 1574 String mapType = null; 1575 String keyType = typeName.substring(0, indexOfSlash); 1576 int indexOfAt = keyType.indexOf("@"); 1577 if (indexOfAt != -1) 1578 { 1579 mapType = keyType.substring(0, indexOfAt); 1580 keyType = keyType.substring(indexOfAt + 1); 1581 } 1582 EClassifier keyEClassifier = resolve(eModelElement, keyType); 1583 1584 String valueType = typeName.substring(indexOfSlash + 1); 1585 EClassifier valueEClassifier = resolve(eModelElement, valueType); 1586 1587 if (mapType == null) 1588 { 1589 eClassifier = resolveMapEntry(ePackage, keyEClassifier, valueEClassifier); 1590 if (eClassifier == null) 1591 { 1592 eClassifier = resolveMapEntry(EcorePackage.eINSTANCE, keyEClassifier, valueEClassifier); 1593 } 1594 } 1595 else 1596 { 1597 eClassifier = resolve(eModelElement, mapType, false); 1598 } 1599 1600 if (eClassifier == null) 1601 { 1602 EClass eClass = EcoreFactory.eINSTANCE.createEClass(); 1603 eClass.setInstanceClassName("java.util.Map$Entry"); 1604 1605 String baseName = mapType != null ? mapType : keyEClassifier.getName() + "To" + valueEClassifier.getName() + "MapEntry"; 1606 String name = baseName; 1607 for (int j = 1; ePackage.getEClassifier(name) != null; ++j) 1608 { 1609 name = baseName + "_" + j; 1610 } 1611 eClass.setName(name); 1612 createFeature(eClass, "key", keyEClassifier); 1613 createFeature(eClass, "value", valueEClassifier); 1614 1615 ePackage.getEClassifiers().add(eClass); 1616 1617 eClassifier = eClass; 1618 } 1619 1620 return eClassifier; 1621 } 1622 1623 String baseName = typeName; 1626 String packageName = ""; 1627 int index = typeName.lastIndexOf("."); 1628 if (index == -1) 1629 { 1630 for (IDOMNode node = (IDOMNode)eModelElementToIDOMNodeMap.get(eModelElement); node != null; node = node.getParent()) 1633 { 1634 if (node.getNodeType() == IDOMNode.COMPILATION_UNIT) 1635 { 1636 boolean firstWildcard = true; 1639 for (IDOMNode child = ((IDOMCompilationUnit)node).getFirstChild(); child != null; child = child.getNextNode()) 1640 { 1641 if (child.getNodeType() == IDOMNode.IMPORT) 1642 { 1643 IDOMImport jImport = (IDOMImport)child; 1644 String importName = jImport.getName(); 1645 if (importName.endsWith("." + baseName)) 1646 { 1647 int importIndex = importName.lastIndexOf("."); 1648 packageName = importName.substring(0, importIndex); 1649 typeName = packageName + "." + baseName; 1650 break; 1651 } 1652 else if (firstWildcard && importName.endsWith(".*")) 1653 { 1654 int importIndex = importName.lastIndexOf("."); 1655 packageName = importName.substring(0, importIndex); 1656 typeName = packageName + "." + baseName; 1657 firstWildcard = false; 1658 } 1659 } 1660 } 1661 1662 EPackage otherEPackage = (EPackage)packageNameToEPackageMap.get(packageName); 1665 if (otherEPackage == null) 1666 { 1667 otherEPackage = (EPackage)externalPackageNameToEPackageMap.get(packageName); 1668 } 1669 if (otherEPackage != null) 1670 { 1671 eClassifier = otherEPackage.getEClassifier(baseName); 1672 } 1673 break; 1674 } 1675 } 1676 if (eClassifier == null && "".equals(packageName)) 1679 { 1680 eClassifier = ePackage.getEClassifier(typeName); 1681 } 1682 } 1683 else 1684 { 1685 packageName = typeName.substring(0, index); 1688 baseName = typeName.substring(index + 1); 1689 EPackage otherEPackage = (EPackage)packageNameToEPackageMap.get(packageName); 1690 if (otherEPackage == null) 1691 { 1692 otherEPackage = (EPackage)externalPackageNameToEPackageMap.get(packageName); 1693 if (otherEPackage == null) 1694 { 1695 if ("org.eclipse.emf.ecore".equals(packageName)) 1696 { 1697 otherEPackage = EcorePackage.eINSTANCE; 1698 } 1699 else if ("org.eclipse.emf.ecore.xml.type".equals(packageName)) 1700 { 1701 otherEPackage = XMLTypePackage.eINSTANCE; 1702 } 1703 else if ("org.eclipse.emf.ecore.xml.namespace".equals(packageName)) 1704 { 1705 otherEPackage = XMLNamespacePackage.eINSTANCE; 1706 } 1707 } 1708 } 1709 if (otherEPackage != null) 1710 { 1711 eClassifier = otherEPackage.getEClassifier(baseName); 1712 } 1713 } 1714 1715 if (eClassifier == null) 1718 { 1719 for (Iterator j = ePackage.getEClassifiers().iterator(); j.hasNext();) 1722 { 1723 EClassifier ePackageClassifier = (EClassifier)j.next(); 1724 String name = ePackageClassifier.getInstanceClassName(); 1725 if (name != null && name.replace('$', '.').equals(typeName.replace('$', '.'))) 1726 { 1727 eClassifier = ePackageClassifier; 1728 break; 1729 } 1730 } 1731 } 1732 1733 if (EcorePackage.eINSTANCE.getEObject().getInstanceClassName().equals(typeName)) 1734 { 1735 eClassifier = EcorePackage.eINSTANCE.getEObject(); 1736 } 1737 1738 if (eClassifier == null && recordDemandCreatedEDataType 1743 && EcorePackage.eINSTANCE.getEFeatureMap().getInstanceClassName().equals(typeName)) 1744 { 1745 eClassifier = EcorePackage.eINSTANCE.getEFeatureMapEntry(); 1746 } 1747 1748 if (eClassifier == null 1751 && (packageName.length() == 0 || packageName.equals("java.lang") || packageName.equals("java.math") || packageName.equals("java.util"))) 1752 { 1753 for (Iterator j = EcorePackage.eINSTANCE.getEClassifiers().iterator(); j.hasNext();) 1754 { 1755 EClassifier ecoreEClassifier = (EClassifier)j.next(); 1756 if (ecoreEClassifier instanceof EDataType) 1757 { 1758 String instanceClassName = ecoreEClassifier.getInstanceClassName(); 1759 if (instanceClassName.equals(typeName) || instanceClassName.equals("java.lang." + typeName)) 1760 { 1761 eClassifier = ecoreEClassifier; 1762 break; 1763 } 1764 } 1765 } 1766 } 1767 1768 if (eClassifier == null && !(eModelElement instanceof EReference)) 1772 { 1773 EDataType eDataType = EcoreFactory.eINSTANCE.createEDataType(); 1774 1775 boolean primitive = false; 1779 if (packageName.length() == 0) 1780 { 1781 int i = typeName.indexOf('['); 1784 String elementTypeName = i == -1 ? typeName : typeName.substring(0, i); 1785 1786 if (CodeGenUtil.isJavaPrimitiveType(elementTypeName)) 1787 { 1788 primitive = true; 1789 } 1790 else if (CodeGenUtil.isJavaLangType(elementTypeName)) 1791 { 1792 packageName = "java.lang"; 1793 typeName = packageName + "." + typeName; 1794 } 1795 else 1796 { 1797 packageName = ePackage.getNsPrefix(); 1798 typeName = packageName + '.' + typeName; 1799 } 1800 } 1801 eDataType.setInstanceClassName(typeName); 1802 1803 String name = baseName; 1806 if (primitive && name.length() > 0) 1807 { 1808 name = name.substring(0, 1).toUpperCase() + name.substring(1); 1809 } 1810 1811 while (name.endsWith("[]")) 1814 { 1815 name = name.substring(0, name.length() - 2) + "Array"; 1816 } 1817 1818 for (int j = 1; ePackage.getEClassifier(name) != null; ++j) 1821 { 1822 name = baseName + "_" + j; 1823 } 1824 eDataType.setName(name); 1825 1826 if (recordDemandCreatedEDataType) 1827 { 1828 demandCreatedEDataTypes.add(eDataType); 1829 ePackage.getEClassifiers().add(eDataType); 1830 } 1831 eClassifier = eDataType; 1832 } 1833 1834 return eClassifier; 1835 } 1836 1837 protected EClass resolveMapEntry(EPackage ePackage, EClassifier keyEClassifier, EClassifier valueEClassifier) 1838 { 1839 for (Iterator j = ePackage.getEClassifiers().iterator(); j.hasNext();) 1840 { 1841 EClassifier ePackageClassifier = (EClassifier)j.next(); 1842 if (ePackageClassifier instanceof EClass 1843 && ("java.util.Map.Entry".equals(ePackageClassifier.getInstanceClassName()) || "java.util.Map$Entry".equals(ePackageClassifier.getInstanceClassName()))) 1844 { 1845 EClass mapEntryInterface = (EClass)ePackageClassifier; 1846 EStructuralFeature keyFeature = mapEntryInterface.getEStructuralFeature("key"); 1847 if (keyFeature != null && resolveType(keyFeature) == keyEClassifier && !keyFeature.isMany()) 1848 { 1849 EStructuralFeature valueFeature = mapEntryInterface.getEStructuralFeature("value"); 1850 if (valueFeature != null && resolveType(valueFeature) == valueEClassifier && !valueFeature.isMany()) 1851 { 1852 return mapEntryInterface; 1853 } 1854 } 1855 } 1856 } 1857 1858 return null; 1859 } 1860 1861 protected EClassifier resolveType(ETypedElement eTypedElement) 1862 { 1863 EClassifier type = eTypedElement.getEType(); 1864 if (type == null) 1865 { 1866 String typeName = (String )eTypedElementToTypeNameMap.get(eTypedElement); 1867 if (typeName != null) 1868 { 1869 type = resolve(eTypedElement, typeName); 1870 } 1871 } 1872 return type; 1873 } 1874 1875 protected void sort(EList eList, final Map nameToIDMap) 1876 { 1877 Collection ordered = new TreeSet (new Comparator () 1878 { 1879 public boolean equals(Object object) 1880 { 1881 return object == this; 1882 } 1883 1884 public int compare(Object firstObject, Object secondObject) 1885 { 1886 int firstValue = getOrderingValue((ENamedElement)firstObject, nameToIDMap); 1887 int secondValue = getOrderingValue((ENamedElement)secondObject, nameToIDMap); 1888 return firstValue - secondValue; 1889 } 1890 }); 1891 ordered.addAll(eList); 1892 int index = 0; 1893 for (Iterator i = ordered.iterator(); i.hasNext(); ++index) 1894 { 1895 eList.move(index, i.next()); 1896 } 1897 } 1898 1899 protected int getOrderingValue(ENamedElement eNamedElement, Map nameToIDMap) 1900 { 1901 Integer result = (Integer )nameToIDMap.get(eNamedElement); 1902 if (result == null) 1903 { 1904 if (eNamedElement instanceof EClassifier) 1905 { 1906 String prefix = (String )ePackageToPrefixMap.get(eNamedElement.eContainer()); 1907 String name = eNamedElement.getName(); 1908 String id = CodeGenUtil.format(name, '_', prefix, true).toUpperCase(); 1909 result = (Integer )nameToIDMap.get(id); 1910 } 1911 else 1912 { 1913 String prefix = (String )ePackageToPrefixMap.get(eNamedElement.eContainer().eContainer()); 1914 String eClassName = ((ENamedElement)eNamedElement.eContainer()).getName(); 1915 String eFeatureName = eNamedElement.getName(); 1916 String id = CodeGenUtil.format(eClassName, '_', prefix, true).toUpperCase() + "__" 1917 + CodeGenUtil.format(eFeatureName, '_', prefix, true).toUpperCase(); 1918 result = (Integer )nameToIDMap.get(id); 1919 } 1920 if (result != null) 1921 { 1922 nameToIDMap.put(eNamedElement, result); 1923 } 1924 } 1925 1926 if (result != null) 1927 { 1928 return result.intValue(); 1929 } 1930 1931 return Integer.MAX_VALUE; 1932 } 1933 1934 1938 public IStatus getStatus() 1939 { 1940 return status; 1941 } 1942 1943 1947 public GenModel getGenModel() 1948 { 1949 return genModel; 1950 } 1951 1952 1956 protected void error(String message) 1957 { 1958 System.err.println("-->Error: " + message); 1959 status.add(new Status(IStatus.ERROR, JavaImporterPlugin.getPlugin().getBundle().getSymbolicName(), 0, message, null)); 1960 } 1961 1962 1966 protected void warning(String message) 1967 { 1968 System.err.println("-->Warning: " + message); 1969 status.add(new Status(IStatus.WARNING, JavaImporterPlugin.getPlugin().getBundle().getSymbolicName(), 0, message, null)); 1970 } 1971} 1972 | Popular Tags |