Fix Delete uses case with '/' in path
[cps.git] / cps-path-parser / src / main / java / org / onap / cps / cpspath / parser / CpsPathBuilder.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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  *
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 package org.onap.cps.cpspath.parser;
22
23 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener;
28 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser;
29 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext;
30 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.DescendantContext;
31 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.IncorrectPrefixContext;
32 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext;
33 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleLeafConditionsContext;
34 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext;
35 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.TextFunctionConditionContext;
36
37 public class CpsPathBuilder extends CpsPathBaseListener {
38
39     private static final String OPEN_BRACKET = "[";
40
41     private static final String CLOSE_BRACKET = "]";
42
43     final CpsPathQuery cpsPathQuery = new CpsPathQuery();
44
45     final Map<String, Object> leavesData = new HashMap<>();
46
47     final StringBuilder normalizedXpathBuilder = new StringBuilder();
48
49     final StringBuilder normalizedAncestorPathBuilder = new StringBuilder();
50
51     boolean processingAncestorAxis = false;
52
53     @Override
54     public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) {
55         throw new PathParsingException(ctx.getText());
56     }
57
58     @Override
59     public void exitPrefix(final PrefixContext ctx) {
60         cpsPathQuery.setXpathPrefix(normalizedXpathBuilder.toString());
61     }
62
63     @Override
64     public void exitParent(final CpsPathParser.ParentContext ctx) {
65         cpsPathQuery.setNormalizedParentPath(normalizedXpathBuilder.toString());
66     }
67
68     @Override
69     public void exitIncorrectPrefix(final IncorrectPrefixContext ctx) {
70         throw new PathParsingException("CPS path can only start with one or two slashes (/)");
71     }
72
73     @Override
74     public void exitLeafCondition(final LeafConditionContext ctx) {
75         Object comparisonValue = null;
76         if (ctx.IntegerLiteral() != null) {
77             comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
78         }
79         if (ctx.StringLiteral() != null) {
80             final boolean wasWrappedInDoubleQuote  = ctx.StringLiteral().getText().startsWith("\"");
81             comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
82             if (wasWrappedInDoubleQuote) {
83                 comparisonValue = String.valueOf(comparisonValue).replace("'", "\\'");
84             }
85         } else if (comparisonValue == null) {
86             throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText());
87         }
88         leavesData.put(ctx.leafName().getText(), comparisonValue);
89         appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), comparisonValue);
90         if (processingAncestorAxis) {
91             appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), comparisonValue);
92         }
93     }
94
95     @Override
96     public void exitDescendant(final DescendantContext ctx) {
97         cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
98         cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(1));
99         normalizedXpathBuilder.insert(0, "/");
100     }
101
102     @Override
103     public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx)  {
104         normalizedXpathBuilder.append(OPEN_BRACKET);
105         leavesData.clear();
106     }
107
108     @Override
109     public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
110         normalizedXpathBuilder.append(CLOSE_BRACKET);
111         cpsPathQuery.setLeavesData(leavesData);
112     }
113
114     @Override
115     public void enterAncestorAxis(final AncestorAxisContext ctx) {
116         processingAncestorAxis = true;
117     }
118
119     @Override
120     public void exitAncestorAxis(final AncestorAxisContext ctx) {
121         cpsPathQuery.setAncestorSchemaNodeIdentifier(normalizedAncestorPathBuilder.substring(1));
122         processingAncestorAxis = false;
123     }
124
125     @Override
126     public void exitTextFunctionCondition(final TextFunctionConditionContext ctx) {
127         cpsPathQuery.setTextFunctionConditionLeafName(ctx.leafName().getText());
128         cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
129     }
130
131     @Override
132     public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
133         normalizedXpathBuilder.append(OPEN_BRACKET);
134         if (processingAncestorAxis) {
135             normalizedAncestorPathBuilder.append(OPEN_BRACKET);
136         }
137     }
138
139     @Override
140     public void exitListElementRef(final CpsPathParser.ListElementRefContext ctx) {
141         normalizedXpathBuilder.append(CLOSE_BRACKET);
142         if (processingAncestorAxis) {
143             normalizedAncestorPathBuilder.append(CLOSE_BRACKET);
144         }
145     }
146
147     CpsPathQuery build() {
148         cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
149         return cpsPathQuery;
150     }
151
152     private static String stripFirstAndLastCharacter(final String wrappedString) {
153         return wrappedString.substring(1, wrappedString.length() - 1);
154     }
155
156     @Override
157     public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
158         normalizedXpathBuilder.append("/")
159                 .append(ctx.getText());
160         if (processingAncestorAxis) {
161             normalizedAncestorPathBuilder.append("/").append(ctx.getText());
162         }
163     }
164
165     private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name,
166                                 final Object value) {
167         final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1);
168         currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " and ")
169                 .append("@")
170                 .append(name)
171                 .append("='")
172                 .append(value)
173                 .append("'");
174     }
175 }