rama-huawei | e37efa4 | 2016-07-11 14:48:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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 | |
| 17 | package org.onosproject.yangutils.parser.impl.listeners; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.YangAppErrorHolder; |
| 20 | import org.onosproject.yangutils.datamodel.YangAppErrorInfo; |
| 21 | import org.onosproject.yangutils.datamodel.utils.Parsable; |
| 22 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 23 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 24 | import org.onosproject.yangutils.parser.impl.TreeWalkListener; |
| 25 | |
| 26 | import static org.onosproject.yangutils.datamodel.utils.YangConstructType.ERROR_MESSAGE_DATA; |
| 27 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; |
| 28 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage; |
| 29 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER; |
| 30 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER; |
| 31 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat; |
| 32 | import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; |
| 33 | |
| 34 | /* |
| 35 | * Reference: RFC6020 and YANG ANTLR Grammar |
| 36 | * |
| 37 | * ABNF grammar as per RFC6020 |
| 38 | * |
| 39 | * error-message-stmt = error-message-keyword sep string stmtend |
| 40 | * |
| 41 | * ANTLR grammar rule |
| 42 | * errorMessageStatement : ERROR_MESSAGE_KEYWORD string STMTEND; |
| 43 | */ |
| 44 | |
| 45 | /** |
| 46 | * Represents listener based call back function corresponding to the |
| 47 | * app error message defined in ANTLR grammar file for corresponding ABNF rule |
| 48 | * in RFC 6020. |
| 49 | */ |
| 50 | public final class ErrorMessageListener { |
| 51 | |
| 52 | /** |
| 53 | * Creates a new must listener. |
| 54 | */ |
| 55 | private ErrorMessageListener() { |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Performs validations and updates the data model tree. It is called when parser |
| 60 | * receives an input matching the grammar rule (app error message). |
| 61 | * |
| 62 | * @param listener listener's object |
| 63 | * @param ctx context object of the grammar rule |
| 64 | */ |
| 65 | public static void processErrorMessageEntry(TreeWalkListener listener, |
VinodKumarS-Huawei | 8f16422 | 2016-08-31 15:47:30 +0530 | [diff] [blame^] | 66 | GeneratedYangParser.ErrorMessageStatementContext ctx) { |
rama-huawei | e37efa4 | 2016-07-11 14:48:12 +0530 | [diff] [blame] | 67 | |
| 68 | // Check for stack to be non empty. |
| 69 | checkStackIsNotEmpty(listener, MISSING_HOLDER, ERROR_MESSAGE_DATA, ctx.string().getText(), ENTRY); |
| 70 | String errorMessage = removeQuotesAndHandleConcat(ctx.string().getText()); |
| 71 | |
| 72 | // Obtain the node of the stack. |
| 73 | Parsable tmpNode = listener.getParsedDataStack().peek(); |
| 74 | if (tmpNode instanceof YangAppErrorHolder) { |
| 75 | YangAppErrorInfo yangAppErrorInfo = ((YangAppErrorHolder) tmpNode).getAppErrorInfo(); |
| 76 | yangAppErrorInfo.setErrorMessage(errorMessage); |
| 77 | } else { |
| 78 | throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, ERROR_MESSAGE_DATA, |
| 79 | ctx.string().getText(), ENTRY)); |
| 80 | } |
| 81 | } |
| 82 | } |