Skip to content

Commit 063ed05

Browse files
ptzieglerHannesWell
authored andcommitted
Initial support for parent context-handling
1 parent b7ca926 commit 063ed05

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

bundles/org.eclipse.e4.emf.xpath/src/org/eclipse/e4/emf/xpath/internal/java/JavaXPathContextFactoryImpl.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,14 @@ public XPathContext newContext(XPathContext parentContext, T contextBean) {
5959
if (!(contextBean instanceof EObject rootObject)) {
6060
throw new IllegalArgumentException();
6161
}
62-
// TODO: consider parent-context (may be null). Require the context-bean to be
63-
// from the same resource? Then we can also reuse all maps and the XPath object
64-
62+
if (parentContext != null) {
63+
EObjectContext parent = ((EObjectContext) parentContext);
64+
Element rootElement = parent.object2domProxy.get(contextBean);
65+
if (rootElement == null) {
66+
throw new IllegalArgumentException("Context bean is not from the same tree its parent context");
67+
}
68+
return new EObjectContext(rootElement, parent);
69+
}
6570
DocumentBuilder documentBuilder;
6671
try {
6772
documentBuilder = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder();
@@ -86,7 +91,7 @@ public XPathContext newContext(XPathContext parentContext, T contextBean) {
8691
domProxy2object.put(proxy, eObject);
8792
});
8893

89-
return new EObjectContext(rootElement, domProxy2object);
94+
return new EObjectContext(rootElement, domProxy2object, object2domProxy);
9095
}
9196

9297
private static class EObjectContext implements XPathContext {
@@ -96,14 +101,24 @@ private static class EObjectContext implements XPathContext {
96101
private final XPath xpath;
97102
private final Element rootElement;
98103
private final Map<Node, EObject> domProxy2object;
104+
private final Map<EObject, Element> object2domProxy;
99105

100-
private EObjectContext(Element rootElement, Map<Node, EObject> domProxy2object) {
106+
private EObjectContext(Element rootElement, Map<Node, EObject> domProxy2object,
107+
Map<EObject, Element> object2domProxy) {
101108
this.rootElement = rootElement;
102109
this.domProxy2object = Map.copyOf(domProxy2object);
110+
this.object2domProxy = Map.copyOf(object2domProxy);
103111
this.xpath = XPATH_FACTORY.newXPath();
104112
this.xpath.setXPathFunctionResolver(this::resolveEMFFunctions);
105113
}
106114

115+
private EObjectContext(Element rootElement, EObjectContext parentContext) {
116+
this.rootElement = rootElement;
117+
this.domProxy2object = Map.copyOf(parentContext.domProxy2object);
118+
this.object2domProxy = Map.copyOf(parentContext.object2domProxy);
119+
this.xpath = parentContext.xpath;
120+
}
121+
107122
@Override
108123
public <R> Stream<R> stream(String path, Class<R> resultType) {
109124
// See XPathResultType for generally supported result types

0 commit comments

Comments
 (0)