BUG-865: deprecate pre-Beryllium parser elements
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / ValidationUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.yang.parser.impl;
10
11 import com.google.common.base.Splitter;
12
13 import java.util.HashSet;
14 import java.util.Set;
15
16 import org.antlr.v4.runtime.tree.ParseTree;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_stmtContext;
18 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Submodule_stmtContext;
19 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
20
21 /**
22  * Validation utilities
23  */
24 final class ValidationUtil {
25     private static final Splitter SPACE_SPLITTER = Splitter.on(' ');
26
27     /**
28      * It isn't desirable to create instance of this class
29      */
30     private ValidationUtil() {
31     }
32
33     static void ex(final String message) {
34         throw new YangValidationException(message);
35     }
36
37     static Set<String> getDuplicates(final Iterable<String> keyList) {
38         Set<String> all = new HashSet<>();
39         Set<String> duplicates = new HashSet<>();
40
41         for (String key : keyList) {
42             if (!all.add(key)) {
43                 duplicates.add(key);
44             }
45         }
46         return duplicates;
47     }
48
49     static Iterable<String> listKeysFromId(final String keys) {
50         return SPACE_SPLITTER.split(keys);
51     }
52
53     static String getRootParentName(final ParseTree ctx) {
54         ParseTree root = getRootParent(ctx);
55         return ValidationUtil.getName(root);
56     }
57
58     private static ParseTree getRootParent(final ParseTree ctx) {
59         ParseTree root = ctx;
60         while (root.getParent() != null) {
61             if (root.getClass().equals(Module_stmtContext.class) || root.getClass().equals(Submodule_stmtContext.class)) {
62                 break;
63             }
64             root = root.getParent();
65         }
66         return root;
67     }
68
69     static String getName(final ParseTree child) {
70         return ParserListenerUtils.stringFromNode(child);
71     }
72
73     static String f(final String base, final Object... args) {
74         return String.format(base, args);
75     }
76
77     /**
78      * Get simple name from statement class e.g. Module from Module_stmt_context
79      */
80     static String getSimpleStatementName(final Class<? extends ParseTree> typeOfStatement) {
81
82         String className = typeOfStatement.getSimpleName();
83         int lastIndexOf = className.indexOf('$');
84         className = lastIndexOf == -1 ? className : className.substring(lastIndexOf + 1);
85         int indexOfStmt = className.indexOf("_stmt");
86         int index = indexOfStmt == -1 ? className.indexOf("_arg") : indexOfStmt;
87         return className.substring(0, index).replace('_', '-');
88     }
89
90     static int countPresentChildrenOfType(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildTypes) {
91         int foundChildrenOfType = 0;
92
93         for (Class<? extends ParseTree> type : expectedChildTypes) {
94             foundChildrenOfType += countPresentChildrenOfType(parent, type);
95         }
96         return foundChildrenOfType;
97     }
98
99     static int countPresentChildrenOfType(final ParseTree parent, final Class<? extends ParseTree> expectedChildType) {
100         int foundChildrenOfType = 0;
101
102         for (int i = 0; i < parent.getChildCount(); i++) {
103             ParseTree child = parent.getChild(i);
104             if (expectedChildType.isInstance(child)) {
105                 foundChildrenOfType++;
106             }
107         }
108         return foundChildrenOfType;
109     }
110
111 }