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