Skip to content

Commit a8abde2

Browse files
committed
[E4] Introduce EModelService.findMatchingElements() as Xpath replacement
1 parent a879598 commit a8abde2

File tree

5 files changed

+54
-15
lines changed

5 files changed

+54
-15
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025, 2025 Hannes Wellmann and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Hannes Wellmann - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.e4.ui.model;
15+
16+
import java.util.stream.Stream;
17+
import org.eclipse.core.runtime.ILog;
18+
import org.eclipse.e4.emf.xpath.XPathContextFactory;
19+
import org.eclipse.emf.ecore.EObject;
20+
21+
public class XPathHelper {
22+
23+
public static <T> Stream<T> findMatchingElements(EObject contextBean, String xpath, Class<T> clazz) {
24+
try {
25+
return XPathContextFactory.newInstance().newContext(contextBean).stream(xpath, clazz);
26+
} catch (Exception ex) {
27+
// custom xpath functions will throw exceptions
28+
ILog.get().error("Failed to evaluate xpath: " + xpath, ex); //$NON-NLS-1$
29+
return Stream.empty();
30+
}
31+
}
32+
33+
// Inline the then still used part of JavaXPathContextFactoryImpl here once the
34+
// e4.emf.xpath bundle is finally removed
35+
}

bundles/org.eclipse.e4.ui.model.workbench/src/org/eclipse/e4/ui/model/fragment/impl/StringModelFragmentImpl.java

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
import java.util.ArrayList;
1919
import java.util.List;
2020
import java.util.regex.Pattern;
21-
import org.eclipse.e4.emf.xpath.EcoreXPathContextFactory;
22-
import org.eclipse.e4.emf.xpath.XPathContext;
23-
import org.eclipse.e4.emf.xpath.XPathContextFactory;
21+
import org.eclipse.e4.ui.model.XPathHelper;
2422
import org.eclipse.e4.ui.model.application.MApplication;
2523
import org.eclipse.e4.ui.model.application.MApplicationElement;
2624
import org.eclipse.e4.ui.model.fragment.MStringModelFragment;
@@ -282,10 +280,10 @@ public boolean eIsSet(int featureID) {
282280
return FEATURENAME_EDEFAULT == null ? featurename != null : !FEATURENAME_EDEFAULT.equals(featurename);
283281
case FragmentPackageImpl.STRING_MODEL_FRAGMENT__PARENT_ELEMENT_ID:
284282
return PARENT_ELEMENT_ID_EDEFAULT == null ? parentElementId != null
285-
: !PARENT_ELEMENT_ID_EDEFAULT.equals(parentElementId);
283+
: !PARENT_ELEMENT_ID_EDEFAULT.equals(parentElementId);
286284
case FragmentPackageImpl.STRING_MODEL_FRAGMENT__POSITION_IN_LIST:
287285
return POSITION_IN_LIST_EDEFAULT == null ? positionInList != null
288-
: !POSITION_IN_LIST_EDEFAULT.equals(positionInList);
286+
: !POSITION_IN_LIST_EDEFAULT.equals(positionInList);
289287
default:
290288
return super.eIsSet(featureID);
291289
}
@@ -355,15 +353,8 @@ private void mergeXPath(MApplication application, List<MApplicationElement> ret,
355353
if ("/".equals(xPath)) {
356354
targetElements = List.of(application);
357355
} else {
358-
XPathContextFactory<EObject> f = EcoreXPathContextFactory.newInstance();
359-
XPathContext xpathContext = f.newContext((EObject) application);
360-
try {
361-
targetElements = xpathContext.stream(xPath, MApplicationElement.class).toList();
362-
} catch (Exception ex) {
363-
targetElements = List.of();
364-
// custom xpath functions will throw exceptions
365-
ex.printStackTrace();
366-
}
356+
EObject appRoot = (EObject) application;
357+
targetElements = XPathHelper.findMatchingElements(appRoot, xPath, MApplicationElement.class).toList();
367358
}
368359
for (MApplicationElement targetElement : targetElements) {
369360
EStructuralFeature feature = ((EObject) targetElement).eClass().getEStructuralFeature(getFeaturename());

bundles/org.eclipse.e4.ui.workbench/META-INF/MANIFEST.MF

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-SymbolicName: org.eclipse.e4.ui.workbench;singleton:=true
4-
Bundle-Version: 1.16.100.qualifier
4+
Bundle-Version: 1.17.0.qualifier
55
Bundle-Name: %pluginName
66
Bundle-Vendor: %providerName
77
Bundle-Localization: plugin

bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/internal/workbench/ModelServiceImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727
import java.util.LinkedHashSet;
2828
import java.util.List;
2929
import java.util.Objects;
30+
import java.util.stream.Stream;
3031
import org.eclipse.core.runtime.Assert;
3132
import org.eclipse.core.runtime.IExtensionRegistry;
3233
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
3334
import org.eclipse.e4.core.contexts.IEclipseContext;
3435
import org.eclipse.e4.core.services.events.IEventBroker;
3536
import org.eclipse.e4.core.services.log.Logger;
37+
import org.eclipse.e4.ui.model.XPathHelper;
3638
import org.eclipse.e4.ui.model.application.MAddon;
3739
import org.eclipse.e4.ui.model.application.MApplication;
3840
import org.eclipse.e4.ui.model.application.MApplicationElement;
@@ -434,6 +436,11 @@ public <T> List<T> findElements(MApplicationElement searchRoot, Class<T> clazz,
434436
return new ArrayList<>(elements);
435437
}
436438

439+
@Override
440+
public <T> Stream<T> findMatchingElements(MApplicationElement searchRoot, String xpath, Class<T> clazz) {
441+
return XPathHelper.findMatchingElements((EObject) searchRoot, xpath, clazz);
442+
}
443+
437444
private <T> Iterable<T> findPerspectiveElements(MUIElement searchRoot, String id,
438445
Class<T> clazz,
439446
List<String> tagsToMatch) {

bundles/org.eclipse.e4.ui.workbench/src/org/eclipse/e4/ui/workbench/modeling/EModelService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.eclipse.e4.ui.workbench.modeling;
1818

1919
import java.util.List;
20+
import java.util.stream.Stream;
2021
import org.eclipse.e4.core.contexts.IEclipseContext;
2122
import org.eclipse.e4.ui.model.application.MApplicationElement;
2223
import org.eclipse.e4.ui.model.application.descriptor.basic.MPartDescriptor;
@@ -279,6 +280,11 @@ <T> List<T> findElements(MUIElement searchRoot, String id, Class<T> clazz,
279280
<T> List<T> findElements(MApplicationElement searchRoot, Class<T> clazz,
280281
int searchFlags, Selector matcher);
281282

283+
/**
284+
* @since 1.17
285+
*/
286+
<T> Stream<T> findMatchingElements(MApplicationElement contextBean, String xpath, Class<T> clazz);
287+
282288
/**
283289
* Returns the first element, recursively searching under the specified search
284290
* root (inclusive)

0 commit comments

Comments
 (0)