Просмотр исходного кода

add generic Java Generator that is executed from within the workspace

holger.willebrandt@gmail.com 14 лет назад
Родитель
Сommit
6230d7bfae

+ 7 - 0
plugins/org.yakindu.sct.generator.core/library/GenericJavaFeatureTypeLibrary.xmi

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="ASCII"?>
+<sgen:FeatureTypeLibrary xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sgen="http://www.yakindu.org/sct/statechart/SGen" xsi:schemaLocation="http://www.yakindu.org/sct/statechart/SGen ../../org.yakindu.sct.model.sgen/model/emf/sgen.ecore" name="Java">
+  <types name="Generator">
+    <parameters name="generatorProject"/>
+    <parameters name="generatorClass"/>
+  </types>
+</sgen:FeatureTypeLibrary>

+ 11 - 0
plugins/org.yakindu.sct.generator.core/plugin.xml

@@ -15,6 +15,11 @@
             generatorId="yakindu::xpand"
             uri="platform:/plugin/org.yakindu.sct.generator.core/library/XpandFeatureTypeLibrary.xmi">
       </FeatureLibrary>
+      <FeatureLibrary 
+   			defaultProvider="org.yakindu.sct.generator.core.features.impl.GenericJavaLibraryDefaultValueProvider"
+      		generatorId="yakindu::generic" 
+          	uri="platform:/plugin/org.yakindu.sct.generator.core/library/GenericJavaFeatureTypeLibrary.xmi">
+   	  </FeatureLibrary>
    </extension>
  <extension
        point="org.yakindu.sct.generator.core.generator">
@@ -24,5 +29,11 @@
           id="yakindu::xpand"
           name="YAKINDU Generic Xpand Generator">
     </SCTGenerator>
+    <SCTGenerator
+          class="org.yakindu.sct.generator.core.impl.GenericJavaBasedGenerator"
+          description="Generate Code using Xpand Templates"
+          id="yakindu::generic"
+          name="YAKINDU Generic Java Generator">
+    </SCTGenerator>
  </extension>
 </plugin>

+ 55 - 0
plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/AbstractWorkspaceGenerator.java

@@ -0,0 +1,55 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU 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:
+ *     committers of YAKINDU - initial API and implementation
+ */
+package org.yakindu.sct.generator.core;
+
+import java.io.File;
+
+import org.yakindu.sct.model.sexec.ExecutionFlow;
+import org.yakindu.sct.model.sgen.FeatureParameterValue;
+import org.yakindu.sct.model.sgen.GeneratorEntry;
+
+/**
+ * Base class for generators that are executed inside the workspace
+ * 
+ * @author holger willebrandt - Initial contribution and API
+ */
+public abstract class AbstractWorkspaceGenerator implements IGeneratorBridge {
+
+	private IGeneratorBridge bridge;
+
+	public final void setBridge(IGeneratorBridge bridge) {
+		this.bridge = bridge;
+	}
+
+	public abstract void generate(ExecutionFlow flow, GeneratorEntry entry);
+
+	public final void writeToConsole(String s) {
+		bridge.writeToConsole(s);
+	}
+
+	public final void writeToConsole(Throwable t) {
+		bridge.writeToConsole(t);
+	}
+
+	public final FeatureParameterValue getFeatureParameter(
+			GeneratorEntry entry, String featureName, String paramName) {
+		return bridge.getFeatureParameter(entry, featureName, paramName);
+	}
+
+	public final void refreshTargetProject(GeneratorEntry entry) {
+		bridge.refreshTargetProject(entry);
+	}
+
+	public final File getTargetProjectPath(GeneratorEntry entry) {
+		return bridge.getTargetProjectPath(entry);
+	}
+
+}

+ 35 - 0
plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/IGeneratorBridge.java

@@ -0,0 +1,35 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU 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:
+ *     committers of YAKINDU - initial API and implementation
+ */
+package org.yakindu.sct.generator.core;
+
+import java.io.File;
+
+import org.yakindu.sct.model.sgen.FeatureParameterValue;
+import org.yakindu.sct.model.sgen.GeneratorEntry;
+
+/**
+ * bridges common generator functionality between plugin and runtime workspace
+ * 
+ * @author holger willebrandt - Initial contribution and API
+ */
+public interface IGeneratorBridge {
+
+	void writeToConsole(String s);
+
+	void writeToConsole(Throwable t);
+
+	FeatureParameterValue getFeatureParameter(GeneratorEntry entry,
+			String featureName, String paramName);
+
+	void refreshTargetProject(GeneratorEntry entry);
+
+	File getTargetProjectPath(GeneratorEntry entry);
+}

+ 61 - 0
plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/features/impl/GenericJavaLibraryDefaultValueProvider.java

@@ -0,0 +1,61 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.generator.core.features.impl;
+
+import static org.yakindu.sct.generator.core.features.impl.IGenericJavaFeatureConstants.GENERATOR_CLASS;
+import static org.yakindu.sct.generator.core.features.impl.IGenericJavaFeatureConstants.GENERATOR_PROJECT;
+import static org.yakindu.sct.generator.core.features.impl.IGenericJavaFeatureConstants.LIBRARY_NAME;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.yakindu.sct.generator.core.features.AbstractDefaultFeatureValueProvider;
+import org.yakindu.sct.model.sgen.FeatureParameterValue;
+import org.yakindu.sct.model.sgen.FeatureTypeLibrary;
+import org.yakindu.sct.model.sgraph.Statechart;
+
+/**
+ * 
+ * @author holger willebrandt - Initial contribution and API
+ */
+public class GenericJavaLibraryDefaultValueProvider extends
+		AbstractDefaultFeatureValueProvider {
+
+	// (ID.)+ID
+	private static final String GENERATOR_CLASS_REGEX = "([a-zA-Z_][a-zA-Z0-9_]*\\.)+[a-zA-Z_][a-zA-Z0-9_]*"; //$NON-NLS-1$
+
+	public boolean isProviderFor(FeatureTypeLibrary library) {
+		return library.getName().equals(LIBRARY_NAME);
+	}
+
+	@Override
+	protected void setDefaultValue(FeatureParameterValue parameterValue,
+			Statechart statechart) {
+		String parameterName = parameterValue.getParameter().getName();
+		if (GENERATOR_PROJECT.equals(parameterName)) {
+			parameterValue.setValue(getProject(statechart).getName());
+		} else if (GENERATOR_CLASS.equals(parameterName)) {
+			parameterValue.setValue("org.yakindu.sct.generator.Generator");
+		}
+	}
+
+	public IStatus validateParameterValue(FeatureParameterValue parameterValue) {
+		String parameterName = parameterValue.getParameter().getName();
+		String value = parameterValue.getStringValue();
+		if (GENERATOR_PROJECT.equals(parameterName) && !projectExists(value)) {
+			return error(String.format("The Project %s does not exist", value));
+		}
+		if (GENERATOR_CLASS.equals(parameterName)
+				&& !value.matches(GENERATOR_CLASS_REGEX)) {
+			return error("Generator class must be a full qualified class name");
+		}
+		return Status.OK_STATUS;
+	}
+}

+ 8 - 0
plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/features/impl/IGenericJavaFeatureConstants.java

@@ -0,0 +1,8 @@
+package org.yakindu.sct.generator.core.features.impl;
+
+public interface IGenericJavaFeatureConstants {
+	public static final String LIBRARY_NAME = "Java";
+	public static final String TEMPLATE_FEATURE = "Generator";
+	public static final String GENERATOR_PROJECT = "generatorProject";
+	public static final String GENERATOR_CLASS = "generatorClass";
+}

+ 14 - 0
plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/features/impl/IXpandFeatureConstants.java

@@ -0,0 +1,14 @@
+package org.yakindu.sct.generator.core.features.impl;
+
+/**
+ * 
+ * @author holger willebrandt - Initial contribution and API
+ */
+public interface IXpandFeatureConstants {
+
+	public static final String LIBRARY_NAME = "Xpand";
+	public static final String TEMPLATE_FEATURE = "Template";
+	public static final String TEMPLATE_FEATURE_TEMPLATE_PATH = "templatePath";
+	public static final String TEMPLATE_FEATURE_TEMPLATE_PROJECT = "templateProject";
+
+}

+ 106 - 0
plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/impl/GenericJavaBasedGenerator.java

@@ -0,0 +1,106 @@
+/**
+ * Copyright (c) 2011 committers of YAKINDU 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:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.generator.core.impl;
+
+import static org.yakindu.sct.generator.core.features.impl.IGenericJavaFeatureConstants.GENERATOR_CLASS;
+import static org.yakindu.sct.generator.core.features.impl.IGenericJavaFeatureConstants.GENERATOR_PROJECT;
+import static org.yakindu.sct.generator.core.features.impl.IGenericJavaFeatureConstants.TEMPLATE_FEATURE;
+
+import java.io.File;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.emf.common.util.URI;
+import org.yakindu.sct.generator.core.AbstractWorkspaceGenerator;
+import org.yakindu.sct.generator.core.IGeneratorBridge;
+import org.yakindu.sct.model.sexec.ExecutionFlow;
+import org.yakindu.sct.model.sgen.FeatureConfiguration;
+import org.yakindu.sct.model.sgen.FeatureParameterValue;
+import org.yakindu.sct.model.sgen.GeneratorEntry;
+
+/**
+ * 
+ * 
+ * @author holger willebrandt - Initial contribution and API
+ */
+public class GenericJavaBasedGenerator extends AbstractSExecModelGenerator {
+
+	final IGeneratorBridge bridge = new IGeneratorBridge() {
+
+		public void writeToConsole(Throwable t) {
+			GenericJavaBasedGenerator.this.writeToConsole(t);
+		}
+
+		public void writeToConsole(String s) {
+			GenericJavaBasedGenerator.this.writeToConsole(s);
+		}
+
+		public FeatureParameterValue getFeatureParameter(GeneratorEntry entry,
+				String featureName, String paramName) {
+			return GenericJavaBasedGenerator.this.getFeatureParameter(entry,
+					featureName, paramName);
+		}
+
+		public void refreshTargetProject(GeneratorEntry entry) {
+			GenericJavaBasedGenerator.this.refreshTargetProject(entry);
+		}
+
+		public File getTargetProjectPath(GeneratorEntry entry) {
+			IProject targetProject = GenericJavaBasedGenerator.this
+					.getTargetProject(entry);
+			return targetProject.getLocation().toFile();
+		}
+	};
+
+	@Override
+	protected void generate(ExecutionFlow flow, GeneratorEntry entry) {
+		String templateClass = entry.getFeatureConfiguration(TEMPLATE_FEATURE)
+				.getParameterValue(GENERATOR_CLASS).getStringValue();
+		IProject project = getLookupRoot(entry);
+		final ClassLoader classLoader = new WorkspaceClassLoaderFactory()
+				.createClassLoader(project);
+		try {
+			@SuppressWarnings("unchecked")
+			Class<AbstractWorkspaceGenerator> delegateGeneratorClass = (Class<AbstractWorkspaceGenerator>) classLoader
+					.loadClass(templateClass);
+			AbstractWorkspaceGenerator delegate = delegateGeneratorClass
+					.newInstance();
+			delegate.setBridge(bridge);
+			delegate.generate(flow, entry);
+		} catch (Exception e) {
+			e.printStackTrace();
+			writeToConsole(e);
+		}
+	}
+
+	/**
+	 * resolve the project that defines the lookup path for the XpandFacade
+	 * 
+	 */
+	protected IProject getLookupRoot(GeneratorEntry entry) {
+		IProject project = null;
+		FeatureConfiguration templateConfig = entry
+				.getFeatureConfiguration(TEMPLATE_FEATURE);
+		FeatureParameterValue projectName = templateConfig
+				.getParameterValue(GENERATOR_PROJECT);
+		if (projectName != null) {
+			project = ResourcesPlugin.getWorkspace().getRoot()
+					.getProject(projectName.getStringValue());
+		} else {
+			URI uri = entry.getStatechart().eResource().getURI();
+			project = ResourcesPlugin.getWorkspace().getRoot()
+					.getFile(new Path(uri.toPlatformString(true))).getProject();
+		}
+		return project;
+	}
+
+}

+ 18 - 1
plugins/org.yakindu.sct.generator.core/src/org/yakindu/sct/generator/core/impl/WorkspaceClassLoaderFactory.java

@@ -36,6 +36,21 @@ import com.google.common.collect.Lists;
  */
 public class WorkspaceClassLoaderFactory {
 
+	private final boolean resolveSimpleProjectReferences;
+
+	public WorkspaceClassLoaderFactory() {
+		this(false);
+	}
+
+	/**
+	 * @param resolveSimpleProjectReferences
+	 *            whether project references should be resolved for non-java
+	 *            projects
+	 */
+	public WorkspaceClassLoaderFactory(boolean resolveSimpleProjectReferences) {
+		this.resolveSimpleProjectReferences = resolveSimpleProjectReferences;
+	}
+
 	/**
 	 * Creates a {@link ClassLoader} that can be used to load resources from the
 	 * workspace.
@@ -59,7 +74,9 @@ public class WorkspaceClassLoaderFactory {
 			} catch (MalformedURLException e1) {
 				e1.printStackTrace();
 			}
-			addReferencedProjectsClasspaths(project, urls);
+			if (resolveSimpleProjectReferences) {
+				addReferencedProjectsClasspaths(project, urls);
+			}
 		}
 	}