Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.parser.impl; |
| 18 | |
| 19 | import org.antlr.v4.runtime.ANTLRFileStream; |
| 20 | import org.antlr.v4.runtime.ANTLRInputStream; |
| 21 | import org.antlr.v4.runtime.CommonTokenStream; |
| 22 | import org.antlr.v4.runtime.tree.ParseTree; |
| 23 | import org.antlr.v4.runtime.tree.ParseTreeWalker; |
| 24 | import org.onosproject.yangutils.datamodel.YangNode; |
| 25 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 26 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer; |
| 27 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 28 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 29 | import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener; |
| 30 | |
| 31 | import java.io.IOException; |
| 32 | |
| 33 | /** |
| 34 | * Manages file parsing, parse tree creation and data model tree creation |
| 35 | * corresponding to an input YANG file. |
| 36 | */ |
| 37 | public class YangUtilsParserManager implements YangUtilsParser { |
| 38 | |
| 39 | @Override |
| 40 | public YangNode getDataModel(String yangFile) throws IOException, ParserException { |
| 41 | |
| 42 | /** |
| 43 | * Create a char stream that reads from YANG file. Throws an exception |
| 44 | * in case input YANG file is either null or non existent. |
| 45 | */ |
| 46 | ANTLRInputStream input = null; |
| 47 | try { |
| 48 | input = new ANTLRFileStream(yangFile); |
| 49 | } catch (IOException e) { |
| 50 | e.printStackTrace(); |
| 51 | throw e; |
| 52 | } |
| 53 | |
| 54 | // Create a lexer that feeds off of input char stream. |
| 55 | GeneratedYangLexer lexer = new GeneratedYangLexer(input); |
| 56 | |
| 57 | // Create a buffer of tokens pulled from the lexer. |
| 58 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 59 | |
| 60 | // Create a parser that feeds off the tokens buffer. |
| 61 | GeneratedYangParser parser = new GeneratedYangParser(tokens); |
| 62 | |
| 63 | // Remove console error listener. |
| 64 | parser.removeErrorListeners(); |
| 65 | |
| 66 | // Create instance of customized error listener. |
| 67 | ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); |
| 68 | |
| 69 | // Add customized error listener to catch errors during parsing. |
| 70 | parser.addErrorListener(parseTreeErrorListener); |
| 71 | |
| 72 | // Begin parsing YANG file and generate parse tree. |
| 73 | ParseTree tree = parser.yangfile(); |
| 74 | |
| 75 | /** |
| 76 | * Throws an parser Exception if exception flag is set i.e. exception has |
| 77 | * occurred during parsing. |
| 78 | */ |
| 79 | if (parseTreeErrorListener.isExceptionFlag()) { |
| 80 | // Get the exception occurred during parsing. |
| 81 | ParserException parserException = parseTreeErrorListener.getParserException(); |
| 82 | parserException.setFileName(yangFile); |
| 83 | throw parserException; |
| 84 | } |
| 85 | |
| 86 | // Create a walker to walk the parse tree. |
| 87 | ParseTreeWalker walker = new ParseTreeWalker(); |
| 88 | |
| 89 | // Create a listener implementation class object. |
| 90 | TreeWalkListener treeWalker = new TreeWalkListener(); |
| 91 | |
| 92 | /** |
| 93 | * Walk parse tree, provide call backs to methods in listener and |
| 94 | * build data model tree. |
| 95 | */ |
| 96 | walker.walk(treeWalker, tree); |
| 97 | |
| 98 | // Throws an parser exception which has occurred during listener walk. |
| 99 | if (treeWalker.getErrorInformation().isErrorFlag()) { |
| 100 | // Create object of listener exception |
| 101 | ParserException listenerException = new ParserException(); |
| 102 | listenerException.setMsg(treeWalker.getErrorInformation().getErrorMsg()); |
| 103 | listenerException.setFileName(yangFile); |
| 104 | throw listenerException; |
| 105 | } |
| 106 | |
| 107 | // Returns the Root Node of the constructed data model tree. |
| 108 | return treeWalker.getRootNode(); |
| 109 | } |
| 110 | } |