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.HashMap;
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;
37 public class CpsPathBuilder extends CpsPathBaseListener {
39 private static final String OPEN_BRACKET = "[";
41 private static final String CLOSE_BRACKET = "]";
43 final CpsPathQuery cpsPathQuery = new CpsPathQuery();
45 final Map<String, Object> leavesData = new HashMap<>();
47 final StringBuilder normalizedXpathBuilder = new StringBuilder();
49 final StringBuilder normalizedAncestorPathBuilder = new StringBuilder();
51 boolean processingAncestorAxis = false;
54 public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) {
55 throw new PathParsingException(ctx.getText());
59 public void exitPrefix(final PrefixContext ctx) {
60 cpsPathQuery.setXpathPrefix(normalizedXpathBuilder.toString());
64 public void exitParent(final CpsPathParser.ParentContext ctx) {
65 cpsPathQuery.setNormalizedParentPath(normalizedXpathBuilder.toString());
69 public void exitIncorrectPrefix(final IncorrectPrefixContext ctx) {
70 throw new PathParsingException("CPS path can only start with one or two slashes (/)");
74 public void exitLeafCondition(final LeafConditionContext ctx) {
75 Object comparisonValue = null;
76 if (ctx.IntegerLiteral() != null) {
77 comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
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("'", "\\'");
85 } else if (comparisonValue == null) {
86 throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText());
88 leavesData.put(ctx.leafName().getText(), comparisonValue);
89 appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), comparisonValue);
90 if (processingAncestorAxis) {
91 appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), comparisonValue);
96 public void exitDescendant(final DescendantContext ctx) {
97 cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
98 cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(1));
99 normalizedXpathBuilder.insert(0, "/");
103 public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
104 normalizedXpathBuilder.append(OPEN_BRACKET);
109 public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
110 normalizedXpathBuilder.append(CLOSE_BRACKET);
111 cpsPathQuery.setLeavesData(leavesData);
115 public void enterAncestorAxis(final AncestorAxisContext ctx) {
116 processingAncestorAxis = true;
120 public void exitAncestorAxis(final AncestorAxisContext ctx) {
121 cpsPathQuery.setAncestorSchemaNodeIdentifier(normalizedAncestorPathBuilder.substring(1));
122 processingAncestorAxis = false;
126 public void exitTextFunctionCondition(final TextFunctionConditionContext ctx) {
127 cpsPathQuery.setTextFunctionConditionLeafName(ctx.leafName().getText());
128 cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
132 public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
133 normalizedXpathBuilder.append(OPEN_BRACKET);
134 if (processingAncestorAxis) {
135 normalizedAncestorPathBuilder.append(OPEN_BRACKET);
140 public void exitListElementRef(final CpsPathParser.ListElementRefContext ctx) {
141 normalizedXpathBuilder.append(CLOSE_BRACKET);
142 if (processingAncestorAxis) {
143 normalizedAncestorPathBuilder.append(CLOSE_BRACKET);
147 CpsPathQuery build() {
148 cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
152 private static String stripFirstAndLastCharacter(final String wrappedString) {
153 return wrappedString.substring(1, wrappedString.length() - 1);
157 public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
158 normalizedXpathBuilder.append("/")
159 .append(ctx.getText());
160 if (processingAncestorAxis) {
161 normalizedAncestorPathBuilder.append("/").append(ctx.getText());
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 ")