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 exitIncorrectPrefix(final IncorrectPrefixContext ctx) {
65 throw new PathParsingException("CPS path can only start with one or two slashes (/)");
69 public void exitLeafCondition(final LeafConditionContext ctx) {
70 Object comparisonValue = null;
71 if (ctx.IntegerLiteral() != null) {
72 comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
74 if (ctx.StringLiteral() != null) {
75 final boolean wasWrappedInDoubleQuote = ctx.StringLiteral().getText().startsWith("\"");
76 comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
77 if (wasWrappedInDoubleQuote) {
78 comparisonValue = String.valueOf(comparisonValue).replace("'", "\\'");
80 } else if (comparisonValue == null) {
81 throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText());
83 leavesData.put(ctx.leafName().getText(), comparisonValue);
84 appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), comparisonValue);
85 if (processingAncestorAxis) {
86 appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), comparisonValue);
91 public void exitDescendant(final DescendantContext ctx) {
92 cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
93 cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(1));
94 normalizedXpathBuilder.insert(0, "/");
98 public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
99 normalizedXpathBuilder.append(OPEN_BRACKET);
104 public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
105 normalizedXpathBuilder.append(CLOSE_BRACKET);
106 cpsPathQuery.setLeavesData(leavesData);
110 public void enterAncestorAxis(final AncestorAxisContext ctx) {
111 processingAncestorAxis = true;
115 public void exitAncestorAxis(final AncestorAxisContext ctx) {
116 cpsPathQuery.setAncestorSchemaNodeIdentifier(normalizedAncestorPathBuilder.substring(1));
117 processingAncestorAxis = false;
121 public void exitTextFunctionCondition(final TextFunctionConditionContext ctx) {
122 cpsPathQuery.setTextFunctionConditionLeafName(ctx.leafName().getText());
123 cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
127 public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
128 normalizedXpathBuilder.append(OPEN_BRACKET);
129 if (processingAncestorAxis) {
130 normalizedAncestorPathBuilder.append(OPEN_BRACKET);
135 public void exitListElementRef(final CpsPathParser.ListElementRefContext ctx) {
136 normalizedXpathBuilder.append(CLOSE_BRACKET);
137 if (processingAncestorAxis) {
138 normalizedAncestorPathBuilder.append(CLOSE_BRACKET);
142 CpsPathQuery build() {
143 cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
147 private static String stripFirstAndLastCharacter(final String wrappedString) {
148 return wrappedString.substring(1, wrappedString.length() - 1);
152 public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
153 normalizedXpathBuilder.append("/")
154 .append(ctx.getText());
155 if (processingAncestorAxis) {
156 normalizedAncestorPathBuilder.append("/").append(ctx.getText());
160 private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name,
161 final Object value) {
162 final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1);
163 currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " and ")