blob: 98b3429dbc20fa2ea12019eb22372f32daa99579 [file] [log] [blame]
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05303 *
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;
Bharat saraswald9822e92016-04-05 15:13:44 +053023import org.onosproject.yangutils.utils.YangConstructType;
24
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053025import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
26import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053028
29/**
Bharat saraswald9822e92016-04-05 15:13:44 +053030 * Represents the detector of YANG construct collision in a YANG file.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053031 */
32public 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 saraswald9822e92016-04-05 15:13:44 +053056 throws ParserException {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053057
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}