KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > DateInputModule


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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
17 package org.apache.cocoon.components.modules.input;
18
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.LinkedList JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.configuration.ConfigurationException;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30
31 /**
32  * DateInputModule returns current date, optionally formated as
33  * string. Format given through attribute "format" of configuration
34  * root node or nested <format/> tag on module declaration.
35  *
36  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
37  * @version CVS $Id: DateInputModule.java 106641 2004-11-26 07:47:26Z rgoers $
38  * @see java.text.SimpleDateFormat
39  */

40 public class DateInputModule extends AbstractInputModule implements ThreadSafe {
41
42     final static Vector JavaDoc returnNames;
43     static {
44         Vector JavaDoc tmp = new Vector JavaDoc();
45         tmp.add("currentDate");
46         returnNames = tmp;
47     }
48
49     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel ) throws ConfigurationException {
50         
51         String JavaDoc format = (String JavaDoc) this.settings.get("format",name);
52         if (modeConf != null) {
53             format = modeConf.getAttribute("format", format);
54             // this is preferred:
55
format = modeConf.getChild("format").getValue(format);
56         }
57
58         if (format==null) {
59             return new Date JavaDoc();
60         } else {
61             try {
62                 return new SimpleDateFormat JavaDoc(format).format(new Date JavaDoc());
63             } catch (Exception JavaDoc e) {
64                 return new Date JavaDoc();
65             }
66         }
67     }
68
69
70     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel ) throws ConfigurationException {
71
72         return DateInputModule.returnNames.iterator();
73     }
74
75
76     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
77         throws ConfigurationException {
78
79             List JavaDoc values = new LinkedList JavaDoc();
80             values.add( this.getAttribute(name, modeConf, objectModel) );
81
82             return values.toArray();
83             
84     }
85
86 }
87
Popular Tags