ad99fb95df85b0a5110366776e40a00b4d8bb98d
[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/eplv10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.impl;
9
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Set;
15 import org.antlr.v4.runtime.tree.ParseTree;
16 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Module_stmtContext;
17 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.Submodule_stmtContext;
18 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
19
20 /**
21  * Validation utilities
22  */
23 final class ValidationUtil {
24
25     /**
26      * It isn't desirable to create instance of this class
27      */
28     private ValidationUtil() {
29     }
30
31     static void ex(String message) {
32         throw new YangValidationException(message);
33     }
34
35     static Set<String> getDuplicates(Collection<String> keyList) {
36         Set<String> all = new HashSet<>();
37         Set<String> duplicates = new HashSet<>();
38
39         for (String key : keyList) {
40             if (!all.add(key)) {
41                 duplicates.add(key);
42             }
43         }
44         return duplicates;
45     }
46
47     static List<String> listKeysFromId(String keys) {
48         return Arrays.asList(keys.split(" "));
49     }
50
51     static String getRootParentName(ParseTree ctx) {
52         ParseTree root = getRootParent(ctx);
53         return ValidationUtil.getName(root);
54     }
55
56     private static ParseTree getRootParent(ParseTree ctx) {
57         ParseTree root = ctx;
58         while (root.getParent() != null) {
59             if (root.getClass().equals(Module_stmtContext.class) || root.getClass().equals(Submodule_stmtContext.class)) {
60                 break;
61             }
62             root = root.getParent();
63         }
64         return root;
65     }
66
67     static String getName(ParseTree child) {
68         return ParserListenerUtils.stringFromNode(child);
69     }
70
71     static String f(String base, Object... args) {
72         return String.format(base, args);
73     }
74
75     /**
76      * Get simple name from statement class e.g. Module from Module_stmt_context
77      */
78     static String getSimpleStatementName(Class<? extends ParseTree> typeOfStatement) {
79
80         String className = typeOfStatement.getSimpleName();
81         int lastIndexOf = className.indexOf('$');
82         className = lastIndexOf == -1 ? className : className.substring(lastIndexOf + 1);
83         int indexOfStmt = className.indexOf("_stmt");
84         int index = indexOfStmt == -1 ? className.indexOf("_arg") : indexOfStmt;
85         return className.substring(0, index).replace('_', '-');
86     }
87
88     static int countPresentChildrenOfType(ParseTree parent, Set<Class<? extends ParseTree>> expectedChildTypes) {
89         int foundChildrenOfType = 0;
90
91         for (Class<? extends ParseTree> type : expectedChildTypes) {
92             foundChildrenOfType += countPresentChildrenOfType(parent, type);
93         }
94         return foundChildrenOfType;
95     }
96
97     static int countPresentChildrenOfType(ParseTree parent, Class<? extends ParseTree> expectedChildType) {
98         int foundChildrenOfType = 0;
99
100         for (int i = 0; i < parent.getChildCount(); i++) {
101             ParseTree child = parent.getChild(i);
102             if (expectedChildType.isInstance(child)) {
103                 foundChildrenOfType++;
104             }
105         }
106         return foundChildrenOfType;
107     }
108
109 }