Skip to main content
summaryrefslogtreecommitdiffstats
blob: 872187dbd4e988f1fc0b266542406de6cb178974 (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
/*
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;

import java.io.InputStream;
import java.util.ArrayList;
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.issues.Issues;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
import org.eclipse.emf.mwe.core.resources.ResourceLoaderFactory;
import org.eclipse.internal.xtend.expression.parser.SyntaxConstants;
import org.eclipse.internal.xtend.xtend.XtendFile;
import org.eclipse.xtend.expression.AbstractExpressionsUsingWorkflowComponent;


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

//    private final Log _log = LogFactory.getLog(getClass());

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

    public void addExtensionAdvice (String resourceName) {
		if ( !_extensionAdvices.contains (resourceName) ) 
			_extensionAdvices.add (resourceName );
	}

    /** Stores the value of the 'invoke' property. Needed for error analysis. */ 
    private String _invokeExpression;
    String _extensionFile = null;
    private String _expression = null;
    private String _outputSlot = WorkflowContext.DEFAULT_SLOT;

    
    @Override
    public String getLogMessage() {
    	return "executing '" + _invokeExpression + "'";
    }
    
    public void setInvoke (String invoke) {
    	_invokeExpression = invoke;
        final int i = invoke.lastIndexOf (SyntaxConstants.NS_DELIM);
        if (i != -1) {
            _extensionFile = invoke.substring(0, i);
            _expression = invoke.substring (i + SyntaxConstants.NS_DELIM.length());
        } 
        else 
            _expression = invoke;
    }

    public void setOutputSlot (String outputSlot) {
        _outputSlot = outputSlot;
    }
    
    @Override
    public void invokeInternal2(final WorkflowContext ctx, final ProgressMonitor monitor, final Issues issues) {
        if (! extensionFileExists ()) {
            issues.addError ("Cannot find extension file: " + _extensionFile);
            return;
        }
        
        final List<String> adviceResources = new ArrayList<String>();
        for (String rawAdvice : _extensionAdvices) 
            for (String a: rawAdvice.split (","))
                adviceResources.add (a.trim());
        
        final Map<String, Object> localVars = new HashMap<String, Object> ();
        for (String slotName: ctx.getSlotNames())
            localVars.put (slotName, ctx.get (slotName));
        
        final Map<String, Object> globalVars = new HashMap<String, Object> ();
        for (GlobalVarDef gvd: globalVarDefs)
            globalVars.put (gvd.getName(), gvd.getValue());
        
        //TODO allow file encoding configuration
        final Object result = XtendBackendFacade.evaluateExpression (_expression, _extensionFile, null, metaModels, localVars, globalVars, adviceResources);
        ctx.set (_outputSlot, result);
    }

    private boolean extensionFileExists() {
        InputStream is = null;
        try {
            is = ResourceLoaderFactory.createResourceLoader().getResourceAsStream (_extensionFile.replace (SyntaxConstants.NS_DELIM, "/") + XtendFile.FILE_EXTENSION);
        }
        catch (Exception exc) {
            // do nothing - an exception just means that the extension file does not exist 
        }
        if (is != null) {
            try {
                is.close ();
            }
            catch (Exception e) {}
        }
        return is != null;
    }

    @Override
    public void checkConfiguration (Issues issues) {
        super.checkConfiguration (issues);
        
        // Try to create detailed error message (see Bug#172567)
        String compPrefix = getId()!=null ? getId()+": " : "";
        
        if (_invokeExpression == null || _invokeExpression.trim().length()==0) {
            issues.addError(compPrefix + "Property 'invoke' not specified.");
            return;
        }
        if (_extensionFile == null) {
            issues.addError (compPrefix + "Error parsing property 'invoke': Could not extract name of the extension file.");
            return;
        }
        if (! extensionFileExists () || _expression == null) {
            issues.addError (compPrefix + "Property 'invoke' not specified properly. Extension file '" + _extensionFile + "' not found.");
            return;
        }
        if (_expression == null) {
            issues.addError (compPrefix + "Error parsing property 'invoke': Could not extract the expression to invoke in extension file '" + _extensionFile + "'.");
            return;
        }
    }
}

Back to the top