import com.google.common.annotations.Beta;
import java.util.Collection;
+import java.util.Map.Entry;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
/**
- * An {@link XPathResult} containing a set of nodes.
+ * An {@link XPathResult} containing a set of nodes. Resulting nodes are identified by their
+ * {@link YangInstanceIdentifier}.
*/
@Beta
-public interface XPathNodesetResult extends XPathResult<Collection<NormalizedNode<?, ?>>> {
+public interface XPathNodesetResult
+ extends XPathResult<Collection<Entry<YangInstanceIdentifier, NormalizedNode<?, ?>>>> {
}
import static java.util.Objects.requireNonNull;
import com.google.common.base.Converter;
+import com.google.common.base.Verify;
import com.google.common.collect.Lists;
+import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nonnull;
}
if (result instanceof String) {
- return Optional.of((XPathStringResult) () -> (String)result);
+ return Optional.of((XPathStringResult) () -> (String) result);
} else if (result instanceof Number) {
return Optional.of((XPathNumberResult) () -> (Number) result);
} else if (result instanceof Boolean) {
return Optional.of((XPathBooleanResult) () -> (Boolean) result);
- } else if (result != null) {
- return Optional.of((XPathNodesetResult) () -> {
- // XXX: Will this really work, or do we need to perform deep transformation?
- return Lists.transform((List<NormalizedNodeContext>) result, NormalizedNodeContext::getNode);
- });
- } else {
+ } else if (result == null) {
return Optional.empty();
}
+
+ Verify.verify(result instanceof List, "Unhandled result %s", result);
+ @SuppressWarnings("unchecked")
+ final List<NormalizedNodeContext> resultList = (List<NormalizedNodeContext>) result;
+ return Optional.of((XPathNodesetResult) () -> {
+ // XXX: Will this really work, or do we need to perform deep transformation?
+ return Lists.transform(resultList,
+ context -> new SimpleImmutableEntry<>(context.getPath(), context.getNode()));
+ });
}
@Nonnull
import javax.annotation.Nullable;
import org.jaxen.Context;
import org.jaxen.ContextSupport;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
/**
setNodeSet(ImmutableList.of(this));
}
+ @Nonnull NormalizedNode<?, ?> getNode() {
+ return node;
+ }
+
@Nullable NormalizedNodeContext getParent() {
return parent;
}
- @Nonnull NormalizedNode<?, ?> getNode() {
- return node;
+ @Nonnull YangInstanceIdentifier getPath() {
+ return (parent == null ? YangInstanceIdentifier.EMPTY : parent.getPath()).node(node.getIdentifier());
}
@Override
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
import javax.xml.xpath.XPathExpressionException;
import org.jaxen.Context;
assertNotNull(resultExpressionEvaluate);
assertTrue(resultExpressionEvaluate.isPresent());
XPathResult<?> xpathResult = resultExpressionEvaluate.get();
- Object value = ((XPathNodesetResult) xpathResult).getValue().iterator().next().getValue();
- assertNotNull(value);
- assertEquals("three", value);
+ assertTrue(xpathResult instanceof XPathNodesetResult);
+ XPathNodesetResult nodeset = (XPathNodesetResult) xpathResult;
+
+ Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = nodeset.getValue().iterator().next();
+ assertNotNull(entry);
+ assertEquals("three", entry.getValue().getValue());
convertNctx = new ConverterNamespaceContext(createPrefixes());
navigator = new NormalizedNodeNavigator(convertNctx, (JaxenDocument) xpathDocument);
.evaluate(xpathDocument, createYangInstanceIdentifier(true));
assertTrue(resultExpressionEvaluate.isPresent());
XPathResult<?> xpathResult = resultExpressionEvaluate.get();
- Object value = ((XPathNodesetResult) xpathResult).getValue().iterator().next().getValue();
- assertEquals("two", value);
+ assertTrue(xpathResult instanceof XPathNodesetResult);
+ XPathNodesetResult nodeset = (XPathNodesetResult) xpathResult;
+
+ Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> entry = nodeset.getValue().iterator().next();
+ assertNotNull(entry);
+ assertEquals("two", entry.getValue().getValue());
}
@Test(expected = VerifyException.class)