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
9 * 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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.cpspath.parser;
23 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
29 import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener;
30 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser;
31 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext;
32 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.DescendantContext;
33 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.IncorrectPrefixContext;
34 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext;
35 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleLeafConditionsContext;
36 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext;
37 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.TextFunctionConditionContext;
39 public class CpsPathBuilder extends CpsPathBaseListener {
41 private static final String OPEN_BRACKET = "[";
43 private static final String CLOSE_BRACKET = "]";
45 final CpsPathQuery cpsPathQuery = new CpsPathQuery();
47 final Map<String, Object> leavesData = new HashMap<>();
49 final StringBuilder normalizedXpathBuilder = new StringBuilder();
51 final StringBuilder normalizedAncestorPathBuilder = new StringBuilder();
53 boolean processingAncestorAxis = false;
55 private List<String> containerNames = new ArrayList<>();
58 public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) {
59 throw new PathParsingException(ctx.getText());
63 public void exitPrefix(final PrefixContext ctx) {
64 cpsPathQuery.setXpathPrefix(normalizedXpathBuilder.toString());
68 public void exitParent(final CpsPathParser.ParentContext ctx) {
69 cpsPathQuery.setNormalizedParentPath(normalizedXpathBuilder.toString());
73 public void exitIncorrectPrefix(final IncorrectPrefixContext ctx) {
74 throw new PathParsingException("CPS path can only start with one or two slashes (/)");
78 public void exitLeafCondition(final LeafConditionContext ctx) {
79 Object comparisonValue = null;
80 if (ctx.IntegerLiteral() != null) {
81 comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
83 if (ctx.StringLiteral() != null) {
84 final boolean wasWrappedInDoubleQuote = ctx.StringLiteral().getText().startsWith("\"");
85 comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
86 if (wasWrappedInDoubleQuote) {
87 comparisonValue = String.valueOf(comparisonValue).replace("'", "\\'");
89 } else if (comparisonValue == null) {
90 throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText());
92 leavesData.put(ctx.leafName().getText(), comparisonValue);
93 appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), comparisonValue);
94 if (processingAncestorAxis) {
95 appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), comparisonValue);
100 public void exitDescendant(final DescendantContext ctx) {
101 cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
102 cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(1));
103 normalizedXpathBuilder.insert(0, "/");
107 public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
108 normalizedXpathBuilder.append(OPEN_BRACKET);
113 public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
114 normalizedXpathBuilder.append(CLOSE_BRACKET);
115 cpsPathQuery.setLeavesData(leavesData);
119 public void enterAncestorAxis(final AncestorAxisContext ctx) {
120 processingAncestorAxis = true;
124 public void exitAncestorAxis(final AncestorAxisContext ctx) {
125 cpsPathQuery.setAncestorSchemaNodeIdentifier(normalizedAncestorPathBuilder.substring(1));
126 processingAncestorAxis = false;
130 public void exitTextFunctionCondition(final TextFunctionConditionContext ctx) {
131 cpsPathQuery.setTextFunctionConditionLeafName(ctx.leafName().getText());
132 cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
136 public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
137 normalizedXpathBuilder.append(OPEN_BRACKET);
138 if (processingAncestorAxis) {
139 normalizedAncestorPathBuilder.append(OPEN_BRACKET);
144 public void exitListElementRef(final CpsPathParser.ListElementRefContext ctx) {
145 normalizedXpathBuilder.append(CLOSE_BRACKET);
146 if (processingAncestorAxis) {
147 normalizedAncestorPathBuilder.append(CLOSE_BRACKET);
151 CpsPathQuery build() {
152 cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
153 cpsPathQuery.setContainerNames(containerNames);
157 private static String stripFirstAndLastCharacter(final String wrappedString) {
158 return wrappedString.substring(1, wrappedString.length() - 1);
162 public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
163 final String containerName = ctx.getText();
164 normalizedXpathBuilder.append("/")
165 .append(containerName);
166 containerNames.add(containerName);
167 if (processingAncestorAxis) {
168 normalizedAncestorPathBuilder.append("/").append(containerName);
172 private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name,
173 final Object value) {
174 final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1);
175 currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " and ")