Skip to content

Commit

Permalink
Initial support for parent context-handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ptziegler authored and HannesWell committed Jan 18, 2025
1 parent b7ca926 commit 063ed05
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,14 @@ public XPathContext newContext(XPathContext parentContext, T contextBean) {
if (!(contextBean instanceof EObject rootObject)) {
throw new IllegalArgumentException();
}
// TODO: consider parent-context (may be null). Require the context-bean to be
// from the same resource? Then we can also reuse all maps and the XPath object

if (parentContext != null) {
EObjectContext parent = ((EObjectContext) parentContext);
Element rootElement = parent.object2domProxy.get(contextBean);
if (rootElement == null) {
throw new IllegalArgumentException("Context bean is not from the same tree its parent context");
}
return new EObjectContext(rootElement, parent);
}
DocumentBuilder documentBuilder;
try {
documentBuilder = DocumentBuilderFactory.newDefaultInstance().newDocumentBuilder();
Expand All @@ -86,7 +91,7 @@ public XPathContext newContext(XPathContext parentContext, T contextBean) {
domProxy2object.put(proxy, eObject);
});

return new EObjectContext(rootElement, domProxy2object);
return new EObjectContext(rootElement, domProxy2object, object2domProxy);
}

private static class EObjectContext implements XPathContext {
Expand All @@ -96,14 +101,24 @@ private static class EObjectContext implements XPathContext {
private final XPath xpath;
private final Element rootElement;
private final Map<Node, EObject> domProxy2object;
private final Map<EObject, Element> object2domProxy;

private EObjectContext(Element rootElement, Map<Node, EObject> domProxy2object) {
private EObjectContext(Element rootElement, Map<Node, EObject> domProxy2object,
Map<EObject, Element> object2domProxy) {
this.rootElement = rootElement;
this.domProxy2object = Map.copyOf(domProxy2object);
this.object2domProxy = Map.copyOf(object2domProxy);
this.xpath = XPATH_FACTORY.newXPath();
this.xpath.setXPathFunctionResolver(this::resolveEMFFunctions);
}

private EObjectContext(Element rootElement, EObjectContext parentContext) {
this.rootElement = rootElement;
this.domProxy2object = Map.copyOf(parentContext.domProxy2object);
this.object2domProxy = Map.copyOf(parentContext.object2domProxy);
this.xpath = parentContext.xpath;
}

@Override
public <R> Stream<R> stream(String path, Class<R> resultType) {
// See XPathResultType for generally supported result types
Expand Down

0 comments on commit 063ed05

Please sign in to comment.