From 2696de664b097c8dceb4332e9896417835e77178 Mon Sep 17 00:00:00 2001 From: Rudrangi Anupriya Date: Tue, 30 May 2023 17:20:20 +0530 Subject: Add <,> operators support to cps-path Issue-ID: CPS-1273 Change-Id: I5d562463b9a49abfe0436047a637857d10596fff Signed-off-by: Rudrangi Anupriya --- .../onap/cps/cpspath/parser/CpsPathBuilder.java | 58 +++++++++++++------- .../cpspath/parser/CpsPathComparativeOperator.java | 64 ++++++++++++++++++++++ .../org/onap/cps/cpspath/parser/CpsPathQuery.java | 3 +- 3 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathComparativeOperator.java (limited to 'cps-path-parser/src/main/java/org') diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java index f44e310a1f..5c47127375 100644 --- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java +++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java @@ -25,10 +25,8 @@ import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT; import java.util.ArrayList; import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Queue; import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener; import org.onap.cps.cpspath.parser.antlr4.CpsPathParser; import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext; @@ -59,7 +57,7 @@ public class CpsPathBuilder extends CpsPathBaseListener { final List booleanOperators = new ArrayList<>(); - final Queue booleanOperatorsQueue = new LinkedList<>(); + final List comparativeOperators = new ArrayList<>(); @Override public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) { @@ -83,25 +81,20 @@ public class CpsPathBuilder extends CpsPathBaseListener { @Override public void exitLeafCondition(final LeafConditionContext ctx) { - Object comparisonValue = null; + Object comparisonValue; if (ctx.IntegerLiteral() != null) { comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText()); - } - if (ctx.StringLiteral() != null) { - final boolean wasWrappedInDoubleQuote = ctx.StringLiteral().getText().startsWith("\""); + } else if (ctx.StringLiteral() != null) { + final boolean wasWrappedInDoubleQuote = ctx.StringLiteral().getText().startsWith("\""); comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText()); if (wasWrappedInDoubleQuote) { comparisonValue = String.valueOf(comparisonValue).replace("'", "\\'"); } - } else if (comparisonValue == null) { - throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText()); - } - leavesData.put(ctx.leafName().getText(), comparisonValue); - final String booleanOperator = booleanOperatorsQueue.poll(); - appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), booleanOperator, comparisonValue); - if (processingAncestorAxis) { - appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), booleanOperator, comparisonValue); + } else { + throw new PathParsingException( + "Unsupported comparison value encountered in expression" + ctx.getText()); } + leafContext(ctx.leafName(), comparisonValue); } @Override @@ -109,8 +102,13 @@ public class CpsPathBuilder extends CpsPathBaseListener { final CpsPathBooleanOperatorType cpsPathBooleanOperatorType = CpsPathBooleanOperatorType.fromString( ctx.getText()); booleanOperators.add(cpsPathBooleanOperatorType.getValues()); - booleanOperatorsQueue.add(cpsPathBooleanOperatorType.getValues()); - cpsPathQuery.setBooleanOperatorsType(booleanOperators); + } + + @Override + public void exitComparativeOperators(final CpsPathParser.ComparativeOperatorsContext ctx) { + final CpsPathComparativeOperator cpsPathComparativeOperator = CpsPathComparativeOperator.fromString( + ctx.getText()); + comparativeOperators.add(cpsPathComparativeOperator.getLabel()); } @Override @@ -174,6 +172,8 @@ public class CpsPathBuilder extends CpsPathBaseListener { CpsPathQuery build() { cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString()); cpsPathQuery.setContainerNames(containerNames); + cpsPathQuery.setBooleanOperators(booleanOperators); + cpsPathQuery.setComparativeOperators(comparativeOperators); return cpsPathQuery; } @@ -192,14 +192,30 @@ public class CpsPathBuilder extends CpsPathBaseListener { } } + private void leafContext(final CpsPathParser.LeafNameContext ctx, final Object comparisonValue) { + leavesData.put(ctx.getText(), comparisonValue); + appendCondition(normalizedXpathBuilder, ctx.getText(), comparisonValue); + if (processingAncestorAxis) { + appendCondition(normalizedAncestorPathBuilder, ctx.getText(), comparisonValue); + } + } + private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name, - final String booleanOperator, final Object value) { + final Object value) { final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1); - currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " " + booleanOperator + " ") - .append("@") + final boolean isStartOfExpression = lastCharacter == '['; + if (!isStartOfExpression) { + currentNormalizedPathBuilder.append(" ").append(getLastElement(booleanOperators)).append(" "); + } + currentNormalizedPathBuilder.append("@") .append(name) - .append("='") + .append(getLastElement(comparativeOperators)) + .append("'") .append(value) .append("'"); } + + private String getLastElement(final List listOfStrings) { + return listOfStrings.get(listOfStrings.size() - 1); + } } diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathComparativeOperator.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathComparativeOperator.java new file mode 100644 index 0000000000..c7ffd0d7ec --- /dev/null +++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathComparativeOperator.java @@ -0,0 +1,64 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2023 Tech Mahindra Ltd + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.cpspath.parser; + +import java.util.HashMap; +import java.util.Map; + +public enum CpsPathComparativeOperator { + EQ("="), + GT(">"), + LT("<"), + GE(">="), + LE("<="); + + private final String label; + + CpsPathComparativeOperator(final String label) { + this.label = label; + } + + public final String getLabel() { + return this.label; + } + + private static final Map cpsPathComparativeOperatorPerLabel = new HashMap<>(); + + static { + for (final CpsPathComparativeOperator cpsPathComparativeOperator : CpsPathComparativeOperator.values()) { + cpsPathComparativeOperatorPerLabel.put(cpsPathComparativeOperator.label, cpsPathComparativeOperator); + } + } + + /** + * Finds the value of the given enumeration. + * + * @param label value of the enum + * @return a comparativeOperatorType + */ + public static CpsPathComparativeOperator fromString(final String label) { + if (!cpsPathComparativeOperatorPerLabel.containsKey(label)) { + throw new PathParsingException("Incomplete leaf condition (no operator)"); + } + return cpsPathComparativeOperatorPerLabel.get(label); + } +} + diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java index 418b5ec55b..3c3cbccf7e 100644 --- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java +++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java @@ -43,7 +43,8 @@ public class CpsPathQuery { private String ancestorSchemaNodeIdentifier = ""; private String textFunctionConditionLeafName; private String textFunctionConditionValue; - private List booleanOperatorsType; + private List booleanOperators; + private List comparativeOperators; private String containsFunctionConditionLeafName; private String containsFunctionConditionValue; -- cgit 1.2.3-korg