source: VirtualCollectionRegistry/trunk/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/service/impl/ReferenceValidator.java @ 5578

Last change on this file since 5578 was 5578, checked in by Twan Goosen, 10 years ago

Also accepting PID resolver URL's for publication without warning. Added help text explaining PID's.

File size: 3.0 KB
Line 
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 */
17package eu.clarin.cmdi.virtualcollectionregistry.service.impl;
18
19import java.util.regex.Pattern;
20import org.apache.wicket.validation.IValidatable;
21import org.apache.wicket.validation.IValidator;
22import org.apache.wicket.validation.Validatable;
23import org.apache.wicket.validation.ValidationError;
24import org.apache.wicket.validation.validator.AbstractValidator;
25import 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 */
34public 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}
Note: See TracBrowser for help on using the repository browser.