[SDNC-5] Rebase sdnc-core
[sdnc/core.git] / sli / common / src / main / java / org / openecomp / sdnc / sli / SvcLogicExpressionFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
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.openecomp.sdnc.sli;
23
24 import java.io.ByteArrayInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27
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;
35
36
37 public class SvcLogicExpressionFactory {
38         
39         private static final Logger LOG = LoggerFactory
40                         .getLogger(SvcLogicExpressionFactory.class);
41
42         
43         public static SvcLogicExpression parse(String exprStr) throws IOException
44         {
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);
51
52                 lexer.removeErrorListeners();
53                 lexer.addErrorListener(SvcLogicExprParserErrorListener.getInstance());          
54                 parser.removeErrorListeners();
55                 parser.addErrorListener(SvcLogicExprParserErrorListener.getInstance());
56
57                 ExprContext expression = null;
58                 
59                 try {
60                         expression = parser.expr();
61                 } catch (Exception e) {
62                         String errorMsg = e.getMessage();
63                         
64                         LOG.error(errorMsg);
65                         throw new SvcLogicParserException(errorMsg);
66                 }
67                 
68         
69                 ParseTreeWalker walker = new ParseTreeWalker();
70                 SvcLogicExprListener listener = new SvcLogicExprListener();
71                 walker.walk(listener, expression);
72                 
73                 
74                 return(listener.getParsedExpr());
75         }
76         
77         public static void main(String argv[]) {
78
79
80                 System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "debug");
81
82                 StringBuffer sbuff = new StringBuffer();
83                 
84                 for (int i = 0 ; i < argv.length ; i++)
85                 {
86                         if (sbuff.length() > 0)
87                         {
88                                 sbuff.append(" ");
89                         }
90                         sbuff.append(argv[i]);
91                 }
92                 
93                 try {
94                         SvcLogicExpressionFactory.parse(sbuff.toString());
95                 } catch (IOException e) {
96                         e.printStackTrace();
97                 }
98         }
99 }