blob: 9cee91fea894c4d6e9a5d077f4d25957ad3539dd [file] [log] [blame]
Vidyashree Rama918f1622016-07-28 17:33:15 +05301/*
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
17package org.onosproject.yangutils.parser.impl.listeners;
18
19import org.onosproject.yangutils.datamodel.YangAppDataStructure;
20import org.onosproject.yangutils.datamodel.utils.Parsable;
21import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
22import org.onosproject.yangutils.parser.exceptions.ParserException;
23import org.onosproject.yangutils.parser.impl.TreeWalkListener;
24
25import static org.onosproject.yangutils.datamodel.utils.YangConstructType.KEY_DATA;
26import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
27import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
28import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
29import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
30import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.removeQuotesAndHandleConcat;
31import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
32import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
33
34/*
35 * Reference: RFC6020 and YANG ANTLR Grammar
36 *
37 * ABNF grammar as per RFC6020
38 * data-structure-key-stmt = prefix:key-keyword string ";"
39 *
40 * ANTLR grammar rule
41 * dataStructureKeyStatement : DATA_STRUCTURE_KEY string STMTEND;
42 */
43
44/**
45 * Represents listener based call back function corresponding to the "key"
46 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
47 */
48public final class DataStructureKeyListener {
49
50 /**
51 * Creates a new data-structure-key listener.
52 */
53 private DataStructureKeyListener() {
54 }
55
56 /**
57 * Performs validation and updates the data model tree. It is called when parser receives an
58 * input matching the grammar rule(key).
59 *
60 * @param listener listener's object
61 * @param ctx context object of the grammar rule
62 */
63 public static void processDataStructureKeyEntry(TreeWalkListener listener,
64 GeneratedYangParser.DataStructureKeyStatementContext ctx) {
65
66 // Check for stack to be non empty.
67 checkStackIsNotEmpty(listener, MISSING_HOLDER, KEY_DATA, ctx.string().getText(), ENTRY);
68
69 Parsable tmpData = listener.getParsedDataStack().peek();
70 if (listener.getParsedDataStack().peek() instanceof YangAppDataStructure) {
71 YangAppDataStructure dataStructure = (YangAppDataStructure) tmpData;
Bharat saraswale3175d32016-08-31 17:50:11 +053072
73 dataStructure.setLineNumber(ctx.getStart().getLine());
74 dataStructure.setCharPosition(ctx.getStart().getCharPositionInLine());
75 dataStructure.setFileName(listener.getFileName());
Vidyashree Rama918f1622016-07-28 17:33:15 +053076 String tmpKeyValue = removeQuotesAndHandleConcat(ctx.string().getText());
77 if (tmpKeyValue.contains(SPACE)) {
78 String[] keyValues = tmpKeyValue.split(SPACE);
79 for (String keyValue : keyValues) {
80 dataStructure.addKey(keyValue);
81 }
82 } else {
83 dataStructure.addKey(tmpKeyValue);
84 }
85 } else {
86 throw new ParserException(constructListenerErrorMessage(INVALID_HOLDER, KEY_DATA, ctx.string().getText(),
87 ENTRY));
88 }
89 }
90}
91