blob: 226a53072378b8b45d3dd8782ea8c84cd3b9ea5b [file] [log] [blame]
Gaurav Agrawal88897632016-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 Agrawala04483c2016-02-13 14:23:40 +053019import org.onosproject.yangutils.parser.ParsableDataType;
20import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal88897632016-02-12 18:37:50 +053021import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053022import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
Gaurav Agrawal88897632016-02-12 18:37:50 +053023
24/**
Gaurav Agrawala04483c2016-02-13 14:23:40 +053025 * It's a utility to carry out listener validation.
Gaurav Agrawal88897632016-02-12 18:37:50 +053026 */
27public final class ListenerValidation {
28
29 /**
Gaurav Agrawala04483c2016-02-13 14:23:40 +053030 * Creates a new listener validation.
Gaurav Agrawal88897632016-02-12 18:37:50 +053031 */
32 private ListenerValidation() {
33 }
34
35 /**
Gaurav Agrawala04483c2016-02-13 14:23:40 +053036 * Checks parsed data stack is not empty.
Gaurav Agrawal88897632016-02-12 18:37:50 +053037 *
38 * @param listener Listener's object.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053039 * @param errorType error type needs to be set in error message.
40 * @param parsableDataType type of parsable data in which error occurred.
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053041 * @param parsableDataTypeName name of parsable data type in which error
42 * occurred.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053043 * @param errorLocation location where error occurred.
Gaurav Agrawal88897632016-02-12 18:37:50 +053044 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053045 public static void checkStackIsNotEmpty(TreeWalkListener listener, ListenerErrorType errorType,
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053046 ParsableDataType parsableDataType, String parsableDataTypeName,
47 ListenerErrorLocation errorLocation) {
Gaurav Agrawal88897632016-02-12 18:37:50 +053048 if (listener.getParsedDataStack().empty()) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053049 /*
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053050 * 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 Agrawala04483c2016-02-13 14:23:40 +053053 */
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053054 String message = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName,
55 errorLocation);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053056 throw new ParserException(message);
Gaurav Agrawal88897632016-02-12 18:37:50 +053057 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +053058 }
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 Agrawal02b05d22016-02-19 12:57:13 +053066 * @param parsableDataTypeName name of parsable data type in which error
67 * occurred.
Gaurav Agrawala04483c2016-02-13 14:23:40 +053068 * @param errorLocation location where error occurred.
69 */
70
71 public static void checkStackIsEmpty(TreeWalkListener listener, ListenerErrorType errorType,
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053072 ParsableDataType parsableDataType, String parsableDataTypeName,
73 ListenerErrorLocation errorLocation) {
Gaurav Agrawala04483c2016-02-13 14:23:40 +053074
75 if (!listener.getParsedDataStack().empty()) {
76 /*
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053077 * 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 Agrawala04483c2016-02-13 14:23:40 +053080 */
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053081 String message = constructListenerErrorMessage(errorType, parsableDataType, parsableDataTypeName,
82 errorLocation);
Gaurav Agrawala04483c2016-02-13 14:23:40 +053083 throw new ParserException(message);
84 }
Gaurav Agrawal88897632016-02-12 18:37:50 +053085 }
Gaurav Agrawal02b05d22016-02-19 12:57:13 +053086}