Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c99501e7e2bb98739a02181f7a444b058f9c7dc (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
/**
 * <copyright> 
 *
 * Copyright (c) 2008 itemis AG 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: 
 *   itemis AG - Initial API and implementation
 *
 * </copyright>
 *
 */
package org.eclipse.emf.editor.extxpt;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.editor.EEPlugin;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.issues.IssuesImpl;
import org.eclipse.internal.xtend.expression.parser.SyntaxConstants;
import org.eclipse.internal.xtend.xtend.ast.ExtensionFile;
import org.eclipse.xtend.XtendFacade;
import org.eclipse.xtend.check.CheckUtils;
import org.eclipse.xtend.expression.EvaluationException;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.shared.ui.Activator;
import org.eclipse.xtend.shared.ui.core.IXtendXpandProject;
import org.eclipse.xtend.shared.ui.core.IXtendXpandResource;

/**
 * @author Dennis Hübner - Initial contribution and API
 * 
 */
public class ExtXptFacade {

	private IProject project;
	private final ExecutionContext context;
	public static final String CHECK_EXT = "Checks";
	public static final String STYLE_EXT = "ItemLabelProvider";
	public static final String PROPOSAL_EXT = "Proposals";

	public ExtXptFacade(IProject project, ExecutionContext context) {
		this.project = project;
		this.context = context;
	}

	public Object style(String extension, EObject object) {
		String extendFile = path(object) + ExtXptFacade.STYLE_EXT;
		Object retVal = evaluate(extendFile, extension, object);
		return retVal;
	}

	/**
	 * @param extensionFile
	 * @param extensionName
	 * @param params
	 * @return
	 */
	private Object evaluate(String extensionFile, String extensionName, Object... params) {
		Object retVal = null;
		try {
			XtendFacade facade = XtendFacade.create(context, extensionFile);
			retVal = facade.call(extensionName, params);
		}
		catch (IllegalArgumentException e) {
			// no extension specified
		}
		catch (EvaluationException e) {
			EEPlugin.logError("Exception during extension evaluation", e);
		}
		catch (RuntimeException e) {
			// TODO check file exists
			// extension file not found
		}
		catch (Throwable e) {
			EEPlugin.logError("Exception during extension evaluation", new RuntimeException(e));
		}
		return retVal;
	}

	// TODO split method
	public List<?> proposals(EStructuralFeature feature, EObject ctx, List<?> fromList) {
		String extFile = path(ctx) + ExtXptFacade.PROPOSAL_EXT;
		List<?> retVal = new ArrayList<Object>();
		Object eval;
		if (fromList != null) {
			retVal = fromList;
			eval = evaluate(extFile, feature.getName(), ctx, fromList);
		}
		else {
			eval = evaluate(extFile, feature.getName(), ctx);
		}
		if (eval != null) {
			if (eval instanceof List<?>) {
				retVal = (List<?>) eval;
			}
			else {
				EEPlugin.logError("Returned type must be a List! File:" + extFile + ", Extension:" + feature.getName());
			}
		}
		return retVal;
	}

	public Issues check(EObject rootObject) {
		String checkFile = path(rootObject) + ExtXptFacade.CHECK_EXT;
		List<EObject> all = new ArrayList<EObject>();
		all.add(rootObject);
		EObject rootContainer = EcoreUtil.getRootContainer(rootObject);
		TreeIterator<EObject> iter = rootContainer.eAllContents();
		while (iter.hasNext())
			all.add(iter.next());
		Issues issuesImpl = new IssuesImpl();
		IXtendXpandProject extxptProject = Activator.getExtXptModelManager().findProject(project);
		if (extxptProject != null) {
			IXtendXpandResource extxptResource = extxptProject.findExtXptResource(checkFile, CheckUtils.FILE_EXTENSION);
			if (extxptResource != null) {
				ExtensionFile file = (ExtensionFile) extxptResource.getExtXptResource();
				try {
					file.check(context, all, issuesImpl, false);
				}
				catch (IllegalArgumentException e) {
					// no extension specified
				}
				catch (Exception e) {
					EEPlugin.logError("Exception during check evaluation", e);
				}
			}
		}
		else {
			EEPlugin.logWarning("Enable Xtend/Xpand-Nature for '" + project.getName() + "' to check models.");
		}
		return issuesImpl;
	}

	private String path(EObject object) {
		return object.eClass().getEPackage().getName() + SyntaxConstants.NS_DELIM;
	}

	public IProject getProject() {
		return project;
	}
}

Back to the top