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.parser.impl;
10 import java.util.Arrays;
11 import java.util.Collection;
12 import java.util.HashSet;
13 import java.util.List;
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;
21 * Validation utilities
23 final class ValidationUtil {
26 * It isn't desirable to create instance of this class
28 private ValidationUtil() {
31 static void ex(String message) {
32 throw new YangValidationException(message);
35 static Set<String> getDuplicates(Collection<String> keyList) {
36 Set<String> all = new HashSet<>();
37 Set<String> duplicates = new HashSet<>();
39 for (String key : keyList) {
47 static List<String> listKeysFromId(String keys) {
48 return Arrays.asList(keys.split(" "));
51 static String getRootParentName(ParseTree ctx) {
52 ParseTree root = getRootParent(ctx);
53 return ValidationUtil.getName(root);
56 private static ParseTree getRootParent(ParseTree ctx) {
58 while (root.getParent() != null) {
59 if (root.getClass().equals(Module_stmtContext.class) || root.getClass().equals(Submodule_stmtContext.class)) {
62 root = root.getParent();
67 static String getName(ParseTree child) {
68 return ParserListenerUtils.stringFromNode(child);
71 static String f(String base, Object... args) {
72 return String.format(base, args);
76 * Get simple name from statement class e.g. Module from Module_stmt_context
78 static String getSimpleStatementName(Class<? extends ParseTree> typeOfStatement) {
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('_', '-');
88 static int countPresentChildrenOfType(ParseTree parent, Set<Class<? extends ParseTree>> expectedChildTypes) {
89 int foundChildrenOfType = 0;
91 for (Class<? extends ParseTree> type : expectedChildTypes) {
92 foundChildrenOfType += countPresentChildrenOfType(parent, type);
94 return foundChildrenOfType;
97 static int countPresentChildrenOfType(ParseTree parent, Class<? extends ParseTree> expectedChildType) {
98 int foundChildrenOfType = 0;
100 for (int i = 0; i < parent.getChildCount(); i++) {
101 ParseTree child = parent.getChild(i);
102 if (expectedChildType.isInstance(child)) {
103 foundChildrenOfType++;
106 return foundChildrenOfType;