Merge "BUG-582: pre-cache Pattern instances"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / validator / 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.validator;
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
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.ParserListenerUtils;
20 import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
21
22 /**
23  * Validation utilities
24  */
25 final class ValidationUtil {
26
27     /**
28      * It isn't desirable to create instance of this class
29      */
30     private ValidationUtil() {
31     }
32
33     static void ex(String message) {
34         throw new YangValidationException(message);
35     }
36
37     static Set<String> getDuplicates(Collection<String> keyList) {
38         Set<String> all = new HashSet<String>();
39         Set<String> duplicates = new HashSet<String>();
40
41         for (String key : keyList) {
42             if (!all.add(key)) {
43                 duplicates.add(key);
44             }
45         }
46         return duplicates;
47     }
48
49     static List<String> listKeysFromId(String keys) {
50         return Arrays.asList(keys.split(" "));
51     }
52
53     static String getRootParentName(ParseTree ctx) {
54         ParseTree root = getRootParent(ctx);
55         return ValidationUtil.getName(root);
56     }
57
58     private static ParseTree getRootParent(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(ParseTree child) {
70         return ParserListenerUtils.stringFromNode(child);
71     }
72
73     static String f(String base, 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(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(ParseTree parent, 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(ParseTree parent, 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 }