Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 562f6cd4275c010cb5d6ac1db5a2582a0e649ab1 (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *    
 * 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:
 *  Ansgar Radermacher  ansgar.radermacher@cea.fr  
 *
 *****************************************************************************/

package org.eclipse.papyrus.qompass.designer.core.acceleo;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.papyrus.qompass.designer.core.transformations.TransformationContext;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Dependency;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Enumeration;
import org.eclipse.uml2.uml.NamedElement;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.UMLPackage;


/**
 * Manage enumerations within Acceleo code
 */
public class EnumService {

	private static final String GLOBALENUMS = "globalenums"; //$NON-NLS-1$

	public static void init() {
		enumHash.clear();
	}

	/**
	 * Return qualified name of enum package which is used to prefix enumerations (namespace)
	 * 
	 * @param dummy
	 * @return
	 */
	public static String enumSvcPrefix() {
		return enumPkg.getQualifiedName();
	}

	public static String literalKey(Element dummy, String enumName, String literalKey) {
		String prefix = "ID_"; //$NON-NLS-1$
		String literal = ""; //$NON-NLS-1$
		if(literalKey.equals("Port")) { //$NON-NLS-1$
			literal = prefix + TransformationContext.port.getName();
		}
		else if(literalKey.equals("Formal")) { //$NON-NLS-1$
			literal = prefix + TransformationContext.formalParameter.getName();
		}
		else if(literalKey.equals("Instance")) { //$NON-NLS-1$
			literal = prefix + TransformationContext.instance.getName();
		}
		else if(literalKey.equals("Interface")) { //$NON-NLS-1$
			literal = prefix + TransformationContext.classifier.getName();
		}
		return literal(enumName, literal);
	}

	public static String quoteLiteral(Element dummy, String enumName, String literal) {
		return "\"" + literal(enumName, literal) + "\""; //$NON-NLS-1$//$NON-NLS-2$
	}

	/**
	 * Create a literal within an enumeration. Both, the literal and the enumeration may be an Acceleo template startiung with [
	 * 
	 * @param dummy
	 * @param enumName
	 *        the name of an enumeration
	 * @param literal
	 *        the name of a literal within that enumeration.
	 * @return
	 */
	public static String literal(String enumName, String literal) {
		// Acceleo does not expand parameters, so we do it here
		/*
		 * if(enumName.contains("[") && enumName.contains("/]")) {
		 * try {
		 * enumName = AcceleoDriver.bind(enumName, dummy);
		 * } catch (TransformationException e) {
		 * return e.toString();
		 * }
		 * }
		 * if(literal.contains("[") && enumName.contains("/]")) {
		 * try {
		 * literal = AcceleoDriver.bind(literal, dummy);
		 * } catch (TransformationException e) {
		 * return e.toString();
		 * }
		 * }
		 */
		Enumeration enumeration = enumHash.get(enumName);
		if(enumPkg == null) {
			return literal;
		}
		if(enumeration == null) {
			if(enumName.startsWith("L")) { //$NON-NLS-1$
				// magic prefix for class local
				enumeration = (Enumeration)
					((Class)TransformationContext.classifier).createNestedClassifier(enumName, UMLPackage.eINSTANCE.getEnumeration());
			}
			else {
				enumeration = enumPkg.createOwnedEnumeration(enumName);
			}
			enumHash.put(enumName, enumeration);
		}
		if(enumeration.getOwnedLiteral(literal) == null) {
			enumeration.createOwnedLiteral(literal);
		}
		// declare a dependency to the enumeration from the current classifier 
		checkAndCreateDependency(TransformationContext.classifier, enumeration);

		return literal;
	}

	public static void checkAndCreateDependency(Classifier classifier, NamedElement target) {
		boolean found = false;
		for(Dependency dep : classifier.getClientDependencies()) {
			if(dep.getSuppliers().contains(target)) {
				found = true;
			}
		}
		if(!found) {
			classifier.createDependency(target);
		}
	}

	public static void createEnumPackage(Package root) {
		enumPkg = root.createNestedPackage(GLOBALENUMS);
	}

	public static Package enumPkg;

	public static void createEnums() {

	}

	private static Map<String, Enumeration> enumHash = new HashMap<String, Enumeration>();
}

Back to the top