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 | |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 19 | import org.onosproject.yangutils.parser.ParsableDataType; |
| 20 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 22 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 23 | |
| 24 | /** |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 25 | * It's a utility to carry out listener validation. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 26 | */ |
| 27 | public final class ListenerValidation { |
| 28 | |
| 29 | /** |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 30 | * Creates a new listener validation. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 31 | */ |
| 32 | private ListenerValidation() { |
| 33 | } |
| 34 | |
| 35 | /** |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 36 | * Checks parsed data stack is not empty. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 37 | * |
| 38 | * @param listener Listener's object. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 39 | * @param errorType error type needs to be set in error message. |
| 40 | * @param parsableDataType type of parsable data in which error occurred. |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 41 | * @param parsableDataTypeName name of parsable data type in which error |
| 42 | * occurred. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 43 | * @param errorLocation location where error occurred. |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 44 | */ |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 45 | public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType, |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 46 | ParsableDataType parsableDataType, String parsableDataTypeName, |
| 47 | ListenerErrorLocation errorLocation) { |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 48 | if (listener.getParsedDataStack().empty()) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 49 | /* |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 50 | * If stack is empty it indicates error condition, value of |
| 51 | * parsableDataTypeName will be null in case there is no name |
| 52 | * attached to parsable data type. |
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 | String message = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName, |
| 55 | errorLocation); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 56 | throw new ParserException(message); |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 57 | } |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Checks parsed data stack is empty. |
| 62 | * |
| 63 | * @param listener Listener's object. |
| 64 | * @param errorType error type needs to be set in error message. |
| 65 | * @param parsableDataType type of parsable data in which error occurred. |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 66 | * @param parsableDataTypeName name of parsable data type in which error |
| 67 | * occurred. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 68 | * @param errorLocation location where error occurred. |
| 69 | */ |
| 70 | |
| 71 | public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType, |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 72 | ParsableDataType parsableDataType, String parsableDataTypeName, |
| 73 | ListenerErrorLocation errorLocation) { |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 74 | |
| 75 | if (!listener.getParsedDataStack().empty()) { |
| 76 | /* |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 77 | * If stack is empty it indicates error condition, value of |
| 78 | * parsableDataTypeName will be null in case there is no name |
| 79 | * attached to parsable data type. |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 80 | */ |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 81 | String message = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName, |
| 82 | errorLocation); |
Gaurav Agrawal | a04483c | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 83 | throw new ParserException(message); |
| 84 | } |
Gaurav Agrawal | 8889763 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 85 | } |
Gaurav Agrawal | 02b05d2 | 2016-02-19 12:57:13 +0530 | [diff] [blame] | 86 | } |