Exemple #1
0
 /**
  * Convert a file to the EPackage containing a metamodel.
  *
  * @param iFile The file that is to be converted to the Epackage.
  * @return The EPackage containing the metamodel loaded from the file.
  * @throws CodeGenerationException
  */
 protected EPackage fileToEPack(IFile iFile) throws CodeGenerationException {
   ResourceSet resSet = new ResourceSetImpl();
   resSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap());
   URI fileURI =
       URI.createPlatformResourceURI(
           "/" + iFile.getProject().getName() + "/" + iFile.getProjectRelativePath().toString(),
           true);
   Resource resource = resSet.createResource(fileURI);
   Map<Object, Object> options = new HashMap<Object, Object>();
   options.put(XMLResource.OPTION_ENCODING, "UTF-8");
   try {
     resource.load(options);
   } catch (IOException e) {
     throw new CodeGenerationException(
         "Error while loading resource of File: " + iFile.getName(), e.getCause());
   }
   EList<EObject> sd = resource.getContents();
   for (EObject object : sd) {
     if (object instanceof EPackage) {
       EPackage ePack = (EPackage) object;
       pi.setNsURI(ePack.getNsURI());
       return ePack;
     }
   }
   return null;
 }
  private void initializeEditingDomain() {
    editingDomain =
        new AdapterFactoryEditingDomain(adapterFactory, new EFactoryCommandStack(), resourceSet) {
          @Override
          public boolean isReadOnly(Resource resource) {
            return super.isReadOnly(resource)
                || getResourceSet().getResources().indexOf(resource) != 0;
          }
        };

    resourceSet.eAdapters().add(new EditingDomainProvider());
    resourceSet.getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap(true));
  }
  @Override
  public void execute() throws BuildException {

    EPackage.Registry.INSTANCE.put(GenModelPackage.eNS_URI, GenModelPackage.eINSTANCE);
    EPackage.Registry.INSTANCE.put(EcorePackage.eNS_URI, EcorePackage.eINSTANCE);
    EPackage.Registry.INSTANCE.put(
        "http://www.eclipse.org/uml2/2.2.0/GenModel", getUML2GenPackage());
    EPackage.Registry.INSTANCE.put(
        "http://www.eclipse.org/uml2/4.0.0/Types", getUML2TypesPackage());

    Map<String, Object> extensionToFactoryMap =
        Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap();
    extensionToFactoryMap.put(
        "ecore", new org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl());
    extensionToFactoryMap.put(
        "genmodel", new org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl());

    // We ignore the deprecation warning until the method is not available
    // anymore to keep this code runnable against old versions of EMF as
    // long as possible.
    @SuppressWarnings("deprecation")
    final Map<String, URI> packageNsURIToGenModelLocationMap =
        EcorePlugin.getEPackageNsURIToGenModelLocationMap();
    URI genModelURIObject = URI.createURI(genModelURI);
    ResourceSet rs = new ResourceSetImpl();
    Resource resource = null;
    try {
      resource = rs.getResource(genModelURIObject, true);

      for (GenPackage genPackage : ((GenModel) resource.getContents().get(0)).getGenPackages()) {
        EPackage ecorePackage = genPackage.getEcorePackage();
        String nsURI = ecorePackage.getNsURI();
        EPackage.Registry.INSTANCE.put(nsURI, ecorePackage);
      }
    } catch (Exception e) {
      e.printStackTrace();
      throw new BuildException("Can't load generator model from " + genModelURIObject);
    }

    packageNsURIToGenModelLocationMap.put(namespaceURI, genModelURIObject);
  }
  public void registerResourceFactories() {
    // in case a old version from last run is registered here
    EPackage.Registry.INSTANCE.remove("http://www.emftext.org/sdk/concretesyntax");
    registerFactory("cs", new CsResourceFactory());

    // TODO: the rest of this method is never used in our build scripts at the moment
    for (GenModelElement genModelElement : genModels) {
      String namespaceURI = genModelElement.getNamespaceURI();
      String genModelURI = genModelElement.getGenModelURI();
      try {
        log("Registering genmodel " + namespaceURI + " at " + genModelURI);

        // We ignore the deprecation warning until the method is not available
        // anymore to keep this code runnable against old versions of EMF as
        // long as possible.
        @SuppressWarnings("deprecation")
        Map<String, URI> ePackageNsURIToGenModelLocationMap =
            EcorePlugin.getEPackageNsURIToGenModelLocationMap();
        ePackageNsURIToGenModelLocationMap.put(namespaceURI, URI.createURI(genModelURI));

        // register a mapping for "resource:/plugin/..." URIs in case
        // one genmodel imports another genmodel using this URI
        String filesSystemURI = genModelURI;
        int startIdx = filesSystemURI.indexOf("/plugins");
        int versionStartIdx = filesSystemURI.indexOf("_", startIdx);
        int filePathStartIdx = filesSystemURI.lastIndexOf("!/") + 1;
        if (startIdx > -1 && versionStartIdx > startIdx && filePathStartIdx > versionStartIdx) {
          String platformPluginURI =
              "platform:" + filesSystemURI.substring(startIdx, versionStartIdx);
          platformPluginURI = platformPluginURI + filesSystemURI.substring(filePathStartIdx);
          platformPluginURI = platformPluginURI.replace("/plugins/", "/plugin/");

          // gen model
          log("adding mapping from " + platformPluginURI + " to " + filesSystemURI);
          URIConverter.URI_MAP.put(URI.createURI(platformPluginURI), URI.createURI(filesSystemURI));

          // ecore model (this code assumes that both files are named equally)
          filesSystemURI = filesSystemURI.replace(".genmodel", ".ecore");
          platformPluginURI = platformPluginURI.replace(".genmodel", ".ecore");
          log("adding mapping from " + platformPluginURI + " to " + filesSystemURI);
          URIConverter.URI_MAP.put(URI.createURI(platformPluginURI), URI.createURI(filesSystemURI));
        }
      } catch (Exception e) {
        throw new BuildException("Error while registering genmodel " + namespaceURI, e);
      }
    }

    for (GenPackageElement genPackage : genPackages) {
      String namespaceURI = genPackage.getNamespaceURI();
      String ePackageClassName = genPackage.getEPackageClassName();
      try {
        log("registering ePackage " + namespaceURI + " at " + ePackageClassName);
        Field factoryInstance = Class.forName(ePackageClassName).getField("eINSTANCE");
        Object ePackageObject = factoryInstance.get(null);
        EPackage.Registry.INSTANCE.put(namespaceURI, ePackageObject);
      } catch (Exception e) {
        e.printStackTrace();
        throw new BuildException("Error while registering EPackage " + namespaceURI, e);
      }
    }
  }