blob: 83867886258379591ab5d4dfba8dba77662a3cc6 [file] [log] [blame]
Gaurav Agrawal22db16d2016-02-12 16:50:55 +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.listeners;
18
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053019import org.onosproject.yangutils.datamodel.YangModule;
20import org.onosproject.yangutils.datamodel.YangNameSpace;
21import org.onosproject.yangutils.parser.Parsable;
22import org.onosproject.yangutils.parser.ParsableDataType;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053023import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053024import org.onosproject.yangutils.parser.exceptions.ParserException;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053025import org.onosproject.yangutils.parser.impl.TreeWalkListener;
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053026import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation;
27import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction;
28import org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType;
29import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
30
31import java.net.URI;
Gaurav Agrawal22db16d2016-02-12 16:50:55 +053032
33/*
34 * Reference: RFC6020 and YANG ANTLR Grammar
35 *
36 * ABNF grammar as per RFC6020
37 * module-header-stmts = ;; these stmts can appear in any order
38 * [yang-version-stmt stmtsep]
39 * namespace-stmt stmtsep
40 * prefix-stmt stmtsep
41 *
42 * namespace-stmt = namespace-keyword sep uri-str optsep stmtend
43 *
44 * ANTLR grammar rule
45 * module_header_statement : yang_version_stmt? namespace_stmt prefix_stmt
46 * | yang_version_stmt? prefix_stmt namespace_stmt
47 * | namespace_stmt yang_version_stmt? prefix_stmt
48 * | namespace_stmt prefix_stmt yang_version_stmt?
49 * | prefix_stmt namespace_stmt yang_version_stmt?
50 * | prefix_stmt yang_version_stmt? namespace_stmt
51 * ;
52 * namespace_stmt : NAMESPACE_KEYWORD string STMTEND;
53 */
54
55/**
56 * Implements listener based call back function corresponding to the "namespace"
57 * rule defined in ANTLR grammar file for corresponding ABNF rule in RFC 6020.
58 */
59public final class NamespaceListener {
60
61 /**
62 * Creates a new namespace listener.
63 */
64 private NamespaceListener() {
65 }
66
67 /**
68 * It is called when parser receives an input matching the grammar
69 * rule (namespace), perform validations and update the data model
70 * tree.
71 *
72 * @param listener Listener's object.
73 * @param ctx context object of the grammar rule.
74 */
75 public static void processNamespaceEntry(TreeWalkListener listener,
76 GeneratedYangParser.NamespaceStatementContext ctx) {
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +053077
78 // Check for stack to be non empty.
79 ListenerValidation.checkStackIsNotEmpty(listener, ListenerErrorType.MISSING_HOLDER,
80 ParsableDataType.NAMESPACE_DATA,
81 String.valueOf(ctx.string().getText()), ListenerErrorLocation.ENTRY);
82
83 if (!validateUriValue(String.valueOf(ctx.string().getText()))) {
84 ParserException parserException = new ParserException("Invalid namespace URI");
85 parserException.setLine(ctx.string().STRING(0).getSymbol().getLine());
86 parserException.setCharPosition(ctx.string().STRING(0).getSymbol().getCharPositionInLine());
87 throw parserException;
88 }
89
90 // Obtain the node of the stack.
91 Parsable tmpNode = listener.getParsedDataStack().peek();
92 switch (tmpNode.getParsableDataType()) {
93 case MODULE_DATA: {
94 YangModule module = (YangModule) tmpNode;
95 YangNameSpace uri = new YangNameSpace();
96 uri.setUri(String.valueOf(ctx.string().getText()));
97 module.setNameSpace(uri);
98 break;
99 }
100 default:
101 throw new ParserException(
102 ListenerErrorMessageConstruction
103 .constructListenerErrorMessage(ListenerErrorType.INVALID_HOLDER,
104 ParsableDataType.NAMESPACE_DATA,
105 String.valueOf(ctx.string().getText()),
106 ListenerErrorLocation.ENTRY));
107 }
Gaurav Agrawal22db16d2016-02-12 16:50:55 +0530108 }
Gaurav Agrawal2737d2a2016-02-13 14:23:40 +0530109
110 /**
111 * Validate input URI.
112 *
113 * @param uri input namespace URI
114 * @return validation result
115 */
116 private static boolean validateUriValue(String uri) {
117 uri = uri.replace("\"", "");
118 final URI tmpUri;
119 try {
120 tmpUri = URI.create(uri);
121 } catch (Exception e1) {
122 return false;
123 }
124 return true;
125 }
126}