Skip to main content
summaryrefslogtreecommitdiffstats
blob: 68d23c115770297478469dd809223b19cee49a9c (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) 2008 Arno Haase.
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:
    Arno Haase - initial API and implementation
 */
package org.eclipse.xtend.middleend.old.xtend;

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;


//TODO test this

/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public class CheckComponent extends AbstractExpressionsUsingWorkflowComponent {

    private String _expression = null;
    private List<String> _checkFiles = new ArrayList<String>();
    private boolean _abortOnError = true;
    private boolean _warnIfNothingChecked = false;
    private String _emfAllChildrenSlot;
    private String _fileEncoding = null;

    public void setAbortOnError (boolean abortOnError) {
        _abortOnError = abortOnError;
    }

    public void addCheckFile (String checkFile) {
        _checkFiles.add(checkFile);
    }

    public void setExpression (String expression) {
        _expression = expression;
    }

    public void setWarnIfNothingChecked (boolean b) {
        _warnIfNothingChecked = b;
    }

    public void setEmfAllChildrenSlot (String childExpression) {
        _emfAllChildrenSlot = childExpression;
    }

    public void setFileEncoding (String fileEncoding) {
        _fileEncoding = fileEncoding;
    }

    @Override
    public String getLogMessage() {
    	final StringBuilder result = new StringBuilder ();
    	if ( _emfAllChildrenSlot != null ) 
    		result.append ("slot " + _emfAllChildrenSlot + " ");
    	else 
    		result.append ("expression " + _expression + " ");
    	
    	result.append ("check file(s): ");
    	for (String f: _checkFiles) 
    		result.append (f + " ");
		
    	return result.toString();
    }    

    
    @Override
    protected void invokeInternal2 (WorkflowContext wfCtx, ProgressMonitor monitor, Issues issues) {
        final Collection<?> allObjects = getExpressionResult (wfCtx, _expression);

        for (String checkFile : _checkFiles) 
            CheckBackendFacade.checkAll (checkFile, _fileEncoding, metaModels, issues, allObjects);

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


    @Override
    public void checkConfiguration (Issues issues) {
        super.checkConfiguration (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 (WorkflowContext wfCtx, String expression2) {
        final Map<String, Object> localVars = new HashMap<String, Object>();
        final String[] names = wfCtx.getSlotNames ();
        for (int i = 0; i < names.length; i++) 
            localVars.put (names[i], wfCtx.get (names[i]));
        
        final Object result = XtendBackendFacade.evaluateExpression (expression2, metaModels, localVars);
        
        if (result instanceof Collection)
            return (Collection<?>) result;

        if (result == null)
            return Collections.EMPTY_SET;

        return Collections.singleton (result);
    }
}

Back to the top