remove ODL/karaf from common
[ccsdk/sli/core.git] / sli / common / src / main / java / org / onap / ccsdk / sli / core / sli / SvcLogicExpressionFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  */
21
22 package org.onap.ccsdk.sli.core.sli;
23
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;
34
35
36 public class SvcLogicExpressionFactory {
37         
38         private static final Logger LOG = LoggerFactory
39                         .getLogger(SvcLogicExpressionFactory.class);
40
41         
42         public static SvcLogicExpression parse(String exprStr) throws IOException
43         {
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);
49
50                 lexer.removeErrorListeners();
51                 lexer.addErrorListener(SvcLogicExprParserErrorListener.getInstance());          
52                 parser.removeErrorListeners();
53                 parser.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
54
55                 ExprContext expression = null;
56                 
57                 try {
58                         expression = parser.expr();
59                 } catch (Exception e) {
60                         String errorMsg = e.getMessage();
61                         
62                         LOG.error(errorMsg);
63                         throw new SvcLogicParserException(errorMsg);
64                 }
65                 
66         
67                 ParseTreeWalker walker = new ParseTreeWalker();
68                 SvcLogicExprListener listener = new SvcLogicExprListener();
69                 walker.walk(listener, expression);
70                 
71                 
72                 return(listener.getParsedExpr());
73         }
74
75 }