001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.util.validators.identifierschemes;
015
016import java.util.Objects;
017import java.util.regex.Pattern;
018
019public class ResearcherIdValidator implements IdentifierSchemeValidator {
020
021  public static final Pattern FORMAT_PATTERN =
022      Pattern.compile(
023          "^(?<prefix>http(?:s)?:\\/\\/(?:www.)?.+)?([A-Z]{1,3}-\\d{4}-(19|20)\\d\\d)$");
024
025  private static final Pattern PUBLONS_PATTERN =
026      Pattern.compile(
027          "^(?<prefix>http(?:s)?:\\/\\/(?:www.)?publons.com\\/researcher\\/)?(([A-Z]{1,3}-\\d{4}-(19|20)\\d\\d)|([0-9]+\\/[a-zA-Z\\-\\_]+\\/?))$");
028
029  @Override
030  public boolean isValid(String value) {
031    if (value == null || value.isEmpty()) {
032      return false;
033    }
034    return FORMAT_PATTERN.matcher(value).matches() || PUBLONS_PATTERN.matcher(value).matches();
035  }
036
037  @Override
038  public String normalize(String value) {
039    Objects.requireNonNull(value, "Identifier value can't be null");
040    String trimmedValue = value.trim();
041    if (FORMAT_PATTERN.matcher(trimmedValue).matches()
042        || PUBLONS_PATTERN.matcher(trimmedValue).matches()) {
043      return trimmedValue;
044    }
045    throw new IllegalArgumentException(value + " it not a valid ResearcherID");
046  }
047}