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;
28 import org.antlr.v4.runtime.ANTLRInputStream;
29 import org.antlr.v4.runtime.CharStream;
30 import org.antlr.v4.runtime.CommonTokenStream;
31 import org.antlr.v4.runtime.tree.ParseTreeWalker;
32 import org.onap.ccsdk.sli.core.sli.ExprGrammarParser.ExprContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 public class SvcLogicExpressionFactory {
39 private static final Logger LOG = LoggerFactory
40 .getLogger(SvcLogicExpressionFactory.class);
43 public static SvcLogicExpression parse(String exprStr) throws IOException
45 InputStream exprStream = new ByteArrayInputStream(exprStr.getBytes());
46 CharStream input = new ANTLRInputStream(exprStream);
47 ExprGrammarLexer lexer = new ExprGrammarLexer(input);
48 CommonTokenStream tokens = new CommonTokenStream(lexer);
49 ExprGrammarParser parser = new ExprGrammarParser(tokens);
51 lexer.removeErrorListeners();
52 lexer.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
53 parser.removeErrorListeners();
54 parser.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
56 ExprContext expression = null;
59 expression = parser.expr();
60 } catch (Exception e) {
61 String errorMsg = e.getMessage();
64 throw new SvcLogicParserException(errorMsg);
68 ParseTreeWalker walker = new ParseTreeWalker();
69 SvcLogicExprListener listener = new SvcLogicExprListener();
70 walker.walk(listener, expression);
73 return(listener.getParsedExpr());
76 public static void main(String argv[]) {
79 System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug");
81 StringBuffer sbuff = new StringBuffer();
83 for (int i = 0 ; i < argv.length ; i++)
85 if (sbuff.length() > 0)
89 sbuff.append(argv[i]);
93 SvcLogicExpressionFactory.parse(sbuff.toString());
94 } catch (IOException e) {
95 LOG.error("Exception in SvcLogicExpressionFactory.parse",e);