DOIUtils.java
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DOIUtils {
// 10. digit+ / not-whitespace+
public static final Pattern DOI_PATTERN = Pattern.compile("^(10\\.[0-9]+)/(\\S+)$");
public static boolean isValidDoi(String doi) {
return DOI_PATTERN.matcher(doi).matches();
}
public static DOI parseDOI(String doi) throws InvalidDOIException {
Matcher matcher = DOI_PATTERN.matcher(doi);
if (matcher.find()) {
return new DOI(matcher.group(1), matcher.group(2));
} else {
throw new InvalidDOIException(doi);
}
}
/**
* Test if string is a DOI.
*
* @param doi
* @throws InvalidDOIException
*/
public static void validateDoi(String doi) throws InvalidDOIException {
if (!DOI_PATTERN.matcher(doi).matches()) {
throw new InvalidDOIException(doi);
}
}
public static class DOI {
private String prefix;
private String id;
public DOI(String prefix, String id) {
this.prefix = prefix;
this.id = id;
}
public String getPrefix() {
return prefix;
}
public String getId() {
return id;
}
public String toString() {
return "doi:" + prefix + "/" + id;
};
}
}