Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +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.parserutils; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
| 20 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 21 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 22 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.utils.YangConstructType; |
| 24 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 25 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; |
| 26 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 28 | |
| 29 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 30 | * Represents the detector of YANG construct collision in a YANG file. |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 31 | */ |
| 32 | public final class ListenerCollisionDetector { |
| 33 | |
| 34 | /** |
| 35 | * Creates a new listener collision. |
| 36 | */ |
| 37 | private ListenerCollisionDetector() { |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Detects that the identifiers of all these child nodes must be unique |
| 42 | * within all cases in a choice. |
| 43 | * |
| 44 | * @param listener listener's object |
| 45 | * @param line line of identifier in YANG file, required for error |
| 46 | * reporting |
| 47 | * @param charPosition character position of identifier in YANG file, |
| 48 | * required for error reporting |
| 49 | * @param identifierName name for which uniqueness is to be detected |
| 50 | * @param constructType type of YANG construct for which collision check is |
| 51 | * to be performed |
| 52 | * @throws ParserException if identifier is not unique |
| 53 | */ |
| 54 | public static void detectCollidingChildUtil(TreeWalkListener listener, int line, int charPosition, |
| 55 | String identifierName, YangConstructType constructType) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 56 | throws ParserException { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 57 | |
| 58 | if (listener.getParsedDataStack().peek() instanceof CollisionDetector) { |
| 59 | try { |
| 60 | ((CollisionDetector) listener.getParsedDataStack().peek()).detectCollidingChild( |
| 61 | identifierName, constructType); |
| 62 | } catch (DataModelException e) { |
| 63 | ParserException parserException = new ParserException(e.getMessage()); |
| 64 | parserException.setLine(line); |
| 65 | parserException.setCharPosition(charPosition); |
| 66 | throw parserException; |
| 67 | } |
| 68 | } else { |
| 69 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, constructType, identifierName, |
| 70 | EXIT)); |
| 71 | } |
| 72 | } |
| 73 | } |