String projectName = javaSource.segment(0); IProject project = null; try { List classpathEntries = new UniqueEList();  progressMonitor.beginTask("", 10); progressMonitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("_UI_CreatingEMFProject_message", new Object [] { projectName })); IWorkspace workspace = ResourcesPlugin.getWorkspace(); project = workspace.getRoot().getProject(projectName);    if (!project.exists()) { IPath location = projectLocationPath; if (location == null) { location = workspace.getRoot().getLocation().append(projectName); } location = location.append(".project"); File projectFile = new File(location.toString()); if (projectFile.exists()) { projectFile.renameTo(new File(location.toString() + ".old")); } }  IJavaProject javaProject = JavaCore.create(project); IProjectDescription projectDescription = null; if (!project.exists()) { projectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(projectName); projectDescription.setLocation(projectLocationPath); project.create(projectDescription, new SubProgressMonitor(progressMonitor, 1)); } else { projectDescription = project.getDescription(); classpathEntries.addAll(Arrays.asList(javaProject.getRawClasspath())); }  boolean isInitiallyEmpty = classpathEntries.isEmpty();  { if (referencedProjects.size() != 0 && (style & (EMF_PLUGIN_PROJECT_STYLE | EMF_EMPTY_PROJECT_STYLE)) == 0) { projectDescription.setReferencedProjects ((IProject [])referencedProjects.toArray(new IProject [referencedProjects.size()])); for (Iterator i = referencedProjects.iterator(); i.hasNext(); ) { IProject referencedProject = (IProject)i.next(); IClasspathEntry referencedProjectClasspathEntry = JavaCore.newProjectEntry(referencedProject.getFullPath()); classpathEntries.add(referencedProjectClasspathEntry); } }  String [] natureIds = projectDescription.getNatureIds(); if (natureIds == null) { natureIds = new String [] { JavaCore.NATURE_ID }; } else { boolean hasJavaNature = false; for (int i = 0; i < natureIds.length; ++i) { if (JavaCore.NATURE_ID.equals(natureIds[i])) { hasJavaNature = true; } } if (!hasJavaNature) { String [] oldNatureIds = natureIds; natureIds = new String [oldNatureIds.length + 1]; System.arraycopy(oldNatureIds, 0, natureIds, 0, oldNatureIds.length); natureIds[oldNatureIds.length] = JavaCore.NATURE_ID; } }  projectDescription.setNatureIds(natureIds); project.open(new SubProgressMonitor(progressMonitor, 1)); project.setDescription(projectDescription, new SubProgressMonitor(progressMonitor, 1));  IContainer sourceContainer = project; if (javaSource.segmentCount() > 1) { sourceContainer = project.getFolder(javaSource.removeFirstSegments(1).makeAbsolute()); if (!sourceContainer.exists()) { ((IFolder)sourceContainer).create(false, true, new SubProgressMonitor(progressMonitor, 1)); } }  if (isInitiallyEmpty) { IClasspathEntry sourceClasspathEntry = JavaCore.newSourceEntry(javaSource); for (Iterator i = classpathEntries.iterator(); i.hasNext(); ) { IClasspathEntry classpathEntry = (IClasspathEntry)i.next(); if (classpathEntry.getPath().isPrefixOf(javaSource)) { i.remove(); } } classpathEntries.add(0, sourceClasspathEntry);  IClasspathEntry jreClasspathEntry = JavaCore.newVariableEntry (new Path(JavaRuntime.JRELIB_VARIABLE), new Path(JavaRuntime.JRESRC_VARIABLE), new Path(JavaRuntime.JRESRCROOT_VARIABLE)); for (Iterator i = classpathEntries.iterator(); i.hasNext(); ) { IClasspathEntry classpathEntry = (IClasspathEntry)i.next(); if (classpathEntry.getPath().isPrefixOf(jreClasspathEntry.getPath())) { i.remove(); } } classpathEntries.add(jreClasspathEntry); }  if ((style & EMF_EMPTY_PROJECT_STYLE) == 0) { if ((style & EMF_PLUGIN_PROJECT_STYLE) != 0) { classpathEntries.add(JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins")));    for (Iterator i = classpathEntries.iterator(); i.hasNext(); ) { IClasspathEntry classpathEntry = (IClasspathEntry)i.next(); if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE && !JavaRuntime.JRELIB_VARIABLE.equals(classpathEntry.getPath().toString()) || classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT) { i.remove(); } } } else { addClasspathEntries(classpathEntries, "ECLIPSE_CORE_RUNTIME", "org.eclipse.core.runtime"); addClasspathEntries(classpathEntries, "ECLIPSE_CORE_RESOURCES", "org.eclipse.core.resources"); addClasspathEntries(classpathEntries, "EMF_COMMON", "org.eclipse.emf.common"); addClasspathEntries(classpathEntries, "EMF_ECORE", "org.eclipse.emf.ecore");  if ((style & EMF_XML_PROJECT_STYLE) != 0) { addClasspathEntries(classpathEntries, "EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi"); }  if ((style & EMF_MODEL_PROJECT_STYLE) == 0) { addClasspathEntries(classpathEntries, "EMF_EDIT", "org.eclipse.emf.edit");  if ((style & EMF_EDIT_PROJECT_STYLE) == 0) { addClasspathEntries(classpathEntries, "ECLIPSE_SWT", "org.eclipse.swt"); addClasspathEntries(classpathEntries, "ECLIPSE_JFACE", "org.eclipse.jface"); addClasspathEntries(classpathEntries, "ECLIPSE_UI_VIEWS", "org.eclipse.ui.views"); addClasspathEntries(classpathEntries, "ECLIPSE_UI_EDITORS", "org.eclipse.ui.editors"); addClasspathEntries(classpathEntries, "ECLIPSE_UI_IDE", "org.eclipse.ui.ide"); addClasspathEntries(classpathEntries, "ECLIPSE_UI_WORKBENCH", "org.eclipse.ui.workbench"); addClasspathEntries(classpathEntries, "EMF_COMMON_UI", "org.eclipse.emf.common.ui"); addClasspathEntries(classpathEntries, "EMF_EDIT_UI", "org.eclipse.emf.edit.ui"); if ((style & EMF_XML_PROJECT_STYLE) == 0) { addClasspathEntries(classpathEntries, "EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi"); } } }  if (pluginVariables != null) { for (Iterator i = pluginVariables.iterator(); i.hasNext(); ) { Object variable = i.next(); if (variable instanceof IClasspathEntry) { classpathEntries.add((IClasspathEntry)variable); } else if (variable instanceof String) { String pluginVariable = (String)variable; String name; String id; int index = pluginVariable.indexOf("="); if (index == -1) { name = pluginVariable.replace('.','_').toUpperCase(); id = pluginVariable; } else { name = pluginVariable.substring(0, index); id = pluginVariable.substring(index + 1); } addClasspathEntries(classpathEntries, name, id); } } } } }  javaProject.setRawClasspath ((IClasspathEntry [])classpathEntries.toArray(new IClasspathEntry [classpathEntries.size()]), new SubProgressMonitor(progressMonitor, 1)); }  if (isInitiallyEmpty) { javaProject.setOutputLocation(new Path("/" + javaSource.segment(0) + "/runtime"), new SubProgressMonitor(progressMonitor, 1)); } } catch (Exception exception) { exception.printStackTrace(); CodeGenEcorePlugin.INSTANCE.log(exception); } finally { progressMonitor.done(); }  return project;
return createEMFProject(javaSource, projectLocationPath, referencedProjects, progressMonitor, style, Collections.EMPTY_LIST);
  public static IProject createEMFProject    (IPath javaSource,     IPath projectLocationPath,     List referencedProjects,     IProgressMonitor progressMonitor,     int style,     List pluginVariables)  {    String projectName = javaSource.segment(0);    IProject project = null;    try    {      List classpathEntries = new UniqueEList();      progressMonitor.beginTask("", 10); // TODO      progressMonitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("_UI_CreatingEMFProject_message", new Object [] { projectName }));      IWorkspace workspace = ResourcesPlugin.getWorkspace();      project = workspace.getRoot().getProject(projectName);      // Clean up any old project information.      //      if (!project.exists())      {        IPath location = projectLocationPath;        if (location == null)        {          location = workspace.getRoot().getLocation().append(projectName);        }        location = location.append(".project");        File projectFile = new File(location.toString());        if (projectFile.exists())        {          projectFile.renameTo(new File(location.toString() + ".old"));        }      }      IJavaProject javaProject = JavaCore.create(project);      IProjectDescription projectDescription = null;      if (!project.exists())      {        projectDescription = ResourcesPlugin.getWorkspace().newProjectDescription(projectName);        projectDescription.setLocation(projectLocationPath);        project.create(projectDescription, new SubProgressMonitor(progressMonitor, 1));      }      else       {        projectDescription = project.getDescription();        classpathEntries.addAll(Arrays.asList(javaProject.getRawClasspath()));      }      boolean isInitiallyEmpty = classpathEntries.isEmpty();      {        if (referencedProjects.size() != 0 && (style & (EMF_PLUGIN_PROJECT_STYLE | EMF_EMPTY_PROJECT_STYLE)) == 0)        {          projectDescription.setReferencedProjects            ((IProject [])referencedProjects.toArray(new IProject [referencedProjects.size()]));          for (Iterator i = referencedProjects.iterator(); i.hasNext(); )          {            IProject referencedProject = (IProject)i.next();            IClasspathEntry referencedProjectClasspathEntry = JavaCore.newProjectEntry(referencedProject.getFullPath());            classpathEntries.add(referencedProjectClasspathEntry);          }        }        String [] natureIds = projectDescription.getNatureIds();        if (natureIds == null)        {          natureIds = new String [] { JavaCore.NATURE_ID };        }        else        {          boolean hasJavaNature = false;          for (int i = 0; i < natureIds.length; ++i)          {            if (JavaCore.NATURE_ID.equals(natureIds[i]))            {              hasJavaNature = true;            }          }          if (!hasJavaNature)          {            String [] oldNatureIds = natureIds;            natureIds = new String [oldNatureIds.length + 1];            System.arraycopy(oldNatureIds, 0, natureIds, 0, oldNatureIds.length);            natureIds[oldNatureIds.length] = JavaCore.NATURE_ID;          }        }        projectDescription.setNatureIds(natureIds);        project.open(new SubProgressMonitor(progressMonitor, 1));        project.setDescription(projectDescription, new SubProgressMonitor(progressMonitor, 1));        IContainer sourceContainer = project;        if (javaSource.segmentCount() > 1)        {          sourceContainer = project.getFolder(javaSource.removeFirstSegments(1).makeAbsolute());          if (!sourceContainer.exists())          {            ((IFolder)sourceContainer).create(false, true, new SubProgressMonitor(progressMonitor, 1));          }        }        if (isInitiallyEmpty)        {          IClasspathEntry sourceClasspathEntry =             JavaCore.newSourceEntry(javaSource);          for (Iterator i = classpathEntries.iterator(); i.hasNext(); )          {            IClasspathEntry classpathEntry = (IClasspathEntry)i.next();            if (classpathEntry.getPath().isPrefixOf(javaSource))            {              i.remove();            }          }          classpathEntries.add(0, sourceClasspathEntry);          IClasspathEntry jreClasspathEntry =            JavaCore.newVariableEntry              (new Path(JavaRuntime.JRELIB_VARIABLE), new Path(JavaRuntime.JRESRC_VARIABLE), new Path(JavaRuntime.JRESRCROOT_VARIABLE));          for (Iterator i = classpathEntries.iterator(); i.hasNext(); )          {            IClasspathEntry classpathEntry = (IClasspathEntry)i.next();            if (classpathEntry.getPath().isPrefixOf(jreClasspathEntry.getPath()))            {              i.remove();            }          }          classpathEntries.add(jreClasspathEntry);        }        if ((style & EMF_EMPTY_PROJECT_STYLE) == 0)        {          if ((style & EMF_PLUGIN_PROJECT_STYLE) != 0)          {            classpathEntries.add(JavaCore.newContainerEntry(new Path("org.eclipse.pde.core.requiredPlugins")));            // Remove variables since the plugin.xml should provide the complete path information.            //            for (Iterator i = classpathEntries.iterator(); i.hasNext(); )            {              IClasspathEntry classpathEntry = (IClasspathEntry)i.next();              if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_VARIABLE &&                     !JavaRuntime.JRELIB_VARIABLE.equals(classpathEntry.getPath().toString()) ||                    classpathEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT)              {                i.remove();              }            }          }          else          {            addClasspathEntries(classpathEntries, "ECLIPSE_CORE_RUNTIME", "org.eclipse.core.runtime");            addClasspathEntries(classpathEntries, "ECLIPSE_CORE_RESOURCES", "org.eclipse.core.resources");            addClasspathEntries(classpathEntries, "EMF_COMMON", "org.eclipse.emf.common");            addClasspathEntries(classpathEntries, "EMF_ECORE", "org.eclipse.emf.ecore");            if ((style & EMF_XML_PROJECT_STYLE) != 0)            {              addClasspathEntries(classpathEntries, "EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi");            }            if ((style & EMF_MODEL_PROJECT_STYLE) == 0)            {              addClasspathEntries(classpathEntries, "EMF_EDIT", "org.eclipse.emf.edit");              if ((style & EMF_EDIT_PROJECT_STYLE) == 0)              {                addClasspathEntries(classpathEntries, "ECLIPSE_SWT", "org.eclipse.swt");                addClasspathEntries(classpathEntries, "ECLIPSE_JFACE", "org.eclipse.jface");                addClasspathEntries(classpathEntries, "ECLIPSE_UI_VIEWS", "org.eclipse.ui.views");                addClasspathEntries(classpathEntries, "ECLIPSE_UI_EDITORS", "org.eclipse.ui.editors");                addClasspathEntries(classpathEntries, "ECLIPSE_UI_IDE", "org.eclipse.ui.ide");                addClasspathEntries(classpathEntries, "ECLIPSE_UI_WORKBENCH", "org.eclipse.ui.workbench");                addClasspathEntries(classpathEntries, "EMF_COMMON_UI", "org.eclipse.emf.common.ui");                addClasspathEntries(classpathEntries, "EMF_EDIT_UI", "org.eclipse.emf.edit.ui");                if ((style & EMF_XML_PROJECT_STYLE) == 0)                {                  addClasspathEntries(classpathEntries, "EMF_ECORE_XMI", "org.eclipse.emf.ecore.xmi");                }              }            }            if (pluginVariables != null)            {              for (Iterator i = pluginVariables.iterator(); i.hasNext(); )              {                Object variable = i.next();                if (variable instanceof IClasspathEntry)                {                  classpathEntries.add((IClasspathEntry)variable);                }                else if (variable instanceof String)                {                  String pluginVariable = (String)variable;                  String name;                  String id;                  int index = pluginVariable.indexOf("=");                  if (index == -1)                  {                    name = pluginVariable.replace('.','_').toUpperCase();                    id = pluginVariable;                  }                  else                  {                    name = pluginVariable.substring(0, index);                    id = pluginVariable.substring(index + 1);                  }                  addClasspathEntries(classpathEntries, name, id);                }              }            }          }        }        javaProject.setRawClasspath          ((IClasspathEntry [])classpathEntries.toArray(new IClasspathEntry [classpathEntries.size()]),           new SubProgressMonitor(progressMonitor, 1));      }      if (isInitiallyEmpty)      {        javaProject.setOutputLocation(new Path("/" + javaSource.segment(0) + "/runtime"), new SubProgressMonitor(progressMonitor, 1));      }    }    catch (Exception exception)    {      exception.printStackTrace();      CodeGenEcorePlugin.INSTANCE.log(exception);    }    finally    {      progressMonitor.done();    }    return project;  }