diff options
Diffstat (limited to 'subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescription.java')
-rw-r--r-- | subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescription.java | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescription.java b/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescription.java new file mode 100644 index 00000000..498a7c57 --- /dev/null +++ b/subprojects/language/src/main/java/tools/refinery/language/resource/ProblemResourceDescription.java | |||
@@ -0,0 +1,106 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2009, 2011 itemis AG (http://www.itemis.eu) and others. | ||
3 | * Copyright (c) 2024 The Refinery Authors <https://refinery.tools/> | ||
4 | * This program and the accompanying materials are made available under the | ||
5 | * terms of the Eclipse Public License 2.0 which is available at | ||
6 | * http://www.eclipse.org/legal/epl-2.0. | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.language.resource; | ||
10 | |||
11 | import org.apache.log4j.Logger; | ||
12 | import org.eclipse.emf.common.util.TreeIterator; | ||
13 | import org.eclipse.emf.ecore.EObject; | ||
14 | import org.eclipse.emf.ecore.resource.Resource; | ||
15 | import org.eclipse.emf.ecore.util.EcoreUtil; | ||
16 | import org.eclipse.xtext.resource.IDefaultResourceDescriptionStrategy; | ||
17 | import org.eclipse.xtext.resource.IEObjectDescription; | ||
18 | import org.eclipse.xtext.resource.impl.DefaultResourceDescription; | ||
19 | import org.eclipse.xtext.resource.impl.EObjectDescriptionLookUp; | ||
20 | import org.eclipse.xtext.util.IAcceptor; | ||
21 | import tools.refinery.language.naming.NamingUtil; | ||
22 | |||
23 | import java.io.IOException; | ||
24 | import java.util.*; | ||
25 | |||
26 | /** | ||
27 | * A resource description that takes {@link ProblemResourceDescriptionStrategy#SHADOWING_KEY} into account when | ||
28 | * describing EObjects. | ||
29 | * <p> | ||
30 | * Based on {@link DefaultResourceDescription}. | ||
31 | */ | ||
32 | public class ProblemResourceDescription extends DefaultResourceDescription { | ||
33 | private static final Logger log = Logger.getLogger(ProblemResourceDescription.class); | ||
34 | |||
35 | private final IDefaultResourceDescriptionStrategy strategy; | ||
36 | |||
37 | public ProblemResourceDescription(Resource resource, IDefaultResourceDescriptionStrategy strategy) { | ||
38 | super(resource, strategy); | ||
39 | this.strategy = strategy; | ||
40 | } | ||
41 | |||
42 | /** | ||
43 | * Based on {@link DefaultResourceDescription#computeExportedObjects()}. | ||
44 | * | ||
45 | * @return The computed exported objects, taking shadowing into account. | ||
46 | */ | ||
47 | @Override | ||
48 | protected List<IEObjectDescription> computeExportedObjects() { | ||
49 | if (!getResource().isLoaded()) { | ||
50 | try { | ||
51 | getResource().load(null); | ||
52 | } catch (IOException e) { | ||
53 | log.error(e.getMessage(), e); | ||
54 | return Collections.emptyList(); | ||
55 | } | ||
56 | } | ||
57 | final Map<ProblemResourceDescriptionStrategy.ShadowingKey, List<IEObjectDescription>> nameToDescriptionsMap = | ||
58 | new LinkedHashMap<>(); | ||
59 | IAcceptor<IEObjectDescription> acceptor = eObjectDescription -> { | ||
60 | var key = ProblemResourceDescriptionStrategy.getShadowingKey(eObjectDescription); | ||
61 | var descriptions = nameToDescriptionsMap.computeIfAbsent(key, ignored -> new ArrayList<>()); | ||
62 | descriptions.add(eObjectDescription); | ||
63 | }; | ||
64 | TreeIterator<EObject> allProperContents = EcoreUtil.getAllProperContents(getResource(), false); | ||
65 | while (allProperContents.hasNext()) { | ||
66 | EObject content = allProperContents.next(); | ||
67 | if (!strategy.createEObjectDescriptions(content, acceptor)) { | ||
68 | allProperContents.prune(); | ||
69 | } | ||
70 | } | ||
71 | return omitShadowedNames(nameToDescriptionsMap); | ||
72 | } | ||
73 | |||
74 | private static List<IEObjectDescription> omitShadowedNames( | ||
75 | Map<ProblemResourceDescriptionStrategy.ShadowingKey, List<IEObjectDescription>> nameToDescriptionsMap) { | ||
76 | final List<IEObjectDescription> exportedEObjects = new ArrayList<>(); | ||
77 | for (var entry : nameToDescriptionsMap.entrySet()) { | ||
78 | var descriptions = entry.getValue(); | ||
79 | if (NamingUtil.isFullyQualified(entry.getKey().name())) { | ||
80 | exportedEObjects.addAll(descriptions); | ||
81 | } else { | ||
82 | boolean foundPreferred = false; | ||
83 | for (var description : descriptions) { | ||
84 | if (ProblemResourceDescriptionStrategy.PREFERRED_NAME_TRUE.equals( | ||
85 | description.getUserData(ProblemResourceDescriptionStrategy.PREFERRED_NAME))) { | ||
86 | exportedEObjects.add(description); | ||
87 | foundPreferred = true; | ||
88 | } | ||
89 | } | ||
90 | if (!foundPreferred) { | ||
91 | exportedEObjects.addAll(descriptions); | ||
92 | } | ||
93 | } | ||
94 | } | ||
95 | return exportedEObjects; | ||
96 | } | ||
97 | |||
98 | // Based on {@code DerivedStateAwareResourceDescriptionManager#createResourceDescription}. | ||
99 | @Override | ||
100 | protected EObjectDescriptionLookUp getLookUp() { | ||
101 | if (lookup == null) { | ||
102 | lookup = new EObjectDescriptionLookUp(computeExportedObjects()); | ||
103 | } | ||
104 | return lookup; | ||
105 | } | ||
106 | } | ||