Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +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 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.datamodel.YangList; |
| 20 | import org.onosproject.yangutils.parser.Parsable; |
| 21 | import org.onosproject.yangutils.parser.ParsableDataType; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation; |
| 26 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction; |
| 27 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType; |
| 28 | import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation; |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 32 | * |
| 33 | * ABNF grammar as per RFC6020 |
| 34 | * key-stmt = key-keyword sep key-arg-str stmtend |
| 35 | * |
| 36 | * ANTLR grammar rule |
| 37 | * keyStatement : KEY_KEYWORD string STMTEND; |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Implements listener based call back function corresponding to the "key" |
| 42 | * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020. |
| 43 | */ |
| 44 | public final class KeyListener { |
| 45 | |
| 46 | /** |
| 47 | * Creates a new key listener. |
| 48 | */ |
| 49 | private KeyListener() { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * It is called when parser receives an input matching the grammar |
| 54 | * rule (key), perform validations and updates the data model |
| 55 | * tree. |
| 56 | * |
| 57 | * @param listener listener's object. |
| 58 | * @param ctx context object of the grammar rule. |
| 59 | */ |
| 60 | public static void processKeyEntry(TreeWalkListener listener, |
| 61 | GeneratedYangParser.KeyStatementContext ctx) { |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 62 | |
Vidyashree Rama | b24ca90 | 2016-02-13 21:47:58 +0530 | [diff] [blame] | 63 | // Check for stack to be non empty. |
| 64 | ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER, |
| 65 | ParsableDataType.KEY_DATA, String.valueOf(ctx.string().getText()), |
| 66 | ListenerErrorLocation.ENTRY); |
| 67 | |
| 68 | Parsable tmpData = listener.getParsedDataStack().peek(); |
| 69 | if (listener.getParsedDataStack().peek() instanceof YangList) { |
| 70 | YangList yangList = (YangList) tmpData; |
| 71 | String tmpKeyValue = ctx.string().getText().replace("\"", ""); |
| 72 | if (tmpKeyValue.contains(" ")) { |
| 73 | String[] keyValues = tmpKeyValue.split(" "); |
| 74 | for (String keyValue : keyValues) { |
| 75 | yangList.addKey(keyValue); |
| 76 | } |
| 77 | } else { |
| 78 | yangList.addKey(tmpKeyValue); |
| 79 | } |
| 80 | } else { |
| 81 | throw new ParserException(ListenerErrorMessageConstruction |
| 82 | .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER, |
| 83 | ParsableDataType.KEY_DATA, |
| 84 | String.valueOf(ctx.string().getText()), |
| 85 | ListenerErrorLocation.ENTRY)); |
| 86 | } |
Vidyashree Rama | fe971cb | 2016-02-12 18:44:12 +0530 | [diff] [blame] | 87 | } |
| 88 | } |