2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.validator;
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.List;
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;
23 * Validation utilities
25 final class ValidationUtil {
28 * It isn't desirable to create instance of this class
30 private ValidationUtil() {
33 static void ex(String message) {
34 throw new YangValidationException(message);
37 static Set<String> getDuplicates(Collection<String> keyList) {
38 Set<String> all = new HashSet<String>();
39 Set<String> duplicates = new HashSet<String>();
41 for (String key : keyList) {
49 static List<String> listKeysFromId(String keys) {
50 return Arrays.asList(keys.split(" "));
53 static String getRootParentName(ParseTree ctx) {
54 ParseTree root = getRootParent(ctx);
55 return ValidationUtil.getName(root);
58 private static ParseTree getRootParent(ParseTree ctx) {
60 while (root.getParent() != null) {
61 if (root.getClass().equals(Module_stmtContext.class) || root.getClass().equals(Submodule_stmtContext.class)) {
64 root = root.getParent();
69 static String getName(ParseTree child) {
70 return ParserListenerUtils.stringFromNode(child);
73 static String f(String base, Object... args) {
74 return String.format(base, args);
78 * Get simple name from statement class e.g. Module from Module_stmt_context
80 static String getSimpleStatementName(Class<? extends ParseTree> typeOfStatement) {
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('_', '-');
90 static int countPresentChildrenOfType(ParseTree parent, Set<Class<? extends ParseTree>> expectedChildTypes) {
91 int foundChildrenOfType = 0;
93 for (Class<? extends ParseTree> type : expectedChildTypes) {
94 foundChildrenOfType += countPresentChildrenOfType(parent, type);
96 return foundChildrenOfType;
99 static int countPresentChildrenOfType(ParseTree parent, Class<? extends ParseTree> expectedChildType) {
100 int foundChildrenOfType = 0;
102 for (int i = 0; i < parent.getChildCount(); i++) {
103 ParseTree child = parent.getChild(i);
104 if (expectedChildType.isInstance(child)) {
105 foundChildrenOfType++;
108 return foundChildrenOfType;