Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 28e74fa13c8774aff23d3dc1e3a41f94400d65d6 (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
/******************************************************************************
 * Copyright (c) 2006, 2020, 2021 Borland Software Corporation, CEA LIST, Artal
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/ 
 * 
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors: 
 *    Artem Tikhomirov (Borland) - initial API and implementation
 *    Aurelien Didier (ARTAL) - aurelien.didier51@gmail.com - Bug 569174
 *    Etienne ALLOGO (ARTAL) - etienne.allogo@artal.fr - Bug 569174 - newline characters preference api consistency
 *****************************************************************************/
package org.eclipse.papyrus.gmf.graphdef.codegen;

import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.ecore.EcorePackage;
import org.eclipse.papyrus.gmf.common.UnexpectedBehaviourException;
import org.eclipse.papyrus.gmf.gmfgraph.Figure;
import org.eclipse.papyrus.gmf.gmfgraph.FigureDescriptor;
import org.eclipse.papyrus.gmf.internal.common.codegen.TextEmitter;
import org.eclipse.papyrus.gmf.internal.graphdef.codegen.Activator;
import org.eclipse.papyrus.gmf.internal.xpand.BufferOutput;
import org.eclipse.papyrus.gmf.internal.xpand.ResourceManager;
import org.eclipse.papyrus.gmf.internal.xpand.XpandFacade;
import org.eclipse.papyrus.gmf.internal.xpand.model.AmbiguousDefinitionException;
import org.eclipse.papyrus.gmf.internal.xpand.model.EvaluationException;
import org.eclipse.papyrus.gmf.internal.xpand.model.Scope;
import org.eclipse.papyrus.gmf.internal.xpand.model.Variable;

public class FigureGenerator implements TextEmitter {

	private static final String VAR_MM_ACCESS = "mapModeAccessor";
	private static final String VAR_RT_TOKEN = "runtimeToken";

	private final ResourceManager resourceManager;

	private final StringBuilder result = new StringBuilder();

	private final ArrayList<Variable> globals = new ArrayList<Variable>();

	private final boolean myIsInnerClassCode;

	private String packageStatement;


	/**
	 * XXX consider using enum for runtimeToken
	 * @param runtimeToken either "full" or null to indicate full GMF runtime use, any other value is to be processed by custom templates 
	 * @param asInnerClass
	 */
	public FigureGenerator(String runtimeToken, String packageStmt, boolean asInnerClass) {
		this(runtimeToken, packageStmt, MapModeCodeGenStrategy.DYNAMIC, "getMapMode().", asInnerClass);
	}

	public FigureGenerator(String runtimeToken, String packageStmt, MapModeCodeGenStrategy mapModeStrategy, String mapModeAccessor, boolean asInnerClass) {
		this(runtimeToken, packageStmt, mapModeStrategy, mapModeAccessor, asInnerClass, null);
	}

	public FigureGenerator(String runtimeToken, String packageStmt, MapModeCodeGenStrategy mapModeStrategy, String mapModeAccessor, boolean asInnerClass, URL[] dynamicTemplates) {
		myIsInnerClassCode = asInnerClass;
		this.packageStatement = packageStmt;
		if (mapModeStrategy == MapModeCodeGenStrategy.STATIC) {
			if (mapModeAccessor != null && mapModeAccessor.trim().length() > 0) {
				throw new IllegalArgumentException("Can't use map mode accessor with identity map mode");
			}
		}
		if (mapModeStrategy == MapModeCodeGenStrategy.DYNAMIC) {
			globals.add(new Variable(VAR_MM_ACCESS, EcorePackage.eINSTANCE.getEString(), mapModeAccessor == null ? "" : mapModeAccessor));
		}
		if (runtimeToken != null) {
			globals.add(new Variable(VAR_RT_TOKEN, EcorePackage.eINSTANCE.getEString(), runtimeToken));
		}
		resourceManager = Activator.createResourceEngine(mapModeStrategy, dynamicTemplates);
	}

	public String getPackageName() {
		return packageStatement;
	}

	public String fqnSwitch(Figure figure) {
		try {
			xpandFacade().evaluate("Runtime::fqn", figure, null);
		} catch (AmbiguousDefinitionException e) {
			throw new EvaluationException(e);
		}
		return result.toString();
	}
	
	/**
	 * @param packageStmt can be null if asInnerClass was true
	 * @param figure
	 */
	public String go(FigureDescriptor figure) {
		try {
			if (myIsInnerClassCode) {
				xpandFacade().evaluate("top::Descriptor::Inner", figure, null);
			} else {
				xpandFacade().evaluate("top::Descriptor::Top", figure, new Object[] { packageStatement });
			}
		} catch (AmbiguousDefinitionException e) {
			throw new EvaluationException(e);
		}
		return result.toString();
	}

	private XpandFacade xpandFacade() {
		result.setLength(0);
		BufferOutput bufferOutput = new BufferOutput(result);

		return new XpandFacade(new Scope(resourceManager, globals, bufferOutput));
	}

	public String generate(IProgressMonitor monitor, Object[] arguments, String lineSeparator) throws InterruptedException, InvocationTargetException, UnexpectedBehaviourException {
		if (arguments == null || arguments.length != 1 || false == arguments[0] instanceof FigureDescriptor) {
			throw new UnexpectedBehaviourException("(FigureDescriptor) expected as arguments, not " + arguments);
		}
		return go((FigureDescriptor) arguments[0]);
	}
}

Back to the top