1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.virtualcollectionregistry.service.impl; |
---|
18 | |
---|
19 | import java.util.regex.Pattern; |
---|
20 | import org.apache.wicket.validation.IValidatable; |
---|
21 | import org.apache.wicket.validation.IValidator; |
---|
22 | import org.apache.wicket.validation.Validatable; |
---|
23 | import org.apache.wicket.validation.ValidationError; |
---|
24 | import org.apache.wicket.validation.validator.AbstractValidator; |
---|
25 | import org.apache.wicket.validation.validator.UrlValidator; |
---|
26 | |
---|
27 | /** |
---|
28 | * String validator that checks whether the value is a valid handle with 'hdl' |
---|
29 | * scheme; if not, it passed the value on to an instance of {@link UrlValidator} |
---|
30 | * (configured not to accept fragments, see {@link UrlValidator#NO_FRAGMENTS}) |
---|
31 | * |
---|
32 | * @author twagoo |
---|
33 | */ |
---|
34 | public class ReferenceValidator extends AbstractValidator<String> { |
---|
35 | |
---|
36 | private static final String HANDLE_SPECIFIC_PART_PATTERN = "[0-9\\.]+\\/.+$"; |
---|
37 | private static final Pattern HANDLE_PATTERN = Pattern.compile("^(hdl|doi):" + HANDLE_SPECIFIC_PART_PATTERN); |
---|
38 | private static final Pattern HANDLE_RESOLVER_PATTERN = Pattern.compile("^http://(hdl\\.handle\\.net|dx\\.doi\\.org|)/" + HANDLE_SPECIFIC_PART_PATTERN); |
---|
39 | private final IValidator<String> urlValidator = new UrlValidator(UrlValidator.NO_FRAGMENTS); |
---|
40 | |
---|
41 | @Override |
---|
42 | protected void onValidate(IValidatable<String> validatable) { |
---|
43 | // first check if it is a valid handle |
---|
44 | if (!HANDLE_PATTERN.matcher(validatable.getValue()).matches()) { |
---|
45 | // check if it is a valid URL |
---|
46 | urlValidator.validate(validatable); |
---|
47 | if (!validatable.isValid()) { |
---|
48 | validatable.error(new ValidationError().setMessage(String.format("'%s' is not a valid handle", validatable.getValue()))); |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | public boolean validate(String value) { |
---|
54 | final Validatable<String> validatable = new Validatable<>(value); |
---|
55 | validate(validatable); |
---|
56 | return validatable.isValid(); |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * |
---|
61 | * @param uri |
---|
62 | * @return true IFF the expression is a URI consisting of a valid handle |
---|
63 | * pattern preceded by a handle scheme expression (hdl: or doi:) OR one of |
---|
64 | * the accepted handle resolver base URL's (http://hdl.handle.net or |
---|
65 | * http://dx.doi.org) |
---|
66 | */ |
---|
67 | public static boolean isPid(CharSequence uri) { |
---|
68 | return HANDLE_PATTERN.matcher(uri).matches() |
---|
69 | || HANDLE_RESOLVER_PATTERN.matcher(uri).matches(); |
---|
70 | } |
---|
71 | |
---|
72 | } |
---|