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/epl-v10.html
9 package org.opendaylight.yangtools.yang.parser.impl;
11 import com.google.common.base.Splitter;
13 import java.util.HashSet;
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 {
25 private static final Splitter SPACE_SPLITTER = Splitter.on(' ');
28 * It isn't desirable to create instance of this class
30 private ValidationUtil() {
33 static void ex(final String message) {
34 throw new YangValidationException(message);
37 static Set<String> getDuplicates(final Iterable<String> keyList) {
38 Set<String> all = new HashSet<>();
39 Set<String> duplicates = new HashSet<>();
41 for (String key : keyList) {
49 static Iterable<String> listKeysFromId(final String keys) {
50 return SPACE_SPLITTER.split(keys);
53 static String getRootParentName(final ParseTree ctx) {
54 ParseTree root = getRootParent(ctx);
55 return ValidationUtil.getName(root);
58 private static ParseTree getRootParent(final 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(final ParseTree child) {
70 return ParserListenerUtils.stringFromNode(child);
73 static String f(final String base, final 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(final 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(final ParseTree parent, final 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(final ParseTree parent, final 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;