Skip to content

Commit

Permalink
Add XPathContext.stream() and strengthen type of getValue(String, Class)
Browse files Browse the repository at this point in the history
  • Loading branch information
HannesWell committed Sep 18, 2024
1 parent 7773e83 commit f8b4a24
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
11 changes: 11 additions & 0 deletions bundles/org.eclipse.e4.emf.xpath/.settings/.api_filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<component id="org.eclipse.e4.emf.xpath" version="2">
<resource path="src/org/eclipse/e4/emf/xpath/XPathContext.java" type="org.eclipse.e4.emf.xpath.XPathContext">
<filter id="404000815">
<message_arguments>
<message_argument value="org.eclipse.e4.emf.xpath.XPathContext"/>
<message_argument value="stream(String, Class&lt;T&gt;)"/>
</message_arguments>
</filter>
</resource>
</component>
2 changes: 1 addition & 1 deletion bundles/org.eclipse.e4.emf.xpath/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Bundle-Name
Bundle-SymbolicName: org.eclipse.e4.emf.xpath
Bundle-Version: 0.4.300.qualifier
Bundle-Version: 0.5.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-17
Require-Bundle: org.eclipse.emf.ecore;bundle-version="2.35.0",
org.eclipse.core.runtime;bundle-version="3.29.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.NodeSet;
import org.apache.commons.jxpath.Pointer;
import org.apache.commons.jxpath.util.TypeUtils;
import org.eclipse.e4.emf.xpath.XPathContext;
import org.eclipse.emf.ecore.EObject;

Expand Down Expand Up @@ -84,8 +85,11 @@ public Object getValue(String xpath) {
}

@Override
public Object getValue(String xpath, Class<?> requiredType) {
return context.getValue(xpath, requiredType);
public <T> T getValue(String xpath, Class<T> requiredType) {
Object value = context.getValue(xpath, requiredType);
@SuppressWarnings("unchecked")
T typedValue = (T) TypeUtils.convert(value, requiredType);
return typedValue;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
package org.eclipse.e4.emf.xpath;

import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
* Context in which the xpath is executed
Expand Down Expand Up @@ -42,7 +46,7 @@ public interface XPathContext {
* required type
* @return Object found
*/
Object getValue(String xpath, Class<?> requiredType);
<T> T getValue(String xpath, Class<T> requiredType);

/**
* Traverses the xpath and returns an Iterator of all results found for the
Expand All @@ -55,4 +59,22 @@ public interface XPathContext {
* @return Iterator&lt;Object&gt;
*/
<T> Iterator<T> iterate(String xpath);

/**
* Traverses the xpath and returns an {@link Stream} of all results found for
* the path. If the xpath matches no properties in the graph, the stream will be
* empty.
*
* @param <T> the expected object type
* @param xpath the xpath expression to iterate
* @param type the type of elements in the returned stream
* @return a stream of elements matching the specified xpath and of the given
* type
* @since 0.5
*/
default <T> Stream<T> stream(String xpath, Class<T> type) {
Iterator<?> iterator = iterate(xpath);
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false) //
.filter(type::isInstance).map(type::cast);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void testAccessingTheMainMenu() {
Object menu = xpathContext.getValue("//mainMenu");
assertNotNull(menu);
assertTrue(menu instanceof MMenu);

MMenu mMenu = xpathContext.getValue("//mainMenu", MMenu.class);
assertNotNull(mMenu);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public void testSimpleQuery() {
assertNotNull(application);
assertSame(RootImpl.class, application.getClass());

RootImpl rootApplication = xpathContext.getValue("/", RootImpl.class);
assertNotNull(rootApplication);

application = xpathContext.getValue(".");
assertNotNull(application);
assertSame(RootImpl.class, application.getClass());
Expand All @@ -85,14 +88,19 @@ public void testSimpleQuery() {
assertNotNull(application);
assertSame(RootImpl.class, application.getClass());

rootApplication = xpathContext.getValue(".[@id='root']", RootImpl.class);
assertNotNull(rootApplication);

assertEquals("element1",xpathContext.getValue("nodes[1]/@id"));

assertEquals(NodeImpl.class, xpathContext.getValue("//.[@id='element2.2']").getClass());
assertEquals(ExtendedNodeImpl.class,xpathContext.getValue("//.[ecore:eClassName(.)='ExtendedNode']").getClass());

ExtendedNodeImpl extendedNode = xpathContext.getValue("//.[ecore:eClassName(.)='ExtendedNode']",
ExtendedNodeImpl.class);
assertNotNull(extendedNode);
}


@Test
public void testMenuQuery() {
Object application = xpathContext.getValue("/");
Expand Down

0 comments on commit f8b4a24

Please sign in to comment.