*/
package org.opendaylight.yangtools.yang.parser.impl;
+import static java.lang.String.format;
+
+import com.google.common.base.Splitter;
+import com.google.common.collect.Sets;
+
import java.text.ParseException;
import java.util.Date;
-import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
import org.opendaylight.yangtools.yang.parser.util.YangValidationException;
-import com.google.common.collect.Sets;
-
/**
* Reusable checks of basic constraints on yang statements
*/
final class BasicValidations {
static final String SUPPORTED_YANG_VERSION = "1";
+ private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
+ private static final Pattern identifierPattern = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
+ private static final Pattern prefixedIdentifierPattern = Pattern.compile("(.+):(.+)");
/**
* It isn't desirable to create instance of this class.
* Check if only one module or submodule is present in session(one yang
* file)
*/
- static void checkOnlyOneModulePresent(final String moduleName, final String globalId) {
+ static void checkIsModuleIdNull(final String globalId) {
if (globalId != null) {
ValidationUtil.ex(ValidationUtil.f("Multiple (sub)modules per file"));
}
}
}
- private static final Pattern identifierPattern = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_.-]*");
-
static void checkIdentifier(final ParseTree statement) {
checkIdentifierInternal(statement, ValidationUtil.getName(statement));
}
}
}
- private static Pattern prefixedIdentifierPattern = Pattern.compile("(.+):(.+)");
-
static void checkPrefixedIdentifier(final ParseTree statement) {
checkPrefixedIdentifierInternal(statement, ValidationUtil.getName(statement));
}
String id = ValidationUtil.getName(statement);
try {
- for (String oneOfId : id.split("/")) {
- if (oneOfId.isEmpty()) {
- continue;
- }
+ for (String oneOfId : SLASH_SPLITTER.split(id)) {
checkPrefixedIdentifierInternal(statement, oneOfId);
}
} catch (YangValidationException e) {
String getMessage();
}
- static void checkPresentChildOfTypeInternal(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildType,
+ private static void checkPresentChildOfTypeInternal(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildType,
final MessageProvider message, final boolean atMostOne) {
if (!checkPresentChildOfTypeSafe(parent, expectedChildType, atMostOne)) {
String str = atMostOne ? "(Expected exactly one statement) " + message.getMessage() : message.getMessage();
final boolean atMostOne) {
// Construct message in checkPresentChildOfTypeInternal only if
- // validaiton fails, not in advance
+ // validation fails, not in advance
MessageProvider message = new MessageProvider() {
@Override
ValidationUtil.getSimpleStatementName(parent.getClass()), ValidationUtil.getName(parent));
String root = ValidationUtil.getRootParentName(parent);
- message = parent.equals(ValidationUtil.getRootParentName(parent)) ? message : ValidationUtil.f(
- "(In (sub)module:%s) %s", root, message);
+ message = format("(In (sub)module:%s) %s", root, message);
return message;
}
};
@Override
public String getMessage() {
StringBuilder childTypes = new StringBuilder();
- String orStr = " OR ";
for (Class<? extends ParseTree> type : expectedChildTypes) {
childTypes.append(ValidationUtil.getSimpleStatementName(type));
- childTypes.append(orStr);
+ childTypes.append(" OR ");
}
String message = ValidationUtil.f("Missing %s statement in %s:%s", childTypes.toString(),
ValidationUtil.getSimpleStatementName(parent.getClass()), ValidationUtil.getName(parent));
String root = ValidationUtil.getRootParentName(parent);
- message = parent.equals(ValidationUtil.getRootParentName(parent)) ? message : ValidationUtil.f(
- "(In (sub)module:%s) %s", root, message);
+ message = format("(In (sub)module:%s) %s", root, message);
return message;
}
- };
+ }
static void checkPresentChildOfTypes(final ParseTree parent,
final Set<Class<? extends ParseTree>> expectedChildTypes, final boolean atMostOne) {
// Construct message in checkPresentChildOfTypeInternal only if
- // validaiton fails, not in advance
+ // validation fails, not in advance
MessageProvider message = new MessageProviderForSetOfChildTypes(expectedChildTypes, parent);
checkPresentChildOfTypeInternal(parent, expectedChildTypes, message, atMostOne);
}
- static boolean checkPresentChildOfTypeSafe(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildType,
+ private static boolean checkPresentChildOfTypeSafe(final ParseTree parent, final Set<Class<? extends ParseTree>> expectedChildType,
final boolean atMostOne) {
int foundChildrenOfType = ValidationUtil.countPresentChildrenOfType(parent, expectedChildType);
- return atMostOne ? foundChildrenOfType == 1 ? true : false : foundChildrenOfType != 0 ? true : false;
+ return atMostOne ? foundChildrenOfType == 1 : foundChildrenOfType != 0;
}
- static boolean checkPresentChildOfTypeSafe(final ParseTree parent, final Class<? extends ParseTree> expectedChildType,
+ private static boolean checkPresentChildOfTypeSafe(final ParseTree parent, final Class<? extends ParseTree> expectedChildType,
final boolean atMostOne) {
int foundChildrenOfType = ValidationUtil.countPresentChildrenOfType(parent, expectedChildType);
- return atMostOne ? foundChildrenOfType == 1 ? true : false : foundChildrenOfType != 0 ? true : false;
+ return atMostOne ? foundChildrenOfType == 1 : foundChildrenOfType != 0;
}
- static List<String> getAndCheckUniqueKeys(final ParseTree ctx) {
+ static Iterable<String> getAndCheckUniqueKeys(final ParseTree ctx) {
String key = ValidationUtil.getName(ctx);
ParseTree parent = ctx.getParent();
String rootParentName = ValidationUtil.getRootParentName(ctx);
- List<String> keyList = ValidationUtil.listKeysFromId(key);
+ Iterable<String> keyList = ValidationUtil.listKeysFromId(key);
Set<String> duplicates = ValidationUtil.getDuplicates(keyList);
if (duplicates.size() != 0) {