source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/provider/FieldValueConverterProviderImpl.java @ 6002

Last change on this file since 6002 was 6002, checked in by Twan Goosen, 9 years ago

Created a value converter for language codes. It is used by a new 'FieldValueLabel?', which is used in various panels. This way the language name is shown but the selection happens on basis of code.
TODO: generate link to language info page
Refs #553

File size: 2.9 KB
Line 
1/*
2 * Copyright (C) 2015 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.vlo.wicket.provider;
18
19import eu.clarin.cmdi.vlo.FacetConstants;
20import eu.clarin.cmdi.vlo.LanguageCodeUtils;
21import java.util.Locale;
22import java.util.regex.Matcher;
23import java.util.regex.Pattern;
24import org.apache.wicket.util.convert.ConversionException;
25import org.apache.wicket.util.convert.IConverter;
26
27/**
28 *
29 * @author Twan Goosen <twan.goosen@mpi.nl>
30 */
31public class FieldValueConverterProviderImpl implements FieldValueConverterProvider {
32
33    private final LanguageCodeUtils languageCodeUtils;
34    private Pattern LANGUAGE_CODE_PATTERN = Pattern.compile("(name|code):(.*)");
35
36    public FieldValueConverterProviderImpl(LanguageCodeUtils languageCodeUtils) {
37        this.languageCodeUtils = languageCodeUtils;
38    }
39
40    @Override
41    public IConverter<String> getConverter(String fieldName) {
42        switch (fieldName) {
43            case FacetConstants.FIELD_LANGUAGE_CODE:
44                return languageCodeConverter;
45            default:
46                return null;
47        }
48
49    }
50
51    private abstract class FieldValueConverter implements IConverter<String> {
52
53        @Override
54        public String convertToObject(String value, Locale locale) throws ConversionException {
55            throw new UnsupportedOperationException("Not supported");
56        }
57
58    }
59
60    private final FieldValueConverter languageCodeConverter = new FieldValueConverter() {
61
62        @Override
63        public String convertToString(String fieldValue, Locale locale) throws ConversionException {
64            final Matcher matcher = LANGUAGE_CODE_PATTERN.matcher(fieldValue);
65            if (matcher.matches() && matcher.groupCount() == 2) {
66                final String type = matcher.group(1);
67                final String value = matcher.group(2);
68                switch (type) {
69                    case "code":
70                        // value is a language code, look up
71                        return languageCodeUtils.getLanguageNameForLanguageCode(value.toUpperCase());
72                    case "name":
73                        // value is the name to be shown
74                        return value;
75                }
76            }
77           
78            // does not match expected pattern, return original value
79            return fieldValue;
80        }
81
82    };
83
84}
Note: See TracBrowser for help on using the repository browser.