BUG-3876: Add jaxen-based XPath evaluation component
[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 com.google.common.base.Function;
11 import com.google.common.base.Preconditions;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.jaxen.Context;
15 import org.jaxen.ContextSupport;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17
18 /**
19  * Context wrapper around a {@link NormalizedNode} for use with Jaxen. It tracks the parent node for purposes of
20  * traversing upwards the NormalizedNode tree.
21  */
22 final class NormalizedNodeContext extends Context implements Function<NormalizedNode<?, ?>, NormalizedNodeContext> {
23     private static final long serialVersionUID = 1L;
24     private final NormalizedNodeContext parent;
25     private final NormalizedNode<?, ?> node;
26
27     NormalizedNodeContext(@Nonnull final ContextSupport contextSupport, @Nonnull final NormalizedNode<?, ?> node,
28         @Nullable final NormalizedNodeContext parent) {
29         super(contextSupport);
30         this.node = Preconditions.checkNotNull(node);
31         this.parent = parent;
32     }
33
34     @Nullable NormalizedNodeContext getParent() {
35         return parent;
36     }
37
38     @Nonnull NormalizedNode<?, ?> getNode() {
39         return node;
40     }
41
42     @Override
43     public NormalizedNodeContext apply(final NormalizedNode<?, ?> input) {
44         return new NormalizedNodeContext(getContextSupport(), input, this);
45     }
46 }