|
| 1 | +/* |
| 2 | + * Copyright 2014 Deutsche Nationalbibliothek |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.culturegraph.mf.morph.functions; |
| 17 | + |
| 18 | +import java.text.ParseException; |
| 19 | +import java.text.SimpleDateFormat; |
| 20 | +import java.util.Collections; |
| 21 | +import java.util.Date; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Locale; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import org.culturegraph.mf.exceptions.MorphDefException; |
| 27 | + |
| 28 | +/** |
| 29 | + * Format date/time strings in Metamorph. By default the input format is |
| 30 | + * dd.MM.yyyy and the output format is dd. MMMM yyyy. |
| 31 | + * |
| 32 | + * Examples of using this function in Metamorph: |
| 33 | + * <ul> |
| 34 | + * <li>Default date format: <code><dateformat /></code></li> |
| 35 | + * <li>Read ISO-dates and generate German style dates: |
| 36 | + * <code><dateformat inputformat="yyyy-MM-dd" outputformat="dd.MM.yyyy" /></code></li> |
| 37 | + * </ul> |
| 38 | + * |
| 39 | + * @author Michael Büchner |
| 40 | + */ |
| 41 | +public class DateFormat extends AbstractSimpleStatelessFunction { |
| 42 | + |
| 43 | + public static final String DEFAULT_INPUT_FORMAT = "dd.MM.yyyy"; |
| 44 | + public static final DateFormats DEFAULT_OUTPUT_FORMAT = DateFormats.LONG; |
| 45 | + |
| 46 | + private static final Set<String> SUPPORTED_LANGUAGES; |
| 47 | + |
| 48 | + private String inputFormat = DEFAULT_INPUT_FORMAT; |
| 49 | + private DateFormats outputFormat = DEFAULT_OUTPUT_FORMAT; |
| 50 | + private Locale outputLocale = Locale.getDefault(); |
| 51 | + |
| 52 | + /** |
| 53 | + * Supported date formats. Maps to the date formats in |
| 54 | + * {@link java.text.DateFormat}. |
| 55 | + * |
| 56 | + * @author Christoph Böhme |
| 57 | + */ |
| 58 | + public enum DateFormats { |
| 59 | + FULL(java.text.DateFormat.FULL), |
| 60 | + LONG(java.text.DateFormat.LONG), |
| 61 | + MEDIUM(java.text.DateFormat.MEDIUM), |
| 62 | + SHORT(java.text.DateFormat.SHORT); |
| 63 | + |
| 64 | + private final int formatId; |
| 65 | + |
| 66 | + DateFormats(final int formatId) { |
| 67 | + this.formatId = formatId; |
| 68 | + } |
| 69 | + |
| 70 | + int getFormatId() { |
| 71 | + return formatId; |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + static { |
| 77 | + final Set<String> set = new HashSet<String>(); |
| 78 | + Collections.addAll(set, Locale.getISOLanguages()); |
| 79 | + SUPPORTED_LANGUAGES = Collections.unmodifiableSet(set); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public final String process(final String value) { |
| 84 | + String result = value; |
| 85 | + try { |
| 86 | + final java.text.DateFormat inputDateFormat = new SimpleDateFormat(inputFormat); |
| 87 | + final Date date = inputDateFormat.parse(value); |
| 88 | + result = java.text.DateFormat.getDateInstance(outputFormat.getFormatId(), outputLocale).format(date); |
| 89 | + } catch (final IllegalArgumentException e) { |
| 90 | + throw new MorphDefException("The date/time format is not supported.", e); |
| 91 | + } catch (final ParseException e) { |
| 92 | + // Nothing to do |
| 93 | + } |
| 94 | + return result; |
| 95 | + } |
| 96 | + |
| 97 | + public final void setInputFormat(final String inputFormat) { |
| 98 | + this.inputFormat = inputFormat; |
| 99 | + } |
| 100 | + |
| 101 | + public final void setOutputFormat(final DateFormats outputFormat) { |
| 102 | + this.outputFormat = outputFormat; |
| 103 | + } |
| 104 | + |
| 105 | + public final void setLanguage(final String language) { |
| 106 | + if (!SUPPORTED_LANGUAGES.contains(language)) { |
| 107 | + throw new MorphDefException("Language '" + language + "' not supported."); |
| 108 | + } |
| 109 | + this.outputLocale = new Locale(language); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments