2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.AncestorAxisContext;
29 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.DescendantContext;
30 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.IncorrectPrefixContext;
31 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext;
32 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleLeafConditionsContext;
33 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext;
34 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.TextFunctionConditionContext;
36 public class CpsPathBuilder extends CpsPathBaseListener {
38 final CpsPathQuery cpsPathQuery = new CpsPathQuery();
40 final Map<String, Object> leavesData = new HashMap<>();
43 public void exitPrefix(final PrefixContext ctx) {
44 cpsPathQuery.setXpathPrefix(ctx.getText());
48 public void exitIncorrectPrefix(final IncorrectPrefixContext ctx) {
49 throw new IllegalStateException("CPS path can only start with one or two slashes (/)");
53 public void exitLeafCondition(final LeafConditionContext ctx) {
54 Object comparisonValue = null;
55 if (ctx.IntegerLiteral() != null) {
56 comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
58 if (ctx.StringLiteral() != null) {
59 comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
60 } else if (comparisonValue == null) {
61 throw new IllegalStateException("Unsupported comparison value encountered in expression" + ctx.getText());
63 leavesData.put(ctx.leafName().getText(), comparisonValue);
67 public void exitDescendant(final DescendantContext ctx) {
68 cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
69 cpsPathQuery.setDescendantName(ctx.getText().substring(2));
73 public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
78 public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
79 cpsPathQuery.setLeavesData(leavesData);
83 public void exitAncestorAxis(final AncestorAxisContext ctx) {
84 cpsPathQuery.setAncestorSchemaNodeIdentifier(ctx.ancestorPath().getText());
88 public void exitTextFunctionCondition(final TextFunctionConditionContext ctx) {
89 cpsPathQuery.setTextFunctionConditionLeafName(ctx.leafName().getText());
90 cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
93 CpsPathQuery build() {
97 private static String stripFirstAndLastCharacter(final String wrappedString) {
98 return wrappedString.substring(1, wrappedString.length() - 1);