blob: 022125ddf1b2f37a8127f770caa1065da7dbd383 (
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
|
/*******************************************************************************
* Copyright (c) 2018 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 is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* CONTRIBUTORS:
* Jan Belle (initial contribution)
*
*******************************************************************************/
package org.eclipse.etrice.generator.base.io;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.emf.common.EMFPlugin;
import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
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.util.EcoreUtil;
import org.eclipse.etrice.generator.base.GeneratorException;
import org.eclipse.etrice.generator.base.args.Arguments;
import org.eclipse.etrice.generator.base.logging.ILogger;
import com.google.inject.Inject;
import com.google.inject.Provider;
/**
* Simple implementation for resource loading and resolving.
* Only supports file paths and <b>not</b> URIs.
*/
public class GeneratorResourceLoader implements IGeneratorResourceLoader {
private Provider<ResourceSet> resourceSetProvider;
private IGeneratorEMFSetup emfSetup;
private boolean initializedEMF;
@Inject
public GeneratorResourceLoader(Provider<ResourceSet> resourceSetProvider, IGeneratorEMFSetup emfSetup) {
this.resourceSetProvider = resourceSetProvider;
this.emfSetup = emfSetup;
initializedEMF = EMFPlugin.IS_ECLIPSE_RUNNING;
}
@Override
public List<Resource> load(List<String> files, Arguments arguments, ILogger logger) throws GeneratorException {
doEMFRegistration();
List<Resource> models = new ArrayList<>(files.size());
ResourceSet resourceSet = resourceSetProvider.get();
Adapter resourceAddedAdapter = new ResourceAddedAdapter(logger);
resourceSet.eAdapters().add(resourceAddedAdapter);
for(String f: files) {
Resource r = loadResource(f, resourceSet, logger);
models.add(r);
}
EcoreUtil.resolveAll(resourceSet);
return models;
}
private void doEMFRegistration() {
if(!initializedEMF) {
emfSetup.doEMFRegistration();
initializedEMF = true;
}
}
private Resource loadResource(String file, ResourceSet rs, ILogger logger) {
try {
URI uri = createURI(file);
return rs.getResource(uri, true);
}
catch(RuntimeException | IOException e) {
logger.logError("couldn't load file " + file + "; " + e.getMessage());
throw new GeneratorException(e);
}
}
private URI createURI(String file) throws IOException {
File f = new File(file);
String canonicalPath = f.getCanonicalPath();
URI uri = URI.createFileURI(canonicalPath);
return uri;
}
private class ResourceAddedAdapter extends AdapterImpl {
private ILogger logger;
public ResourceAddedAdapter(ILogger logger) {
this.logger = logger;
}
@Override
public void notifyChanged(Notification msg) {
if(msg.getEventType() == Notification.ADD) {
Resource addedResource = (Resource) msg.getNewValue();
logger.logDebug("added resource " + addedResource.getURI());
}
}
}
}
|