Populate model/ hierarchy
[yangtools.git] / yang / yang-xpath-api / src / main / java / org / opendaylight / yangtools / yang / xpath / api / YangXPathAxis.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o.  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.xpath.api;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Collection;
15 import org.opendaylight.yangtools.yang.common.AbstractQName;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.AxisStep;
18 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.AxisStepWithPredicates;
19 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.NodeTypeStep;
20 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.NodeTypeStepWithPredicates;
21 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.ProcessingInstructionStep;
22 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.ProcessingInstructionStepWithPredicates;
23 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.ResolvedQNameStep;
24 import org.opendaylight.yangtools.yang.xpath.api.YangLocationPath.UnresolvedQNameStep;
25
26 /**
27  * XPath evaluation axis, as defined in <a href="https://www.w3.org/TR/1999/REC-xpath-19991116/#axes">XPath 1.0</a>.
28  *
29  * @author Robert Varga
30  */
31 @Beta
32 public enum YangXPathAxis {
33     /**
34      * The {@code child} axis.
35      */
36     CHILD("child"),
37     /**
38      * The {@code descendant} axis.
39      */
40     DESCENDANT("descendant"),
41     /**
42      * The {@code parent} axis.
43      */
44     PARENT("parent"),
45     /**
46      * The {@code ancestor} axis.
47      */
48     ANCESTOR("ancestor"),
49     /**
50      * The {@code following-sibling} axis.
51      */
52     FOLLOWING_SIBLING("following-sibling"),
53     /**
54      * The {@code preceding-sibling} axis.
55      */
56     PRECEDING_SIBLING("preceding-sibling"),
57     /**
58      * The {@code following} axis.
59      */
60     FOLLOWING("following"),
61     /**
62      * The {@code preceding} axis.
63      */
64     PRECEDING("preceding"),
65     /**
66      * The {@code attribute} axis.
67      */
68     ATTRIBUTE("attribute"),
69     /**
70      * The {@code namespace} axis.
71      */
72     NAMESPACE("namespace"),
73     /**
74      * The {@code self} axis.
75      */
76     SELF("self"),
77     /**
78      * The {@code descendant-or-self} axis.
79      */
80     DESCENDANT_OR_SELF("descendant-or-self"),
81     /**
82      * The {@code ancestor-or-self} axis.
83      */
84     ANCESTOR_OR_SELF("ancestor-or-self");
85
86     private final AxisStep step = new AxisStep(this);
87     private final String str;
88
89     YangXPathAxis(final String str) {
90         this.str = requireNonNull(str);
91     }
92
93     /**
94      * Return the name-independent {@link AxisStep} along this axis. XPath defines following axis {@code AxisStep}s:
95      * <ul>
96      *     <li>{@link #SELF} axis this equals to the "." step</li>
97      *     <li>{@link #PARENT} axis this equals to the ".." step</li>
98      *     <li>{@link #DESCENDANT_OR_SELF} axis this equals to the "//" separator</li>
99      * </ul>
100      * other axes have these defined as a courtesy.
101      *
102      * @return Name-independent AnyNameStep.
103      */
104     public final AxisStep asStep() {
105         return step;
106     }
107
108     public final AxisStep asStep(final Collection<YangExpr> predicates) {
109         return predicates.isEmpty() ? step : new AxisStepWithPredicates(this, ImmutableSet.copyOf(predicates));
110     }
111
112     public final ResolvedQNameStep asStep(final QName qname) {
113         return new ResolvedQNameStep(this, qname);
114     }
115
116     public final ResolvedQNameStep asStep(final QName qname, final Collection<YangExpr> predicates) {
117         return ResolvedQNameStep.of(this, qname, predicates);
118     }
119
120     public final UnresolvedQNameStep asStep(final AbstractQName qname) {
121         return new UnresolvedQNameStep(this, qname);
122     }
123
124     public final UnresolvedQNameStep asStep(final AbstractQName qname, final Collection<YangExpr> predicates) {
125         return UnresolvedQNameStep.of(this, qname, predicates);
126     }
127
128     public final NodeTypeStep asStep(final YangXPathNodeType type) {
129         return new NodeTypeStep(this, type);
130     }
131
132     public final NodeTypeStep asStep(final YangXPathNodeType type, final Collection<YangExpr> predicates) {
133         return predicates.isEmpty() ? asStep(type) : new NodeTypeStepWithPredicates(this, type,
134             ImmutableSet.copyOf(predicates));
135     }
136
137     public final ProcessingInstructionStep asStep(final String name) {
138         return new ProcessingInstructionStep(this, name);
139     }
140
141     public final ProcessingInstructionStep asStep(final String name, final Collection<YangExpr> predicates) {
142         return predicates.isEmpty() ? asStep(name) : new ProcessingInstructionStepWithPredicates(this, name,
143             ImmutableSet.copyOf(predicates));
144     }
145
146     @Override
147     public String toString() {
148         return str;
149     }
150 }