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.openecomp.sdnc.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.openecomp.sdnc.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 LOG.trace("parse("+exprStr+")");
46 InputStream exprStream = new ByteArrayInputStream(exprStr.getBytes());
47 CharStream input = new ANTLRInputStream(exprStream);
48 ExprGrammarLexer lexer = new ExprGrammarLexer(input);
49 CommonTokenStream tokens = new CommonTokenStream(lexer);
50 ExprGrammarParser parser = new ExprGrammarParser(tokens);
52 lexer.removeErrorListeners();
53 lexer.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
54 parser.removeErrorListeners();
55 parser.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
57 ExprContext expression = null;
60 expression = parser.expr();
61 } catch (Exception e) {
62 String errorMsg = e.getMessage();
65 throw new SvcLogicParserException(errorMsg);
69 ParseTreeWalker walker = new ParseTreeWalker();
70 SvcLogicExprListener listener = new SvcLogicExprListener();
71 walker.walk(listener, expression);
74 return(listener.getParsedExpr());
77 public static void main(String argv[]) {
80 System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug");
82 StringBuffer sbuff = new StringBuffer();
84 for (int i = 0 ; i < argv.length ; i++)
86 if (sbuff.length() > 0)
90 sbuff.append(argv[i]);
94 SvcLogicExpressionFactory.parse(sbuff.toString());
95 } catch (IOException e) {