diff options
author | Priyank Maheshwari <priyank.maheshwari@est.tech> | 2024-09-04 16:18:43 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2024-09-04 16:18:43 +0000 |
commit | 66f6c6d2634d83d972e8f226156359b72a1f6841 (patch) | |
tree | 5868c40bf90a669eaa473ab9ce34bd2c818ff456 /cps-path-parser/src/main | |
parent | 150ad7378588b459b423c8ef9557a3f229652b69 (diff) | |
parent | 8e22c6a80b491cc98d4052e05dc94ee645bf2197 (diff) |
Merge "[Cps Path Parser] Refactoring leaf conditions"
Diffstat (limited to 'cps-path-parser/src/main')
-rw-r--r-- | cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java | 33 | ||||
-rw-r--r-- | cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java | 18 |
2 files changed, 18 insertions, 33 deletions
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 66964bbc47..0325bdcacb 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation + * Copyright (C) 2021-2024 Nordix Foundation * Modifications Copyright (C) 2023 TechMahindra Ltd * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -42,7 +42,7 @@ public class CpsPathBuilder extends CpsPathBaseListener { private final CpsPathQuery cpsPathQuery = new CpsPathQuery(); - private final List<CpsPathQuery.DataLeaf> leavesData = new ArrayList<>(); + private final List<CpsPathQuery.LeafCondition> leafConditions = new ArrayList<>(); private final StringBuilder normalizedXpathBuilder = new StringBuilder(); @@ -54,8 +54,6 @@ public class CpsPathBuilder extends CpsPathBaseListener { private final List<String> booleanOperators = new ArrayList<>(); - private final List<String> comparativeOperators = new ArrayList<>(); - @Override public void exitPrefix(final PrefixContext ctx) { cpsPathQuery.setXpathPrefix(normalizedXpathBuilder.toString()); @@ -68,6 +66,8 @@ public class CpsPathBuilder extends CpsPathBaseListener { @Override public void exitLeafCondition(final LeafConditionContext ctx) { + final String leafName = ctx.leafName().getText(); + final String operator = ctx.comparativeOperators().getText(); final Object comparisonValue; if (ctx.IntegerLiteral() != null) { comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText()); @@ -76,7 +76,7 @@ public class CpsPathBuilder extends CpsPathBaseListener { } else { throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText()); } - leafContext(ctx.leafName(), comparisonValue); + leafContext(leafName, operator, comparisonValue); } @Override @@ -85,11 +85,6 @@ public class CpsPathBuilder extends CpsPathBaseListener { } @Override - public void exitComparativeOperators(final CpsPathParser.ComparativeOperatorsContext ctx) { - comparativeOperators.add(ctx.getText()); - } - - @Override public void exitDescendant(final DescendantContext ctx) { cpsPathQuery.setCpsPathPrefixType(DESCENDANT); cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(1)); @@ -99,15 +94,14 @@ public class CpsPathBuilder extends CpsPathBaseListener { @Override public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx) { normalizedXpathBuilder.append(OPEN_BRACKET); - leavesData.clear(); + leafConditions.clear(); booleanOperators.clear(); - comparativeOperators.clear(); } @Override public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) { normalizedXpathBuilder.append(CLOSE_BRACKET); - cpsPathQuery.setLeavesData(leavesData); + cpsPathQuery.setLeafConditions(leafConditions); } @Override @@ -153,7 +147,6 @@ public class CpsPathBuilder extends CpsPathBaseListener { cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString()); cpsPathQuery.setContainerNames(containerNames); cpsPathQuery.setBooleanOperators(booleanOperators); - cpsPathQuery.setComparativeOperators(comparativeOperators); if (cpsPathQuery.hasAncestorAxis() && cpsPathQuery.getXpathPrefix() .endsWith("/" + cpsPathQuery.getAncestorSchemaNodeIdentifier())) { cpsPathQuery.setAncestorSchemaNodeIdentifier(""); @@ -172,16 +165,16 @@ public class CpsPathBuilder extends CpsPathBaseListener { } } - private void leafContext(final CpsPathParser.LeafNameContext ctx, final Object comparisonValue) { - leavesData.add(new CpsPathQuery.DataLeaf(ctx.getText(), comparisonValue)); - appendCondition(normalizedXpathBuilder, ctx.getText(), comparisonValue); + private void leafContext(final String leafName, final String operator, final Object comparisonValue) { + leafConditions.add(new CpsPathQuery.LeafCondition(leafName, operator, comparisonValue)); + appendCondition(normalizedXpathBuilder, leafName, operator, comparisonValue); if (processingAncestorAxis) { - appendCondition(normalizedAncestorPathBuilder, ctx.getText(), comparisonValue); + appendCondition(normalizedAncestorPathBuilder, leafName, operator, comparisonValue); } } private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name, - final Object value) { + final String operator, final Object value) { final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1); final boolean isStartOfExpression = lastCharacter == '['; if (!isStartOfExpression) { @@ -189,7 +182,7 @@ public class CpsPathBuilder extends CpsPathBaseListener { } currentNormalizedPathBuilder.append("@") .append(name) - .append(getLastElement(comparativeOperators)) + .append(operator) .append("'") .append(value.toString().replace("'", "''")) .append("'"); 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 f98df05a28..03602b64f6 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021-2023 Nordix Foundation + * Copyright (C) 2021-2024 Nordix Foundation * Modifications Copyright (C) 2023 TechMahindra Ltd * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,8 +25,6 @@ import static org.onap.cps.cpspath.parser.CpsPathPrefixType.ABSOLUTE; import java.util.List; import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @@ -40,12 +38,11 @@ public class CpsPathQuery { private List<String> containerNames; private CpsPathPrefixType cpsPathPrefixType = ABSOLUTE; private String descendantName; - private List<DataLeaf> leavesData; + private List<LeafCondition> leafConditions; private String ancestorSchemaNodeIdentifier = ""; private String textFunctionConditionLeafName; private String textFunctionConditionValue; private List<String> booleanOperators; - private List<String> comparativeOperators; private String containsFunctionConditionLeafName; private String containsFunctionConditionValue; @@ -74,7 +71,7 @@ public class CpsPathQuery { * @return boolean value. */ public boolean hasLeafConditions() { - return leavesData != null; + return leafConditions != null; } /** @@ -104,11 +101,6 @@ public class CpsPathQuery { return cpsPathPrefixType == ABSOLUTE && hasLeafConditions(); } - @Getter - @EqualsAndHashCode - @AllArgsConstructor - public static class DataLeaf { - private final String name; - private final Object value; - } + public record LeafCondition(String name, String operator, Object value) { } + } |