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 HuhValidator implements IdentifierSchemeValidator {
020
021  private static final Pattern HUH_KIKI_PATTERN =
022      Pattern.compile(
023          "^(?<scheme>http(?:s)?:\\/\\/kiki.huh.harvard.edu\\/)(databases\\/rdfgen.php\\?uuid=)([0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12})$");
024
025  private static final Pattern HUH_KIKI_SEARCH_PATTERN =
026      Pattern.compile(
027          "^(?<scheme>http(?:s)?:\\/\\/kiki.huh.harvard.edu\\/)(databases\\/botanist_search.php\\?(mode=details&)?id=)([0-9]+)$");
028
029  private static final Pattern HUH_PURL_PATTERN =
030      Pattern.compile(
031          "^(?<scheme>http(?:s)?:\\/\\/purl.oclc.org\\/)(net\\/edu.harvard.huh\\/guid\\/uuid\\/)([0-9a-fA-F]{8}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12})$");
032
033  @Override
034  public boolean isValid(String value) {
035    if (value == null || value.isEmpty()) {
036      return false;
037    }
038    return HUH_KIKI_PATTERN.matcher(value).matches()
039        || HUH_KIKI_SEARCH_PATTERN.matcher(value).matches()
040        || HUH_PURL_PATTERN.matcher(value).matches();
041  }
042
043  @Override
044  public String normalize(String value) {
045    Objects.requireNonNull(value, "Identifier value can't be null");
046    String trimmedValue = value.trim();
047    if (HUH_KIKI_PATTERN.matcher(trimmedValue).matches()
048        || HUH_KIKI_SEARCH_PATTERN.matcher(trimmedValue).matches()
049        || HUH_PURL_PATTERN.matcher(trimmedValue).matches()) {
050      return trimmedValue;
051    }
052    throw new IllegalArgumentException(value + " it not a valid HUH");
053  }
054}