Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3566d4242efc99f4d70acd713c12a6a919ede978 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*****************************************************************************
 * Copyright (c) 2014, 2015, 2018 Christian W. Damus 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:
 *   Christian W. Damus - Initial API and implementation
 *   Ansgar Radermacher - Bug 526155, re-generation from profile: use ID as XML-ID
 *   
 *****************************************************************************/

package org.eclipse.papyrus.uml.profile.types.generator;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
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.XMLResource;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.types.ElementTypeConfiguration;
import org.eclipse.papyrus.infra.types.ElementTypeSetConfiguration;
import org.eclipse.papyrus.uml.profile.types.generator.internal.Activator;
import org.eclipse.uml2.common.util.UML2Util;

import com.google.common.base.Strings;
import com.google.common.collect.Sets;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;

/**
 * Scaffolding for an Xtend model-to-model transformation.
 * 
 * @param <T>
 *            the kind of model element that I generate from a UML profile
 */
public abstract class AbstractGenerator<I extends EObject, O extends EObject> {

	private final Injector injector;

	@Inject
	@InputModel
	private EClass inputType;

	@Inject
	@OutputModel
	private EClass outputType;

	@Inject
	private Identifiers identifiers;

	public AbstractGenerator(Identifiers identifiers) {
		this(new GeneratorModule(identifiers));
	}

	public AbstractGenerator(GeneratorModule module) {
		super();

		this.injector = Guice.createInjector(module);
		injector.injectMembers(this);
	}

	public String getLabel() {
		String result = identifiers.getLabel(outputType);

		return Strings.isNullOrEmpty(result) ? "output model" : result;
	}

	public IStatus generate(URI inputURI, URI outputURI) {
		IStatus result;
		ResourceSet resourceSet = new ResourceSetImpl();

		try {
			I input = UML2Util.load(resourceSet, inputURI, inputType);
			result = generate(input, outputURI);
		} finally {
			EMFHelper.unload(resourceSet);
		}

		return result;
	}

	public IStatus generate(I input, URI outputURI) {
		IStatus result;
		ResourceSet resourceSet = new ResourceSetImpl();

		try {
			Resource output = resourceSet.createResource(outputURI);

			result = generate(input, output.getContents());

			// use Identifier as XML-ID. This implies that the same XML-ID is used when re-generating
			EObject set = output.getContents().size() > 0 ? output.getContents().get(0) : null;
			if (set instanceof ElementTypeSetConfiguration) {
				for (ElementTypeConfiguration elemTypeConfig : ((ElementTypeSetConfiguration) set).getElementTypeConfigurations()) {
					String id = elemTypeConfig.getIdentifier();
					if (id != null && id.length() > 0) {
						((XMLResource) output).setID(elemTypeConfig, escapeID(id));
					}
				}
			}

			try {
				output.save(null);
			} catch (IOException e) {
				throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Failed to save output", e));//$NON-NLS-1$
			}
			refreshContainer(outputURI);
		} catch (CoreException e) {
			result = e.getStatus();
		} finally {
			EMFHelper.unload(resourceSet);
		}

		return result;
	}

	/**
	 * Replace problematic characters in identifier with "_", before using it as XML-id
 	 * in particular, the :: can be used by the generator.
 	 * 
	 * @param id an ID
	 * @return
	 */
	public String escapeID(String id) {
		id = id.replaceAll(" ", "_");
		id = id.replaceAll(":", "_");
		return id;
	}

	public IStatus generate(I input, EList<? super EObject> output) {
		IStatus result = Status.OK_STATUS;

		output.add(generate(input));

		return result;
	}

	protected abstract O generate(I input);

	protected void refreshContainer(URI resourceURI) throws CoreException {
		if (resourceURI.isPlatformResource()) {
			IContainer container = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(resourceURI.toPlatformString(true))).getParent();
			if ((container != null) && container.isAccessible()) {
				container.refreshLocal(IResource.DEPTH_ONE, null);
			}
		} else if (resourceURI.isFile()) {
			try {
				IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(new java.net.URI(resourceURI.toString()));
				Set<IContainer> containers = Sets.newHashSet();
				for (IFile next : files) {
					containers.add(next.getParent());
				}

				for (IContainer next : containers) {
					next.refreshLocal(IResource.DEPTH_ONE, null);
				}
			} catch (URISyntaxException e) {
				throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Bad resource URI", e));//$NON-NLS-1$
			}
		}
	}
}

Back to the top