[hg]: improve reference validator

* make common validator independent from Repository instance;
* make name restrictions more precise;
This commit is contained in:
Nadya Zabrodina
2015-11-10 12:55:19 +03:00
parent eefdbefdd5
commit 6239369f6a
3 changed files with 47 additions and 27 deletions
@@ -20,22 +20,12 @@ import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.repo.HgRepository;
import java.util.Collection;
import java.util.regex.Pattern;
public class HgBranchReferenceValidator extends HgReferenceValidator {
private static final Pattern DIGITS_ILLEGAL = Pattern.compile("[0-9]*"); // reference names couldn't contain only digits
private final HgRepository myRepository;
public HgBranchReferenceValidator(@NotNull HgRepository repository) {
super(repository);
}
@Override
public boolean checkInput(@Nullable String name) {
if (name != null && DIGITS_ILLEGAL.matcher(name).matches()) {
myErrorText = "Invalid name for branch/tag";
return false;
}
return super.checkInput(name);
myRepository = repository;
}
@Override
@@ -15,13 +15,21 @@
*/
package org.zmlx.hg4idea.util;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.repo.HgRepository;
public class HgPatchReferenceValidator extends HgReferenceValidator {
private final HgRepository myRepository;
public HgPatchReferenceValidator(@NotNull HgRepository repository) {
super(repository);
myRepository = repository;
}
@Override
public boolean checkInput(@Nullable String name) {
return !StringUtil.isEmptyOrSpaces(name) && !containsIllegalSymbols(name) && !hasConflictsWithAnotherNames(name);
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,31 +17,51 @@ package org.zmlx.hg4idea.util;
import com.intellij.openapi.ui.InputValidatorEx;
import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.zmlx.hg4idea.repo.HgRepository;
import java.util.regex.Pattern;
import static org.zmlx.hg4idea.util.HgUtil.TIP_REFERENCE;
public abstract class HgReferenceValidator implements InputValidatorEx {
protected final HgRepository myRepository;
public class HgReferenceValidator implements InputValidatorEx {
protected String myErrorText;
private static final HgReferenceValidator INSTANCE = new HgReferenceValidator();
protected HgReferenceValidator(@NotNull HgRepository repository) {
myRepository = repository;
private static final Pattern DIGITS_ILLEGAL = Pattern.compile("[0-9]*"); // reference names couldn't contain only digits
private static final Pattern ILLEGAL = Pattern.compile(
"[:]" // contains ':' character
);
public static HgReferenceValidator getInstance() {
return INSTANCE;
}
protected HgReferenceValidator() {
}
@Override
public boolean checkInput(@Nullable String name) {
if (StringUtil.isEmptyOrSpaces(name)) {
public boolean checkInput(String inputString) {
if (StringUtil.isEmptyOrSpaces(inputString)) {
return false;
}
if (name.contains(":")) {
if (containsIllegalSymbols(inputString)) return false;
return !isReservedWord(inputString) && !onlyDigits(inputString) && !hasConflictsWithAnotherNames(inputString);
}
protected boolean containsIllegalSymbols(@Nullable String inputString) {
if (inputString != null && ILLEGAL.matcher(inputString).find()) {
myErrorText = "Name could not contain colons";
return false;
return true;
}
return !isReservedWord(name) && !hasConflictsWithAnotherNames(name);
return false;
}
private boolean onlyDigits(@Nullable String inputString) {
if (inputString != null && DIGITS_ILLEGAL.matcher(inputString).matches()) {
myErrorText = "Invalid name for hg reference";
return true;
}
return false;
}
@Override
@@ -54,7 +74,9 @@ public abstract class HgReferenceValidator implements InputValidatorEx {
return myErrorText != null;
}
protected abstract boolean hasConflictsWithAnotherNames(@Nullable String name);
protected boolean hasConflictsWithAnotherNames(@Nullable String name) {
return false;
}
@Nullable
@Override