Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +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.parserutils; |
| 18 | |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame^] | 19 | import org.onosproject.yangutils.datamodel.YangContainer; |
| 20 | import org.onosproject.yangutils.datamodel.YangList; |
| 21 | import org.onosproject.yangutils.datamodel.YangNode; |
| 22 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.parser.ParsableDataType; |
| 24 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 26 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 27 | |
| 28 | /** |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 29 | * It's a utility to carry out listener validation. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 30 | */ |
| 31 | public final class ListenerValidation { |
| 32 | |
| 33 | /** |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 34 | * Creates a new listener validation. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 35 | */ |
| 36 | private ListenerValidation() { |
| 37 | } |
| 38 | |
| 39 | /** |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 40 | * Checks parsed data stack is not empty. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 41 | * |
| 42 | * @param listener Listener's object. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 43 | * @param errorType error type needs to be set in error message. |
| 44 | * @param parsableDataType type of parsable data in which error occurred. |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 45 | * @param parsableDataTypeName name of parsable data type in which error |
| 46 | * occurred. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 47 | * @param errorLocation location where error occurred. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 48 | */ |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 49 | public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType, |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 50 | ParsableDataType parsableDataType, String parsableDataTypeName, |
| 51 | ListenerErrorLocation errorLocation) { |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 52 | if (listener.getParsedDataStack().empty()) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 53 | /* |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 54 | * If stack is empty it indicates error condition, value of |
| 55 | * parsableDataTypeName will be null in case there is no name |
| 56 | * attached to parsable data type. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 57 | */ |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 58 | String message = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName, |
| 59 | errorLocation); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 60 | throw new ParserException(message); |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 61 | } |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Checks parsed data stack is empty. |
| 66 | * |
| 67 | * @param listener Listener's object. |
| 68 | * @param errorType error type needs to be set in error message. |
| 69 | * @param parsableDataType type of parsable data in which error occurred. |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 70 | * @param parsableDataTypeName name of parsable data type in which error |
| 71 | * occurred. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 72 | * @param errorLocation location where error occurred. |
| 73 | */ |
| 74 | |
| 75 | public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType, |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 76 | ParsableDataType parsableDataType, String parsableDataTypeName, |
| 77 | ListenerErrorLocation errorLocation) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 78 | |
| 79 | if (!listener.getParsedDataStack().empty()) { |
| 80 | /* |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 81 | * If stack is empty it indicates error condition, value of |
| 82 | * parsableDataTypeName will be null in case there is no name |
| 83 | * attached to parsable data type. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 84 | */ |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 85 | String message = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName, |
| 86 | errorLocation); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 87 | throw new ParserException(message); |
| 88 | } |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 89 | } |
Vidyashree Rama | f4c617c | 2016-02-24 12:28:22 +0530 | [diff] [blame^] | 90 | |
| 91 | /** |
| 92 | * Returns parent node config value, if top node does not specify a config statement |
| 93 | * then default value true is returned. |
| 94 | * |
| 95 | * @param listener listener's object. |
| 96 | * @return true/false parent's config value. |
| 97 | */ |
| 98 | public static boolean getParentNodeConfig(TreeWalkListener listener) { |
| 99 | YangNode parentNode; |
| 100 | Parsable curData = listener.getParsedDataStack().peek(); |
| 101 | if (curData instanceof YangNode) { |
| 102 | parentNode = ((YangNode) curData).getParent(); |
| 103 | if (parentNode instanceof YangContainer) { |
| 104 | return ((YangContainer) parentNode).isConfig(); |
| 105 | } else if (parentNode instanceof YangList) { |
| 106 | return ((YangList) parentNode).isConfig(); |
| 107 | } |
| 108 | } |
| 109 | return true; |
| 110 | } |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 111 | } |