From 8e22c6a80b491cc98d4052e05dc94ee645bf2197 Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Sat, 31 Aug 2024 22:30:16 +0100 Subject: [Cps Path Parser] Refactoring leaf conditions Instead of DataLeaf and ComparativeOperators classes, we more simply have LeafConditions class with all info. Issue-ID: CPS-2365 Signed-off-by: danielhanrahan Change-Id: I8fd18bb56b8ed9d26a3f9d36f487d00a9274c8bc --- .../onap/cps/cpspath/parser/CpsPathBuilder.java | 33 +++++++++------------- .../org/onap/cps/cpspath/parser/CpsPathQuery.java | 18 ++++-------- 2 files changed, 18 insertions(+), 33 deletions(-) (limited to 'cps-path-parser/src/main') 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 0bb09235ff..1993e17c41 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"); @@ -43,7 +43,7 @@ public class CpsPathBuilder extends CpsPathBaseListener { private final CpsPathQuery cpsPathQuery = new CpsPathQuery(); - private final List leavesData = new ArrayList<>(); + private final List leafConditions = new ArrayList<>(); private final StringBuilder normalizedXpathBuilder = new StringBuilder(); @@ -55,8 +55,6 @@ public class CpsPathBuilder extends CpsPathBaseListener { private final List booleanOperators = new ArrayList<>(); - private final List comparativeOperators = new ArrayList<>(); - @Override public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) { throw new PathParsingException(ctx.getText()); @@ -79,6 +77,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()); @@ -87,7 +87,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 @@ -95,11 +95,6 @@ public class CpsPathBuilder extends CpsPathBaseListener { booleanOperators.add(ctx.getText()); } - @Override - public void exitComparativeOperators(final CpsPathParser.ComparativeOperatorsContext ctx) { - comparativeOperators.add(ctx.getText()); - } - @Override public void exitDescendant(final DescendantContext ctx) { cpsPathQuery.setCpsPathPrefixType(DESCENDANT); @@ -110,15 +105,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 @@ -164,7 +158,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(""); @@ -183,16 +176,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) { @@ -200,7 +193,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 containerNames; private CpsPathPrefixType cpsPathPrefixType = ABSOLUTE; private String descendantName; - private List leavesData; + private List leafConditions; private String ancestorSchemaNodeIdentifier = ""; private String textFunctionConditionLeafName; private String textFunctionConditionValue; private List booleanOperators; - private List 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) { } + } -- cgit 1.2.3-korg