Skip to main content
summaryrefslogtreecommitdiffstats
blob: 412c087a4ae4f249f6b7103c691c3001af4d3ae1 (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
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare 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 openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.xtend.typesystem.uml2;

import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.common.util.URI;
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.mwe.core.WorkflowComponentHelper;
import org.eclipse.emf.mwe.core.WorkflowContext;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
import org.eclipse.uml2.common.util.UML2Util;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;

/**
 * This workflow component writes UML2 models with their profiles to a directory.
 * 
 * <h2>Properties</h2>
 * <ul>
 * <li>inputSlot (required): Slot name containing the UML model.
 * <li>outPath: Output path (within destinationFolder)
 * <li>destinationFolder: Base path (default: './')
 * <li>useUML1x: Write .uml2 files (extension used by UML2 v1.x). Default: false.
 * </ul> 
 * @author Benedikt Niehues (Initial implementation)
 * @author Karsten Thoms (docs, maintainance)
 * @since 4.2
 */
public class UML2Writer extends AbstractWorkflowComponent {
	private static final Log LOG = LogFactory.getLog(UML2Writer.class);

	private String outPath;

	private String destinationFolder = "./";

	private String inputSlot = WorkflowContext.DEFAULT_SLOT;
	
	private boolean useUML1x = false;

	public String getDestinationFolder() {
		return destinationFolder;
	}

	public void setDestinationFolder(final String destinationFolder) {
		this.destinationFolder = destinationFolder;
		if (!this.destinationFolder.equals("")) {
			if (!this.destinationFolder.endsWith("/")) {
				this.destinationFolder += "/";
			}
		} else {
			this.destinationFolder = "./";
		}
	}

	public void setInputSlot(final String p) {
		this.inputSlot = p;
	}

	public void setOutPath(final String p) {
		this.outPath = p;
		if (!this.outPath.endsWith("/")) {
			this.outPath += "/";
		}
		if (this.outPath.startsWith("/")) {
			this.outPath = this.outPath.substring(1);
		}
	}
	
	public void setUseUML1x(boolean useUML1x) {
		this.useUML1x = useUML1x;
	}

	public void checkConfiguration(final Issues issues) {
		if (!WorkflowComponentHelper.isParamSet(inputSlot)) {
			issues.addError("Parameter 'inputSlot' must be set");
		}
		if (!WorkflowComponentHelper.isLegalDir(destinationFolder)) {
			issues.addError("Property 'destinationFolder' does not specify a legal directory");
		}
	}

	protected void invokeInternal(final WorkflowContext ctx,
			final ProgressMonitor monitor, final Issues issues) {

		LOG.info("running UML2Writer");
		final Object slotContent = ctx.get(inputSlot);
		if (slotContent == null) {
			issues.addError(this, "slot '" + inputSlot + "' is empty.");
			return;
		}
		if (!(slotContent instanceof Collection<?>
				|| slotContent instanceof org.eclipse.uml2.uml.Package)) {
			issues.addError(this, "slot '" + inputSlot
					+ "' does not contain a list, a model or a profile",
					slotContent);
			return;
		}
		ResourceSetImpl rsImpl = new ResourceSetImpl();
		//this is for Rational Software Modeler
		Resource.Factory factory = (Resource.Factory) Resource.Factory.Registry.INSTANCE
				.getExtensionToFactoryMap().get(getUMLFileExtension());
		rsImpl.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
				"emx", factory);
		rsImpl.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
				"epx", factory);

		if (slotContent instanceof Collection<?>) {
			Iterator it = ((Collection) slotContent).iterator();
			while (it.hasNext()) {
				Object o = it.next();
				if (o instanceof Package) {
					Package model = (Package) o;
					saveModel(model, rsImpl);
				}
			}
		} else if (slotContent instanceof Package){
			saveModel((Package)slotContent, rsImpl);
		}
	}

	private void saveModel(Package model, ResourceSet rsImpl) {
		// compute file name, e.g. './src-gen/myModel.uml"
		String modelFile = destinationFolder + outPath + model.getName() + "."+getUMLFileExtension();
		
		// create a resource with the model as contents
		final URI fileURI = URI.createFileURI(modelFile);
		Resource r = rsImpl.createResource(fileURI);
		r.getContents().add(model);
		// copy the contents
		for (Iterator allContents = UML2Util.getAllContents(model, true, false); allContents.hasNext();) {
			EObject eObject = (EObject) allContents.next();
			if (eObject instanceof Element) {
				r.getContents().addAll(((Element) eObject).getStereotypeApplications());
			}
		}
		try {
			r.save(null);
		} catch (IOException e) {
			LOG.debug(e.getMessage());
		}
	}
	
	/**
	 * Returns the proper UML file extension.
	 * @return "uml2" for UML2 v1.x, "uml" for UML2 v2.x
	 */
	private String getUMLFileExtension () {
		return useUML1x ? "uml2" : "uml";
	}

}

Back to the top