Skip to main content
summaryrefslogtreecommitdiffstats
blob: 68d39667cf1d6da2d1d5db7be483308b1d2a2775 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*******************************************************************************
 * Copyright (c) 2005, 2007 committers of openArchitectureWare 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/

package org.eclipse.xpand2;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.mwe.core.ConfigurationException;
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.internal.xpand2.ast.Definition;
import org.eclipse.internal.xpand2.ast.ExpandStatement;
import org.eclipse.internal.xpand2.ast.Template;
import org.eclipse.internal.xpand2.codeassist.XpandTokens;
import org.eclipse.internal.xpand2.parser.XpandParseFacade;
import org.eclipse.internal.xpand2.pr.ProtectedRegionResolverImpl;
import org.eclipse.internal.xtend.util.ProfileCollector;
import org.eclipse.internal.xtend.xtend.parser.ParseException;
import org.eclipse.xpand2.output.Outlet;
import org.eclipse.xpand2.output.Output;
import org.eclipse.xpand2.output.OutputImpl;
import org.eclipse.xpand2.output.PostProcessor;
import org.eclipse.xtend.expression.AbstractExpressionsUsingWorkflowComponent;
import org.eclipse.xtend.expression.ExceptionHandler;
import org.eclipse.xtend.expression.Variable;
import org.eclipse.xtend.typesystem.MetaModel;

public class Generator extends AbstractExpressionsUsingWorkflowComponent {

    private String genPath = null;

    private String srcPath = null;

    private String prSrcPaths = null;

    private String prExcludes = null;

    private boolean prDefaultExcludes = true;

    private String expand = null;

    private String fileEncoding = null;

    private List<?> beautifier = new ArrayList<Object>();

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

    private List<String> extensionAdvices = new ArrayList<String>();
    
    private boolean automaticHyphens = false;
    
    private ExceptionHandler exceptionHandler = null;

    private String collectProfileSummary = null;
    private String verboseProfileFilename = null;
    
    private Output output = null;
    
    public void setCollectProfileSummary (String c) {
        collectProfileSummary = c;
    }
    
    public void setVerboseProfileFilename (String f) {
        verboseProfileFilename = f;
    }
     
    public void setAutomaticHyphens(boolean automaticHyphens) {
        this.automaticHyphens = automaticHyphens;
    }

    @Override
	public String getLogMessage() {
    	return "generating '"+expand+"' => directory '"+genPath+"'";
    }
    
    @Override
    public void addAdvices(final String advice) {
        if ( !this.advices.contains(advice) ) this.advices.add( advice );
    }
    
    @Override
    public void addAdvice(final String advice) {
    	if ( !this.advices.contains(advice) ) this.advices.add( advice );
    }
    
    @Override
    public void addExtensionAdvices(String extensionAdvices) {
    	if ( !this.extensionAdvices.contains(extensionAdvices) ) 
    		this.extensionAdvices.add( extensionAdvices );
	}

    @Override
    public void addExtensionAdvice(String extensionAdvice) {
    	if ( !this.extensionAdvices.contains(extensionAdvice) ) 
		this.extensionAdvices.add( extensionAdvice );
	}

    public List<?> getBeautifier() {
        return beautifier;
    }

    public void setBeautifier(final List<?> beautifiers) {
        beautifier = beautifiers;
    }

    public void setFileEncoding(final String fileEncoding) {
        this.fileEncoding = fileEncoding;
    }
    
	public String getFileEncoding() {
		return fileEncoding;
	}

    /**
     * 
     * @deprecated use outlets instead
     */
    @Deprecated
    public void setGenPath(final String genPath) {
        this.genPath = fixPath(genPath);
    }

    public void setExpand(final String invoke) {
        expand = invoke;
    }

    public void setPrDefaultExcludes(final boolean prDefaultExcludes) {
        this.prDefaultExcludes = prDefaultExcludes;
    }

    public void setPrExcludes(final String prExcludes) {
        this.prExcludes = prExcludes;
    }

    public void setPrSrcPaths(final String prSrcPathes) {
        this.prSrcPaths = prSrcPathes;
    }
    
    public void setExceptionHandler(final ExceptionHandler exceptionHandler) {
		this.exceptionHandler = exceptionHandler;
	}

	/**
     * 
     * @deprecated use outlets instead
     */
    @Deprecated
    public void setSrcPath(final String srcPath) {
        this.srcPath = fixPath(srcPath);
    }

    private String fixPath(final String p) {
        if (p.endsWith("\\"))
            return p.replace('\\', '/');
        if (p.endsWith("/"))
            return p;
        return p + "/";
    }
    
    @Override
    protected void invokeInternal2(final WorkflowContext ctx, final ProgressMonitor monitor, final Issues issues) {
        OutputStream verboseProfileOutputStream = null;

        if (verboseProfileFilename != null) {
            try {
                verboseProfileOutputStream = new BufferedOutputStream (new FileOutputStream (verboseProfileFilename));
                ProfileCollector.getInstance().setDetailedLoggingWriter(verboseProfileOutputStream);
            }
            catch (IOException exc) {
                log.warn("could nto open profiling log file", exc);
            }
        }

        final Output out = getOutput();
        final List<Outlet> outlets = getInitializedOutlets();
        for (final Iterator<Outlet> iter = outlets.iterator(); iter.hasNext();) {
            out.addOutlet(iter.next());
        }

        ProtectedRegionResolverImpl prs = null;
        if (prSrcPaths != null) {
            prs = new ProtectedRegionResolverImpl();
            prs.setDefaultExcludes(prDefaultExcludes);
            prs.setIgnoreList(prExcludes);
            prs.setSrcPathes(prSrcPaths);
            prs.setFileEncoding(fileEncoding);
        }
        
        XpandExecutionContextImpl executionContext = new XpandExecutionContextImpl(out, prs, getGlobalVars(ctx), exceptionHandler,getNullEvaluationHandler());
        if (monitor!=null) {
        	executionContext.setMonitor(monitor);
        }
        
        
        if (fileEncoding != null) {
            executionContext.setFileEncoding(fileEncoding);
        }
        
        for (MetaModel mm : metaModels) {
            executionContext.registerMetaModel(mm);
        }

        final ExpandStatement es = getStatement();
        if (es == null)
            throw new ConfigurationException("property 'expand' has wrong syntax!");

        final String[] names = ctx.getSlotNames();
        for (int i = 0; i < names.length; i++) {
            final String name = names[i];
            executionContext = (XpandExecutionContextImpl) executionContext.cloneWithVariable(new Variable(name, ctx
                    .get(name)));
        }

        for (String advice : advices) {
            final String[] allAdvices = advice.split(",");
            for (int i = 0; i < allAdvices.length; i++) {
                final String string = allAdvices[i];
                executionContext.registerAdvices(string.trim());
            }
		}
        
        for (String advice : extensionAdvices) {
        	final String[] allAdvices = advice.split(",");
        	for (int i = 0; i < allAdvices.length; i++) {
        		final String string = allAdvices[i];
        		executionContext.registerExtensionAdvices(string.trim());
        	}
        }
        
        	es.evaluate(executionContext);
        
        for (final Iterator<Outlet> iter = outlets.iterator(); iter.hasNext();) {
            final Outlet element = iter.next();
            final String name = (element.getName() == null ? "[default]" : element.getName()) + "(" + element.getPath()
                    + ")";
            if (element.getFilesWrittenAndClosed() > 0) {
                log.info("Written " + element.getFilesWrittenAndClosed() + " files to outlet " + name);
            }
            if (element.getFilesCreated() > element.getFilesWrittenAndClosed()) {
                log.info("Skipped writing of " + (element.getFilesCreated() - element.getFilesWrittenAndClosed())
                        + " files to outlet " + name);
            }
        }
        
        ProfileCollector.getInstance().finish();
        if ("true".equalsIgnoreCase(this.collectProfileSummary)) {
            log.info ("profiling info: \n" + ProfileCollector.getInstance().toString());
        }
        
        if (verboseProfileOutputStream != null) {
            try {
                verboseProfileOutputStream.close ();
            }
            catch (IOException exc) {
                log.warn("problem closing profile log file", exc);
            }
        }
    }

    private final List<Outlet> outlets = new ArrayList<Outlet>();

    public void addOutlet(final Outlet outlet) {
        outlets.add(outlet);
    }
    
    public void setOutput (final Output output) {
    	this.output = output;
    }
    
    private Output getOutput () {
    	if (output==null) {
    		// lazy initialization
    		OutputImpl out = new OutputImpl();
    		out.setAutomaticHyphens(automaticHyphens);
    		this.output = out;
    	} 
    	return output;
    }

    private List<Outlet> initializedOutlets = null;

    private List<Outlet> getInitializedOutlets() {
        if (initializedOutlets == null) {
            final List<Outlet> result = new ArrayList<Outlet>(outlets);
            if (result.isEmpty()) {
                if (genPath != null) { // backward compatibility
                    Outlet o = new Outlet();
                    o.setAppend(false);
                    o.setFileEncoding(fileEncoding);
                    o.setOverwrite(true);
                    o.setPath(genPath);
                    result.add(o);

                    o = new Outlet();
                    o.setAppend(true);
                    o.setFileEncoding(fileEncoding);
                    o.setName("APPEND");
                    o.setOverwrite(true);
                    o.setPath(genPath);
                    result.add(o);
                }
                if (srcPath != null) {
                    final Outlet o = new Outlet();
                    o.setAppend(false);
                    o.setFileEncoding(fileEncoding);
                    o.setName("ONCE");
                    o.setOverwrite(false);
                    o.setPath(srcPath);

                    result.add(o);
                }
            }
            for (final Iterator<Outlet> iter = result.iterator(); iter.hasNext();) {
                final Outlet o = iter.next();
                if (o.postprocessors.isEmpty()) {
                    for (final Iterator<?> iterator = beautifier.iterator(); iterator.hasNext();) {
                        final PostProcessor element = (PostProcessor) iterator.next();
                        o.addPostprocessor(element);
                    }
                }
                // Initialize file encoding for outlets. If it is not set then take the Generator
                // default encoding. If this not set also then take System default.
                if (o.hasDefaultEncoding() && fileEncoding!=null) {
                    o.setFileEncoding(fileEncoding);
                }
            }
            initializedOutlets = result;
        }
        return initializedOutlets;
    }

    /**
     * Retrieves the configured and initialized outlets of the generator.
     * @since 4.2
     */
    public final List<Outlet> getOutlets () {
    	return Collections.unmodifiableList(getInitializedOutlets());
    }
    
    private ExpandStatement getStatement() {
        Template tpl = XpandParseFacade.file(new StringReader(XpandTokens.LT + "DEFINE test FOR test" + XpandTokens.RT
                + XpandTokens.LT + "EXPAND " + expand + XpandTokens.RT + XpandTokens.LT + "ENDDEFINE" + XpandTokens.RT),null);
        ExpandStatement es = null;
        try {
            es = (ExpandStatement) ((Definition) tpl.getDefinitions()[0]).getBody()[1];
        } catch (final Exception e) {
            log.error(e);
        }
        return es;
    }

    @Override
    public void checkConfiguration(final Issues issues) {
        super.checkConfiguration(issues);
        if (genPath == null && getInitializedOutlets().isEmpty()) {
            issues.addError(this, "You need to configure at least one outlet!");
        }
        if ((genPath != null || srcPath != null) && !outlets.isEmpty()) {
            issues.addWarning(this, "'genPath' and 'srcPath' properties are ignored since you have specified outlets!");
        }
        int defaultOutlets = 0;
        for (final Iterator<Outlet> iter = getInitializedOutlets().iterator(); iter.hasNext();) {
            final Outlet o = iter.next();
            if (o.getName() == null)
                defaultOutlets++;
        }
        if (defaultOutlets > 1) {
            issues.addError(this,
                    "Only one outlet can be the default outlet. Please specifiy a name for the other outlets!");
        } else if (defaultOutlets == 0) {
            issues.addWarning(this, "No default outlet configured!");
        }
        if (expand == null) {
            issues.addError(this, "property 'expand' not configured!");
        } else {
        	try {
				final ExpandStatement es = getStatement();
				if (es == null) {
					issues.addError(this, "property 'expand' has wrong syntax!");
				}
			} catch (ParseException e) {
				issues.addError(this, "property 'expand' has wrong syntax : "+e.getMessage());
			}
        }
    }

}

Back to the top