Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 23e98ce7414a10ed50648a78a521f57ef3f92945 (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 * 
 * Contributors:
 *     Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.m2m.atl.engine.parser;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.m2m.atl.common.ATLLogger;
import org.eclipse.m2m.atl.common.ATL.ATLPackage;
import org.eclipse.m2m.atl.common.Problem.ProblemPackage;
import org.eclipse.m2m.atl.core.ATLCoreException;
import org.eclipse.m2m.atl.core.IExtractor;
import org.eclipse.m2m.atl.core.IInjector;
import org.eclipse.m2m.atl.core.IModel;
import org.eclipse.m2m.atl.core.IReferenceModel;
import org.eclipse.m2m.atl.core.ModelFactory;
import org.eclipse.m2m.atl.core.emf.EMFInjector;
import org.eclipse.m2m.atl.core.emf.EMFModel;
import org.eclipse.m2m.atl.core.emf.EMFModelFactory;
import org.eclipse.m2m.atl.dsls.core.EMFTCSExtractor;
import org.eclipse.m2m.atl.dsls.core.EMFTCSInjector;

/**
 * TCS utilities Wrapper.
 * 
 * @author <a href="mailto:william.piers@obeo.fr">William Piers</a>
 */
public final class AtlParser implements IInjector, IExtractor {

	private static ModelFactory modelFactory;

	private static IInjector injector;

	private static IModel atlTcsModel;

	private static IReferenceModel problemMetamodel;

	private static IReferenceModel atlMetamodel;

	private static AtlParser defaultInstance;

	/**
	 * Returns the default ATL parser.
	 * 
	 * @return the default ATL parser
	 */
	public static AtlParser getDefault() {
		if (defaultInstance == null) {
			defaultInstance = new AtlParser();
		}
		return defaultInstance;
	}

	public ModelFactory getModelFactory() {
		return modelFactory;
	}

	public IModel getAtlTcsModel() {
		return atlTcsModel;
	}

	public IReferenceModel getProblemMetamodel() {
		return problemMetamodel;
	}

	public IReferenceModel getAtlMetamodel() {
		return atlMetamodel;
	}

	static {
		modelFactory = new EMFModelFactory();
		try {
			URL tcsMetamodelURL = AtlParser.class.getResource("resources/TCS.ecore"); //$NON-NLS-1$
			URL atlTcsModelURL = AtlParser.class.getResource("resources/ATL-TCS.xmi"); //$NON-NLS-1$
			injector = new EMFInjector();
			atlMetamodel = modelFactory.newReferenceModel();
			((EMFInjector)injector).inject(atlMetamodel, ATLPackage.eINSTANCE.eResource());
			problemMetamodel = modelFactory.newReferenceModel();		
			((EMFInjector)injector).inject(problemMetamodel, ProblemPackage.eINSTANCE.eResource());
			IReferenceModel tcsMetamodel = modelFactory.newReferenceModel();
			injector.inject(tcsMetamodel, tcsMetamodelURL.openStream(), null);
			atlTcsModel = modelFactory.newModel(tcsMetamodel);
			injector.inject(atlTcsModel, atlTcsModelURL.openStream(), null);
		} catch (ATLCoreException e) {
			ATLLogger.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} catch (IOException e) {
			ATLLogger.log(Level.SEVERE, e.getLocalizedMessage(), e);
		}
	}

	/**
	 * Create a new Model then injects data using the given options.
	 * 
	 * @param source
	 *            the {@link InputStream} containing the model
	 * @param options
	 *            the injection parameters
	 * @return the new ATL Model
	 * @throws ATLCoreException
	 */
	public IModel[] inject(InputStream source, Map options) throws ATLCoreException {
		IModel targetModel = modelFactory.newModel(atlMetamodel);
		targetModel.setIsTarget(true);
		final Map params = new HashMap();
		params.put("name", "ATL"); //$NON-NLS-1$ //$NON-NLS-2$
		params.put("problems", modelFactory.newModel(problemMetamodel)); //$NON-NLS-1$
		if (options != null) {
			params.putAll(options);
		}
		inject(targetModel, source, params);
		targetModel.setIsTarget(false);
		return new IModel[] {targetModel, (IModel)params.get("problems")}; //$NON-NLS-1$
	}

	/**
	 * Create a new Model then injects data using the given options.
	 * 
	 * @param source
	 *            the {@link Reader} containing the model
	 * @param options
	 *            the injection parameters
	 * @return the new ATL Model
	 * @throws ATLCoreException
	 */
	public IModel[] inject(Reader source, Map options) throws ATLCoreException {
		IModel targetModel = modelFactory.newModel(atlMetamodel);
		targetModel.setIsTarget(true);
		final Map params = new HashMap();
		params.put("name", "ATL"); //$NON-NLS-1$ //$NON-NLS-2$
		params.put("problems", modelFactory.newModel(problemMetamodel)); //$NON-NLS-1$
		if (options != null) {
			params.putAll(options);
		}
		inject(targetModel, source, params);
		targetModel.setIsTarget(false);
		return new IModel[] {targetModel, (IModel)params.get("problems")}; //$NON-NLS-1$
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.m2m.atl.core.IInjector#inject(org.eclipse.m2m.atl.core.IModel, java.io.InputStream,
	 *      java.util.Map)
	 */
	public void inject(IModel targetModel, InputStream source, Map options) throws ATLCoreException {
		final EMFTCSInjector ebnfi = new EMFTCSInjector();
		final Map params = new HashMap();
		params.put("name", "ATL"); //$NON-NLS-1$ //$NON-NLS-2$
		params.put("problems", modelFactory.newModel(problemMetamodel)); //$NON-NLS-1$
		if (options != null) {
			params.putAll(options);
		}
		try {
			ebnfi.inject((EMFModel)targetModel, source, params);
		} catch (IOException e) {
			//fail silently
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.m2m.atl.core.IInjector#inject(org.eclipse.m2m.atl.core.IModel, java.io.Reader,
	 *      java.util.Map)
	 */
	public void inject(IModel targetModel, Reader source, Map options) throws ATLCoreException {
		final EMFTCSInjector ebnfi = new EMFTCSInjector();
		final Map params = new HashMap();
		params.put("name", "ATL"); //$NON-NLS-1$ //$NON-NLS-2$
		params.put("problems", modelFactory.newModel(problemMetamodel)); //$NON-NLS-1$
		if (options != null) {
			params.putAll(options);
		}
		try {
			ebnfi.inject((EMFModel)targetModel, source, params);
		} catch (IOException e) {
			//fail silently
		}
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.m2m.atl.core.IInjector#inject(org.eclipse.m2m.atl.core.IModel, java.lang.String,
	 *      java.util.Map)
	 */
	public void inject(IModel targetModel, String source, Map options) throws ATLCoreException {
		try {
			FileInputStream fis = new FileInputStream(formatFileName(source));
			inject(targetModel, fis, options);
			fis.close();
		} catch (IOException e) {
			throw new ATLCoreException(e.getMessage(), e);
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.m2m.atl.core.IInjector#inject(org.eclipse.m2m.atl.core.IModel, java.lang.String)
	 */
	public void inject(IModel targetModel, String source) throws ATLCoreException {
		inject(targetModel, source, null);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.m2m.atl.core.IExtractor#extract(org.eclipse.m2m.atl.core.IModel, java.io.OutputStream,
	 *      java.util.Map)
	 */
	public void extract(IModel sourceModel, OutputStream target, Map options) throws ATLCoreException {
		final EMFTCSExtractor ebnfe = new EMFTCSExtractor();
		final Map params = new HashMap();
		params.put("format", atlTcsModel); //$NON-NLS-1$
		params.put("indentString", " "); //$NON-NLS-1$ //$NON-NLS-2$
		params.put("identEsc", "\""); //$NON-NLS-1$ //$NON-NLS-2$
		params.put("stringDelim", "'"); //$NON-NLS-1$//$NON-NLS-2$
		params.put("serializeComments", "true"); //$NON-NLS-1$//$NON-NLS-2$
		if (options != null) {
			params.putAll(options);
		}
		ebnfe.extract((EMFModel)sourceModel, target, params);
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.m2m.atl.core.IExtractor#extract(org.eclipse.m2m.atl.core.IModel, java.lang.String,
	 *      java.util.Map)
	 */
	public void extract(IModel sourceModel, String target, Map options) throws ATLCoreException {
		try {
			FileOutputStream fos = new FileOutputStream(formatFileName(target));
			extract(sourceModel, fos, options);
			fos.close();
		} catch (IOException e) {
			throw new ATLCoreException(e.getMessage(), e);
		}
	}

	/**
	 * {@inheritDoc}
	 * 
	 * @see org.eclipse.m2m.atl.core.IExtractor#extract(org.eclipse.m2m.atl.core.IModel, java.lang.String)
	 */
	public void extract(IModel sourceModel, String target) throws ATLCoreException {
		extract(sourceModel, target, null);
	}

	private static String formatFileName(String fileName) {
		if (fileName.startsWith("file:")) { //$NON-NLS-1$
			return fileName.substring(5);
		}
		return fileName;
	}

	/**
	 * Parses the given input stream.
	 * 
	 * @param in
	 *            an input stream
	 * @return the resulting EObject
	 */
	public EObject parse(InputStream in) throws ATLCoreException {
		return parseWithProblems(in)[0];
	}

	/**
	 * Returns An array of EObject, the first one being an ATL!Unit and the following ones Problem!Problem.
	 * 
	 * @param in
	 *            InputStream to parse ATL code from.
	 * @return An array of EObject, the first one being an ATL!Unit and the following ones Problem!Problem.
	 */
	public EObject[] parseWithProblems(InputStream in) throws ATLCoreException {
		return convertToEmf(parseToModelWithProblems(in, true), "Unit"); //$NON-NLS-1$
	}

	/**
	 * Parses the given input stream.
	 * 
	 * @param in
	 *            an input stream
	 * @return the resulting IModel
	 */
	public IModel parseToModel(InputStream in) throws ATLCoreException {
		return parseToModelWithProblems(in, true)[0];
	}

	/**
	 * Parses the given input stream.
	 * 
	 * @param in
	 *            an input stream
	 * @param hideErrors
	 *            disable standard output in order to hide errors
	 * @return the parser resulting IModel[model,problemModel]
	 */
	public IModel[] parseToModelWithProblems(InputStream in, boolean hideErrors) throws ATLCoreException {
		return inject(in, null);
	}

	/**
	 * ATL injector launcher.
	 * 
	 * @param expression
	 *            an ATL expression
	 * @param expressionType
	 *            the ATL expression type the Syntax Element parsed
	 * @return outputs models
	 */
	public EObject[] parseExpression(String expression, String expressionType) {
		if (expressionType == null) {
			return null;
		}
		IModel[] ret = null;
		Map params = new HashMap();
		params.put("name", "ATL-" + expressionType); //$NON-NLS-1$ //$NON-NLS-2$
		try {
			ret = inject(new ByteArrayInputStream(expression.getBytes()), params);
		} catch (ATLCoreException e) {
			// fail silently;
		}

		String rootTypeName = Character.toUpperCase(expressionType.charAt(0)) + expressionType.substring(1);
		return convertToEmf(ret, rootTypeName);
	}

	private static EObject[] convertToEmf(IModel[] parsed, String rootTypeName) {
		EObject[] ret = null;
		EObject retUnit = null;
		Collection pbs = null;

		IModel atlmodel = parsed[0];
		IModel problems = parsed[1];
		if (atlmodel instanceof EMFModel) {
			Collection roots = getElementsByType(atlmodel, rootTypeName);
			if (roots.size() > 0) {
				retUnit = (EObject)roots.iterator().next();
			}
			pbs = getElementsByType(problems, "Problem"); //$NON-NLS-1$
		}

		if (pbs != null) {
			ret = new EObject[1 + pbs.size()];
			int k = 1;
			for (Iterator i = pbs.iterator(); i.hasNext();) {
				ret[k++] = (EObject)i.next();
			}
		} else {
			ret = new EObject[1];
		}
		ret[0] = retUnit;

		return ret;
	}

	private static Collection getElementsByType(IModel model, String typeName) {
		return model.getElementsByType(model.getReferenceModel().getMetaElementByName(typeName));
	}
}

Back to the top