Skip to content

Commit 6ab1980

Browse files
ptzieglerHannesWell
authored andcommitted
Initial support for parent context-handling
1 parent e02a06f commit 6ab1980

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
@@ -60,9 +60,14 @@ public XPathContext newContext(XPathContext parentContext, T contextBean) {
6060
if (!(contextBean instanceof EObject rootObject)) {
6161
throw new IllegalArgumentException();
6262
}
63-
// TODO: consider parent-context (may be null). Require the context-bean to be
64-
// from the same resource? Then we can also reuse all maps and the XPath object
65-
63+
if (parentContext != null) {
64+
EObjectContext parent = ((EObjectContext) parentContext);
65+
Element rootElement = parent.object2domProxy.get(contextBean);
66+
if (rootElement == null) {
67+
throw new IllegalArgumentException("Context bean is not from the same tree its parent context");
68+
}
69+
return new EObjectContext(rootElement, parent);
70+
}
6671
DocumentBuilder documentBuilder;
6772
try {
6873
documentBuilder = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder();
@@ -87,7 +92,7 @@ public XPathContext newContext(XPathContext parentContext, T contextBean) {
8792
domProxy2object.put(proxy, eObject);
8893
});
8994

90-
return new EObjectContext(rootElement, domProxy2object);
95+
return new EObjectContext(rootElement, domProxy2object, object2domProxy);
9196
}
9297

9398
private static class EObjectContext implements XPathContext {
@@ -97,14 +102,24 @@ private static class EObjectContext implements XPathContext {
97102
private final XPath xpath;
98103
private final Element rootElement;
99104
private final Map<Node, EObject> domProxy2object;
105+
private final Map<EObject, Element> object2domProxy;
100106

101-
private EObjectContext(Element rootElement, Map<Node, EObject> domProxy2object) {
107+
private EObjectContext(Element rootElement, Map<Node, EObject> domProxy2object,
108+
Map<EObject, Element> object2domProxy) {
102109
this.rootElement = rootElement;
103110
this.domProxy2object = Map.copyOf(domProxy2object);
111+
this.object2domProxy = Map.copyOf(object2domProxy);
104112
this.xpath = XPATH_FACTORY.newXPath();
105113
this.xpath.setXPathFunctionResolver(this::resolveEMFFunctions);
106114
}
107115

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

0 commit comments

Comments
 (0)