|
| 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.DateFormat; |
| 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 | +import java.util.TimeZone; |
| 26 | + |
| 27 | +import org.culturegraph.mf.exceptions.MorphDefException; |
| 28 | + |
| 29 | +/** |
| 30 | + * This function creates a timestamp. By default it returns a unix |
| 31 | + * timestamp. Other formats are and different |
| 32 | + * timezones can be specified. |
| 33 | + * |
| 34 | + * Examples for using the function in Metamorph: |
| 35 | + * <ul> |
| 36 | + * <li>Generate a default unix timestamp: <code><timestamp /></code></li> |
| 37 | + * <li>Generate a nicely formatted timestamp using central european time: |
| 38 | + * <code><timestamp format="yyyy-MM-dd'T'HH:mmZ" timezone="Europe/Berlin" /></code></li> |
| 39 | + * </ul> |
| 40 | + * |
| 41 | + * @author Michael Büchner |
| 42 | + */ |
| 43 | +public final class Timestamp extends AbstractSimpleStatelessFunction { |
| 44 | + |
| 45 | + public static final String FORMAT_TIMESTAMP = "timestamp"; |
| 46 | + |
| 47 | + public static final String DEFAULT_FORMAT = FORMAT_TIMESTAMP; |
| 48 | + public static final String DEFAULT_TIMEZONE = "UTC"; |
| 49 | + |
| 50 | + private static final int MS_PER_SECOND = 1000; |
| 51 | + private static final Set<String> SUPPORTED_LANGUAGES; |
| 52 | + |
| 53 | + private String format = DEFAULT_FORMAT; |
| 54 | + private String timezone = DEFAULT_TIMEZONE; |
| 55 | + private Locale locale = Locale.getDefault(); |
| 56 | + |
| 57 | + static { |
| 58 | + final Set<String> set = new HashSet<String>(); |
| 59 | + Collections.addAll(set, Locale.getISOLanguages()); |
| 60 | + SUPPORTED_LANGUAGES = Collections.unmodifiableSet(set); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String process(final String value) { |
| 65 | + if (FORMAT_TIMESTAMP.equals(format)) { |
| 66 | + return Long.toString(System.currentTimeMillis() / MS_PER_SECOND); |
| 67 | + } |
| 68 | + final DateFormat dateFormat; |
| 69 | + try { |
| 70 | + dateFormat = new SimpleDateFormat(format, locale); |
| 71 | + } catch (final IllegalArgumentException e) { |
| 72 | + throw new MorphDefException("The date/time format '" + format + "' is not supported. ", e); |
| 73 | + } |
| 74 | + dateFormat.setTimeZone(TimeZone.getTimeZone(timezone)); |
| 75 | + return dateFormat.format(new Date()); |
| 76 | + } |
| 77 | + |
| 78 | + public void setFormat(final String format) { |
| 79 | + this.format = format; |
| 80 | + } |
| 81 | + |
| 82 | + public void setTimezone(final String timezone) { |
| 83 | + this.timezone = timezone; |
| 84 | + } |
| 85 | + |
| 86 | + public void setLanguage(final String language) { |
| 87 | + if (!SUPPORTED_LANGUAGES.contains(language)) { |
| 88 | + throw new MorphDefException("Language '" + language + "' not supported."); |
| 89 | + } |
| 90 | + this.locale = new Locale(language); |
| 91 | + } |
| 92 | + |
| 93 | +} |
0 commit comments