Commit f5af16a3 authored by Matija Obreza's avatar Matija Obreza

DOI Utils

parent 92d07c8c
/*
* 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.genesys2.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;
};
}
}
/*
* 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.genesys2.util;
public class InvalidDOIException extends Exception {
private static final long serialVersionUID = 5335103695130893198L;
private String candidateDoi;
public InvalidDOIException(String doi) {
super("The DOI is not valid: " + doi);
this.candidateDoi = doi;
}
public String getCandidateDoi() {
return candidateDoi;
}
}
/*
* 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.genesys2.util;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.genesys2.util.DOIUtils.DOI;
import org.junit.Test;
public class DOITest {
@Test
public void properDoiTest() throws InvalidDOIException {
DOIUtils.validateDoi("10.01032/rmh0000008");
DOIUtils.validateDoi("10.04105/XXJPH.2009.160184");
DOIUtils.validateDoi("10.0305/XXJPH/2009-160184-2");
DOIUtils.validateDoi("10.1/A");
DOIUtils.validateDoi("10.18730/AB1R6");
DOIUtils.validateDoi("10.0155/AB1R6");
}
@Test(expected = InvalidDOIException.class)
public void withWhitespace() throws InvalidDOIException {
DOIUtils.validateDoi(" 10.0155/AB1R6");
}
@Test(expected = InvalidDOIException.class)
public void withWhitespace2() throws InvalidDOIException {
DOIUtils.validateDoi("10.0155/AB1R6 ");
}
@Test(expected = InvalidDOIException.class)
public void no10Dot() throws InvalidDOIException {
DOIUtils.validateDoi("2.18730/A");
}
@Test(expected = InvalidDOIException.class)
public void noSlashTest() throws InvalidDOIException {
DOIUtils.validateDoi("10.18730");
}
@Test(expected = InvalidDOIException.class)
public void noIdTest() throws InvalidDOIException {
DOIUtils.validateDoi("10.18730/");
}
@Test
public void parseDoi() throws InvalidDOIException {
DOI doi = DOIUtils.parseDOI("10.18730/AB1R6");
assertThat(doi.getPrefix(), is("10.18730"));
assertThat(doi.getId(), is("AB1R6"));
assertThat(doi.toString(), is("doi:10.18730/AB1R6"));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment