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