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.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.Function;
15 import com.google.common.collect.ImmutableList;
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.jaxen.Context;
19 import org.jaxen.ContextSupport;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
27 * Context wrapper around a {@link NormalizedNode} for use with Jaxen. It tracks the parent node for purposes of
28 * traversing upwards the NormalizedNode tree.
31 final class NormalizedNodeContext extends Context implements Function<NormalizedNode<?, ?>, NormalizedNodeContext> {
32 private static final long serialVersionUID = 1L;
33 private final @Nullable NormalizedNodeContext parent;
34 private final DataSchemaContextNode<?> schema;
35 private final NormalizedNode<?, ?> node;
37 private NormalizedNodeContext(final ContextSupport contextSupport, final DataSchemaContextNode<?> schema,
38 final NormalizedNode<?, ?> node, final @Nullable NormalizedNodeContext parent) {
39 super(contextSupport);
40 this.schema = requireNonNull(schema);
41 this.node = requireNonNull(node);
44 setNodeSet(ImmutableList.of(this));
47 static NormalizedNodeContext forRoot(final NormalizedNodeContextSupport contextSupport) {
48 final JaxenDocument document = contextSupport.getNavigator().getDocument();
49 return new NormalizedNodeContext(contextSupport, document.getSchema(), document.getRootNode(), null);
52 NormalizedNode<?, ?> getNode() {
56 @Nullable NormalizedNodeContext getParent() {
60 YangInstanceIdentifier getPath() {
61 return (parent == null ? YangInstanceIdentifier.EMPTY : parent.getPath()).node(node.getIdentifier());
64 DataSchemaContextNode<?> getSchema() {
69 public NormalizedNodeContext apply(final NormalizedNode<?, ?> input) {
70 DataSchemaContextNode<?> childSchema = schema.getChild(input.getIdentifier());
71 if (childSchema == null) {
72 /* This feels very much like a hack: but solves lookup of child nodes with predicates.
74 * What is happening is that a Map context gets queried for its children, which results in contexts being
75 * backed by UnorderedMapMixinContextNode, which requires us to unmask it.
77 * When the predicate is being evaluated, each child is queried for its child -- but since it is protected,
80 final DataSchemaNode mySchema = schema.getDataSchemaNode();
81 if (mySchema instanceof ListSchemaNode) {
82 childSchema = verifyNotNull(schema.getChild(mySchema.getQName())).getChild(input.getIdentifier());
86 checkArgument(childSchema != null, "Failed to find schema for child %s", input);
87 return new NormalizedNodeContext(getContextSupport(), childSchema, input, this);