diff --git a/maven/org.eclipse.acceleo.maven/pom.xml b/maven/org.eclipse.acceleo.maven/pom.xml index f38caabd9..67c2f1234 100644 --- a/maven/org.eclipse.acceleo.maven/pom.xml +++ b/maven/org.eclipse.acceleo.maven/pom.xml @@ -22,6 +22,11 @@ parser 3.2.1 + + org.eclipse.acceleo + model + 3.2.1 + org.eclipse.acceleo common diff --git a/maven/org.eclipse.acceleo.maven/src/main/java/org/eclipse/acceleo/maven/AcceleoParserMojo.java b/maven/org.eclipse.acceleo.maven/src/main/java/org/eclipse/acceleo/maven/AcceleoParserMojo.java index 4d060d09a..a7a16af9a 100644 --- a/maven/org.eclipse.acceleo.maven/src/main/java/org/eclipse/acceleo/maven/AcceleoParserMojo.java +++ b/maven/org.eclipse.acceleo.maven/src/main/java/org/eclipse/acceleo/maven/AcceleoParserMojo.java @@ -15,15 +15,23 @@ import java.io.File; import java.lang.reflect.Field; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLClassLoader; import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; +import java.util.StringTokenizer; +import org.apache.maven.artifact.Artifact; +import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.model.Dependency; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugin.logging.Log; +import org.apache.maven.project.MavenProject; import org.eclipse.acceleo.common.internal.utils.AcceleoPackageRegistry; import org.eclipse.acceleo.internal.parser.compiler.AcceleoParser; import org.eclipse.acceleo.internal.parser.compiler.AcceleoProjectClasspathEntry; @@ -38,11 +46,19 @@ * * @goal acceleo-compile * @phase compile + * @requiresDependencyResolution runtime * @author Stephane Begaudeau * @since 3.2 */ public class AcceleoParserMojo extends AbstractMojo { + /** + * @parameter expression="${project}" + * @required + * @readonly + */ + private MavenProject project; + /** * Indicates if we are compiling the Acceleo modules as binary resources. * @@ -74,39 +90,49 @@ public class AcceleoParserMojo extends AbstractMojo { */ public void execute() throws MojoExecutionException, MojoFailureException { Log log = getLog(); - log.info("ACCELEO MAVEN STAND ALONE BUILD"); + log.info("Acceleo maven stand alone build..."); - log.info("Registering packages..."); + log.info("Starting packages registration..."); for (String packageToRegister : this.packagesToRegister) { try { - Class forName = Class.forName(packageToRegister); + List runtimeClasspathElements = project.getRuntimeClasspathElements(); + URL[] runtimeUrls = new URL[runtimeClasspathElements.size()]; + for (int i = 0; i < runtimeClasspathElements.size(); i++) { + String element = (String)runtimeClasspathElements.get(i); + runtimeUrls[i] = new File(element).toURI().toURL(); + } + URLClassLoader newLoader = new URLClassLoader(runtimeUrls, Thread.currentThread() + .getContextClassLoader()); + + Class forName = Class.forName(packageToRegister, true, newLoader); Field nsUri = forName.getField("eNS_URI"); Field eInstance = forName.getField("eINSTANCE"); - Object newInstance = forName.newInstance(); - Object nsURIInvoked = nsUri.get(newInstance); + Object nsURIInvoked = nsUri.get(null); if (nsURIInvoked instanceof String) { log.info("Registering package '" + packageToRegister + "'."); - AcceleoPackageRegistry.INSTANCE.put((String)nsURIInvoked, eInstance.get(newInstance)); + AcceleoPackageRegistry.INSTANCE.put((String)nsURIInvoked, eInstance.get(null)); } else { log.error("The URI field is not a string."); } } catch (ClassNotFoundException e) { log.error(e); - } catch (InstantiationException e) { - log.error(e); } catch (IllegalAccessException e) { log.error(e); } catch (SecurityException e) { log.error(e); } catch (NoSuchFieldException e) { log.error(e); + } catch (DependencyResolutionRequiredException e) { + log.error(e); + } catch (MalformedURLException e) { + log.error(e); } } - log.info("Starting the build sequence..."); + log.info("Starting the build sequence for the project '" + this.acceleoProject.getRoot() + "'..."); log.info("Mapping the pom.xml to AcceleoProject..."); Preconditions.checkNotNull(this.acceleoProject); @@ -123,6 +149,10 @@ public void execute() throws MojoExecutionException, MojoFailureException { for (Entry entry : entries) { File inputDirectory = new File(root, entry.getInput()); File outputDirectory = new File(root, entry.getOutput()); + + log.debug("Input: " + inputDirectory.getAbsolutePath()); + log.debug("Output: " + outputDirectory.getAbsolutePath()); + AcceleoProjectClasspathEntry classpathEntry = new AcceleoProjectClasspathEntry(inputDirectory, outputDirectory); classpathEntries.add(classpathEntry); @@ -154,12 +184,92 @@ public void execute() throws MojoExecutionException, MojoFailureException { } log.info("Adding jar dependencies..."); - List jars = this.acceleoProject.getJars(); + List jars = this.acceleoProject.getJars(); if (jars != null) { Set newDependencies = new LinkedHashSet(); - for (File jar : jars) { - URI uri = URI.createFileURI(jar.getAbsolutePath()); - newDependencies.add(uri); + for (String jar : jars) { + log.info("Resolving jar: '" + jar + "'..."); + File jarFile = new File(jar); + if (jarFile.isFile()) { + URI uri = URI.createFileURI(jar); + newDependencies.add(uri); + log.info("Found jar for '" + jar + "' on the filesystem: '" + jarFile.getAbsolutePath() + + "'."); + } else { + StringTokenizer tok = new StringTokenizer(jar, ":"); + + String groupdId = null; + String artifactId = null; + String version = null; + + int c = 0; + while (tok.hasMoreTokens()) { + String nextToken = tok.nextToken(); + if (c == 0) { + groupdId = nextToken; + } else if (c == 1) { + artifactId = nextToken; + } else if (c == 2) { + version = nextToken; + } + + c++; + } + + Set artifacts = this.project.getArtifacts(); + for (Object object : artifacts) { + if (object instanceof Artifact) { + Artifact artifact = (Artifact)object; + if (groupdId != null && groupdId.equals(artifact.getGroupId()) + && artifactId != null && artifactId.equals(artifact.getArtifactId())) { + if (version != null && version.equals(artifact.getVersion())) { + File artifactFile = artifact.getFile(); + if (artifactFile != null && artifactFile.exists()) { + URI uri = URI.createFileURI(artifactFile.getAbsolutePath()); + newDependencies.add(uri); + log.info("Found jar for '" + jar + "' on the filesystem: '" + + uri.toString() + "'."); + } + } else if (version == null) { + File artifactFile = artifact.getFile(); + if (artifactFile != null && artifactFile.exists()) { + URI uri = URI.createFileURI(artifactFile.getAbsolutePath()); + newDependencies.add(uri); + log.info("Found jar for '" + jar + "' on the filesystem: '" + + uri.toString() + "'."); + } + } + } + } + } + + List mavenDependencies = this.project.getDependencies(); + for (Object object : mavenDependencies) { + if (object instanceof Dependency) { + Dependency dependency = (Dependency)object; + if (groupdId != null && groupdId.equals(dependency.getGroupId()) + && artifactId != null && artifactId.equals(dependency.getArtifactId())) { + if (version != null && version.equals(dependency.getVersion())) { + String systemPath = dependency.getSystemPath(); + if (systemPath != null && new File(systemPath).exists()) { + URI uri = URI.createFileURI(systemPath); + newDependencies.add(uri); + log.info("Found jar for '" + jar + "' on the filesystem: '" + + uri.toString() + "'."); + } + } else if (version == null) { + String systemPath = dependency.getSystemPath(); + if (systemPath != null && new File(systemPath).exists()) { + URI uri = URI.createFileURI(systemPath); + newDependencies.add(uri); + log.info("Found jar for '" + jar + "' on the filesystem: '" + + uri.toString() + "'."); + } + } + } + } + } + } } aProject.addDependencies(newDependencies); } diff --git a/maven/org.eclipse.acceleo.maven/src/main/java/org/eclipse/acceleo/maven/AcceleoProject.java b/maven/org.eclipse.acceleo.maven/src/main/java/org/eclipse/acceleo/maven/AcceleoProject.java index d496f7686..629b0e45e 100644 --- a/maven/org.eclipse.acceleo.maven/src/main/java/org/eclipse/acceleo/maven/AcceleoProject.java +++ b/maven/org.eclipse.acceleo.maven/src/main/java/org/eclipse/acceleo/maven/AcceleoProject.java @@ -34,7 +34,7 @@ public class AcceleoProject { /** * The jars. */ - private List jars; + private List jars; /** * The dependencies. @@ -64,7 +64,7 @@ public List getDependencies() { * * @return The jar dependencies. */ - public List getJars() { + public List getJars() { return jars; } diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/.classpath b/tests/org.eclipse.acceleo.maven.plugin.tests/.classpath new file mode 100644 index 000000000..e177039ad --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/.gitignore b/tests/org.eclipse.acceleo.maven.plugin.tests/.gitignore new file mode 100644 index 000000000..9f970225a --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/.project b/tests/org.eclipse.acceleo.maven.plugin.tests/.project new file mode 100644 index 000000000..922a8f720 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/.project @@ -0,0 +1,34 @@ + + + org.eclipse.acceleo.maven.plugin.tests + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.pde.ManifestBuilder + + + + + org.eclipse.pde.SchemaBuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.pde.PluginNature + org.eclipse.jdt.core.javanature + + diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/.settings/org.eclipse.jdt.core.prefs b/tests/org.eclipse.acceleo.maven.plugin.tests/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..43d1ad47e --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,9 @@ +#Mon Jan 16 16:02:24 CET 2012 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/.settings/org.eclipse.m2e.core.prefs b/tests/org.eclipse.acceleo.maven.plugin.tests/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 000000000..94f44114b --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,5 @@ +#Mon Jan 16 16:02:24 CET 2012 +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/META-INF/MANIFEST.MF b/tests/org.eclipse.acceleo.maven.plugin.tests/META-INF/MANIFEST.MF new file mode 100644 index 000000000..221988e74 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/META-INF/MANIFEST.MF @@ -0,0 +1,7 @@ +Manifest-Version: 1.0 +Bundle-ManifestVersion: 2 +Bundle-Name: Tests +Bundle-SymbolicName: org.eclipse.acceleo.maven.plugin.tests +Bundle-Version: 1.0.0.qualifier +Bundle-RequiredExecutionEnvironment: JavaSE-1.6 +Require-Bundle: org.obeonetwork.pim.uml2.gen.java;bundle-version="1.0.0" diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/build.properties b/tests/org.eclipse.acceleo.maven.plugin.tests/build.properties new file mode 100644 index 000000000..076dffca6 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/build.properties @@ -0,0 +1,5 @@ +source.. = src/main/java/ +output.. = target/classes/ +bin.includes = META-INF/,\ + . +jre.compilation.profile = J2SE-1.5 diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/lib/myFile.jar b/tests/org.eclipse.acceleo.maven.plugin.tests/lib/myFile.jar new file mode 100644 index 000000000..e69de29bb diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/pom.xml b/tests/org.eclipse.acceleo.maven.plugin.tests/pom.xml new file mode 100644 index 000000000..2f16afc24 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/pom.xml @@ -0,0 +1,178 @@ + + 4.0.0 + org.eclipse.acceleo + maven.plugin.tests + 1.0.0.qualifier + eclipse-plugin + + + 0.12.0 + + + + + platform-indigo + + true + + platform-version-name + indigo + + + + http://marketplace.obeonetwork.com/updates/od5/ + http://download.eclipse.org/releases/indigo + [3.7,3.8) + + + + + + + eclipse-platform + p2 + ${eclipse-site} + + + uml-to-java + p2 + ${uml-to-java-site} + + + github snapshot + https://github.com/sbegaudeau/maven-repository/raw/master/snapshots + + + github release + https://github.com/sbegaudeau/maven-repository/raw/master/releases + + + + + + + codehaus.snapshots + http://snapshots.repository.codehaus.org/ + + + + + + org.eclipse.acceleo + maven + 3.2.1-SNAPSHOT + + + org.eclipse.acceleo + engine + 3.2.1 + + + org.eclipse.uml2 + uml + 3.2.1 + + + org.eclipse.uml2 + common + 1.6.0 + + + org.slf4j + slf4j-simple + 1.6.4 + + + + + + + org.eclipse.tycho + tycho-maven-plugin + ${tycho-version} + true + + + org.eclipse.tycho + target-platform-configuration + ${tycho-version} + + p2 + consider + + + + org.eclipse.acceleo + maven + 3.2.1-SNAPSHOT + + + compile + + + + false + + ${project.basedir} + + + src/main/java + target/classes + + + + p2.eclipse-plugin:org.obeonetwork.pim.uml2.gen.java:1.0.0.201112151044 + org.eclipse.acceleo:engine:3.2.1 + org.eclipse.acceleo:common + C:\Users\sbegaudeau\.m2\repository\com\google\collections\google-collections\1.0\google-collections-1.0.jar + ${project.basedir}\lib\guava-11.0.1.jar + + + + org.eclipse.uml2.uml.UMLPackage + + + + + + + + org.eclipse.tycho + tycho-compiler-plugin + ${tycho-version} + + UTF-8 + + + + + org.eclipse.tycho + tycho-source-plugin + ${tycho-version} + + + attach-source + process-classes + + plugin-source + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.4.1 + + ISO-8859-1 + + + + org.apache.maven.plugins + maven-antrun-plugin + 1.3 + + + + + \ No newline at end of file diff --git a/tests/org.eclipse.acceleo.maven.plugin.tests/src/main/java/org/eclipse/acceleo/maven/plugin/tests/myModule.mtl b/tests/org.eclipse.acceleo.maven.plugin.tests/src/main/java/org/eclipse/acceleo/maven/plugin/tests/myModule.mtl new file mode 100644 index 000000000..6d71077aa --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.plugin.tests/src/main/java/org/eclipse/acceleo/maven/plugin/tests/myModule.mtl @@ -0,0 +1,16 @@ +[comment] + Copyright 2008, 2010 Obeo + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License 1.0 + + Any license can be applied to the files generated with this template. + + author Stephane Bouchet +[/comment] +[module myModule('http://www.eclipse.org/uml2/3.0.0/UML')/] + +[import org::obeonetwork::pim::uml2::gen::java::common::common /] + +[template public packagePath(cl : Classifier)] +[cl.genJavadoc()/] +[/template] diff --git a/tests/org.eclipse.acceleo.maven.tests/.classpath b/tests/org.eclipse.acceleo.maven.tests/.classpath new file mode 100644 index 000000000..966e22987 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/tests/org.eclipse.acceleo.maven.tests/.gitignore b/tests/org.eclipse.acceleo.maven.tests/.gitignore new file mode 100644 index 000000000..9f970225a --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/.gitignore @@ -0,0 +1 @@ +target/ \ No newline at end of file diff --git a/tests/org.eclipse.acceleo.maven.tests/.project b/tests/org.eclipse.acceleo.maven.tests/.project new file mode 100644 index 000000000..d9fa38e0c --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/.project @@ -0,0 +1,23 @@ + + + org.eclipse.acceleo.maven.tests + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/tests/org.eclipse.acceleo.maven.tests/.settings/org.eclipse.jdt.core.prefs b/tests/org.eclipse.acceleo.maven.tests/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 000000000..8a6308521 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,6 @@ +#Mon Jan 16 14:33:31 CET 2012 +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/tests/org.eclipse.acceleo.maven.tests/.settings/org.eclipse.m2e.core.prefs b/tests/org.eclipse.acceleo.maven.tests/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 000000000..1a813a067 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,5 @@ +#Mon Jan 16 14:33:30 CET 2012 +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/tests/org.eclipse.acceleo.maven.tests/build.acceleo b/tests/org.eclipse.acceleo.maven.tests/build.acceleo new file mode 100644 index 000000000..fb14dcfe5 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/build.acceleo @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/org.eclipse.acceleo.maven.tests/build.properties b/tests/org.eclipse.acceleo.maven.tests/build.properties new file mode 100644 index 000000000..bf974327c --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/build.properties @@ -0,0 +1,17 @@ +################################################################################ +# Copyright (c) 2008, 2010 Obeo. +# 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: +# Obeo - initial API and implementation +################################################################################ +source.. = src/ +output.. = bin/ +bin.includes = META-INF/,\ + . +customBuildCallbacks = build.acceleo + + diff --git a/tests/org.eclipse.acceleo.maven.tests/model/example.uml b/tests/org.eclipse.acceleo.maven.tests/model/example.uml new file mode 100644 index 000000000..b95d61cd6 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/model/example.uml @@ -0,0 +1,69 @@ + + + + + + + + + + + + + + Calculate the age from birthdate to now. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/org.eclipse.acceleo.maven.tests/pom.xml b/tests/org.eclipse.acceleo.maven.tests/pom.xml new file mode 100644 index 000000000..d7b743f52 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/pom.xml @@ -0,0 +1,75 @@ + + 4.0.0 + org.eclipse.acceleo + maven-test + 3.2.1-SNAPSHOT + jar + org.eclipse.acceleo.maven.tests + The Acceleo maven tests plugin. + + + org.eclipse.acceleo + maven + 3.2.1-SNAPSHOT + + + org.eclipse.acceleo + engine + 3.2.1 + + + org.eclipse.uml2 + uml + 3.2.1 + + + org.eclipse.uml2 + common + 1.6.0 + + + org.slf4j + slf4j-simple + 1.6.4 + + + + + github snapshot + https://github.com/sbegaudeau/maven-repository/raw/master/snapshots + + + github release + https://github.com/sbegaudeau/maven-repository/raw/master/releases + + + + + + org.eclipse.acceleo + maven + 3.2.1-SNAPSHOT + + + compile + + + + false + + ${project.basedir} + + + src/main/java + target/classes + + + + + org.eclipse.uml2.uml.UMLPackage + + + + + + diff --git a/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/GenerateJava.java b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/GenerateJava.java new file mode 100644 index 000000000..dd7f3dc08 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/GenerateJava.java @@ -0,0 +1,353 @@ +/******************************************************************************* + * Copyright (c) 2008, 2010 Obeo. + * 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: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.acceleo.module.example.uml2java.helios; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.acceleo.engine.event.IAcceleoTextGenerationListener; +import org.eclipse.acceleo.engine.generation.strategy.DefaultStrategy; +import org.eclipse.acceleo.engine.generation.strategy.IAcceleoGenerationStrategy; +import org.eclipse.acceleo.engine.service.AbstractAcceleoGenerator; +import org.eclipse.emf.common.util.BasicMonitor; +import org.eclipse.emf.common.util.Monitor; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.uml2.uml.resource.UMLResource; + +/** + * Entry point of the 'GenerateJava' generation module. + * + * @generated + */ +public class GenerateJava extends AbstractAcceleoGenerator { + + /** + * The name of the module. + * + * @generated + */ + public static final String MODULE_FILE_NAME = "/org/eclipse/acceleo/module/example/uml2java/helios/generateJava"; + + /** + * The name of the templates that are to be generated. + * + * @generated + */ + public static final String[] TEMPLATE_NAMES = { "generateClass", "generateInterface" }; + + /** + * The list of properties files from the launch parameters (Launch configuration). + * + * @generated + */ + private List propertiesFiles = new ArrayList(); + + /** + * Allows the public constructor to be used. Note that a generator created this way cannot be used to + * launch generations before one of {@link #initialize(EObject, File, List)} or + * {@link #initialize(URI, File, List)} is called. + *

+ * The main reason for this constructor is to allow clients of this generation to call it from another + * Java file, as it allows for the retrieval of {@link #getProperties()} and + * {@link #getGenerationListeners()}. + *

+ * + * @generated + */ + public GenerateJava() { + // Empty implementation + } + + /** + * Constructor. + * + * @param modelURI + * is the URI of the model. + * @param targetFolder + * is the output folder + * @param arguments + * are the other arguments + * @throws IOException + * Thrown when the output cannot be saved. + * @generated + */ + public GenerateJava(URI modelURI, File targetFolder, List arguments) throws IOException { + initialize(modelURI, targetFolder, arguments); + } + + /** + * Constructor. + * + * @param model + * is the root element of the model. + * @param targetFolder + * is the output folder + * @param arguments + * are the other arguments + * @throws IOException + * Thrown when the output cannot be saved. + * @generated + */ + public GenerateJava(EObject model, File targetFolder, List arguments) + throws IOException { + initialize(model, targetFolder, arguments); + } + + /** + * Updates the registry used for looking up a package based namespace, in the resource set. + * + * @param resourceSet + * is the resource set + * @generated + */ + @Override + public void registerPackages(ResourceSet resourceSet) { + super.registerPackages(resourceSet); + if (!isInWorkspace(org.eclipse.uml2.uml.UMLPackage.class)) { + resourceSet.getPackageRegistry().put(org.eclipse.uml2.uml.UMLPackage.eINSTANCE.getNsURI(), org.eclipse.uml2.uml.UMLPackage.eINSTANCE); + } + + /* + * TODO If you need additional package registrations, you can register them here. The following line + * (in comment) is an example of the package registration for UML. If you want to change the content + * of this method, do NOT forget to change the "@generated" tag in the Javadoc of this method to + * "@generated NOT". Without this new tag, any compilation of the Acceleo module with the main template + * that has caused the creation of this class will revert your modifications. You can use the method + * "isInWorkspace(Class c)" to check if the package that you are about to register is in the workspace. + * To register a package properly, please follow the following conventions: + * + * if (!isInWorkspace(UMLPackage.class)) { + * // The normal package registration if your metamodel is in a plugin. + * resourceSet.getPackageRegistry().put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE); + * } else { + * // The package registration that will be used if the metamodel is not deployed in a plugin. + * // This should be used if your metamodel is in your workspace and if you are using binary resource serialization. + * resourceSet.getPackageRegistry().put("/myproject/myfolder/mysubfolder/MyUMLMetamodel.ecore", UMLPackage.eINSTANCE); + * } + * + * To learn more about Package Registration, have a look at the Acceleo Launcher documentation (Help -> Help Contents). + */ + } + + /** + * Updates the registry used for looking up resources factory in the given resource set. + * + * @param resourceSet + * The resource set that is to be updated. + * @generated NOT + */ + @Override + public void registerResourceFactories(ResourceSet resourceSet) { + super.registerResourceFactories(resourceSet); + /* + * TODO If you need additional resource factories registrations, you can register them here. the following line + * (in comment) is an example of the resource factory registration for UML. If you want to change the content + * of this method, do NOT forget to change the "@generated" tag in the Javadoc of this method to "@generated NOT". + * Without this new tag, any compilation of the Acceleo module with the main template that has caused the creation + * of this class will revert your modifications. + * + * To learn more about the registration of Resource Factories, have a look at the Acceleo Launcher documentation (Help -> Help Contents). + */ + + resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(UMLResource.FILE_EXTENSION, UMLResource.Factory.INSTANCE); + } + + /** + * The main method. + * + * @param args + * are the arguments + * @generated + */ + public static void main(String[] args) { + try { + if (args.length < 2) { + System.out.println("Arguments not valid : {model, folder}."); + } else { + URI modelURI = URI.createFileURI(args[0]); + File folder = new File(args[1]); + + List arguments = new ArrayList(); + + /* + * Add in this list all the arguments used by the starting point of the generation + * If your main template is called on an element of your model and a String, you can + * add in "arguments" this "String" attribute. + */ + + GenerateJava generator = new GenerateJava(modelURI, folder, arguments); + + /* + * Add the properties from the launch arguments. + * If you want to programmatically add new properties, add them in "propertiesFiles" + * You can add the absolute path of a properties files, or even a project relative path. + * If you want to add another "protocol" for your properties files, please override + * "getPropertiesLoaderService(AcceleoService)" in order to return a new property loader. + * The behavior of the properties loader service is explained in the Acceleo documentation + * (Help -> Help Contents). + */ + + for (int i = 2; i < args.length; i++) { + generator.addPropertiesFile(args[i]); + } + + generator.doGenerate(new BasicMonitor()); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Launches the generation. + * + * @param monitor + * This will be used to display progress information to the user. + * @throws IOException + * Thrown when the output cannot be saved. + * @generated + */ + @Override + public void doGenerate(Monitor monitor) throws IOException { + /* + * TODO if you wish to change the generation as a whole, override this. The default behavior should + * be sufficient in most cases. If you want to change the content of this method, do NOT forget to + * change the "@generated" tag in the Javadoc of this method to "@generated NOT". Without this new tag, + * any compilation of the Acceleo module with the main template that has caused the creation of this + * class will revert your modifications. If you encounter a problem with an unresolved proxy during the + * generation, you can remove the comments in the following instructions to check for problems. Please + * note that those instructions may have a significant impact on the performances. + */ + + //org.eclipse.emf.ecore.util.EcoreUtil.resolveAll(model); + + //if (model != null && model.eResource() != null) { + // List errors = model.eResource().getErrors(); + // for (org.eclipse.emf.ecore.resource.Resource.Diagnostic diagnostic : errors) { + // System.err.println(diagnostic.toString()); + // } + //} + + super.doGenerate(monitor); + } + + /** + * If this generator needs to listen to text generation events, listeners can be returned from here. + * + * @return List of listeners that are to be notified when text is generated through this launch. + * @generated + */ + @Override + public List getGenerationListeners() { + List listeners = super.getGenerationListeners(); + /* + * TODO if you need to listen to generation event, add listeners to the list here. If you want to change + * the content of this method, do NOT forget to change the "@generated" tag in the Javadoc of this method + * to "@generated NOT". Without this new tag, any compilation of the Acceleo module with the main template + * that has caused the creation of this class will revert your modifications. + */ + return listeners; + } + + /** + * If you need to change the way files are generated, this is your entry point. + *

+ * The default is {@link org.eclipse.acceleo.engine.generation.strategy.DefaultStrategy}; it generates + * files on the fly. If you only need to preview the results, return a new + * {@link org.eclipse.acceleo.engine.generation.strategy.PreviewStrategy}. Both of these aren't aware of + * the running Eclipse and can be used standalone. + *

+ *

+ * If you need the file generation to be aware of the workspace (A typical example is when you wanna + * override files that are under clear case or any other VCS that could forbid the overriding), then + * return a new {@link org.eclipse.acceleo.engine.generation.strategy.WorkspaceAwareStrategy}. + * Note, however, that this cannot be used standalone. + *

+ *

+ * All three of these default strategies support merging through JMerge. + *

+ */ + public IAcceleoGenerationStrategy getGenerationStrategy() { + return new DefaultStrategy(); + } + + /** + * This will be called in order to find and load the module that will be launched through this launcher. + * We expect this name not to contain file extension, and the module to be located beside the launcher. + * + * @return The name of the module that is to be launched. + * @generated + */ + @Override + public String getModuleName() { + return MODULE_FILE_NAME; + } + + /** + * If the module(s) called by this launcher require properties files, return their qualified path from + * here.Take note that the first added properties files will take precedence over subsequent ones if they + * contain conflicting keys. + *

+ * Properties need to be in source folders, the path that we expect to get as a result of this call are of + * the form <package>.<properties file name without extension>. For example, if you have a file + * named "messages.properties" in package "org.eclipse.acceleo.sample", the path that needs be returned by + * a call to {@link #getProperties()} is "org.eclipse.acceleo.sample.messages". + *

+ * + * @return The list of properties file we need to add to the generation context. + * @see java.util.ResourceBundle#getBundle(String) + * @generated + */ + @Override + public List getProperties() { + /* + * TODO if your generation module requires access to properties files, add their qualified path to the list here. + * Properties files are expected to be in source folders, and the path here to be the qualified path as if referring + * to a Java class. For example, if you have a file named "messages.properties" in package "org.eclipse.acceleo.sample", + * the path that needs be added to this list is "/org/eclipse/acceleo/sample/messages.properties". If you want to change the + * contentof this method, do NOT forget to change the "@generated" tag in the Javadoc of this method to "@generated NOT". + * Without this new tag, any compilation of the Acceleo module with the main template that has caused the creation of + * this class will revert your modifications. + * + * To learn more about Properties Files, have a look at the Acceleo Launcher documentation (Help -> Help Contents). + */ + return propertiesFiles; + } + + /** + * Adds a properties file in the list of properties files. + * + * @param propertiesFile + * The properties file to add. + * @generated + * @since 3.1 + */ + @Override + public void addPropertiesFile(String propertiesFile) { + this.propertiesFiles.add(propertiesFile); + } + + /** + * This will be used to get the list of templates that are to be launched by this launcher. + * + * @return The list of templates to call on the module {@link #getModuleName()}. + * @generated + */ + @Override + public String[] getTemplateNames() { + return TEMPLATE_NAMES; + } + +} diff --git a/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/classBody.mtl b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/classBody.mtl new file mode 100644 index 000000000..dce827a57 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/classBody.mtl @@ -0,0 +1,89 @@ +[comment] + Copyright 2008, 2010 Obeo + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License 1.0 + + Any license can be applied to the files generated with this template. + + author Stephane Bouchet +[/comment] +[module classBody('http://www.eclipse.org/uml2/3.0.0/UML')/] +[import org::eclipse::acceleo::module::example::uml2java::helios::common::common/] + +[template public generateClassBody(c : Class)] +public[if (c.isAbstract)] abstract[/if] class [c.name.toUpperFirst()/][for (superC : Class | c.superClass) before(' extends ') separator(',')] [superC.name/][/for][for (interf : Interface | c.getImplementedInterfaces()) before(' implements ') separator(',')] [interf.name/][/for] { +[for (p : Property | c.getAllAttributes())] +[if (p.upper = -1 or p.upper > 1)] + /** + * the [p.name/] attribute. + */ + private List<[p.type.name/]> [p.name/]; +[else] + /** + * the [p.name/] attribute. + */ + private [p.type.name/] [p.name/]; +[/if] +[/for] +[for (p : Property | c.getAllAttributes())] + /** + * the [p.name/] getter. + * @return the [p.name/]. + */ + public [if (p.upper = -1 or p.upper > 1)]List<[p.type.name/]>[else][p.type.name/][/if] get[p.name.toUpperFirst()/]() { + return this.[p.name/]; + } + + /** + * the [p.name/] setter. + * @param p_[p.name/] the [p.name/] to set. + */ + public void set[p.name.toUpperFirst()/]([if (p.upper = -1 or p.upper > 1)]List<[p.type.name/]>[else][p.type.name/][/if] p_[p.name/]) { + this.[p.name/] = p_[p.name/]; + } +[/for] +[for (p : Property | c.getAssociations().memberEnd)] +[if (p.upper = -1 or p.upper > 1)] + /** + * the [p.name/] attribute. + */ + private List<[p.type.name/]> [p.name/]; +[else] + /** + * the [p.name/] attribute. + */ + private [p.type.name/] [p.name/]; +[/if] +[/for] +[for (o : Operation | c.getOperations())] +[o.operationBody()/] +[/for] +[for (interf : Interface | c.getImplementedInterfaces())] +[for (o : Operation | interf.ownedOperation)] +[o.operationBody()/] +[/for] +[/for] +} +[/template] + +[template private operationBody(o : Operation)] + /** + *[for (cmt : Comment | o.ownedComment)] [cmt._body/][/for] +[for (p : Parameter | o.ownedParameter) ? (p.direction <> ParameterDirectionKind::return)] +[paramOperationComment(p)/] +[/for] +[if (not o.type.oclIsUndefined())] +[returnOperationComment()/] +[/if] + */ + public [o.returnTypeOperation()/]([o.getInParameter()/]) { + // [protected ('for operation '.concat(o.name))] +[if (not o.type.oclIsUndefined())] + // TODO should be implemented + return null; +[else] + // TODO should be implemented +[/if] + // [/protected] + } +[/template] \ No newline at end of file diff --git a/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/common.mtl b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/common.mtl new file mode 100644 index 000000000..71095098d --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/common.mtl @@ -0,0 +1,64 @@ +[comment] + Copyright 2008, 2010 Obeo + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License 1.0 + + Any license can be applied to the files generated with this template. + + author Stephane Bouchet +[/comment] +[module common('http://www.eclipse.org/uml2/3.0.0/UML')/] + +[template public packagePath(cl : Classifier)] +[cl.getNearestPackage().normalizeName()/] +[/template] + +[template public getFullPathFile(cl : Classifier)] +[if (not cl.getNearestPackage().oclIsUndefined())] +[cl.getNearestPackage().normalizeName().substituteAll('.', '/').concat('/').concat(cl.name).concat('.java')/] +[else] +[cl.name.concat('.java')/] +[/if] +[/template] + +[template public getInParameter(o : Operation)] +[for (p : Parameter | o.ownedParameter) separator(',') ? (p.direction <> ParameterDirectionKind::return)][p.type.name/] [p.name/][/for] +[/template] + +[template public returnTypeOperation(o : Operation)] +[if (o.type.oclIsUndefined())]void [o.name/][else][o.type.name/] [o.name/][/if] +[/template] + +[template public importBlock(traceabilityContext : OclAny)] +// [protected ('for imports')] +import java.util.*; +// [/protected] +[/template] + +[template public packageBlock(cl : Classifier)] +[if (not cl.getNearestPackage().oclIsUndefined())]package [cl.packagePath()/];[/if] +[/template] + +[template private normalizeName(element : NamedElement)] +[if ((element.name = 'package') or (element.name = 'interface') or (element.name = 'class'))][element.name.concat('_')/][else][element.name/][/if] +[/template] + +[template public _commentFileBlock(traceabilityContext : OclAny)] +/** + * Generated with MTL UML 2 Java example + */ +[/template] + +[template public _commentBodyBlock(traceabilityContext : OclAny)] +/** + * @author MTL + */ +[/template] + +[template public returnOperationComment(traceabilityContext : OclAny)] + * @return +[/template] + +[template public paramOperationComment(p : Parameter)] + * @param [p.name/] +[/template] diff --git a/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/interfaceBody.mtl b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/interfaceBody.mtl new file mode 100644 index 000000000..71cc23a66 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/common/interfaceBody.mtl @@ -0,0 +1,29 @@ +[comment] + Copyright 2008, 2010 Obeo + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License 1.0 + + Any license can be applied to the files generated with this template. + + author Stephane Bouchet +[/comment] +[module interfaceBody('http://www.eclipse.org/uml2/3.0.0/UML')/] +[import org::eclipse::acceleo::module::example::uml2java::helios::common::common/] + +[template public generateInterfaceBody(i : Interface)] +public interface [i.name.toUpperFirst()/] { + +[for (o : Operation | i.ownedOperation)] + /** + *[for (cmt : Comment | o.ownedComment)] [cmt._body/][/for] +[for (p : Parameter | o.ownedParameter) ? (p.direction <> ParameterDirectionKind::return)] +[paramOperationComment(p)/] +[/for] +[if (not o.type.name.oclIsUndefined())] +[returnOperationComment()/] +[/if] + */ + public [o.returnTypeOperation()/]([o.getInParameter()/]); +[/for] +} +[/template] \ No newline at end of file diff --git a/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/generateJava.mtl b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/generateJava.mtl new file mode 100644 index 000000000..467bb7016 --- /dev/null +++ b/tests/org.eclipse.acceleo.maven.tests/src/main/java/org/eclipse/acceleo/module/example/uml2java/helios/generateJava.mtl @@ -0,0 +1,41 @@ +[comment] + Copyright 2008, 2010 Obeo + All rights reserved. This program and the accompanying materials + are made available under the terms of the Eclipse Public License 1.0 + + Any license can be applied to the files generated with this template. + + author Stephane Bouchet +[/comment] +[module generateJava('http://www.eclipse.org/uml2/3.0.0/UML')/] +[import org::eclipse::acceleo::module::example::uml2java::helios::common::common/] +[import org::eclipse::acceleo::module::example::uml2java::helios::common::interfaceBody/] +[import org::eclipse::acceleo::module::example::uml2java::helios::common::classBody/] + +[template public generateClass(c : Class)] +[comment @main /] +[file (c.getFullPathFile().trim(), false)] +[_commentFileBlock()/] +[c.packageBlock()/] + +[importBlock()/] + +[_commentBodyBlock()/] +[c.generateClassBody()/] +[/file] +[/template] + +[template public generateInterface(i : Interface)] +[comment @main /] +[file (i.getFullPathFile().trim(), false)] +[_commentFileBlock()/] +[i.packageBlock()/] + +[importBlock()/] + +[_commentBodyBlock()/] +[i.generateInterfaceBody()/] +[/file] +[/template] + +