2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.core.sli;
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import org.antlr.v4.runtime.ANTLRInputStream;
28 import org.antlr.v4.runtime.CharStream;
29 import org.antlr.v4.runtime.CommonTokenStream;
30 import org.antlr.v4.runtime.tree.ParseTreeWalker;
31 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ExprContext;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class SvcLogicExpressionFactory {
38 private static final Logger LOG = LoggerFactory
39 .getLogger(SvcLogicExpressionFactory.class);
42 public static SvcLogicExpression parse(String exprStr) throws IOException
44 InputStream exprStream = new ByteArrayInputStream(exprStr.getBytes());
45 CharStream input = new ANTLRInputStream(exprStream);
46 ExprGrammarLexer lexer = new ExprGrammarLexer(input);
47 CommonTokenStream tokens = new CommonTokenStream(lexer);
48 ExprGrammarParser parser = new ExprGrammarParser(tokens);
50 lexer.removeErrorListeners();
51 lexer.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
52 parser.removeErrorListeners();
53 parser.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
55 ExprContext expression = null;
58 expression = parser.expr();
59 } catch (Exception e) {
60 String errorMsg = e.getMessage();
63 throw new SvcLogicParserException(errorMsg);
67 ParseTreeWalker walker = new ParseTreeWalker();
68 SvcLogicExprListener listener = new SvcLogicExprListener();
69 walker.walk(listener, expression);
72 return(listener.getParsedExpr());