Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 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 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangDesc; |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 24 | |
Bharat saraswal | c2d3be1 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 25 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.DESCRIPTION_DATA; |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 34 | * |
| 35 | * ABNF grammar as per RFC6020 |
| 36 | * description-stmt = description-keyword sep string optsep stmtend |
| 37 | * |
| 38 | * ANTLR grammar rule |
| 39 | * descriptionStatement : DESCRIPTION_KEYWORD string STMTEND; |
| 40 | */ |
| 41 | |
| 42 | /** |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 43 | * Represents listener based call back function corresponding to the |
| 44 | * "description" rule defined in ANTLR grammar file for corresponding ABNF rule |
| 45 | * in RFC 6020. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 46 | */ |
| 47 | public final class DescriptionListener { |
| 48 | |
| 49 | /** |
| 50 | * Creates a new description listener. |
| 51 | */ |
| 52 | private DescriptionListener() { |
| 53 | } |
| 54 | |
| 55 | /** |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 56 | * It is called when parser receives an input matching the grammar rule |
| 57 | * (description), perform validations and updates the data model tree. |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 58 | * |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 59 | * @param listener listener's object |
| 60 | * @param ctx context object of the grammar rule |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 61 | */ |
| 62 | public static void processDescriptionEntry(TreeWalkListener listener, |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 63 | GeneratedYangParser.DescriptionStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 64 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 65 | // Check for stack to be non empty. |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 66 | checkStackIsNotEmpty(listener, MISSING_HOLDER, DESCRIPTION_DATA, ctx.string().getText(), ENTRY); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 67 | |
| 68 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 69 | if (tmpData instanceof YangDesc) { |
| 70 | YangDesc description = (YangDesc) tmpData; |
| 71 | description.setDescription(ctx.string().getText()); |
| 72 | } else { |
Vidyashree Rama | e300f70 | 2016-02-20 19:27:56 +0530 | [diff] [blame] | 73 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, DESCRIPTION_DATA, |
| 74 | ctx.string().getText(), ENTRY)); |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 75 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 76 | } |
| 77 | } |