Introducing Antlr4 for cpsPath parsing
[cps.git] / cps-path-parser / src / main / java / org / onap / cps / cpspath / parser / CpsPathBuilder.java
1 package org.onap.cps.cpspath.parser;
2 /*
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2021 Nordix Foundation
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener;
24 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext;
25 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithDescendantAndLeafConditionsContext;
26 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithDescendantContext;
27 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithSingleLeafConditionContext;
28 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext;
29 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleValueConditionsContext;
30 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext;
31 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.SingleValueConditionContext;
32
33 public class CpsPathBuilder extends CpsPathBaseListener {
34
35     final CpsPathQuery cpsPathQuery = new CpsPathQuery();
36
37     final Map<String, Object> leavesData = new HashMap<>();
38
39     @Override
40     public void exitPrefix(final PrefixContext ctx) {
41         cpsPathQuery.setXpathPrefix(ctx.getText());
42     }
43
44     @Override
45     public void exitLeafCondition(final LeafConditionContext ctx) {
46         Object comparisonValue = null;
47         if (ctx.IntegerLiteral() != null) {
48             comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
49         }
50         if (ctx.StringLiteral() != null) {
51             comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
52         } else if (comparisonValue == null) {
53             throw new IllegalStateException("Unsupported comparison value encountered in expression" + ctx.getText());
54         }
55         leavesData.put(ctx.leafName().getText(), comparisonValue);
56     }
57
58     @Override
59     public void enterSingleValueCondition(final SingleValueConditionContext ctx) {
60         leavesData.clear();
61     }
62
63     @Override
64     public void enterMultipleValueConditions(final MultipleValueConditionsContext ctx) {
65         leavesData.clear();
66     }
67
68     @Override
69     public void exitSingleValueCondition(final SingleValueConditionContext ctx) {
70         final String leafName = ctx.leafCondition().leafName().getText();
71         cpsPathQuery.setLeafName(leafName);
72         cpsPathQuery.setLeafValue(leavesData.get(leafName));
73     }
74
75     @Override
76     public void exitCpsPathWithSingleLeafCondition(final CpsPathWithSingleLeafConditionContext ctx) {
77         cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE);
78     }
79
80     @Override
81     public void exitCpsPathWithDescendant(final CpsPathWithDescendantContext ctx) {
82         cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE);
83         cpsPathQuery.setDescendantName(cpsPathQuery.getXpathPrefix().substring(1));
84     }
85
86     @Override
87     public void exitCpsPathWithDescendantAndLeafConditions(
88         final CpsPathWithDescendantAndLeafConditionsContext ctx) {
89         cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES);
90         cpsPathQuery.setDescendantName(cpsPathQuery.getXpathPrefix().substring(1));
91         cpsPathQuery.setLeavesData(leavesData);
92     }
93
94     @Override
95     public void exitAncestorAxis(final AncestorAxisContext ctx) {
96         cpsPathQuery.setAncestorSchemaNodeIdentifier(ctx.ancestorPath().getText());
97     }
98
99     CpsPathQuery build() {
100         return cpsPathQuery;
101     }
102
103     private static String stripFirstAndLastCharacter(final String wrappedString) {
104         return wrappedString.substring(1, wrappedString.length() - 1);
105     }
106
107 }