Fix last SQ violation
[cps.git] / cps-path-parser / src / main / java / org / onap / cps / cpspath / parser / CpsPathQuery.java
1 /*
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
8  *
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.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.cpspath.parser;
21
22 import java.util.Map;
23 import lombok.AccessLevel;
24 import lombok.Getter;
25 import lombok.Setter;
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;
33
34 @Getter
35 @Setter(AccessLevel.PACKAGE)
36 public class CpsPathQuery {
37
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 = "";
45
46     /**
47      * Returns a cps path query.
48      *
49      * @param cpsPathSource cps path
50      * @return a CpsPathQuery object.
51      */
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() {
57             @Override
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);
61             }
62         });
63         final var cpsPathBuilder = new CpsPathBuilder();
64         cpsPathParser.addParseListener(cpsPathBuilder);
65         cpsPathParser.cpsPath();
66         return cpsPathBuilder.build();
67     }
68
69     /**
70      * Has ancestor axis been populated.
71      *
72      * @return boolean value.
73      */
74     public boolean hasAncestorAxis() {
75         return !(ancestorSchemaNodeIdentifier.isEmpty());
76     }
77
78 }