Vidyashree Rama | 918f162 | 2016-07-28 17:33:15 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 19 | import org.onosproject.yangutils.datamodel.YangAppDataStructure; |
| 20 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 21 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 22 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 23 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 24 | |
| 25 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.KEY_DATA; |
| 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.ListenerUtil.removeQuotesAndHandleConcat; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 32 | import static org.onosproject.yangutils.utils.UtilConstants.SPACE; |
| 33 | |
| 34 | /* |
| 35 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 36 | * |
| 37 | * ABNF grammar as per RFC6020 |
| 38 | * data-structure-key-stmt = prefix:key-keyword string ";" |
| 39 | * |
| 40 | * ANTLR grammar rule |
| 41 | * dataStructureKeyStatement : DATA_STRUCTURE_KEY string STMTEND; |
| 42 | */ |
| 43 | |
| 44 | /** |
| 45 | * Represents listener based call back function corresponding to the "key" |
| 46 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 47 | */ |
| 48 | public final class DataStructureKeyListener { |
| 49 | |
| 50 | /** |
| 51 | * Creates a new data-structure-key listener. |
| 52 | */ |
| 53 | private DataStructureKeyListener() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Performs validation and updates the data model tree. It is called when parser receives an |
| 58 | * input matching the grammar rule(key). |
| 59 | * |
| 60 | * @param listener listener's object |
| 61 | * @param ctx context object of the grammar rule |
| 62 | */ |
| 63 | public static void processDataStructureKeyEntry(TreeWalkListener listener, |
| 64 | GeneratedYangParser.DataStructureKeyStatementContext ctx) { |
| 65 | |
| 66 | // Check for stack to be non empty. |
| 67 | checkStackIsNotEmpty(listener, MISSING_HOLDER, KEY_DATA, ctx.string().getText(), ENTRY); |
| 68 | |
| 69 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 70 | if (listener.getParsedDataStack().peek() instanceof YangAppDataStructure) { |
| 71 | YangAppDataStructure dataStructure = (YangAppDataStructure) tmpData; |
| 72 | String tmpKeyValue = removeQuotesAndHandleConcat(ctx.string().getText()); |
| 73 | if (tmpKeyValue.contains(SPACE)) { |
| 74 | String[] keyValues = tmpKeyValue.split(SPACE); |
| 75 | for (String keyValue : keyValues) { |
| 76 | dataStructure.addKey(keyValue); |
| 77 | } |
| 78 | } else { |
| 79 | dataStructure.addKey(tmpKeyValue); |
| 80 | } |
| 81 | } else { |
| 82 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, KEY_DATA, ctx.string().getText(), |
| 83 | ENTRY)); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |