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