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.cocoon.components.modules.input;
19:
20: import java.text.SimpleDateFormat;
21: import java.util.Date;
22: import java.util.Iterator;
23: import java.util.LinkedList;
24: import java.util.List;
25: import java.util.Map;
26: import java.util.Vector;
27:
28: import org.apache.avalon.framework.configuration.Configuration;
29: import org.apache.avalon.framework.configuration.ConfigurationException;
30: import org.apache.avalon.framework.thread.ThreadSafe;
31:
32: /**
33: * DateInputModule returns current date, optionally formated as
34: * string. Format given through attribute "format" of configuration
35: * root node or nested <format/> tag on module declaration.
36: *
37: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
38: * @version CVS $Id: DateInputModule.java 433543 2006-08-22 06:22:54Z crossley $
39: * @see java.text.SimpleDateFormat
40: */
41: public class DateInputModule extends AbstractInputModule implements
42: ThreadSafe {
43:
44: final static Vector returnNames;
45: static {
46: Vector tmp = new Vector();
47: tmp.add("currentDate");
48: returnNames = tmp;
49: }
50:
51: public Object getAttribute(String name, Configuration modeConf,
52: Map objectModel) throws ConfigurationException {
53:
54: String format = (String) this .settings.get("format", name);
55: if (modeConf != null) {
56: format = modeConf.getAttribute("format", format);
57: // this is preferred:
58: format = modeConf.getChild("format").getValue(format);
59: }
60:
61: if (format == null) {
62: return new Date();
63: } else {
64: try {
65: return new SimpleDateFormat(format).format(new Date());
66: } catch (Exception e) {
67: return new Date();
68: }
69: }
70: }
71:
72: public Iterator getAttributeNames(Configuration modeConf,
73: Map objectModel) throws ConfigurationException {
74:
75: return DateInputModule.returnNames.iterator();
76: }
77:
78: public Object[] getAttributeValues(String name,
79: Configuration modeConf, Map objectModel)
80: throws ConfigurationException {
81:
82: List values = new LinkedList();
83: values.add(this.getAttribute(name, modeConf, objectModel));
84:
85: return values.toArray();
86:
87: }
88:
89: }
|