01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.lenya.cms.cocoon.components.modules.input;
19:
20: import java.text.SimpleDateFormat;
21: import java.util.Date;
22: import java.util.Map;
23:
24: import org.apache.avalon.framework.configuration.Configuration;
25: import org.apache.avalon.framework.configuration.ConfigurationException;
26: import org.apache.avalon.framework.thread.ThreadSafe;
27: import org.apache.cocoon.components.modules.input.AbstractInputModule;
28:
29: /**
30: * The DateConverterModule converts a date string from one format into
31: * another format.
32: * The conversion is defined by the nested elements <src-pattern/> and
33: * <pattern/> of the module declaration.
34: *
35: */
36: public class DateConverterModule extends AbstractInputModule implements
37: ThreadSafe {
38:
39: public Object getAttribute(String name, Configuration modeConf,
40: Map objectModel) throws ConfigurationException {
41:
42: String srcPattern = (String) this .settings.get("src-pattern");
43: String pattern = (String) this .settings.get("pattern");
44:
45: if (modeConf != null) {
46: srcPattern = modeConf.getChild("src-pattern").getValue(
47: srcPattern);
48: pattern = modeConf.getChild("pattern").getValue(pattern);
49: }
50:
51: if (srcPattern == null) {
52: throw new ConfigurationException(
53: "Source date pattern not specified.");
54: }
55: if (pattern == null) {
56: throw new ConfigurationException(
57: "Date pattern not specified.");
58: }
59:
60: try {
61: SimpleDateFormat srcFormat = new SimpleDateFormat(
62: srcPattern);
63: SimpleDateFormat format = new SimpleDateFormat(pattern);
64: Date date = srcFormat.parse(name);
65: return format.format(date);
66: } catch (Exception e) {
67: throw new ConfigurationException("Could not convert date: "
68: + name, e);
69: }
70: }
71: }
|