Skip to content

Commit 82be110

Browse files
committed
Adds a Metamorph function for generating timestamps.
(cherry picked from commit 0b882fe)
1 parent 606e78d commit 82be110

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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>&lt;timestamp /&gt;</code></li>
37+
* <li>Generate a nicely formatted timestamp using central european time:
38+
* <code>&lt;timestamp format="yyyy-MM-dd'T'HH:mmZ" timezone="Europe/Berlin" /&gt;</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+
}

src/main/resources/morph-functions.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ buffer org.culturegraph.mf.morph.functions.Buffer
2121
switch-name-value org.culturegraph.mf.morph.functions.SwitchNameValue
2222
unique org.culturegraph.mf.morph.functions.Unique
2323
script org.culturegraph.mf.morph.functions.Script
24-
setreplace org.culturegraph.mf.morph.functions.SetReplace
24+
setreplace org.culturegraph.mf.morph.functions.SetReplace
25+
timestamp org.culturegraph.mf.morph.functions.Timestamp

src/main/resources/schemata/metamorph.xsd

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@
659659
<element ref="tns:equals" />
660660
<element ref="tns:not-equals" />
661661
<element ref="tns:case" />
662+
<element ref="tns:timestamp" />
662663
<element ref="tns:htmlanchor" />
663664
<element ref="tns:trim" />
664665
<element ref="tns:split" />
@@ -965,6 +966,17 @@
965966
</complexType>
966967
</element>
967968

969+
<element name="timestamp">
970+
<annotation>
971+
<documentation>Current timestamp/time.</documentation>
972+
</annotation>
973+
<complexType>
974+
<attribute name="format" type="string" use="optional" default="timestamp"/>
975+
<attribute name="timezone" type="string" use="optional" default="UTC"/>
976+
<attribute name="language" type="string" use="optional" />
977+
</complexType>
978+
</element>
979+
968980
<element name="constant">
969981
<annotation>
970982
<documentation>Sets literal value to a constant.</documentation>

0 commit comments

Comments
 (0)