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.gui; |
---|
18 | |
---|
19 | import java.util.regex.Matcher; |
---|
20 | import java.util.regex.Pattern; |
---|
21 | import org.apache.wicket.model.IModel; |
---|
22 | |
---|
23 | /** |
---|
24 | * Model that takes a link from an inner model and in case of a handle (any link |
---|
25 | * starting with "hdl:"), will replace the scheme with the handle proxy base URL |
---|
26 | * |
---|
27 | * @author twagoo |
---|
28 | */ |
---|
29 | public class HandleLinkModel implements IModel<String> { |
---|
30 | |
---|
31 | private final IModel<String> linkModel; |
---|
32 | public static final Pattern HANDLE_PATTERN = Pattern.compile("^(hdl|doi):(.*)$", Pattern.CASE_INSENSITIVE); |
---|
33 | public static final String HANDLE_PROXY = "http://hdl.handle.net/"; |
---|
34 | public static final String URN_NBN_PREFIX = "urn:nbn"; |
---|
35 | public static final String URN_NBN_RESOLVER_URL = "http://www.nbn-resolving.org/redirect/"; |
---|
36 | private static final int HANDLE_ID_GROUP = 2; |
---|
37 | |
---|
38 | public HandleLinkModel(IModel<String> linkModel) { |
---|
39 | this.linkModel = linkModel; |
---|
40 | } |
---|
41 | |
---|
42 | @Override |
---|
43 | public String getObject() { |
---|
44 | final String link = linkModel.getObject(); |
---|
45 | if (link != null) { |
---|
46 | final Matcher handleMatcher = HANDLE_PATTERN.matcher(link); |
---|
47 | if (handleMatcher.matches()) { |
---|
48 | return HANDLE_PROXY + handleMatcher.group(HANDLE_ID_GROUP); |
---|
49 | } |
---|
50 | if (link.toLowerCase().startsWith(URN_NBN_PREFIX)) { |
---|
51 | return URN_NBN_RESOLVER_URL + link; |
---|
52 | } |
---|
53 | } |
---|
54 | return link; |
---|
55 | } |
---|
56 | |
---|
57 | @Override |
---|
58 | public void setObject(String object) { |
---|
59 | linkModel.setObject(object); |
---|
60 | } |
---|
61 | |
---|
62 | @Override |
---|
63 | public void detach() { |
---|
64 | linkModel.detach(); |
---|
65 | } |
---|
66 | } |
---|