blob: 4399cc54d0cdf5ec8414482163ddb601dfc04ea0 [file] [log] [blame]
Gaurav Agrawal91c69612016-02-12 18:37: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
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053019import org.onosproject.yangutils.parser.ParsableDataType;
20import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal91c69612016-02-12 18:37:50 +053021import org.onosproject.yangutils.parser.impl.TreeWalkListener;
22
23/**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053024 * It's a utility to carry out listener validation.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053025 */
26public final class ListenerValidation {
27
28 /**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053029 * Creates a new listener validation.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053030 */
31 private ListenerValidation() {
32 }
33
34 /**
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053035 * Checks parsed data stack is not empty.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053036 *
37 * @param listener Listener's object.
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053038 * @param errorType error type needs to be set in error message.
39 * @param parsableDataType type of parsable data in which error occurred.
40 * @param parsableDataTypeName name of parsable data type in which error occurred.
41 * @param errorLocation location where error occurred.
Gaurav Agrawal91c69612016-02-12 18:37:50 +053042 */
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053043 public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType,
44 ParsableDataType parsableDataType, String parsableDataTypeName,
45 ListenerErrorLocation errorLocation) {
Gaurav Agrawal91c69612016-02-12 18:37:50 +053046 if (listener.getParsedDataStack().empty()) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053047 /*
48 * If stack is empty it indicates error condition, value of parsableDataTypeName will be null in case there
49 * is no name attached to parsable data type.
50 */
51 String message = ListenerErrorMessageConstruction.constructListenerErrorMessage(errorType, parsableDataType,
52 parsableDataTypeName, errorLocation);
53 throw new ParserException(message);
Gaurav Agrawal91c69612016-02-12 18:37:50 +053054 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053055 }
56
57 /**
58 * Checks parsed data stack is empty.
59 *
60 * @param listener Listener's object.
61 * @param errorType error type needs to be set in error message.
62 * @param parsableDataType type of parsable data in which error occurred.
63 * @param parsableDataTypeName name of parsable data type in which error occurred.
64 * @param errorLocation location where error occurred.
65 */
66
67 public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType,
68 ParsableDataType parsableDataType, String parsableDataTypeName,
69 ListenerErrorLocation errorLocation) {
70
71 if (!listener.getParsedDataStack().empty()) {
72 /*
73 * If stack is empty it indicates error condition, value of parsableDataTypeName will be null in case there
74 * is no name attached to parsable data type.
75 */
76 String message = ListenerErrorMessageConstruction.constructListenerErrorMessage(errorType, parsableDataType,
77 parsableDataTypeName, errorLocation);
78 throw new ParserException(message);
79 }
Gaurav Agrawal91c69612016-02-12 18:37:50 +053080 }
81}