1 package org.onap.cps.cpspath.parser;
3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2021 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * 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 import java.util.HashMap;
23 import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener;
24 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext;
25 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithDescendantAndLeafConditionsContext;
26 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithDescendantContext;
27 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithSingleLeafConditionContext;
28 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext;
29 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleValueConditionsContext;
30 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext;
31 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.SingleValueConditionContext;
33 public class CpsPathBuilder extends CpsPathBaseListener {
35 final CpsPathQuery cpsPathQuery = new CpsPathQuery();
37 final Map<String, Object> leavesData = new HashMap<>();
40 public void exitPrefix(final PrefixContext ctx) {
41 cpsPathQuery.setXpathPrefix(ctx.getText());
45 public void exitLeafCondition(final LeafConditionContext ctx) {
46 Object comparisonValue = null;
47 if (ctx.IntegerLiteral() != null) {
48 comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
50 if (ctx.StringLiteral() != null) {
51 comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
52 } else if (comparisonValue == null) {
53 throw new IllegalStateException("Unsupported comparison value encountered in expression" + ctx.getText());
55 leavesData.put(ctx.leafName().getText(), comparisonValue);
59 public void enterSingleValueCondition(final SingleValueConditionContext ctx) {
64 public void enterMultipleValueConditions(final MultipleValueConditionsContext ctx) {
69 public void exitSingleValueCondition(final SingleValueConditionContext ctx) {
70 final String leafName = ctx.leafCondition().leafName().getText();
71 cpsPathQuery.setLeafName(leafName);
72 cpsPathQuery.setLeafValue(leavesData.get(leafName));
76 public void exitCpsPathWithSingleLeafCondition(final CpsPathWithSingleLeafConditionContext ctx) {
77 cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE);
81 public void exitCpsPathWithDescendant(final CpsPathWithDescendantContext ctx) {
82 cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE);
83 cpsPathQuery.setDescendantName(cpsPathQuery.getXpathPrefix().substring(1));
87 public void exitCpsPathWithDescendantAndLeafConditions(
88 final CpsPathWithDescendantAndLeafConditionsContext ctx) {
89 cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES);
90 cpsPathQuery.setDescendantName(cpsPathQuery.getXpathPrefix().substring(1));
91 cpsPathQuery.setLeavesData(leavesData);
95 public void exitAncestorAxis(final AncestorAxisContext ctx) {
96 cpsPathQuery.setAncestorSchemaNodeIdentifier(ctx.ancestorPath().getText());
99 CpsPathQuery build() {
103 private static String stripFirstAndLastCharacter(final String wrappedString) {
104 return wrappedString.substring(1, wrappedString.length() - 1);