Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +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.listeners; |
| 18 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 19 | import org.onosproject.yangutils.datamodel.YangImport; |
| 20 | import org.onosproject.yangutils.datamodel.YangInclude; |
| 21 | import org.onosproject.yangutils.parser.Parsable; |
| 22 | import org.onosproject.yangutils.parser.ParsableDataType; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 26 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation; |
| 27 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction; |
| 28 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType; |
| 29 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 33 | * |
| 34 | * ABNF grammar as per RFC6020 |
| 35 | * import-stmt = import-keyword sep identifier-arg-str optsep |
| 36 | * "{" stmtsep |
| 37 | * prefix-stmt stmtsep |
| 38 | * [revision-date-stmt stmtsep] |
| 39 | * "}" |
| 40 | * include-stmt = include-keyword sep identifier-arg-str optsep |
| 41 | * (";" / |
| 42 | * "{" stmtsep |
| 43 | * [revision-date-stmt stmtsep] |
| 44 | * "}") |
| 45 | * revision-date-stmt = revision-date-keyword sep revision-date stmtend |
| 46 | * |
| 47 | * ANTLR grammar rule |
| 48 | * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body |
| 49 | * RIGHT_CURLY_BRACE; |
| 50 | * import_stmt_body : prefix_stmt revision_date_stmt?; |
| 51 | * |
| 52 | * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE |
| 53 | * revision_date_stmt_body? RIGHT_CURLY_BRACE); |
| 54 | * |
| 55 | * revision_date_stmt : REVISION_DATE_KEYWORD DATE_ARG STMTEND; |
| 56 | * |
| 57 | */ |
| 58 | |
| 59 | /** |
| 60 | * Implements listener based call back function corresponding to the "revision date" |
| 61 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 62 | */ |
| 63 | public final class RevisionDateListener { |
| 64 | |
| 65 | /** |
| 66 | * Creates a new revision date listener. |
| 67 | */ |
| 68 | private RevisionDateListener() { |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * It is called when parser receives an input matching the grammar |
| 73 | * rule (revision date),perform validations and update the data model |
| 74 | * tree. |
| 75 | * |
| 76 | * @param listener Listener's object. |
| 77 | * @param ctx context object of the grammar rule. |
| 78 | */ |
| 79 | public static void processRevisionDateEntry(TreeWalkListener listener, |
| 80 | GeneratedYangParser.RevisionDateStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 81 | |
| 82 | // Check for stack to be non empty. |
| 83 | ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, |
| 84 | ParsableDataType.REVISION_DATE_DATA, |
| 85 | String.valueOf(ctx.DATE_ARG().getText()), |
| 86 | ListenerErrorLocation.ENTRY); |
| 87 | |
| 88 | // Obtain the node of the stack. |
| 89 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 90 | switch (tmpNode.getParsableDataType()) { |
| 91 | case IMPORT_DATA: { |
| 92 | YangImport importNode = (YangImport) tmpNode; |
| 93 | importNode.setRevision(String.valueOf(ctx.DATE_ARG().getText())); |
| 94 | break; |
| 95 | } |
| 96 | case INCLUDE_DATA: { |
| 97 | YangInclude includeNode = (YangInclude) tmpNode; |
| 98 | includeNode.setRevision(String.valueOf(ctx.DATE_ARG().getText())); |
| 99 | break; |
| 100 | } |
| 101 | default: |
| 102 | throw new ParserException( |
| 103 | ListenerErrorMessageConstruction |
| 104 | .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER, |
| 105 | ParsableDataType.REVISION_DATE_DATA, |
| 106 | String.valueOf(ctx.DATE_ARG().getText()), |
| 107 | ListenerErrorLocation.ENTRY)); |
| 108 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 109 | } |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame^] | 110 | // TODO Implement the DATE_ARG validation as per RFC 6020. |
| 111 | } |