Refactor dblib
[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 ONAP
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.sli.core.sli;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26
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                 LOG.trace("parse("+exprStr+")");
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);
50
51                 lexer.removeErrorListeners();
52                 lexer.addErrorListener(SvcLogicExprParserErrorListener.getInstance());          
53                 parser.removeErrorListeners();
54                 parser.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
55
56                 ExprContext expression = null;
57                 
58                 try {
59                         expression = parser.expr();
60                 } catch (Exception e) {
61                         String errorMsg = e.getMessage();
62                         
63                         LOG.error(errorMsg);
64                         throw new SvcLogicParserException(errorMsg);
65                 }
66                 
67         
68                 ParseTreeWalker walker = new ParseTreeWalker();
69                 SvcLogicExprListener listener = new SvcLogicExprListener();
70                 walker.walk(listener, expression);
71                 
72                 
73                 return(listener.getParsedExpr());
74         }
75         
76         public static void main(String argv[]) {
77
78
79                 System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug");
80
81                 StringBuffer sbuff = new StringBuffer();
82                 
83                 for (int i = 0 ; i < argv.length ; i++)
84                 {
85                         if (sbuff.length() > 0)
86                         {
87                                 sbuff.append(" ");
88                         }
89                         sbuff.append(argv[i]);
90                 }
91                 
92                 try {
93                         SvcLogicExpressionFactory.parse(sbuff.toString());
94                 } catch (IOException e) {
95                         e.printStackTrace();
96                 }
97         }
98 }