2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.jaxen;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13 import static java.util.Objects.requireNonNull;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterators;
17 import java.util.Collection;
18 import java.util.Iterator;
19 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.jaxen.Context;
24 import org.jaxen.ContextSupport;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
30 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
39 * Context wrapper around a {@link NormalizedNode} for use with Jaxen. It tracks the parent node for purposes of
40 * traversing upwards the NormalizedNode tree.
43 final class NormalizedNodeContext extends Context {
44 private static final long serialVersionUID = 1L;
45 private final @Nullable NormalizedNodeContext parent;
46 private final DataSchemaContextNode<?> schema;
47 private final NormalizedNode node;
49 private NormalizedNodeContext(final ContextSupport contextSupport, final DataSchemaContextNode<?> schema,
50 final NormalizedNode node, final @Nullable NormalizedNodeContext parent) {
51 super(contextSupport);
52 this.schema = requireNonNull(schema);
53 this.node = requireNonNull(node);
56 setNodeSet(ImmutableList.of(this));
59 static NormalizedNodeContext forRoot(final NormalizedNodeContextSupport contextSupport) {
60 final JaxenDocument document = contextSupport.getNavigator().getDocument();
61 return new NormalizedNodeContext(contextSupport, document.getSchema(), document.getRootNode(), null);
64 NormalizedNode getNode() {
68 @Nullable NormalizedNodeContext getParent() {
72 YangInstanceIdentifier getPath() {
73 return (parent == null ? YangInstanceIdentifier.empty() : parent.getPath()).node(node.getIdentifier());
76 DataSchemaContextNode<?> getSchema() {
80 NormalizedNodeContext createChild(final NormalizedNode input) {
81 DataSchemaContextNode<?> childSchema = schema.getChild(input.getIdentifier());
82 if (childSchema == null) {
83 /* This feels very much like a hack: but solves lookup of child nodes with predicates.
85 * What is happening is that a Map context gets queried for its children, which results in contexts being
86 * backed by UnorderedMapMixinContextNode, which requires us to unmask it.
88 * When the predicate is being evaluated, each child is queried for its child -- but since it is protected,
91 final DataSchemaNode mySchema = schema.getDataSchemaNode();
92 if (mySchema instanceof ListSchemaNode) {
93 childSchema = verifyNotNull(schema.getChild(mySchema.getQName())).getChild(input.getIdentifier());
97 checkArgument(childSchema != null, "Failed to find schema for child %s", input);
98 return new NormalizedNodeContext(getContextSupport(), childSchema, input, this);
101 Optional<NormalizedNodeContext> findChild(final PathArgument arg) {
102 return node instanceof DataContainerNode
103 ? ((DataContainerNode<?>)node).findChildByArg(arg).map(this::createChild) : Optional.empty();
106 Optional<NormalizedNodeContext> findDescendant(final YangInstanceIdentifier path) {
107 if (path.isEmpty()) {
108 return Optional.of(this);
111 NormalizedNodeContext ctxWalk = this;
112 NormalizedNode dataWalk = node;
113 for (PathArgument arg : path.getPathArguments()) {
114 checkArgument(dataWalk instanceof DataContainerNode, "Path %s refers beyond node %s", path, dataWalk);
116 dataWalk = ((DataContainerNode<?>)dataWalk).childByArg(arg);
117 if (dataWalk == null) {
118 return Optional.empty();
121 ctxWalk = createChild(dataWalk);
124 return Optional.of(ctxWalk.createChild(dataWalk));
127 Iterator<NormalizedNodeContext> iterateChildren(final DataContainerNode<?> data) {
128 return Iterators.transform(((DataContainerNode<?>) node).body().iterator(), this::createChild);
131 @Nullable Iterator<NormalizedNodeContext> iterateChildrenNamed(final DataContainerNode<?> data, final QName qname) {
132 final DataContainerChild child = data.childByArg(new NodeIdentifier(qname));
137 final Collection<? extends NormalizedNode> collection;
138 // The child may be a structural node
139 if (child instanceof MapNode) {
140 collection = ((MapNode)child).body();
141 } else if (child instanceof LeafSetNode) {
142 collection = ((LeafSetNode<?>)child).body();
144 return Iterators.singletonIterator(createChild(child));
147 return Iterators.transform(collection.iterator(), this::createChild);
150 static NormalizedNodeContext cast(@Nullable final Context context) {
151 verify(context instanceof NormalizedNodeContext);
152 return (@NonNull NormalizedNodeContext) context;