Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e2d3cd73bc1fd0f584c3caf25550f24f5b7e87d5 (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
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.emf.emfatic.core.generator.ecore;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
import org.eclipse.emf.emfatic.core.lang.gen.parser.EmfaticParserDriver;
import org.eclipse.gymnast.runtime.core.parser.ParseContext;
import org.eclipse.gymnast.runtime.core.parser.ParseMessage;
import org.eclipse.gymnast.runtime.core.util.MarkerUtil;

/**
 * 
 * @author cjdaly@us.ibm.com
 */
public class EcoreGenerator {
	
	public EcoreGenerator() {
	}

	public void generate(IFile emfFile, IProgressMonitor monitor) {
		generate(emfFile, true, monitor);
	}

	public void generate(IFile emfFile, boolean writeEcore, IProgressMonitor monitor) {
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(emfFile.getContents()));
			EmfaticParserDriver parser = new EmfaticParserDriver();
			ParseContext parseContext = parser.parse(reader);
			String filePath = getEcoreFilePath(emfFile);
			Resource resource = createResource(filePath, true);
			Builder builder = new Builder();
			builder.build(parseContext, resource, monitor);
			if (!parseContext.hasErrors()) {
				Connector connector = new Connector(builder);
				connector.connect(parseContext, resource, monitor);
			}
			MarkerUtil.updateMarkers(emfFile, parseContext);

			boolean showStopper = false;
			/*
			 * we let ecores to be written as long as they contain only errors
			 * detected by EcoreValidatorDiagnostic, as such errors can be
			 * solved with Sample Ecore Editor
			 */
			for (ParseMessage pm : parseContext.getMessages()) {
				showStopper |= !(pm instanceof EmfaticSemanticWarning.EcoreValidatorDiagnostic);
			}

			if (!showStopper && writeEcore)
				resource.save(null);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public void generate(File emfFile, boolean writeEcore) throws Exception {
		NullProgressMonitor monitor = new NullProgressMonitor();
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(emfFile)));
		EmfaticParserDriver parser = new EmfaticParserDriver();
		ParseContext parseContext = parser.parse(reader);
		String filePath = emfFile.getAbsolutePath().replaceAll("\\.emf$", ".ecore");
		Resource resource = createResource(filePath, false);
		Builder builder = new Builder();
		builder.build(parseContext, resource, monitor);

		if (!parseContext.hasErrors() && writeEcore) {
			Connector connector = new Connector(builder);
			connector.connect(parseContext, resource, monitor);
			resource.save(null);
		}
		else {
			String message = parseContext.getMessages()[0].getMessage();
			message = message.replaceAll("\\r|\\n", " ");
			message = message.trim();
			throw new Exception("Syntax error: " + message);
		}
	}
	
	private String getEcoreFilePath(IFile emfFile) {
		String emfFileExt = emfFile.getFileExtension();
		int extLen = emfFileExt != null ? emfFileExt.length() + 1 : 0;
		String emfFileName = emfFile.getName();
		String fileName = emfFileName.substring(0, emfFileName.length() - extLen);
		fileName = fileName + ".ecore";
		String filePath = emfFile.getFullPath().removeLastSegments(1).append(fileName).toString();
		return filePath;
	}

	private Resource createResource(String filePath, boolean inWorkspace) {
		ResourceSet resourceSet = new ResourceSetImpl();
		URI uri = null;
		if (!inWorkspace) {
			resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("ecore", new XMIResourceFactoryImpl());
			uri = URI.createFileURI(filePath);
		}
		else {
			uri = URI.createPlatformResourceURI(filePath, false);
		}
		Resource resource = resourceSet.createResource(uri);
		return resource;
	}
}

Back to the top