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
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.onap.cps.cpspath.parser;
23 import lombok.AccessLevel;
26 import org.antlr.v4.runtime.BaseErrorListener;
27 import org.antlr.v4.runtime.CharStreams;
28 import org.antlr.v4.runtime.CommonTokenStream;
29 import org.antlr.v4.runtime.RecognitionException;
30 import org.antlr.v4.runtime.Recognizer;
31 import org.onap.cps.cpspath.parser.antlr4.CpsPathLexer;
32 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser;
35 @Setter(AccessLevel.PACKAGE)
36 public class CpsPathQuery {
38 private CpsPathQueryType cpsPathQueryType;
39 private String xpathPrefix;
40 private String leafName;
41 private Object leafValue;
42 private String descendantName;
43 private Map<String, Object> leavesData;
44 private String ancestorSchemaNodeIdentifier = "";
47 * Returns a cps path query.
49 * @param cpsPathSource cps path
50 * @return a CpsPathQuery object.
52 public static CpsPathQuery createFrom(final String cpsPathSource) {
53 final var inputStream = CharStreams.fromString(cpsPathSource);
54 final var cpsPathLexer = new CpsPathLexer(inputStream);
55 final var cpsPathParser = new CpsPathParser(new CommonTokenStream(cpsPathLexer));
56 cpsPathParser.addErrorListener(new BaseErrorListener() {
58 public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line,
59 final int charPositionInLine, final String msg, final RecognitionException e) {
60 throw new IllegalStateException("failed to parse at line " + line + " due to " + msg, e);
63 final var cpsPathBuilder = new CpsPathBuilder();
64 cpsPathParser.addParseListener(cpsPathBuilder);
65 cpsPathParser.cpsPath();
66 return cpsPathBuilder.build();
70 * Has ancestor axis been populated.
72 * @return boolean value.
74 public boolean hasAncestorAxis() {
75 return !(ancestorSchemaNodeIdentifier.isEmpty());