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; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 25 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 26 | import java.text.ParseException; |
| 27 | import java.text.SimpleDateFormat; |
| 28 | import java.util.Date; |
| 29 | |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 30 | import static org.onosproject.yangutils.parser.ParsableDataType.REVISION_DATE_DATA; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 33 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 34 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 35 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 36 | |
| 37 | /* |
| 38 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 39 | * |
| 40 | * ABNF grammar as per RFC6020 |
| 41 | * import-stmt = import-keyword sep identifier-arg-str optsep |
| 42 | * "{" stmtsep |
| 43 | * prefix-stmt stmtsep |
| 44 | * [revision-date-stmt stmtsep] |
| 45 | * "}" |
| 46 | * include-stmt = include-keyword sep identifier-arg-str optsep |
| 47 | * (";" / |
| 48 | * "{" stmtsep |
| 49 | * [revision-date-stmt stmtsep] |
| 50 | * "}") |
| 51 | * revision-date-stmt = revision-date-keyword sep revision-date stmtend |
| 52 | * |
| 53 | * ANTLR grammar rule |
| 54 | * import_stmt : IMPORT_KEYWORD IDENTIFIER LEFT_CURLY_BRACE import_stmt_body |
| 55 | * RIGHT_CURLY_BRACE; |
| 56 | * import_stmt_body : prefix_stmt revision_date_stmt?; |
| 57 | * |
| 58 | * include_stmt : INCLUDE_KEYWORD IDENTIFIER (STMTEND | LEFT_CURLY_BRACE |
| 59 | * revision_date_stmt_body? RIGHT_CURLY_BRACE); |
| 60 | * |
| 61 | * revision_date_stmt : REVISION_DATE_KEYWORD DATE_ARG STMTEND; |
| 62 | * |
| 63 | */ |
| 64 | |
| 65 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 66 | * Implements listener based call back function corresponding to the |
| 67 | * "revision date" rule defined in ANTLR grammar file for corresponding ABNF |
| 68 | * rule in RFC 6020. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 69 | */ |
| 70 | public final class RevisionDateListener { |
| 71 | |
| 72 | /** |
| 73 | * Creates a new revision date listener. |
| 74 | */ |
| 75 | private RevisionDateListener() { |
| 76 | } |
| 77 | |
| 78 | /** |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 79 | * It is called when parser receives an input matching the grammar rule |
| 80 | * (revision date),perform validations and update the data model tree. |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 81 | * |
| 82 | * @param listener Listener's object. |
| 83 | * @param ctx context object of the grammar rule. |
| 84 | */ |
| 85 | public static void processRevisionDateEntry(TreeWalkListener listener, |
| 86 | GeneratedYangParser.RevisionDateStatementContext ctx) { |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 87 | |
| 88 | // Check for stack to be non empty. |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 89 | checkStackIsNotEmpty(listener, MISSING_HOLDER, REVISION_DATE_DATA, ctx.DATE_ARG().getText(), |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 90 | ENTRY); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 91 | |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 92 | if (!isDateValid(ctx.DATE_ARG().getText())) { |
| 93 | ParserException parserException = new ParserException("Input date is not correct"); |
| 94 | parserException.setLine(ctx.DATE_ARG().getSymbol().getLine()); |
| 95 | parserException.setCharPosition(ctx.DATE_ARG().getSymbol().getCharPositionInLine()); |
| 96 | throw parserException; |
| 97 | } |
| 98 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 99 | // Obtain the node of the stack. |
| 100 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 101 | switch (tmpNode.getParsableDataType()) { |
| 102 | case IMPORT_DATA: { |
| 103 | YangImport importNode = (YangImport) tmpNode; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 104 | importNode.setRevision(ctx.DATE_ARG().getText()); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 105 | break; |
| 106 | } |
| 107 | case INCLUDE_DATA: { |
| 108 | YangInclude includeNode = (YangInclude) tmpNode; |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 109 | includeNode.setRevision(ctx.DATE_ARG().getText()); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 110 | break; |
| 111 | } |
| 112 | default: |
Gaurav Agrawal | 800a8e7 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 113 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, REVISION_DATE_DATA, |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 114 | ctx.DATE_ARG().getText(), ENTRY)); |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 115 | } |
Gaurav Agrawal | 22db16d | 2016-02-12 16:50:55 +0530 | [diff] [blame] | 116 | } |
Gaurav Agrawal | 0520c7c | 2016-02-21 02:56:46 +0530 | [diff] [blame] | 117 | |
| 118 | /** |
| 119 | * Validates the revision date. |
| 120 | * |
| 121 | * @param dateToValidate input revision date. |
| 122 | * @return validation result, true for success, false for failure. |
| 123 | */ |
| 124 | private static boolean isDateValid(String dateToValidate) { |
| 125 | |
| 126 | if (dateToValidate == null) { |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| 131 | sdf.setLenient(false); |
| 132 | |
| 133 | try { |
| 134 | //if not valid, it will throw ParseException |
| 135 | Date date = sdf.parse(dateToValidate); |
| 136 | System.out.println(date); |
| 137 | } catch (ParseException e) { |
| 138 | return false; |
| 139 | } |
| 140 | return true; |
| 141 | } |
| 142 | } |