Jaxen binding should use DataSchemaContextTree
[yangtools.git] / yang / yang-data-jaxen / src / main / java / org / opendaylight / yangtools / yang / data / jaxen / NormalizedNodeContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.jaxen;
9
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;
13
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;
25
26 /**
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.
29  */
30 @NonNullByDefault
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;
36
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);
42         this.parent = parent;
43
44         setNodeSet(ImmutableList.of(this));
45     }
46
47     static NormalizedNodeContext forRoot(final NormalizedNodeContextSupport contextSupport) {
48         final JaxenDocument document = contextSupport.getNavigator().getDocument();
49         return new NormalizedNodeContext(contextSupport, document.getSchema(), document.getRootNode(), null);
50     }
51
52     NormalizedNode<?, ?> getNode() {
53         return node;
54     }
55
56     @Nullable NormalizedNodeContext getParent() {
57         return parent;
58     }
59
60     YangInstanceIdentifier getPath() {
61         return (parent == null ? YangInstanceIdentifier.EMPTY : parent.getPath()).node(node.getIdentifier());
62     }
63
64     DataSchemaContextNode<?> getSchema() {
65         return schema;
66     }
67
68     @Override
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.
73              *
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.
76              *
77              * When the predicate is being evaluated, each child is queried for its child -- but since it is protected,
78              * we cannot find it.
79              */
80             final DataSchemaNode mySchema = schema.getDataSchemaNode();
81             if (mySchema instanceof ListSchemaNode) {
82                 childSchema = verifyNotNull(schema.getChild(mySchema.getQName())).getChild(input.getIdentifier());
83             }
84         }
85
86         checkArgument(childSchema != null, "Failed to find schema for child %s", input);
87         return new NormalizedNodeContext(getContextSupport(), childSchema, input, this);
88     }
89 }