source: VirtualCollectionRegistry/tags/VirtualCollectionRegistry-0.4.0-alpha2/VirtualCollectionRegistry/src/main/java/eu/clarin/cmdi/virtualcollectionregistry/service/impl/ReferenceValidator.java @ 5557

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

tag for VCR alpha 2

File size: 2.4 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 Pattern HANDLE_PATTERN = Pattern.compile("^(hdl|doi):[0-9\\.]+\\/.+$");
37    private final IValidator<String> urlValidator = new UrlValidator(UrlValidator.NO_FRAGMENTS);
38
39    @Override
40    protected void onValidate(IValidatable<String> validatable) {
41        // first check if it is a valid handle
42        if (!HANDLE_PATTERN.matcher(validatable.getValue()).matches()) {
43            // check if it is a valid URL
44            urlValidator.validate(validatable);
45            if (!validatable.isValid()) {
46                validatable.error(new ValidationError().setMessage(String.format("'%s' is not a valid handle", validatable.getValue())));
47            }
48        }
49    }
50
51    public boolean validate(String value) {
52        final Validatable<String> validatable = new Validatable<>(value);
53        validate(validatable);
54        return validatable.isValid();
55    }
56   
57    public static boolean isPid(CharSequence uri) {
58        return HANDLE_PATTERN.matcher(uri).matches();
59    }
60
61}
Note: See TracBrowser for help on using the repository browser.