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;
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;
22 * Validation utilities
24 final class ValidationUtil {
27 * It isn't desirable to create instance of this class
29 private ValidationUtil() {
32 static void ex(String message) {
33 throw new YangValidationException(message);
36 static Set<String> getDuplicates(Collection<String> keyList) {
37 Set<String> all = new HashSet<String>();
38 Set<String> duplicates = new HashSet<String>();
40 for (String key : keyList) {
48 static List<String> listKeysFromId(String keys) {
49 return Arrays.asList(keys.split(" "));
52 static String getRootParentName(ParseTree ctx) {
53 ParseTree root = getRootParent(ctx);
54 return ValidationUtil.getName(root);
57 private static ParseTree getRootParent(ParseTree ctx) {
59 while (root.getParent() != null) {
60 if (root.getClass().equals(Module_stmtContext.class) || root.getClass().equals(Submodule_stmtContext.class)) {
63 root = root.getParent();
68 static String getName(ParseTree child) {
69 return ParserListenerUtils.stringFromNode(child);
72 static String f(String base, Object... args) {
73 return String.format(base, args);
77 * Get simple name from statement class e.g. Module from Module_stmt_context
79 static String getSimpleStatementName(Class<? extends ParseTree> typeOfStatement) {
81 String className = typeOfStatement.getSimpleName();
82 int lastIndexOf = className.indexOf('$');
83 className = lastIndexOf == -1 ? className : className.substring(lastIndexOf + 1);
84 int indexOfStmt = className.indexOf("_stmt");
85 int index = indexOfStmt == -1 ? className.indexOf("_arg") : indexOfStmt;
86 return className.substring(0, index).replace('_', '-');
89 static int countPresentChildrenOfType(ParseTree parent, Set<Class<? extends ParseTree>> expectedChildTypes) {
90 int foundChildrenOfType = 0;
92 for (Class<? extends ParseTree> type : expectedChildTypes) {
93 foundChildrenOfType += countPresentChildrenOfType(parent, type);
95 return foundChildrenOfType;
98 static int countPresentChildrenOfType(ParseTree parent, Class<? extends ParseTree> expectedChildType) {
99 int foundChildrenOfType = 0;
101 for (int i = 0; i < parent.getChildCount(); i++) {
102 ParseTree child = parent.getChild(i);
103 if (expectedChildType.isInstance(child)) {
104 foundChildrenOfType++;
107 return foundChildrenOfType;