Added plugin for reflective EPackage loading
diff --git a/extra/org.eclipse.emf.ecp.reflective/.classpath b/extra/org.eclipse.emf.ecp.reflective/.classpath
new file mode 100644
index 0000000..098194c
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/extra/org.eclipse.emf.ecp.reflective/.project b/extra/org.eclipse.emf.ecp.reflective/.project
new file mode 100644
index 0000000..fec9a59
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.emf.ecp.reflective</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/extra/org.eclipse.emf.ecp.reflective/.settings/org.eclipse.jdt.core.prefs b/extra/org.eclipse.emf.ecp.reflective/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..f42de36
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.7
diff --git a/extra/org.eclipse.emf.ecp.reflective/META-INF/MANIFEST.MF b/extra/org.eclipse.emf.ecp.reflective/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..668bdb6
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/META-INF/MANIFEST.MF
@@ -0,0 +1,15 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Reflective
+Bundle-SymbolicName: org.eclipse.emf.ecp.reflective; singleton:=true
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.eclipse.emf.ecp.reflective.Activator
+Require-Bundle: org.eclipse.ui,
+ org.eclipse.core.runtime
+Bundle-ActivationPolicy: lazy
+Bundle-RequiredExecutionEnvironment: JavaSE-1.7
+Import-Package: org.eclipse.emf.common.util,
+ org.eclipse.emf.ecore,
+ org.eclipse.emf.ecore.resource,
+ org.eclipse.emf.ecore.resource.impl,
+ org.eclipse.emf.ecore.xmi.impl
diff --git a/extra/org.eclipse.emf.ecp.reflective/build.properties b/extra/org.eclipse.emf.ecp.reflective/build.properties
new file mode 100644
index 0000000..0d3d3a7
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/build.properties
@@ -0,0 +1,6 @@
+source.. = src/
+output.. = bin/
+bin.includes = plugin.xml,\
+ META-INF/,\
+ .,\
+ icons/
diff --git a/extra/org.eclipse.emf.ecp.reflective/icons/sample.gif b/extra/org.eclipse.emf.ecp.reflective/icons/sample.gif
new file mode 100644
index 0000000..34fb3c9
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/icons/sample.gif
Binary files differ
diff --git a/extra/org.eclipse.emf.ecp.reflective/plugin.xml b/extra/org.eclipse.emf.ecp.reflective/plugin.xml
new file mode 100644
index 0000000..28dcaa0
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/plugin.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<?eclipse version="3.4"?>
+<plugin>
+
+ <extension
+ point="org.eclipse.ui.commands">
+ <command
+ name="Import Ecore ..."
+ id="org.eclipse.emf.ecp.reflective.commands.sampleCommand">
+ </command>
+ </extension>
+ <extension
+ point="org.eclipse.ui.handlers">
+ <handler
+ commandId="org.eclipse.emf.ecp.reflective.commands.sampleCommand"
+ class="org.eclipse.emf.ecp.reflective.handlers.ReflectiveEPackageHandler">
+ </handler>
+ </extension>
+ <extension
+ point="org.eclipse.ui.menus">
+ <menuContribution
+ locationURI="menu:org.eclipse.ui.main.menu?after=additions">
+ <menu
+ label="Tools"
+ mnemonic="M"
+ id="org.eclipse.emf.ecp.reflective.menus.sampleMenu">
+ <command
+ commandId="org.eclipse.emf.ecp.reflective.commands.sampleCommand"
+ mnemonic="S"
+ id="org.eclipse.emf.ecp.reflective.menus.sampleCommand">
+ </command>
+ </menu>
+ </menuContribution>
+ </extension>
+
+</plugin>
diff --git a/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/Activator.java b/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/Activator.java
new file mode 100644
index 0000000..18d9e55
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/Activator.java
@@ -0,0 +1,116 @@
+/*******************************************************************************
+ * Copyright (c) 2011-2012 EclipseSource Muenchen GmbH and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ * Eugen Neufeld - JavaDoc
+ *******************************************************************************/
+package org.eclipse.emf.ecp.reflective;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.util.List;
+
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.emf.ecore.EPackage;
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
+import org.eclipse.emf.ecp.reflective.handlers.EPackageHelper;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ *
+ * @author Tobias Verhoeven
+ */
+public class Activator extends AbstractUIPlugin {
+
+ // The plug-in ID
+ public static final String PLUGIN_ID = "org.eclipse.emf.ecp.reflective"; //$NON-NLS-1$
+
+ public final File location = this.getStateLocation().toFile();
+
+ // The shared instance
+ private static Activator plugin;
+
+ /**
+ * The constructor
+ */
+ public Activator() {
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ plugin = this;
+ this.loadDynamicModels();
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(BundleContext context) throws Exception {
+ plugin = null;
+ super.stop(context);
+ }
+
+ /**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static Activator getDefault() {
+ return plugin;
+ }
+
+ /**
+ * Returns an image descriptor for the image file at the given
+ * plug-in relative path
+ *
+ * @param path the path
+ * @return the image descriptor
+ */
+ public static ImageDescriptor getImageDescriptor(String path) {
+ return imageDescriptorFromPlugin(PLUGIN_ID, path);
+ }
+
+ private void loadDynamicModels() {
+ File dir = this.location;
+ File[] files = null;
+
+ files = dir.listFiles(new FilenameFilter() {
+ public boolean accept(File d, String name) {
+ return name.endsWith(".ecore");
+ }
+ });
+
+ if (files != null) {
+ for (File file : files) {
+ ResourceSet resourceSet = new ResourceSetImpl();
+ resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
+ .put("ecore", new EcoreResourceFactoryImpl());
+ Resource resource = resourceSet.getResource(URI.createFileURI(file.getAbsolutePath()), true);
+ EPackage model = (EPackage) resource.getContents().get(0);
+ EPackage.Registry.INSTANCE.put(model.getNsURI(), model);
+ List<EPackage> packages = EPackageHelper.getAllSubPackages(model);
+ for (EPackage subPkg : packages) {
+ EPackage.Registry.INSTANCE.put(subPkg.getNsURI(), subPkg);
+ }
+ System.out.println("Dynamic Model \"" + model.getNsURI() + "\" loaded.");
+ }
+ }
+ }
+}
diff --git a/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/handlers/EPackageHelper.java b/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/handlers/EPackageHelper.java
new file mode 100644
index 0000000..eb184cc
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/handlers/EPackageHelper.java
@@ -0,0 +1,65 @@
+/*******************************************************************************
+ * Copyright (c) 2008-2011 Chair for Applied Software Engineering,
+ * Technische Universitaet Muenchen.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ ******************************************************************************/
+package org.eclipse.emf.ecp.reflective.handlers;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.emf.ecore.EPackage;
+
+
+/**
+ * The EPackageHelper provides additional methods for handling EPackages.
+ *
+ * @author Tobias Verhoeven
+ */
+public final class EPackageHelper {
+
+ private EPackageHelper() {
+ }
+
+ /**
+ * Gets the all sub packages.
+ *
+ * @param ePackage the e package
+ * @return the all sub packages
+ */
+ public static List<EPackage> getAllSubPackages(EPackage ePackage) {
+ List<EPackage> resultList = new ArrayList<EPackage>();
+ extractAllSubPackages(ePackage, resultList);
+ return resultList;
+ }
+
+ private static void extractAllSubPackages(EPackage pkg, List<EPackage> packages) {
+ packages.addAll(pkg.getESubpackages());
+ for (EPackage subPkg : pkg.getESubpackages()) {
+ extractAllSubPackages(subPkg, packages);
+ }
+ }
+
+ /**
+ * Removes subpackages from an EPackage .
+ *
+ * @param ePackage the EPackage
+ * @param subPackagesRm the subpackages to be removed.
+ */
+ public static void removeSubPackages(EPackage ePackage, Set<EPackage> subPackagesRm) {
+ if (subPackagesRm == null || subPackagesRm.isEmpty()) {
+ return;
+ }
+ ePackage.getESubpackages().removeAll(subPackagesRm);
+ for (EPackage pkg : ePackage.getESubpackages()) {
+ removeSubPackages(pkg, subPackagesRm);
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/handlers/ReflectiveEPackageHandler.java b/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/handlers/ReflectiveEPackageHandler.java
new file mode 100644
index 0000000..16f450f
--- /dev/null
+++ b/extra/org.eclipse.emf.ecp.reflective/src/org/eclipse/emf/ecp/reflective/handlers/ReflectiveEPackageHandler.java
@@ -0,0 +1,133 @@
+/*******************************************************************************
+ * Copyright (c) 2011-2012 EclipseSource Muenchen GmbH and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ * Eugen Neufeld - JavaDoc
+ *******************************************************************************/
+package org.eclipse.emf.ecp.reflective.handlers;
+
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.FileDialog;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.emf.ecore.xmi.impl.EcoreResourceFactoryImpl;
+import org.eclipse.emf.ecore.EPackage;
+
+import org.eclipse.emf.ecore.resource.Resource;
+import org.eclipse.emf.ecore.resource.ResourceSet;
+import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
+import org.eclipse.emf.ecp.reflective.Activator;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.jface.dialogs.MessageDialog;
+
+/**
+ *
+ * @author Tobias Verhoeven
+ */
+public class ReflectiveEPackageHandler extends AbstractHandler {
+ /**
+ * The constructor.
+ */
+ public ReflectiveEPackageHandler() {
+ }
+
+ /**
+ * the command has been executed, so extract extract the needed information
+ * from the application context.
+ */
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+
+ IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
+
+ FileDialog fileDialog = new FileDialog(window.getShell(), SWT.OPEN);
+ fileDialog.setText("Open");
+ fileDialog.setFilterExtensions(new String[]{"ecore"});
+ //fileDialog.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toString());
+
+ String path = fileDialog.open();
+ if (path != null) {
+ ResourceSet resourceSet = new ResourceSetImpl();
+ resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
+ .put("ecore", new EcoreResourceFactoryImpl());
+ Resource resource = resourceSet.getResource(URI.createFileURI(path), true);
+ EPackage ePackage = (EPackage) resource.getContents().get(0);
+ registerEPackage(ePackage,window.getShell());
+ }
+ return null;
+ }
+
+ public void registerEPackage(EPackage ePackage,Shell shell) {
+ List<EPackage> packages = EPackageHelper.getAllSubPackages(ePackage);
+ Set<EPackage> rmPackages = new LinkedHashSet<EPackage>();
+ packages.add(ePackage);
+
+ // check for subpackages that are already registered
+ for (EPackage subPkg : packages) {
+ if (EPackage.Registry.INSTANCE.getEPackage(subPkg.getNsURI()) != null) {
+ rmPackages.add(subPkg);
+ }
+ }
+ packages.removeAll(rmPackages);
+
+ // remove subpackages that are already registered from
+ // input-EPackage, the diff-package is registered and saved.
+ EPackageHelper.removeSubPackages(ePackage, rmPackages);
+
+ if (packages.isEmpty()) {
+ MessageDialog.openError(shell,"Error",
+ "Registration failed: Package(s) with supplied NsUris(s) is/are already registred!");
+ return;
+ }
+
+ // Save the EPackages to disc as an "ecore"-file
+ String uriFileName = null;
+ try {
+ uriFileName = URLEncoder.encode(ePackage.getNsURI(), "UTF-8");
+ } catch (UnsupportedEncodingException e1) {
+ MessageDialog.openError(shell,"Error","Registration failed: Could not convert NsUri to filename!");
+ return;
+ }
+
+
+ URI fileUri = URI.createFileURI(Activator.getDefault().location + "/" + uriFileName
+ + (uriFileName.endsWith(".ecore") ? "" : ".ecore"));
+
+ // create a resource to save the file to disc
+ ResourceSet resourceSet = new ResourceSetImpl();
+ resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap()
+ .put("ecore", new EcoreResourceFactoryImpl());
+ Resource resource = resourceSet.createResource(fileUri);
+ resource.getContents().add(ePackage);
+
+ try {
+ resource.save(null);
+ } catch (IOException e) {
+ e.printStackTrace();
+ MessageDialog.openError(shell,"Error","Cannot save imported Ecore! Import canceled.");
+ return;
+ }
+ // Finally register EPackages in global EPackage-registry.
+ for (EPackage registerPackage : packages) {
+ EPackage.Registry.INSTANCE.put(registerPackage.getNsURI(), registerPackage);
+ }
+ MessageDialog.openInformation(shell, "Info", "EPackage \"" + ePackage.getNsURI() + "\" registered and saved.");
+ }
+}