Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3a1c657a74736b2849069f0089a9ab54a6d212f5 (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
/*******************************************************************************
 * Copyright (c) 2014 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/

package org.eclipse.etrice.generator.base;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import org.eclipse.emf.common.EMFPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.etrice.core.common.scoping.ModelLocatorUriResolver;
import org.eclipse.etrice.generator.base.args.Arguments;
import org.eclipse.etrice.generator.base.io.IGeneratorEMFSetup;
import org.eclipse.etrice.generator.base.io.IGeneratorResourceLoader;
import org.eclipse.etrice.generator.base.logging.ILogger;
import org.eclipse.etrice.generator.base.logging.NullLogger;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.resource.XtextResourceSet;
import org.eclipse.xtext.util.CancelIndicator;
import org.xml.sax.SAXException;

import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * @author Henrik Rentz-Reichert
 *
 */
public class ModelLoader implements IGeneratorResourceLoader {
	
	/**
	 * The injected resource set provider
	 */
	@Inject
	private Provider<ResourceSet> resourceSetProvider;
	
	/**
	 * The injected platform relative URI resolver
	 */
	@Inject
	protected ModelLocatorUriResolver uriResolver;
	
	@Inject
	private IGeneratorEMFSetup emfSetup;
	
	protected ILogger logger;
	private boolean initializedEMF = EMFPlugin.IS_ECLIPSE_RUNNING;
	private ResourceSet resourceSet;
	private HashSet<URI> modelURIs = new HashSet<URI>();
	private HashSet<URI> mainModelURIs = new HashSet<URI>();
	private HashSet<URI> loadedModelURIs = new HashSet<URI>();

	@Override
	public List<Resource> load(List<String> files, Arguments arguments, ILogger logger) {
		if(!initializedEMF) {
			emfSetup.doEMFRegistration();
			initializedEMF = true;
		}
		
		logger.logInfo("-- reading models");
		
		if(loadModels(files, logger)) {
			List<Resource> resources = getResourceSet().getResources().stream()
					.filter(r -> getMainModelURIs().contains(r.getURI())).collect(Collectors.toList());
			return resources;
		}
		else {
			logger.logError("reading models failed");
			logger.logInfo("-- terminating");
			throw new GeneratorException("reading models failed");
		}
	}
	
    public boolean loadModels(List<String> uriList) {
        return loadModels(uriList, null);
    }
    
	public boolean loadModels(List<String> uriList, ILogger logger) {
	    if (logger==null) {
	        logger = new NullLogger();
	    }
	    this.logger = logger;
	    
		resourceSet = resourceSetProvider.get();
		if (resourceSet instanceof XtextResourceSet) {
			((XtextResourceSet) resourceSet).setClasspathURIContext(getClass().getClassLoader());
		}
		modelURIs.clear();
		loadedModelURIs.clear();
		
		for (String uri : uriList) {
			addResourceURI(uriResolver.resolve(uri, null));
		}
		
		// now that we have a list of normalized input models we make a copy of them
		mainModelURIs.addAll(modelURIs);
		
		boolean ok = true;
		while (!modelURIs.isEmpty()) {
			URI uri = modelURIs.iterator().next();
			//logger.logInfo("Loading " + uriString);
			try {
				if (loadModel(uri)) {
					Resource resource = resourceSet.getResources().get(resourceSet.getResources().size()-1);
					for (EObject root : resource.getContents()) {
						Iterator<EObject> it = root.eContents().iterator();
						while (it.hasNext()) {
							EObject obj = it.next();
							String importUri = uriResolver.resolve(obj);
							if (importUri!=null) {
								addResourceURI(importUri);
							}
						}
					}
				}
			}
			catch (Exception e) {
				ok = false;
				if (e instanceof FileNotFoundException)
					logger.logError("couldn't load '"+uri+"' (file not found)");
				if(e instanceof SAXException)
					logger.logError("couldn't load '"+uri+"' (maybe unknown or wrong file extension, eTrice file extensions have to be lower case)");
				else
					logger.logError(e.getMessage());
			}
			modelURIs.remove(uri);
		}
		
		// make a copy to avoid concurrent modification
		ArrayList<Resource> resources = new ArrayList<Resource>(resourceSet.getResources());
		for (Resource res : resources) {
			EcoreUtil2.resolveAll(res, CancelIndicator.NullImpl);
		}
		
		return ok;
	}

	/**
	 * Called by {@link #loadModels(List)} for each single model.
	 * 
	 * @param uri the model URI
	 * @return <code>true</code> if successfully loaded or already loaded
	 * @throws RuntimeException
	 * @throws IOException
	 */
	private boolean loadModel(URI uri)
			throws RuntimeException, IOException {
		
		if (loadedModelURIs.contains(uri))
			return true;
		
		if (resourceSet.getResource(uri, false) != null)
			// already loaded
			return false;
		
		logger.logInfo("loading model        " + uri);
		resourceSet.getResource(uri, true);	// Could throw an exception...
		loadedModelURIs.add(uri);
		return true;
	}
	
	/**
	 * This implementation of the method assumes the URI is already normalized and adds it to the
	 * list of models to load if not already loaded.
	 */
	private boolean addResourceURI(String uri) {
		URI can = null;
		try {
			// try valid uri
			can = URI.createURI(uri);
		} catch(IllegalArgumentException e1) {
		}
		if(can == null || !(can.isFile() || can.isArchive() || can.isPlatform() || can.scheme() == "classpath")) {
			// try file path
			can = URI.createFileURI(uri);
		}
		
		if (loadedModelURIs.contains(can))
			return false;
		
		boolean added = modelURIs.add(can);
		if (added) {
			if (loadedModelURIs.isEmpty())
				logger.logInfo("added model          "+uri);
			else
				logger.logInfo("added imported model "+uri);
		}
		return added;
	}

	/**
	 * @return
	 */
	public ResourceSet getResourceSet() {
		return resourceSet;
	}

	/**
	 * @return the mainModelURIs
	 */
	public HashSet<URI> getMainModelURIs() {
		return mainModelURIs;
	}
}

Back to the top