Skip to main content
summaryrefslogtreecommitdiffstats
blob: c71f9f6f7cd12bf43e86bba1ba9812975727678b (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
/*******************************************************************************
 * Copyright (c) 2005-2009 itemis AG (http://www.itemis.eu) 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
 *
 *******************************************************************************/

package org.eclipse.xtend.check;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.emf.mwe.core.WorkflowContext;
import org.eclipse.emf.mwe.core.WorkflowInterruptedException;
import org.eclipse.emf.mwe.core.issues.Issues;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
import org.eclipse.xtend.expression.AbstractExpressionsUsingWorkflowComponent;
import org.eclipse.xtend.expression.ExecutionContext;
import org.eclipse.xtend.expression.ExecutionContextImpl;
import org.eclipse.xtend.expression.ExpressionFacade;

public class CheckComponent extends AbstractExpressionsUsingWorkflowComponent {

	private static final String COMPONENT_NAME = "Check component";

	private String expression = null;

	private List<String> checkFiles = new ArrayList<String>();

	private boolean abortOnError = true;

	private boolean warnIfNothingChecked = false;

	private String emfAllChildrenSlot;

	/**
	 * Sets if execution should be aborted on error.
	 * 
	 * @param abortOnError
	 *            If <code>true</code>, the execution is aborted on error,
	 *            otherwise, the execution is continued normally.
	 */
	public void setAbortOnError(final boolean abortOnError) {
		this.abortOnError = abortOnError;
	}

	/**
	 * Adds a check file.
	 * 
	 * @param checkFile
	 *            the check file
	 */
	public void addCheckFile(final String checkFile) {
		this.checkFiles.add(checkFile);
	}

	/**
	 * Sets the expression to check. This property only works for non-EMF based
	 * models. For EMF based models, use
	 * <code>setEmfAllChildrenSlot(String)</code>.
	 * 
	 * @param expression
	 *            the expression to check
	 */
	public void setExpression(final String expression) {
		this.expression = expression;
	}

	/**
	 * Sets if a warning should be issued if nothing has been checked.
	 * 
	 * @param warn
	 *            If <code>true</code>, a warning is issued in case nothing has
	 *            been checked, otherwise no warning is issued.
	 */
	public void setWarnIfNothingChecked(boolean warn) {
		warnIfNothingChecked = warn;
	}

	/**
	 * Sets the expression for the <code>emfAllChildren</code> property. This
	 * property only works for EMF based models. For all other kinds of models
	 * use <code>setExpression(String)</code>.
	 * 
	 * @param childExpression
	 *            the expression
	 */
	public void setEmfAllChildrenSlot(final String childExpression) {
		emfAllChildrenSlot = childExpression;
	}

	/**
	 * @see org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent#getLogMessage()
	 */
	@Override
	public String getLogMessage() {
		StringBuilder b = new StringBuilder();
		if (emfAllChildrenSlot != null) {
			b.append("slot " + emfAllChildrenSlot + " ");
		}
		else {
			b.append("expression " + expression + " ");
		}
		b.append("check file(s): ");
		for (String f : checkFiles) {
			b.append(f + " ");
		}
		return b.toString();
	}

	/**
	 * @see org.eclipse.emf.mwe.core.lib.AbstractWorkflowComponent#getComponentName()
	 */
	@Override
	public String getComponentName() {
		return COMPONENT_NAME;
	}

	@Override
	protected void invokeInternal2(final WorkflowContext ctx, final ProgressMonitor monitor, final Issues issues) {
		final ExecutionContextImpl executionContext = getExecutionContext(ctx);
		if (monitor != null) {
			executionContext.setMonitor(monitor);
		}

		final Collection<?> model = getExpressionResult(executionContext, ctx, expression);

		for (String checkFile : checkFiles) {
			CheckFacade.checkAll(checkFile, model, executionContext, issues, warnIfNothingChecked);
		}

		if (abortOnError && issues.hasErrors())
			throw new WorkflowInterruptedException("Errors during validation.");
	}

	@Override
	protected void checkConfigurationInternal(final Issues issues) {
		super.checkConfigurationInternal(issues);

		if ((expression == null) && (emfAllChildrenSlot != null)) {
			expression = emfAllChildrenSlot + ".eAllContents.union( {" + emfAllChildrenSlot + "} )";
		}
		else if ((expression != null) && (emfAllChildrenSlot == null)) {
			// ok - do nothing, expression already has a reasonable value
		}
		else {
			issues.addError(this, "You have to set one of the properties 'expression' and 'emfAllChildrenSlot'!");
		}
		if (checkFiles.isEmpty()) {
			issues.addError(this, "Property 'checkFile' not set!");
		}
	}

	private Collection<?> getExpressionResult(final ExecutionContext exeCtx, final WorkflowContext context,
			final String expression2) {
		final ExpressionFacade f = new ExpressionFacade(exeCtx);
		final Map<String, Object> ctx = new HashMap<String, Object>();
		final String[] names = context.getSlotNames();
		for (int i = 0; i < names.length; i++) {
			final String name = names[i];
			ctx.put(name, context.get(name));
		}
		final Object result = f.evaluate(expression2, ctx);
		if (result instanceof Collection)
			return (Collection<?>) result;
		else if (result == null)
			return Collections.EMPTY_SET;
		else
			return Collections.singleton(result);

	}
}

Back to the top