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